View Javadoc

1   package org.apache.jetspeed.modules.parameters;
2   
3   /*
4    * Copyright 2000-2004 The Apache Software Foundation.
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.Comparator;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.jetspeed.om.registry.Parameter;
27  import org.apache.jetspeed.om.registry.PortletInfoEntry;
28  import org.apache.jetspeed.om.registry.RegistryEntry;
29  import org.apache.jetspeed.om.security.JetspeedUser;
30  import org.apache.jetspeed.services.JetspeedSecurity;
31  import org.apache.jetspeed.services.Registry;
32  import org.apache.jetspeed.services.rundata.JetspeedRunData;
33  import org.apache.jetspeed.services.security.PortalResource;
34  import org.apache.turbine.services.localization.Localization;
35  import org.apache.turbine.util.RunData;
36  import org.apache.velocity.context.Context;
37  
38  /***
39   * Returns list box control populated with registry entries from selected registry.
40   * <p>Options:
41   * <UL>
42   * <LI><code>registry</code> [Portlet|Security|PortletControl|PortletController|Skin|security|MediaType|Client] - registry name</LI> 
43   * <LI><code>sort</code> [<strong>true</strong>|false] - return sorted list of items</LI>  
44   * <LI><code>select-hidden</code> [<strong>false</strong>|true] - allow multiple selections</LI>  
45   * <LI><code>null-if-empty</code> [<strong>true</strong>|false] - do not return a select control if item list is empty</LI>   
46   * <LI><code>set-label</code> [<strong>false</strong>|true] - put label in front of the list box based on the registry name</LI>    
47   * <LI><code>disabled-if-wml</code> [<strong>false</strong>|true] - set disabled attribute for the list box if using wml</LI>     
48   * <LI><code>select-if-simple</code> [<strong>false</strong>|true] - select only entries with parameter "simple" set to true</LI>   
49   * </UL> 
50   * @author <a href="morciuch@apache.org">Mark Orciuch</a>
51   * @version $Id: RegistryEntryListBox.java,v 1.8 2004/02/23 03:01:20 jford Exp $  
52   */
53  
54  public class RegistryEntryListBox extends VelocityParameterPresentationStyle
55  {
56      public static final String OPTION_REGISTRY = "registry";
57      public static final String OPTION_SORT = "sort";
58      public static final String OPTION_SELECT_HIDDEN = "select-hidden";
59      public static final String OPTION_NULL_IF_EMPTY = "null-if-empty";
60      public static final String OPTION_SET_LABEL = "set-label";
61      public static final String OPTION_DISABLED_IF_WML = "disabled-if-wml";
62      public static final String OPTION_SELECT_IF_SIMPLE = "select-if-simple";
63  
64      /***
65       * Put custom objects in the velocity context
66       * 
67       * @param data
68       * @param name
69       * @param value
70       * @param parms
71       * @param context
72       */
73      public void buildContext(RunData data, String name, String value, Map parms, Context context)
74      {
75          // Initialize options
76          JetspeedRunData jdata = (JetspeedRunData)data;
77          String mediaType = jdata.getProfile().getMediaType();
78          String regName = (String)getParm(OPTION_REGISTRY, Registry.PORTLET);
79          boolean sort = (new Boolean((String)getParm(OPTION_SORT, "true"))).booleanValue();
80          boolean selectHidden = (new Boolean((String)getParm(OPTION_SELECT_HIDDEN, "false"))).booleanValue();
81          String nullIfEmpty = (String)getParm(OPTION_NULL_IF_EMPTY, "true");
82          boolean setLabel = (new Boolean((String)getParm(OPTION_SET_LABEL, "false"))).booleanValue();
83          boolean disabledIfWML = (new Boolean((String)getParm(OPTION_DISABLED_IF_WML, "false"))).booleanValue();
84          boolean selectIfSimple = (new Boolean((String)getParm(OPTION_SELECT_IF_SIMPLE, "false"))).booleanValue();
85          String defaultEntry = null;
86  
87          // Iterate thru entries from selected registry
88          List list = new ArrayList();
89          for (Iterator i = Registry.get(regName).listEntryNames(); i.hasNext();)
90          {
91              RegistryEntry entry = Registry.getEntry(regName, (String)i.next());
92              boolean selected = false;
93              selected = JetspeedSecurity.checkPermission((JetspeedUser) data.getUser(), new PortalResource(entry), "customize");
94              if (selected && !selectHidden)
95              {
96                  selected = !entry.isHidden();
97              }
98              if (selected && (entry instanceof PortletInfoEntry))
99              {
100                 selected = ((PortletInfoEntry) entry).hasMediaType(mediaType);
101             }
102             if (selected && selectIfSimple)
103             {
104                 Parameter simpleParam = ((PortletInfoEntry) entry).getParameter("simple");
105                 if (simpleParam != null)
106                 {
107                     selected = new Boolean(simpleParam.getValue()).booleanValue();
108                 }
109                 else
110                 {
111                     selected = false;
112                 }
113             }
114             if (selected)
115             {
116                 list.add(entry);
117             }
118         }
119 
120         // Perform optional sort of list box items
121         if (sort)
122             Collections.sort(list, 
123                              new Comparator()
124                              {
125                                  public int compare(Object o1, Object o2)
126                                  {
127                                      String t1 = (((RegistryEntry) o1).getTitle() != null)
128                                      ? ((RegistryEntry) o1).getTitle()
129                                      : ((RegistryEntry) o1).getName();
130                                      String t2 = (((RegistryEntry) o2).getTitle() != null)
131                                      ? ((RegistryEntry) o2).getTitle()
132                                      : ((RegistryEntry) o2).getName();
133 
134                                      return t1.compareTo(t2);
135                                  }
136                              });
137 
138         // Set list box label
139         String label = null;
140         if (regName.equals(Registry.PORTLET))
141         {
142             label = Localization.getString(data, "CUSTOMIZER_PORTLET");        
143         }
144         else if (regName.equals(Registry.SECURITY))
145         {
146             label = Localization.getString(data, "CUSTOMIZER_SECURITY_REF");
147 //            SecurityReference defaultRef = PortalToolkit.getDefaultSecurityRef(
148 //                ((JetspeedRunData) data).getCustomizedProfile());
149 //            if (defaultRef != null)
150 //            {
151 //                defaultEntry = defaultRef.getParent();
152 //            }
153         }
154         else if (regName.equals(Registry.MEDIA_TYPE))
155         {
156             label = Localization.getString(data, "CUSTOMIZER_MEDIATYPE");
157         }
158         else if (regName.equals(Registry.PORTLET_CONTROLLER))
159         {
160             label = Localization.getString(data, "CUSTOMIZER_LAYOUT");
161         }
162         else if (regName.equals(Registry.PORTLET_CONTROL))
163         {
164             label = Localization.getString(data, "CUSTOMIZER_DECORATION");
165         }
166         else if (regName.equals(Registry.CLIENT))
167         {
168             label = "Client";
169         }
170         else
171         {
172             label = "";
173         }
174         context.put("entries", list);
175         context.put("nullIfEmpty", nullIfEmpty);
176         if (setLabel)
177             context.put("label", label);
178         if (disabledIfWML && mediaType.equalsIgnoreCase("wml"))
179             context.put("disabled", "disabled");
180         context.put("defaultEntry", defaultEntry);
181     }
182 
183 }