1 5 package org.exoplatform.services.portletcontainer.impl.portletAPIImp; 6 7 import java.io.IOException ; 8 import java.io.Serializable ; 9 import java.util.ArrayList ; 10 import java.util.Collection ; 11 import java.util.Collections ; 12 import java.util.Enumeration ; 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Map ; 17 import java.util.Set ; 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 39 public class PortletPreferencesImp implements PortletPreferences, Serializable { 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 collection = defaultPreferences_.values(); 73 Preference wrapper; 74 for (Iterator 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 values = preferenceType.getValues(); 80 for (int j = 0; j < values.size(); j++) { 81 wrapper.addValue((String ) 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 s) { 96 if (s == null) 97 throw new IllegalArgumentException ("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 getValue(String s, String s1) { 107 if (s == null) 108 throw new IllegalArgumentException ("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 ) wrapper.getValues().iterator().next(); 115 } 116 117 public String [] getValues(String s, String [] strings) { 118 if (s == null) 119 throw new IllegalArgumentException ("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 [] arr = wrapper.getValues().toArray(); 127 String [] sA = new String [arr.length]; 128 for (int i = 0; i < arr.length; i++) { 129 Object o = arr[i]; 130 sA[i] = (String ) o; 131 } 132 return sA; 133 } 134 135 public void setValue(String s, String s1) throws ReadOnlyException { 136 if (s == null) 137 throw new IllegalArgumentException ("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 s, String [] strings) throws ReadOnlyException { 149 if (s == null) 150 throw new IllegalArgumentException ("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 c = new ArrayList (strings.length); 159 for (int i = 0; i < strings.length; i++) { 160 String string = strings[i]; 161 c.add(string); 162 } 163 wrapper.getValues().addAll(c); 164 165 modifiedPreferences_.put(s, wrapper); 166 } 167 168 public Enumeration getNames() { 169 Collection c = new ArrayList (); 170 171 Set names = mergeModifiedPreference().keySet(); 172 for (Iterator iterator = names.iterator(); iterator.hasNext();) { 173 String s = (String ) iterator.next(); 174 c.add(s); 175 } 176 177 return Collections.enumeration(c); 178 } 179 180 public Map getMap() { 181 Map toReturn = new HashMap (); 182 Collection keys = mergeModifiedPreference().keySet(); 183 for (Iterator iter = keys.iterator(); iter.hasNext();) { 184 String key = (String ) iter.next(); 185 Preference element = (Preference) mergeModifiedPreference().get(key); 186 Collection c2 = element.getValues(); 187 String [] myArray = new String [c2.size()]; 188 int i = 0; 189 for (Iterator iterator3 = c2.iterator(); iterator3.hasNext(); i++) { 190 String value = (String ) 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 keys = preferences_.keySet(); 204 for (Iterator iter = keys.iterator(); iter.hasNext();) { 205 String key = (String ) 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 s) throws ReadOnlyException { 213 if (s == null) 214 throw new IllegalArgumentException ("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 defaultValues = preferenceType.getValues(); 227 for (int i = 0; i < defaultValues.size(); i++) { 228 wrapper.addValue((String ) defaultValues.get(i)); 229 } 230 } 231 modifiedPreferences_.remove(s); 232 if(persister_.getPortletPreferences(windowID_) != null) 233 save(preferences_); 234 } catch (Exception e) { 235 throw new RuntimeException ("can not remove preference", e); 236 } 237 } 238 239 246 public void store() throws IOException , ValidatorException { 247 if (!isMethodCalledIsAction()) 248 throw new IllegalStateException ( 249 "the store() method can not be called from a render method"); 250 if (!isStateChangeAuthorized()) 251 throw new IllegalStateException ( 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 { 264 try { 265 persister_.savePortletPreferences(windowID_, preferences); 266 } catch (Exception ex) { 267 throw new IOException (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 |