001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.services.title;
018:
019: import java.util.Iterator;
020: import java.util.Locale;
021:
022: import javax.servlet.http.HttpServletRequest;
023:
024: import org.apache.jetspeed.PortalReservedParameters;
025: import org.apache.jetspeed.request.RequestContext;
026: import org.apache.pluto.om.common.Preference;
027: import org.apache.pluto.om.entity.PortletEntity;
028: import org.apache.pluto.om.window.PortletWindow;
029:
030: public class DynamicTitleServiceImpl implements DynamicTitleService {
031:
032: public void setDynamicTitle(PortletWindow window,
033: HttpServletRequest request, String titleArg) {
034: //String title = getTitleFromPreference(window, request);
035: String title = null;
036: // if (title == null || title.length() < 0)
037: // {
038: if (titleArg == null || titleArg.length() == 0) {
039: title = getTitleFromPortletDefinition(window, request);
040: } else {
041: title = titleArg;
042: }
043:
044: // }
045:
046: request.setAttribute(
047: PortalReservedParameters.OVERRIDE_PORTLET_TITLE_ATTR
048: + "::window.id::" + window.getId(), title);
049:
050: }
051:
052: public String getDynamicTitle(PortletWindow window,
053: HttpServletRequest request) {
054: return (String) request
055: .getAttribute(PortalReservedParameters.OVERRIDE_PORTLET_TITLE_ATTR
056: + "::window.id::" + window.getId());
057: }
058:
059: protected final String getTitleFromPortletDefinition(
060: PortletWindow window, HttpServletRequest request) {
061: String title = null;
062: RequestContext requestContext = (RequestContext) request
063: .getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
064:
065: PortletEntity entity = window.getPortletEntity();
066: if (entity != null && entity.getPortletDefinition() != null) {
067: title = requestContext.getPreferedLanguage(
068: entity.getPortletDefinition()).getTitle();
069: }
070:
071: if (title == null && entity.getPortletDefinition() != null) {
072: title = entity.getPortletDefinition().getName();
073: } else if (title == null) {
074: title = "Invalid portlet entity " + entity.getId();
075: }
076:
077: return title;
078: }
079:
080: protected final String getTitleFromPreference(PortletWindow window,
081: HttpServletRequest request) {
082: Locale locale = request.getLocale();
083: String titleKey = createTitleKey(locale, false);
084:
085: Preference titlePref = window.getPortletEntity()
086: .getPreferenceSet().get(titleKey);
087: if (titlePref == null) {
088: titleKey = createTitleKey(locale, true);
089: titlePref = window.getPortletEntity().getPreferenceSet()
090: .get(titleKey);
091: }
092:
093: if (titlePref != null) {
094: Iterator values = titlePref.getValues();
095: if (values.hasNext()) {
096: return (String) titlePref.getValues().next();
097: }
098: }
099:
100: return null;
101: }
102:
103: public static String createTitleKey(Locale locale,
104: boolean languageOnly) {
105: if (languageOnly) {
106: return "jetspeed.title." + locale.getLanguage();
107: } else {
108: return "jetspeed.title." + locale.toString();
109: }
110: }
111:
112: }
|