001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.model.Portlet;
026: import com.liferay.portal.service.PortletLocalServiceUtil;
027: import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
028: import com.liferay.portal.util.PortalUtil;
029: import com.liferay.portal.util.PortletKeys;
030: import com.liferay.util.xml.XMLFormatter;
031:
032: import java.io.IOException;
033: import java.io.Serializable;
034:
035: import java.util.Collections;
036: import java.util.Enumeration;
037: import java.util.HashMap;
038: import java.util.Iterator;
039: import java.util.Map;
040:
041: import javax.portlet.PortletPreferences;
042: import javax.portlet.PreferencesValidator;
043: import javax.portlet.ReadOnlyException;
044: import javax.portlet.ValidatorException;
045:
046: import org.apache.commons.logging.Log;
047: import org.apache.commons.logging.LogFactory;
048:
049: /**
050: * <a href="PortletPreferencesImpl.java.html"><b><i>View Source</i></b></a>
051: *
052: * @author Brian Wing Shun Chan
053: *
054: */
055: public class PortletPreferencesImpl implements Cloneable,
056: PortletPreferences, Serializable {
057:
058: public PortletPreferencesImpl() {
059: this (0, 0, 0, 0, null, new HashMap());
060: }
061:
062: public PortletPreferencesImpl(long companyId, long ownerId,
063: int ownerType, long plid, String portletId, Map preferences) {
064:
065: _companyId = companyId;
066: _ownerId = ownerId;
067: _ownerType = ownerType;
068: _plid = plid;
069: _portletId = portletId;
070: _preferences = preferences;
071: }
072:
073: public Map getMap() {
074: Map map = new HashMap();
075:
076: Iterator itr = _preferences.entrySet().iterator();
077:
078: while (itr.hasNext()) {
079: Map.Entry entry = (Map.Entry) itr.next();
080:
081: String key = (String) entry.getKey();
082: Preference preference = (Preference) entry.getValue();
083:
084: map.put(key, _getActualValues(preference.getValues()));
085: }
086:
087: return Collections.unmodifiableMap(map);
088: }
089:
090: public Enumeration getNames() {
091: return Collections.enumeration(_preferences.keySet());
092: }
093:
094: public String getValue(String key, String def) {
095: if (key == null) {
096: throw new IllegalArgumentException();
097: }
098:
099: Preference preference = (Preference) _preferences.get(key);
100:
101: String[] values = null;
102:
103: if (preference != null) {
104: values = preference.getValues();
105: }
106:
107: if (values != null && values.length > 0) {
108: return _getActualValue(values[0]);
109: } else {
110: return _getActualValue(def);
111: }
112: }
113:
114: public void setValue(String key, String value)
115: throws ReadOnlyException {
116: if (key == null) {
117: throw new IllegalArgumentException();
118: }
119:
120: value = _getXmlSafeValue(value);
121:
122: Preference preference = (Preference) _preferences.get(key);
123:
124: if (preference == null) {
125: preference = new Preference(key, value);
126:
127: _preferences.put(key, preference);
128: }
129:
130: if (preference.isReadOnly()) {
131: throw new ReadOnlyException(key);
132: } else {
133: preference.setValues(new String[] { value });
134: }
135: }
136:
137: public String[] getValues(String key, String[] def) {
138: if (key == null) {
139: throw new IllegalArgumentException();
140: }
141:
142: Preference preference = (Preference) _preferences.get(key);
143:
144: String[] values = null;
145: if (preference != null) {
146: values = preference.getValues();
147: }
148:
149: if (values != null && values.length > 0) {
150: return _getActualValues(values);
151: } else {
152: return _getActualValues(def);
153: }
154: }
155:
156: public void setValues(String key, String[] values)
157: throws ReadOnlyException {
158:
159: if (key == null) {
160: throw new IllegalArgumentException();
161: }
162:
163: values = _getXmlSafeValues(values);
164:
165: Preference preference = (Preference) _preferences.get(key);
166:
167: if (preference == null) {
168: preference = new Preference(key, values);
169:
170: _preferences.put(key, preference);
171: }
172:
173: if (preference.isReadOnly()) {
174: throw new ReadOnlyException(key);
175: } else {
176: preference.setValues(values);
177: }
178: }
179:
180: public boolean isReadOnly(String key) {
181: if (key == null) {
182: throw new IllegalArgumentException();
183: }
184:
185: Preference preference = (Preference) _preferences.get(key);
186:
187: if (preference != null && preference.isReadOnly()) {
188: return true;
189: } else {
190: return false;
191: }
192: }
193:
194: public void reset() {
195: _preferences.clear();
196: }
197:
198: public void reset(String key) throws ReadOnlyException {
199: if (isReadOnly(key)) {
200: throw new ReadOnlyException(key);
201: }
202:
203: if (_defaultPreferences == null) {
204: try {
205: if ((_portletId != null)
206: && (!_portletId
207: .equals(PortletKeys.LIFERAY_PORTAL))) {
208:
209: _defaultPreferences = PortletPreferencesLocalServiceUtil
210: .getDefaultPreferences(_companyId,
211: _portletId);
212: }
213: } catch (Exception e) {
214: _log.error(e, e);
215: }
216: }
217:
218: String[] defaultValues = null;
219:
220: if (_defaultPreferences != null) {
221: defaultValues = _defaultPreferences.getValues(key,
222: defaultValues);
223: }
224:
225: if (defaultValues != null) {
226: setValues(key, defaultValues);
227: } else {
228: _preferences.remove(key);
229: }
230: }
231:
232: public void store() throws IOException, ValidatorException {
233: if (_portletId == null) {
234: throw new UnsupportedOperationException();
235: }
236:
237: try {
238: Portlet portlet = PortletLocalServiceUtil.getPortletById(
239: _companyId, _portletId);
240:
241: if (!_portletId.equals(PortletKeys.LIFERAY_PORTAL)) {
242: PreferencesValidator prefsValidator = PortalUtil
243: .getPreferencesValidator(portlet);
244:
245: if (prefsValidator != null) {
246: prefsValidator.validate(this );
247: }
248: }
249:
250: PortletPreferencesLocalServiceUtil.updatePreferences(
251: _ownerId, _ownerType, _plid, _portletId, this );
252: } catch (PortalException pe) {
253: _log.error(pe, pe);
254:
255: throw new IOException(pe.getMessage());
256: } catch (SystemException se) {
257: throw new IOException(se.getMessage());
258: }
259: }
260:
261: public Object clone() {
262: Map preferencesClone = new HashMap();
263:
264: Iterator itr = _preferences.entrySet().iterator();
265:
266: while (itr.hasNext()) {
267: Map.Entry entry = (Map.Entry) itr.next();
268:
269: String key = (String) entry.getKey();
270: Preference preference = (Preference) entry.getValue();
271:
272: preferencesClone.put(key, preference.clone());
273: }
274:
275: return new PortletPreferencesImpl(_companyId, _ownerId,
276: _ownerType, _plid, _portletId, preferencesClone);
277: }
278:
279: protected long getCompanyId() {
280: return _companyId;
281: }
282:
283: protected long getOwnerId() {
284: return _ownerId;
285: }
286:
287: protected int getOwnerType() {
288: return _ownerType;
289: }
290:
291: protected long getPlid() {
292: return _plid;
293: }
294:
295: protected String getPortletId() {
296: return _portletId;
297: }
298:
299: protected Map getPreferences() {
300: return _preferences;
301: }
302:
303: private String _getActualValue(String value) {
304: if ((value == null) || (value.equals(_NULL_VALUE))) {
305: return null;
306: } else {
307: return XMLFormatter.fromCompactSafe(value);
308: }
309: }
310:
311: private String[] _getActualValues(String[] values) {
312: if (values == null) {
313: return null;
314: }
315:
316: if ((values.length == 1)
317: && (_getActualValue(values[0]) == null)) {
318: return null;
319: }
320:
321: String[] actualValues = new String[values.length];
322:
323: System.arraycopy(values, 0, actualValues, 0, values.length);
324:
325: for (int i = 0; i < actualValues.length; i++) {
326: actualValues[i] = _getActualValue(actualValues[i]);
327: }
328:
329: return actualValues;
330: }
331:
332: private String _getXmlSafeValue(String value) {
333: if (value == null) {
334: return _NULL_VALUE;
335: } else {
336: return XMLFormatter.toCompactSafe(value);
337: }
338: }
339:
340: private String[] _getXmlSafeValues(String[] values) {
341: if (values == null) {
342: return new String[] { _getXmlSafeValue(null) };
343: }
344:
345: String[] xmlSafeValues = new String[values.length];
346:
347: System.arraycopy(values, 0, xmlSafeValues, 0, values.length);
348:
349: for (int i = 0; i < xmlSafeValues.length; i++) {
350: if (xmlSafeValues[i] == null) {
351: xmlSafeValues[i] = _getXmlSafeValue(xmlSafeValues[i]);
352: }
353: }
354:
355: return xmlSafeValues;
356: }
357:
358: private static final String _NULL_VALUE = "NULL_VALUE";
359:
360: private static Log _log = LogFactory
361: .getLog(PortletPreferencesImpl.class);
362:
363: private long _companyId;
364: private long _ownerId;
365: private int _ownerType;
366: private long _plid;
367: private String _portletId;
368: private Map _preferences;
369: private PortletPreferences _defaultPreferences;
370:
371: }
|