KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > impl > portletAPIImp > PortletPreferencesImp


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.portletcontainer.impl.portletAPIImp;
6
7 import java.io.IOException JavaDoc;
8 import java.io.Serializable JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Collections JavaDoc;
12 import java.util.Enumeration JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import javax.portlet.PortletPreferences;
20 import javax.portlet.PreferencesValidator;
21 import javax.portlet.ReadOnlyException;
22 import javax.portlet.ValidatorException;
23
24 import org.exoplatform.services.portletcontainer.pci.WindowID;
25 import org.exoplatform.services.portletcontainer.pci.model.ExoPortletPreferences;
26 import org.exoplatform.services.portletcontainer.pci.model.Preference;
27 import org.exoplatform.services.portletcontainer.persistence.PortletPreferencesPersister;
28
29 /**
30  * Created by The eXo Platform SARL Author : Mestrallet Benjamin
31  * benjmestrallet@users.sourceforge.net Date: Jul 27, 2003 Time: 9:21:41 PM
32  *
33  * Should be pooled using fillPortletPreferences() and emptyPortletPrferences()
34  *
35  * Every persistent storing is deleguate to a PersistentManager object that is
36  * aware of the user identity.
37  *
38  */

39 public class PortletPreferencesImp implements PortletPreferences, Serializable JavaDoc {
40
41   transient private PreferencesValidator validator_;
42
43   transient private ExoPortletPreferences defaultPreferences_;
44
45   transient private boolean methodCalledIsAction_;
46
47   transient private boolean stateChangeAuthorized_ = true;
48
49   private ExoPortletPreferences preferences_ = new ExoPortletPreferences();
50
51   private ExoPortletPreferences modifiedPreferences_ = new ExoPortletPreferences();
52
53   private boolean stateSaveOnClient_;
54
55   protected WindowID windowID_;
56
57   private PortletPreferencesPersister persister_;
58
59   public PortletPreferencesImp(PreferencesValidator validator,
60       ExoPortletPreferences defaultPreferences, WindowID windowID,
61       PortletPreferencesPersister persister) {
62     this.validator_ = validator;
63     this.defaultPreferences_ = defaultPreferences;
64     this.windowID_ = windowID;
65     this.persister_ = persister;
66     fillCurrentPreferences();
67   }
68
69   private void fillCurrentPreferences() {
70     if (defaultPreferences_ == null)
71       return;
72     Collection JavaDoc collection = defaultPreferences_.values();
73     Preference wrapper;
74     for (Iterator JavaDoc i = collection.iterator(); i.hasNext();) {
75       Preference preferenceType = (Preference) i.next();
76       wrapper = new Preference();
77       wrapper.setName(preferenceType.getName());
78       wrapper.setReadOnly(preferenceType.isReadOnly());
79       List JavaDoc values = preferenceType.getValues();
80       for (int j = 0; j < values.size(); j++) {
81         wrapper.addValue((String JavaDoc) values.get(j));
82       }
83       preferences_.put(preferenceType.getName(), wrapper);
84     }
85   }
86
87   public ExoPortletPreferences getCurrentPreferences() {
88     return this.preferences_;
89   }
90
91   public void setCurrentPreferences(ExoPortletPreferences map) {
92     this.preferences_ = map;
93   }
94
95   public boolean isReadOnly(String JavaDoc s) {
96     if (s == null)
97       throw new IllegalArgumentException JavaDoc("the key given is null");
98     Preference wrapper = (Preference) modifiedPreferences_.get(s);
99     if (wrapper == null)
100       wrapper = (Preference) preferences_.get(s);
101     if (wrapper == null)
102       return false;
103     return wrapper.isReadOnly();
104   }
105
106   public String JavaDoc getValue(String JavaDoc s, String JavaDoc s1) {
107     if (s == null)
108       throw new IllegalArgumentException JavaDoc("the key given is null");
109     Preference wrapper = (Preference) modifiedPreferences_.get(s);
110     if (wrapper == null)
111       wrapper = (Preference) preferences_.get(s);
112     if (wrapper == null || wrapper.getValues().isEmpty())
113       return s1;
114     return (String JavaDoc) wrapper.getValues().iterator().next();
115   }
116
117   public String JavaDoc[] getValues(String JavaDoc s, String JavaDoc[] strings) {
118     if (s == null)
119       throw new IllegalArgumentException JavaDoc("the key given is null");
120     Preference wrapper = (Preference) modifiedPreferences_.get(s);
121     if (wrapper == null)
122       wrapper = (Preference) preferences_.get(s);
123     if (wrapper == null || wrapper.getValues().isEmpty())
124       return strings;
125
126     Object JavaDoc[] arr = wrapper.getValues().toArray();
127     String JavaDoc[] sA = new String JavaDoc[arr.length];
128     for (int i = 0; i < arr.length; i++) {
129       Object JavaDoc o = arr[i];
130       sA[i] = (String JavaDoc) o;
131     }
132     return sA;
133   }
134
135   public void setValue(String JavaDoc s, String JavaDoc s1) throws ReadOnlyException {
136     if (s == null)
137       throw new IllegalArgumentException JavaDoc("the key given is null");
138     if (isReadOnly(s))
139       throw new ReadOnlyException("the value " + s + " can not be changed");
140
141     Preference wrapper = new Preference();
142     wrapper.setName(s);
143     wrapper.setReadOnly(false);
144     wrapper.getValues().add(s1);
145     modifiedPreferences_.put(s, wrapper);
146   }
147
148   public void setValues(String JavaDoc s, String JavaDoc[] strings) throws ReadOnlyException {
149     if (s == null)
150       throw new IllegalArgumentException JavaDoc("the key given is null");
151     if (isReadOnly(s))
152       throw new ReadOnlyException("the value " + s + " can not be changed");
153
154     Preference wrapper = new Preference();
155     wrapper.setName(s);
156     wrapper.setReadOnly(false);
157
158     Collection JavaDoc c = new ArrayList JavaDoc(strings.length);
159     for (int i = 0; i < strings.length; i++) {
160       String JavaDoc string = strings[i];
161       c.add(string);
162     }
163     wrapper.getValues().addAll(c);
164
165     modifiedPreferences_.put(s, wrapper);
166   }
167
168   public Enumeration JavaDoc getNames() {
169     Collection JavaDoc c = new ArrayList JavaDoc();
170
171     Set JavaDoc names = mergeModifiedPreference().keySet();
172     for (Iterator JavaDoc iterator = names.iterator(); iterator.hasNext();) {
173       String JavaDoc s = (String JavaDoc) iterator.next();
174       c.add(s);
175     }
176
177     return Collections.enumeration(c);
178   }
179
180   public Map JavaDoc getMap() {
181     Map JavaDoc toReturn = new HashMap JavaDoc();
182     Collection JavaDoc keys = mergeModifiedPreference().keySet();
183     for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
184       String JavaDoc key = (String JavaDoc) iter.next();
185       Preference element = (Preference) mergeModifiedPreference().get(key);
186       Collection JavaDoc c2 = element.getValues();
187       String JavaDoc[] myArray = new String JavaDoc[c2.size()];
188       int i = 0;
189       for (Iterator JavaDoc iterator3 = c2.iterator(); iterator3.hasNext(); i++) {
190         String JavaDoc value = (String JavaDoc) iterator3.next();
191         myArray[i] = value;
192       }
193       toReturn.put(key, myArray);
194     }
195
196     return Collections.unmodifiableMap(toReturn);
197   }
198
199   private ExoPortletPreferences mergeModifiedPreference() {
200     ExoPortletPreferences tmpMap = new ExoPortletPreferences();
201
202     tmpMap.putAll(modifiedPreferences_);
203     Collection JavaDoc keys = preferences_.keySet();
204     for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
205       String JavaDoc key = (String JavaDoc) iter.next();
206       if (!tmpMap.containsKey(key))
207         tmpMap.put(key, preferences_.get(key));
208     }
209     return tmpMap;
210   }
211
212   public void reset(String JavaDoc s) throws ReadOnlyException {
213     if (s == null)
214       throw new IllegalArgumentException JavaDoc("the key given is null");
215     if (isReadOnly(s))
216       throw new ReadOnlyException("the value " + s + " can not be changed");
217     Preference preferenceType = null;
218     if (defaultPreferences_ != null)
219       preferenceType = (Preference) defaultPreferences_.get(s);
220     try {
221       if (preferenceType == null) {
222         preferences_.remove(s);
223       } else {
224         Preference wrapper = (Preference) preferences_.get(s);
225         wrapper.getValues().clear();
226         List JavaDoc defaultValues = preferenceType.getValues();
227         for (int i = 0; i < defaultValues.size(); i++) {
228           wrapper.addValue((String JavaDoc) defaultValues.get(i));
229         }
230       }
231       modifiedPreferences_.remove(s);
232       if(persister_.getPortletPreferences(windowID_) != null)
233         save(preferences_);
234     } catch (Exception JavaDoc e) {
235       throw new RuntimeException JavaDoc("can not remove preference", e);
236     }
237   }
238
239   /**
240    * We first validate every field then we deleguates the storing to an object
241    * that implements the PersistentManager interface
242    *
243    * @throws IOException
244    * @throws ValidatorException
245    */

