1
22
23 package com.liferay.portlet;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.util.Validator;
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 String value = null;
106
107 if (Validator.isNotNull(values)) {
108 value = _getActualValue(values[0]);
109 }
110
111 if (Validator.isNull(value)) {
112 value = _getActualValue(def);
113 }
114
115 return value;
116 }
117
118 public void setValue(String key, String value) throws ReadOnlyException {
119 if (key == null) {
120 throw new IllegalArgumentException();
121 }
122
123 value = _getXmlSafeValue(value);
124
125 Preference preference = _preferences.get(key);
126
127 if (preference == null) {
128 preference = new Preference(key, value);
129
130 _preferences.put(key, preference);
131 }
132
133 if (preference.isReadOnly()) {
134 throw new ReadOnlyException(key);
135 }
136 else {
137 preference.setValues(new String[] {value});
138 }
139 }
140
141 public String[] getValues(String key, String[] def) {
142 if (key == null) {
143 throw new IllegalArgumentException();
144 }
145
146 Preference preference = _preferences.get(key);
147
148 String[] values = null;
149 if (preference != null) {
150 values = preference.getValues();
151 }
152
153 if (Validator.isNotNull(values)) {
154 return _getActualValues(values);
155 }
156 else {
157 return _getActualValues(def);
158 }
159 }
160
161 public void setValues(String key, String[] values)
162 throws ReadOnlyException {
163
164 if (key == null) {
165 throw new IllegalArgumentException();
166 }
167
168 values = _getXmlSafeValues(values);
169
170 Preference preference = _preferences.get(key);
171
172 if (preference == null) {
173 preference = new Preference(key, values);
174
175 _preferences.put(key, preference);
176 }
177
178 if (preference.isReadOnly()) {
179 throw new ReadOnlyException(key);
180 }
181 else {
182 preference.setValues(values);
183 }
184 }
185
186 public boolean isReadOnly(String key) {
187 if (key == null) {
188 throw new IllegalArgumentException();
189 }
190
191 Preference preference = _preferences.get(key);
192
193 if (preference != null && preference.isReadOnly()) {
194 return true;
195 }
196 else {
197 return false;
198 }
199 }
200
201 public void reset() {
202 _preferences.clear();
203 }
204
205 public void reset(String key) throws ReadOnlyException {
206 if (isReadOnly(key)) {
207 throw new ReadOnlyException(key);
208 }
209
210 if (_defaultPreferences == null) {
211 try {
212 if ((_portletId != null) &&
213 (!_portletId.equals(PortletKeys.LIFERAY_PORTAL))) {
214
215 _defaultPreferences = PortletPreferencesLocalServiceUtil.
216 getDefaultPreferences(_companyId, _portletId);
217 }
218 }
219 catch (Exception e) {
220 _log.error(e, e);
221 }
222 }
223
224 String[] defaultValues = null;
225
226 if (_defaultPreferences != null) {
227 defaultValues = _defaultPreferences.getValues(key, defaultValues);
228 }
229
230 if (defaultValues != null) {
231 setValues(key, defaultValues);
232 }
233 else {
234 _preferences.remove(key);
235 }
236 }
237
238 public void store() throws IOException, ValidatorException {
239 if (_portletId == null) {
240 throw new UnsupportedOperationException();
241 }
242
243 try {
244 Portlet portlet = PortletLocalServiceUtil.getPortletById(
245 _companyId, _portletId);
246
247 if (!_portletId.equals(PortletKeys.LIFERAY_PORTAL)) {
248 PreferencesValidator prefsValidator =
249 PortalUtil.getPreferencesValidator(portlet);
250
251 if (prefsValidator != null) {
252 prefsValidator.validate(this);
253 }
254 }
255
256 PortletPreferencesLocalServiceUtil.updatePreferences(
257 _ownerId, _ownerType, _plid, _portletId, this);
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 }