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