246   public void store() throws IOException JavaDoc, ValidatorException {
247     if (!isMethodCalledIsAction())
248       throw new IllegalStateException JavaDoc(
249           "the store() method can not be called from a render method");
250     if (!isStateChangeAuthorized())
251       throw new IllegalStateException JavaDoc(
252           "the state of the portlet can not be changed");
253     if (validator_ != null) {
254       validator_.validate(this);
255     }
256     preferences_ = mergeModifiedPreference();
257     modifiedPreferences_ = new ExoPortletPreferences();
258     if (!isStateSaveOnClient()) {
259       save(getCurrentPreferences());
260     }
261   }
262   
263   private void save(ExoPortletPreferences preferences) throws IOException JavaDoc{
264     try {
265       persister_.savePortletPreferences(windowID_, preferences);
266     } catch (Exception JavaDoc ex) {
267       throw new IOException JavaDoc(ex.getMessage());
268     }
269   }
270
271   public void discard() {
272     modifiedPreferences_ = new ExoPortletPreferences();
273   }
274
275   public void setMethodCalledIsAction(boolean b) {
276     methodCalledIsAction_ = b;
277   }
278
279   public boolean isMethodCalledIsAction() {
280     return methodCalledIsAction_;
281   }
282
283   public boolean isStateChangeAuthorized() {
284     return stateChangeAuthorized_;
285   }
286
287   public void setStateChangeAuthorized(boolean stateChangeAuthorized) {
288     this.stateChangeAuthorized_ = stateChangeAuthorized;
289   }
290
291   public void setStateSaveOnClient(boolean stateSaveOnClient) {
292     this.stateSaveOnClient_ = stateSaveOnClient;
293   }
294
295   public boolean isStateSaveOnClient() {
296     return stateSaveOnClient_;
297   }
298 }
Popular Tags