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