View Javadoc

1   /*
2    * Copyright 2003,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /* 
17  
18   */
19  
20  package org.apache.pluto.core.impl;
21  
22  import java.util.ArrayList;
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.Iterator;
27  import java.util.LinkedList;
28  import java.util.List;
29  import java.util.ListIterator;
30  import java.util.Map;
31  
32  import javax.portlet.PortletPreferences;
33  import javax.portlet.PreferencesValidator;
34  import javax.portlet.ReadOnlyException;
35  import javax.portlet.ValidatorException;
36  
37  import org.apache.pluto.om.ControllerObjectAccess;
38  import org.apache.pluto.om.common.Preference;
39  import org.apache.pluto.om.common.PreferenceCtrl;
40  import org.apache.pluto.om.common.PreferenceSet;
41  import org.apache.pluto.om.common.PreferenceSetCtrl;
42  import org.apache.pluto.om.entity.PortletEntity;
43  import org.apache.pluto.om.entity.PortletEntityCtrl;
44  import org.apache.pluto.om.portlet.PortletDefinition;
45  import org.apache.pluto.om.portlet.PortletDefinitionCtrl;
46  import org.apache.pluto.util.Enumerator;
47  import org.apache.pluto.util.StringUtils;
48  
49  
50  public class PortletPreferencesImpl implements PortletPreferences
51  {
52      private List    preferenceSetList  = new LinkedList();
53      private HashMap changedPreferences = new HashMap();
54      private HashSet removedPreferences = new HashSet();
55  
56  
57      // current method used for managing these preferences
58      private Integer methodId = null;
59  
60      private PortletEntity portletEntity = null;
61      private PortletDefinition portletDefinition = null;
62  
63  
64      public PortletPreferencesImpl(Integer methodId, PortletEntity portletEntity)
65      {
66          this.methodId = methodId;
67  
68          this.portletEntity = portletEntity;
69          this.portletDefinition = portletEntity.getPortletDefinition();
70  
71          // fill list of preference sets
72          preferenceSetList.add(portletEntity.getPreferenceSet());
73          preferenceSetList.add(portletDefinition.getPreferenceSet());
74  
75      }
76  
77      public PortletPreferencesImpl(Integer methodId, PortletDefinition portletDefinition)
78      {
79          this.methodId = methodId;
80  
81          this.portletDefinition = portletDefinition;     
82  
83          // fill list of preference sets
84          preferenceSetList.add(portletDefinition.getPreferenceSet());
85      }
86  
87      // javax.portlet.PortletPreferences implementation --------------------------------------------
88      public boolean isReadOnly(String key)
89      {
90          if (key == null)
91          {
92              throw new IllegalArgumentException("key == null");
93          }
94  
95          // default is false
96          boolean isReadOnly = false;
97  
98          // if managing the first layer of the preferences 
99          // modifiable returns always true (for administration purposes)
100         if (preferenceSetList.size() != 1)
101         { // otherwise
102             // iterate through all preferences
103             Preference preference = null;
104             ListIterator iter = preferenceSetList.listIterator();
105             while ((preference == null) && (iter.hasNext()))
106             {
107                 preference = ((PreferenceSet)iter.next()).get(key);
108             }
109             if (preference != null)
110             {
111                 isReadOnly = preference.isReadOnly();
112             }
113         }
114         return isReadOnly;
115     }
116 
117     public String getValue(String key, String def)
118     {
119         if (key == null)
120         {
121             throw new IllegalArgumentException("key == null");
122         }
123 
124         String[] defStr = new String[1];
125         defStr[0] = def;
126 
127         String[] values = this.getValues(key, defStr);
128 
129         // null values are allowed
130         if ((values == null) || (values.length==0))
131         {
132             return null;
133         }
134 
135         return values[0];
136     }
137 
138     public String[] getValues(String key, String[] def)
139     {
140         if (key == null)
141         {
142             throw new IllegalArgumentException("key == null");
143         }
144 
145         // get modified preferences
146         if (changedPreferences.containsKey(key))
147         {
148             return StringUtils.copy((String[]) changedPreferences.get(key));
149         }
150 
151         // get all preference sets
152         ListIterator iter = preferenceSetList.listIterator();
153 
154         // if removed preference do not look in first layer
155         if (removedPreferences.contains(key))
156         {
157             iter.next();
158         }
159 
160         // iterate through all preference sets
161         Preference preference = null;
162         while ((preference == null) && (iter.hasNext()))
163         {
164             preference = ((PreferenceSet)iter.next()).get(key);
165         }
166 
167         if (preference == null || !preference.isValueSet())
168         {
169             // if preference not exists
170             return def;
171         }
172         else
173         { // if preference exists
174             String[] result = this.getValuesFromPreference(preference);
175             if (result != null)
176                 result = StringUtils.copy(result);
177             return result;
178         }
179     }
180     
181     public void setValue(String key, String value) throws ReadOnlyException
182     {
183         if (key == null)
184         {
185             throw new IllegalArgumentException("key == null");
186         }
187 
188         String[] values =  new String[1];
189         values[0] = value;
190         setValues(key, values);
191     }
192 
193     public void setValues(String key, String[] values) throws ReadOnlyException
194     {
195         if (key == null)
196         {
197             throw new IllegalArgumentException("key == null");
198         }
199 
200         if (isReadOnly(key))
201         {
202             throw new ReadOnlyException("Preference attribute called " + key + " may not be modified");
203         }
204 
205         changedPreferences.put(key, StringUtils.copy(values));
206         removedPreferences.remove(key);
207 
208     }
209 
210     public Enumeration getNames()
211     {
212         HashSet keyset = new HashSet();
213         ListIterator listIter = preferenceSetList.listIterator();
214         Iterator changedIter = changedPreferences.keySet().iterator();
215         Iterator removedIter = removedPreferences.iterator();
216 
217         // iterate through all modified preferences of first layer
218         while (changedIter.hasNext())
219         {
220             keyset.add(changedIter.next());
221         }
222 
223         // iterate through all existing preferences of first layer
224         Iterator preferencesIter = ((PreferenceSet)listIter.next()).iterator();
225         while (preferencesIter.hasNext())
226         {
227             String name = ((Preference)preferencesIter.next()).getName();
228             keyset.add(name);
229         }
230 
231         // iterate through all removed preferences of first layer      
232         while (removedIter.hasNext())
233         {
234             keyset.remove(removedIter.next());
235         }
236 
237 
238         // iterate through all other preference sets
239         while (listIter.hasNext())
240         {
241             preferencesIter = ((PreferenceSet)listIter.next()).iterator();
242 
243             // iterate through all preferences
244             while (preferencesIter.hasNext())
245             {
246                 String name = ((Preference)preferencesIter.next()).getName();
247                 keyset.add(name);
248             }
249         }
250 
251 
252         return new Enumerator(keyset.iterator());
253 
254     }
255 
256     public Map getMap()
257     {
258         HashMap map = new HashMap();
259         Enumeration enum = this.getNames();
260         while (enum.hasMoreElements())
261         {
262             String name = (String)enum.nextElement();
263             map.put(name, getValues(name,null));
264         }
265 
266         return map;
267     }
268 
269     public void reset(String key) throws ReadOnlyException
270     {
271         if (key == null)
272         {
273             throw new IllegalArgumentException("key == null");
274         }
275 
276         if (isReadOnly(key))
277         {
278             throw new ReadOnlyException("preference attribute called " + key + " may not be modified");
279         }
280 
281         changedPreferences.remove(key);
282         removedPreferences.add(key);
283     }
284 
285     public void store() throws java.io.IOException,ValidatorException
286     {
287         // not allowed when not called in action
288         if ( ! this.methodId.equals(org.apache.pluto.Constants.METHOD_ACTION))
289         {
290             throw new java.lang.IllegalStateException("store is only allowed inside a processAction call");
291         }
292 
293         // validate preferences
294         PreferencesValidator validator = portletDefinition.getPreferenceSet().getPreferencesValidator();
295         if (validator != null)
296         {
297             validator.validate(this);
298         }
299 
300         // transfer changes to the top preference set 
301         PreferenceSet preferences = (PreferenceSet)preferenceSetList.get(0);
302         PreferenceSetCtrl preferencesCtrl = (PreferenceSetCtrl)ControllerObjectAccess.get(preferences);
303 
304         // modified preferences
305         Iterator iter = changedPreferences.keySet().iterator();
306         while (iter.hasNext())
307         {
308             String key = (String) iter.next();
309             String[] values = (String[])changedPreferences.get(key);
310 
311             // null values are allowed
312             List newValues = null;
313             if (values != null)
314             {
315                 // convert values from string[] to collection
316                 newValues = new ArrayList(values.length);
317                 for (int i=0;i<values.length;i++)
318                     newValues.add(values[i]);
319             }
320 
321             // transfer changings
322             Preference preference = preferences.get(key);
323             if (preference != null)
324             {
325                 // change preference
326                 PreferenceCtrl preferenceCtrl = (PreferenceCtrl)ControllerObjectAccess.get(preference);
327                 preferenceCtrl.setValues(newValues);
328             }
329             else
330             {
331                 // add new preference
332                 preferencesCtrl.add(key, newValues); 
333             }
334         }
335         changedPreferences.clear();
336 
337         // removed preferences
338         iter = removedPreferences.iterator();
339         while (iter.hasNext())
340         {
341             String key = (String) iter.next();
342             preferencesCtrl.remove(key);
343         }
344         removedPreferences.clear();
345 
346         // store changes to the top preference set
347         if (portletEntity != null)
348         {
349             PortletEntityCtrl portletEntityCtrl = (PortletEntityCtrl)ControllerObjectAccess.get(portletEntity);
350             portletEntityCtrl.store();
351         }
352         else
353         {
354             PortletDefinitionCtrl portletDefinitionCtrl = (PortletDefinitionCtrl)ControllerObjectAccess.get(portletDefinition);
355             portletDefinitionCtrl.store();
356         }
357 
358     }
359     // --------------------------------------------------------------------------------------------
360 
361 
362     // internal methods ---------------------------------------------------------------------------
363     private String[] getValuesFromPreference(Preference preference)
364     {
365         if (preference == null)
366         {
367             return null;
368         }
369 
370         Iterator values = preference.getValues();
371 
372         // null values are allowed
373         if (values == null)
374         {
375             return null;
376         }
377 
378         if (!values.hasNext())
379         {
380             return new String[0];
381         }
382 
383         // convert values from Iterator to string[]
384         List newValues = new ArrayList();
385         while (values.hasNext())
386         {
387             newValues.add(values.next());
388         }
389 
390         return(String[])newValues.toArray(new String[newValues.size()]);
391     }
392     // --------------------------------------------------------------------------------------------
393 }