View Javadoc

1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.services.title;
18  
19  import java.util.Iterator;
20  import java.util.Locale;
21  
22  import javax.servlet.http.HttpServletRequest;
23  
24  import org.apache.jetspeed.PortalReservedParameters;
25  import org.apache.jetspeed.request.RequestContext;
26  import org.apache.pluto.om.common.Preference;
27  import org.apache.pluto.om.entity.PortletEntity;
28  import org.apache.pluto.om.window.PortletWindow;
29  
30  public class DynamicTitleServiceImpl implements DynamicTitleService
31  {
32  
33      public void setDynamicTitle(PortletWindow window,
34              HttpServletRequest request, String titleArg)
35      {
36          //String title = getTitleFromPreference(window, request);
37          String title = null;
38  //        if (title == null || title.length() < 0)
39  //        {
40              if (titleArg == null || titleArg.length() == 0)
41              {
42                  title = getTitleFromPortletDefinition(window, request);
43              }
44              else
45              {
46                  title = titleArg;
47              }
48  
49  //        }
50  
51          request.setAttribute(
52                  PortalReservedParameters.OVERRIDE_PORTLET_TITLE_ATTR
53                          + "::window.id::" + window.getId(), title);
54  
55      }
56      
57      public String getDynamicTitle(PortletWindow window,
58              HttpServletRequest request)
59      {
60          return (String)request.getAttribute(PortalReservedParameters.OVERRIDE_PORTLET_TITLE_ATTR
61                          + "::window.id::" + window.getId());
62      }
63  
64      protected final String getTitleFromPortletDefinition(PortletWindow window,
65              HttpServletRequest request)
66      {
67          String title = null;
68          RequestContext requestContext = (RequestContext) request
69                  .getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
70          
71          PortletEntity entity = window.getPortletEntity();
72          if (entity != null && entity.getPortletDefinition() != null)
73          {
74              title = requestContext.getPreferedLanguage(
75                      entity.getPortletDefinition()).getTitle();
76          }
77  
78          if (title == null && entity.getPortletDefinition() != null)
79          {
80              title = entity.getPortletDefinition().getName();
81          }
82          else if (title == null)
83          {
84              title = "Invalid portlet entity " + entity.getId();
85          }
86          
87          return title;
88      }
89  
90      protected final String getTitleFromPreference(PortletWindow window,
91              HttpServletRequest request)
92      {
93          Locale locale = request.getLocale();
94          String titleKey = createTitleKey(locale, false);
95  
96          Preference titlePref = window.getPortletEntity().getPreferenceSet()
97                  .get(titleKey);
98          if (titlePref == null)
99          {
100             titleKey = createTitleKey(locale, true);
101             titlePref = window.getPortletEntity().getPreferenceSet().get(
102                     titleKey);
103         }
104 
105         if (titlePref != null)
106         {
107             Iterator values = titlePref.getValues();
108             if (values.hasNext())
109             {
110                 return (String) titlePref.getValues().next();
111             }
112         }
113 
114         return null;
115     }
116 
117     public static String createTitleKey(Locale locale, boolean languageOnly)
118     {
119         if(languageOnly)
120         {
121             return "jetspeed.title."+locale.getLanguage();
122         }
123         else
124         {
125             return "jetspeed.title."+locale.toString();
126         }
127     }
128 
129 }