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