1   /**
2    * Copyright (c) 2000-2008 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.kernel.util.Validator;
28  import com.liferay.portal.model.Portlet;
29  import com.liferay.portal.service.PortletLocalServiceUtil;
30  import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.util.xml.XMLFormatter;
34  
35  import java.io.IOException;
36  import java.io.Serializable;
37  
38  import java.util.Collections;
39  import java.util.Enumeration;
40  import java.util.HashMap;
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<String, Preference>());
62      }
63  
64      public PortletPreferencesImpl(
65          long companyId, long ownerId, int ownerType, long plid,
66          String portletId, Map<String, Preference> 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<String, String[]> getMap() {
77          Map<String, String[]> map = new HashMap<String, String[]>();
78  
79          for (Map.Entry<String, Preference> entry : _preferences.entrySet()) {
80              String key = entry.getKey();
81              Preference preference = entry.getValue();
82  
83              map.put(key, _getActualValues(preference.getValues()));
84          }
85  
86          return Collections.unmodifiableMap(map);
87      }
88  
89      public Enumeration<String> getNames() {
90          return Collections.enumeration(_preferences.keySet());
91      }
92  
93      public String getValue(String key, String def) {
94          if (key == null) {
95              throw new IllegalArgumentException();
96          }
97  
98          Preference preference = _preferences.get(key);
99  
100         String[] values = null;
101 
102         if (preference != null) {
103             values = preference.getValues();
104         }
105 
106         String value = null;
107 
108         if (Validator.isNotNull(values)) {
109             value = _getActualValue(values[0]);
110         }
111 
112         if (Validator.isNull(value)) {
113             value = _getActualValue(def);
114         }
115 
116         return value;
117     }
118 
119     public void setValue(String key, String value) throws ReadOnlyException {
120         if (key == null) {
121             throw new IllegalArgumentException();
122         }
123 
124         value = _getXmlSafeValue(value);
125 
126         Preference preference = _preferences.get(key);
127 
128         if (preference == null) {
129             preference = new Preference(key, value);
130 
131             _preferences.put(key, preference);
132         }
133 
134         if (preference.isReadOnly()) {
135             throw new ReadOnlyException(key);
136         }
137         else {
138             preference.setValues(new String[] {value});
139         }
140     }
141 
142     public String[] getValues(String key, String[] def) {
143         if (key == null) {
144             throw new IllegalArgumentException();
145         }
146 
147         Preference preference = _preferences.get(key);
148 
149         String[] values = null;
150         if (preference != null) {
151             values = preference.getValues();
152         }
153 
154         if (Validator.isNotNull(values)) {
155             return _getActualValues(values);
156         }
157         else {
158             return _getActualValues(def);
159         }
160     }
161 
162     public void setValues(String key, String[] values)
163         throws ReadOnlyException {
164 
165         if (key == null) {
166             throw new IllegalArgumentException();
167         }
168 
169         values = _getXmlSafeValues(values);
170 
171         Preference preference = _preferences.get(key);
172 
173         if (preference == null) {
174             preference = new Preference(key, values);
175 
176             _preferences.put(key, preference);
177         }
178 
179         if (preference.isReadOnly()) {
180             throw new ReadOnlyException(key);
181         }
182         else {
183             preference.setValues(values);
184         }
185     }
186 
187     public boolean isReadOnly(String key) {
188         if (key == null) {
189             throw new IllegalArgumentException();
190         }
191 
192         Preference preference = _preferences.get(key);
193 
194         if (preference != null && preference.isReadOnly()) {
195             return true;
196         }
197         else {
198             return false;
199         }
200     }
201 
202     public void reset() {
203         _preferences.clear();
204     }
205 
206     public void reset(String key) throws ReadOnlyException {
207         if (isReadOnly(key)) {
208             throw new ReadOnlyException(key);
209         }
210 
211         if (_defaultPreferences == null) {
212             try {
213                 if ((_portletId != null) &&
214                     (!_portletId.equals(PortletKeys.LIFERAY_PORTAL))) {
215 
216                     _defaultPreferences = PortletPreferencesLocalServiceUtil.
217                         getDefaultPreferences(_companyId, _portletId);
218                 }
219             }
220             catch (Exception e) {
221                 _log.error(e, e);
222             }
223         }
224 
225         String[] defaultValues = null;
226 
227         if (_defaultPreferences != null) {
228             defaultValues = _defaultPreferences.getValues(key, defaultValues);
229         }
230 
231         if (defaultValues != null) {
232             setValues(key, defaultValues);
233         }
234         else {
235             _preferences.remove(key);
236         }
237     }
238 
239     public void store() throws IOException, ValidatorException {
240         if (_portletId == null) {
241             throw new UnsupportedOperationException();
242         }
243 
244         try {
245             Portlet portlet = PortletLocalServiceUtil.getPortletById(
246                 _companyId, _portletId);
247 
248             if (!_portletId.equals(PortletKeys.LIFERAY_PORTAL)) {
249                 PreferencesValidator prefsValidator =
250                     PortalUtil.getPreferencesValidator(portlet);
251 
252                 if (prefsValidator != null) {
253                     prefsValidator.validate(this);
254                 }
255             }
256 
257             PortletPreferencesLocalServiceUtil.updatePreferences(
258                 _ownerId, _ownerType, _plid, _portletId, this);
259         }
260         catch (PortalException pe) {
261             _log.error(pe, pe);
262 
263             throw new IOException(pe.getMessage());
264         }
265         catch (SystemException se) {
266             throw new IOException(se.getMessage());
267         }
268     }
269 
270     public Object clone() {
271         Map<String, Preference> preferencesClone =
272             new HashMap<String, Preference>();
273 
274         for (Map.Entry<String, Preference> entry : _preferences.entrySet()) {
275             String key = entry.getKey();
276             Preference preference = entry.getValue();
277 
278             preferencesClone.put(key, (Preference)preference.clone());
279         }
280 
281         return new PortletPreferencesImpl(
282             _companyId, _ownerId, _ownerType, _plid, _portletId,
283             preferencesClone);
284     }
285 
286     public boolean equals(Object obj) {
287         PortletPreferencesImpl portletPreferences = (PortletPreferencesImpl)obj;
288 
289         if (this == portletPreferences) {
290             return true;
291         }
292 
293         if ((getCompanyId() == portletPreferences.getCompanyId()) &&
294             (getOwnerId() == portletPreferences.getOwnerId()) &&
295             (getOwnerType() == portletPreferences.getOwnerType()) &&
296             (getPlid() == portletPreferences.getPlid()) &&
297             (getPortletId().equals(portletPreferences.getPortletId())) &&
298             (getMap().equals(portletPreferences.getMap()))) {
299 
300             return true;
301         }
302         else {
303             return false;
304         }
305     }
306 
307     protected long getCompanyId() {
308         return  _companyId;
309     }
310 
311     protected long getOwnerId() {
312         return _ownerId;
313     }
314 
315     protected int getOwnerType() {
316         return _ownerType;
317     }
318 
319     protected long getPlid() {
320         return _plid;
321     }
322 
323     protected String getPortletId() {
324         return _portletId;
325     }
326 
327     protected Map<String, Preference> getPreferences() {
328         return _preferences;
329     }
330 
331     private String _getActualValue(String value) {
332         if ((value == null) || (value.equals(_NULL_VALUE))) {
333             return null;
334         }
335         else {
336             return XMLFormatter.fromCompactSafe(value);
337         }
338     }
339 
340     private String[] _getActualValues(String[] values) {
341         if (values == null) {
342             return null;
343         }
344 
345         if ((values.length == 1) && (_getActualValue(values[0]) == null)) {
346             return null;
347         }
348 
349         String[] actualValues = new String[values.length];
350 
351         System.arraycopy(values, 0, actualValues, 0, values.length);
352 
353         for (int i = 0; i < actualValues.length; i++) {
354             actualValues[i] = _getActualValue(actualValues[i]);
355         }
356 
357         return actualValues;
358     }
359 
360     private String _getXmlSafeValue(String value) {
361         if (value == null) {
362             return _NULL_VALUE;
363         }
364         else {
365             return XMLFormatter.toCompactSafe(value);
366         }
367     }
368 
369     private String[] _getXmlSafeValues(String[] values) {
370         if (values == null) {
371             return new String[] {
372                     _getXmlSafeValue(null)
373                 };
374         }
375 
376         String[] xmlSafeValues = new String[values.length];
377 
378         System.arraycopy(values, 0, xmlSafeValues, 0, values.length);
379 
380         for (int i = 0; i < xmlSafeValues.length; i++) {
381             if (xmlSafeValues[i] == null) {
382                 xmlSafeValues[i] = _getXmlSafeValue(xmlSafeValues[i]);
383             }
384         }
385 
386         return xmlSafeValues;
387     }
388 
389     private static final String _NULL_VALUE = "NULL_VALUE";
390 
391     private static Log _log = LogFactory.getLog(PortletPreferencesImpl.class);
392 
393     private long _companyId;
394     private long _ownerId;
395     private int _ownerType;
396     private long _plid;
397     private String _portletId;
398     private Map<String, Preference> _preferences;
399     private PortletPreferences _defaultPreferences;
400 
401 }