1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.model.Portlet;
28  import com.liferay.portal.service.PortletLocalServiceUtil;
29  import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.PortletKeys;
32  import com.liferay.util.xml.XMLFormatter;
33  
34  import java.io.IOException;
35  import java.io.Serializable;
36  
37  import java.util.Collections;
38  import java.util.Enumeration;
39  import java.util.HashMap;
40  import java.util.Iterator;
41  import java.util.Map;
42  
43  import javax.portlet.PortletPreferences;
44  import javax.portlet.PreferencesValidator;
45  import javax.portlet.ReadOnlyException;
46  import javax.portlet.ValidatorException;
47  
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
50  
51  /**
52   * <a href="PortletPreferencesImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class PortletPreferencesImpl
58      implements Cloneable, PortletPreferences, Serializable {
59  
60      public PortletPreferencesImpl() {
61          this(0, 0, 0, 0, null, new HashMap());
62      }
63  
64      public PortletPreferencesImpl(long companyId, long ownerId, int ownerType,
65                                    long plid, String portletId,
66                                    Map preferences) {
67  
68          _companyId = companyId;
69          _ownerId = ownerId;
70          _ownerType = ownerType;
71          _plid = plid;
72          _portletId = portletId;
73          _preferences = preferences;
74      }
75  
76      public Map getMap() {
77          Map map = new HashMap();
78  
79          Iterator itr = _preferences.entrySet().iterator();
80  
81          while (itr.hasNext()) {
82              Map.Entry entry = (Map.Entry)itr.next();
83  
84              String key = (String)entry.getKey();
85              Preference preference = (Preference)entry.getValue();
86  
87              map.put(key, _getActualValues(preference.getValues()));
88          }
89  
90          return Collections.unmodifiableMap(map);
91      }
92  
93      public Enumeration getNames() {
94          return Collections.enumeration(_preferences.keySet());
95      }
96  
97      public String getValue(String key, String def) {
98          if (key == null) {
99              throw new IllegalArgumentException();
100         }
101 
102         Preference preference = (Preference)_preferences.get(key);
103 
104         String[] values = null;
105 
106         if (preference != null) {
107             values = preference.getValues();
108         }
109 
110         if (values != null && values.length > 0) {
111             return _getActualValue(values[0]);
112         }
113         else {
114             return _getActualValue(def);
115         }
116     }
117 
118     public void setValue(String key, String value) throws ReadOnlyException {
119         if (key == null) {
120             throw new IllegalArgumentException();
121         }
122 
123         value = _getXmlSafeValue(value);
124 
125         Preference preference = (Preference)_preferences.get(key);
126 
127         if (preference == null) {
128             preference = new Preference(key, value);
129 
130             _preferences.put(key, preference);
131         }
132 
133         if (preference.isReadOnly()) {
134             throw new ReadOnlyException(key);
135         }
136         else {
137             preference.setValues(new String[] {value});
138         }
139     }
140 
141     public String[] getValues(String key, String[] def) {
142         if (key == null) {
143             throw new IllegalArgumentException();
144         }
145 
146         Preference preference = (Preference)_preferences.get(key);
147 
148         String[] values = null;
149         if (preference != null) {
150             values = preference.getValues();
151         }
152 
153         if (values != null && values.length > 0) {
154             return _getActualValues(values);
155         }
156         else {
157             return _getActualValues(def);
158         }
159     }
160 
161     public void setValues(String key, String[] values)
162         throws ReadOnlyException {
163 
164         if (key == null) {
165             throw new IllegalArgumentException();
166         }
167 
168         values = _getXmlSafeValues(values);
169 
170         Preference preference = (Preference)_preferences.get(key);
171 
172         if (preference == null) {
173             preference = new Preference(key, values);
174 
175             _preferences.put(key, preference);
176         }
177 
178         if (preference.isReadOnly()) {
179             throw new ReadOnlyException(key);
180         }
181         else {
182             preference.setValues(values);
183         }
184     }
185 
186     public boolean isReadOnly(String key) {
187         if (key == null) {
188             throw new IllegalArgumentException();
189         }
190 
191         Preference preference = (Preference)_preferences.get(key);
192 
193         if (preference != null && preference.isReadOnly()) {
194             return true;
195         }
196         else {
197             return false;
198         }
199     }
200 
201     public void reset() {
202         _preferences.clear();
203     }
204 
205     public void reset(String key) throws ReadOnlyException {
206         if (isReadOnly(key)) {
207             throw new ReadOnlyException(key);
208         }
209 
210         if (_defaultPreferences == null) {
211             try {
212                 if ((_portletId != null) &&
213                     (!_portletId.equals(PortletKeys.LIFERAY_PORTAL))) {
214 
215                     _defaultPreferences = PortletPreferencesLocalServiceUtil.
216                         getDefaultPreferences(_companyId, _portletId);
217                 }
218             }
219             catch (Exception e) {
220                 _log.error(e, e);
221             }
222         }
223 
224         String[] defaultValues = null;
225 
226         if (_defaultPreferences != null) {
227             defaultValues = _defaultPreferences.getValues(key, defaultValues);
228         }
229 
230         if (defaultValues != null) {
231             setValues(key, defaultValues);
232         }
233         else {
234             _preferences.remove(key);
235         }
236     }
237 
238     public void store() throws IOException, ValidatorException {
239         if (_portletId == null) {
240             throw new UnsupportedOperationException();
241         }
242 
243         try {
244             Portlet portlet = PortletLocalServiceUtil.getPortletById(
245                 _companyId, _portletId);
246 
247             if (!_portletId.equals(PortletKeys.LIFERAY_PORTAL)) {
248                 PreferencesValidator prefsValidator =
249                     PortalUtil.getPreferencesValidator(portlet);
250 
251                 if (prefsValidator != null) {
252                     prefsValidator.validate(this);
253                 }
254             }
255 
256             PortletPreferencesLocalServiceUtil.updatePreferences(
257                 _ownerId, _ownerType, _plid, _portletId, this);
258         }
259         catch (PortalException pe) {
260             _log.error(pe, pe);
261 
262             throw new IOException(pe.getMessage());
263         }
264         catch (SystemException se) {
265             throw new IOException(se.getMessage());
266         }
267     }
268 
269     public Object clone() {
270         Map preferencesClone = new HashMap();
271 
272         Iterator itr = _preferences.entrySet().iterator();
273 
274         while (itr.hasNext()) {
275             Map.Entry entry = (Map.Entry)itr.next();
276 
277             String key = (String)entry.getKey();
278             Preference preference = (Preference)entry.getValue();
279 
280             preferencesClone.put(key, preference.clone());
281         }
282 
283         return new PortletPreferencesImpl(
284             _companyId, _ownerId, _ownerType, _plid, _portletId,
285             preferencesClone);
286     }
287 
288     protected long getCompanyId() {
289         return  _companyId;
290     }
291 
292     protected long getOwnerId() {
293         return _ownerId;
294     }
295 
296     protected int getOwnerType() {
297         return _ownerType;
298     }
299 
300     protected long getPlid() {
301         return _plid;
302     }
303 
304     protected String getPortletId() {
305         return _portletId;
306     }
307 
308     protected Map getPreferences() {
309         return _preferences;
310     }
311 
312     private String _getActualValue(String value) {
313         if ((value == null) || (value.equals(_NULL_VALUE))) {
314             return null;
315         }
316         else {
317             return XMLFormatter.fromCompactSafe(value);
318         }
319     }
320 
321     private String[] _getActualValues(String[] values) {
322         if (values == null) {
323             return null;
324         }
325 
326         if ((values.length == 1) && (_getActualValue(values[0]) == null)) {
327             return null;
328         }
329 
330         String[] actualValues = new String[values.length];
331 
332         System.arraycopy(values, 0, actualValues, 0, values.length);
333 
334         for (int i = 0; i < actualValues.length; i++) {
335             actualValues[i] = _getActualValue(actualValues[i]);
336         }
337 
338         return actualValues;
339     }
340 
341     private String _getXmlSafeValue(String value) {
342         if (value == null) {
343             return _NULL_VALUE;
344         }
345         else {
346             return XMLFormatter.toCompactSafe(value);
347         }
348     }
349 
350     private String[] _getXmlSafeValues(String[] values) {
351         if (values == null) {
352             return new String[] {
353                     _getXmlSafeValue(null)
354                 };
355         }
356 
357         String[] xmlSafeValues = new String[values.length];
358 
359         System.arraycopy(values, 0, xmlSafeValues, 0, values.length);
360 
361         for (int i = 0; i < xmlSafeValues.length; i++) {
362             if (xmlSafeValues[i] == null) {
363                 xmlSafeValues[i] = _getXmlSafeValue(xmlSafeValues[i]);
364             }
365         }
366 
367         return xmlSafeValues;
368     }
369 
370     private static final String _NULL_VALUE = "NULL_VALUE";
371 
372     private static Log _log = LogFactory.getLog(PortletPreferencesImpl.class);
373 
374     private long _companyId;
375     private long _ownerId;
376     private int _ownerType;
377     private long _plid;
378     private String _portletId;
379     private Map _preferences;
380     private PortletPreferences _defaultPreferences;
381 
382 }