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.om.preference.impl;
18  
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import javax.portlet.PreferencesValidator;
26  
27  import org.apache.jetspeed.om.common.preference.PreferenceSetComposite;
28  import org.apache.jetspeed.om.page.Fragment;
29  import org.apache.pluto.om.common.Preference;
30  
31  /***
32   * This is a per-request wrapper for a PreferenceSet that allows 
33   * the use of fragment-specified Preferences within a portlet instance
34   * in a page.
35   * 
36   * @author <href a="mailto:weaver@apache.org">Scott T. Weaver</a>
37   *
38   */
39  public class FragmentPortletPreferenceSet implements PreferenceSetComposite
40  {
41      private final PreferenceSetComposite preferenceSet;
42      private final Map prefs;
43      
44      public FragmentPortletPreferenceSet(PreferenceSetComposite preferenceSet, Fragment fragment)
45      {
46          // save mutable preference set and read only fragment
47          this.preferenceSet = preferenceSet;
48          // construct merged portlet definition prefs map;
49          // note that user specific preferences accessed via
50          // the portlet entity should override these defaults
51          int prefsSize = preferenceSet.size() + 1;        
52          if (fragment != null && fragment.getPreferences() != null)
53          {
54              prefsSize += fragment.getPreferences().size();
55          }
56          this.prefs = new HashMap(prefsSize);
57  
58          // add global portlet definition defaults to prefs
59          Iterator iterator = preferenceSet.iterator();
60          while(iterator.hasNext())
61          {
62              Preference pref = (Preference) iterator.next();
63              prefs.put(pref.getName(), pref);
64          }        
65  
66          // add/override global portlet definition defaults
67          // using more specific fragment preferences
68          if (fragment != null && fragment.getPreferences() != null)
69          {
70              Iterator itr = fragment.getPreferences().iterator();        
71              while(itr.hasNext())
72              {
73                  Preference pref = (Preference) itr.next();
74                  prefs.put(pref.getName(), pref);
75              }
76          }
77      }
78  
79      public Preference add(String arg0, List arg1)
80      {        
81          Preference pref = preferenceSet.add(arg0, arg1);
82          prefs.put(arg0, pref);
83          return pref;
84      }
85  
86      public Preference get(String name)
87      {
88          return (Preference) prefs.get(name);
89      }
90  
91      public Set getNames()
92      {
93          return prefs.keySet();
94      }
95  
96      public PreferencesValidator getPreferencesValidator()
97      {
98          return preferenceSet.getPreferencesValidator();
99      }
100 
101     public Iterator iterator()
102     {
103         return prefs.values().iterator();
104     }
105 
106     public void remove(Preference pref)
107     {
108         prefs.remove(pref.getName());
109         preferenceSet.remove(pref);
110     }
111 
112     public Preference remove(String name)
113     {
114         Preference pref = (Preference) prefs.remove(name);
115         preferenceSet.remove(name);
116         return pref;
117     }
118 
119     public int size()
120     {
121         return prefs.size();
122     }
123 }