001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.kernel.concurrent.LockRegistry;
018 import com.liferay.portal.kernel.dao.db.DB;
019 import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.exception.SystemException;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.Validator;
027 import com.liferay.portal.model.Portlet;
028 import com.liferay.portal.model.PortletConstants;
029 import com.liferay.portal.model.PortletPreferences;
030 import com.liferay.portal.model.PortletPreferencesIds;
031 import com.liferay.portal.service.base.PortletPreferencesLocalServiceBaseImpl;
032 import com.liferay.portal.util.PropsValues;
033 import com.liferay.portlet.BasePreferencesImpl;
034 import com.liferay.portlet.PortletPreferencesFactoryUtil;
035 import com.liferay.portlet.PortletPreferencesImpl;
036 import com.liferay.portlet.PortletPreferencesThreadLocal;
037
038 import java.io.Serializable;
039
040 import java.util.List;
041 import java.util.Map;
042 import java.util.concurrent.locks.Lock;
043
044
048 public class PortletPreferencesLocalServiceImpl
049 extends PortletPreferencesLocalServiceBaseImpl {
050
051 public PortletPreferences addPortletPreferences(
052 long companyId, long ownerId, int ownerType, long plid,
053 String portletId, Portlet portlet, String defaultPreferences)
054 throws SystemException {
055
056 long portletPreferencesId = counterLocalService.increment();
057
058 PortletPreferences portletPreferences =
059 portletPreferencesPersistence.create(portletPreferencesId);
060
061 portletPreferences.setOwnerId(ownerId);
062 portletPreferences.setOwnerType(ownerType);
063 portletPreferences.setPlid(plid);
064 portletPreferences.setPortletId(portletId);
065
066 if (Validator.isNull(defaultPreferences)) {
067 if (portlet == null) {
068 defaultPreferences = PortletConstants.DEFAULT_PREFERENCES;
069 }
070 else {
071 defaultPreferences = portlet.getDefaultPreferences();
072 }
073 }
074
075 portletPreferences.setPreferences(defaultPreferences);
076
077 try {
078 portletPreferencesPersistence.update(portletPreferences, false);
079 }
080 catch (SystemException se) {
081 if (_log.isWarnEnabled()) {
082 _log.warn(
083 "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
084 ownerType + ", plid=" + plid + ", portletId=" +
085 portletId + "}");
086 }
087
088 portletPreferences = portletPreferencesPersistence.fetchByO_O_P_P(
089 ownerId, ownerType, plid, portletId, false);
090
091 if (portletPreferences == null) {
092 throw se;
093 }
094 }
095
096 return portletPreferences;
097 }
098
099 @Override
100 public void deletePortletPreferences(long portletPreferencesId)
101 throws PortalException, SystemException {
102
103 PortletPreferences portletPreferences =
104 portletPreferencesPersistence.findByPrimaryKey(
105 portletPreferencesId);
106
107 deletePortletPreferences(portletPreferences);
108 }
109
110 public void deletePortletPreferences(long ownerId, int ownerType, long plid)
111 throws SystemException {
112
113 portletPreferencesPersistence.removeByO_O_P(ownerId, ownerType, plid);
114
115 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
116 }
117
118 public void deletePortletPreferences(
119 long ownerId, int ownerType, long plid, String portletId)
120 throws PortalException, SystemException {
121
122 PortletPreferences portletPreferences =
123 portletPreferencesPersistence.findByO_O_P_P(
124 ownerId, ownerType, plid, portletId);
125
126 deletePortletPreferences(portletPreferences);
127 }
128
129 @Override
130 public void deletePortletPreferences(PortletPreferences portletPreferences)
131 throws SystemException {
132
133 long ownerId = portletPreferences.getOwnerId();
134 int ownerType = portletPreferences.getOwnerType();
135
136 portletPreferencesPersistence.remove(portletPreferences);
137
138 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
139 }
140
141 public javax.portlet.PortletPreferences getDefaultPreferences(
142 long companyId, String portletId)
143 throws SystemException {
144
145 Portlet portlet = portletLocalService.getPortletById(
146 companyId, portletId);
147
148 return PortletPreferencesFactoryUtil.fromDefaultXML(
149 portlet.getDefaultPreferences());
150 }
151
152 public List<PortletPreferences> getPortletPreferences()
153 throws SystemException {
154
155 return portletPreferencesPersistence.findAll();
156 }
157
158 public List<PortletPreferences> getPortletPreferences(
159 long ownerId, int ownerType, long plid)
160 throws SystemException {
161
162 return portletPreferencesPersistence.findByO_O_P(
163 ownerId, ownerType, plid);
164 }
165
166 public PortletPreferences getPortletPreferences(
167 long ownerId, int ownerType, long plid, String portletId)
168 throws PortalException, SystemException {
169
170 return portletPreferencesPersistence.findByO_O_P_P(
171 ownerId, ownerType, plid, portletId);
172 }
173
174 public List<PortletPreferences> getPortletPreferences(
175 long companyId, long groupId, long ownerId, int ownerType,
176 String portletId, boolean privateLayout)
177 throws SystemException {
178
179 return portletPreferencesFinder.findByC_G_O_O_P_P(
180 companyId, groupId, ownerId, ownerType, portletId, privateLayout);
181 }
182
183 public List<PortletPreferences> getPortletPreferences(
184 long plid, String portletId)
185 throws SystemException {
186
187 return portletPreferencesPersistence.findByP_P(plid, portletId);
188 }
189
190 public List<PortletPreferences> getPortletPreferencesByPlid(long plid)
191 throws SystemException {
192
193 return portletPreferencesPersistence.findByPlid(plid);
194 }
195
196 public javax.portlet.PortletPreferences getPreferences(
197 long companyId, long ownerId, int ownerType, long plid,
198 String portletId)
199 throws SystemException {
200
201 return getPreferences(
202 companyId, ownerId, ownerType, plid, portletId, null);
203 }
204
205 public javax.portlet.PortletPreferences getPreferences(
206 long companyId, long ownerId, int ownerType, long plid,
207 String portletId, String defaultPreferences)
208 throws SystemException {
209
210 DB db = DBFactoryUtil.getDB();
211
212 String dbType = db.getType();
213
214 if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
215 return doGetPreferences(
216 companyId, ownerId, ownerType, plid, portletId,
217 defaultPreferences);
218 }
219
220 StringBundler sb = new StringBundler(7);
221
222 sb.append(ownerId);
223 sb.append(StringPool.POUND);
224 sb.append(ownerType);
225 sb.append(StringPool.POUND);
226 sb.append(plid);
227 sb.append(StringPool.POUND);
228 sb.append(portletId);
229
230 String groupName = getClass().getName();
231 String key = sb.toString();
232
233 Lock lock = LockRegistry.allocateLock(groupName, key);
234
235 lock.lock();
236
237 try {
238 return doGetPreferences(
239 companyId, ownerId, ownerType, plid, portletId,
240 defaultPreferences);
241 }
242 finally {
243 lock.unlock();
244
245 LockRegistry.freeLock(groupName, key);
246 }
247 }
248
249 public javax.portlet.PortletPreferences getPreferences(
250 PortletPreferencesIds portletPreferencesIds)
251 throws SystemException {
252
253 return getPreferences(
254 portletPreferencesIds.getCompanyId(),
255 portletPreferencesIds.getOwnerId(),
256 portletPreferencesIds.getOwnerType(),
257 portletPreferencesIds.getPlid(),
258 portletPreferencesIds.getPortletId());
259 }
260
261 public javax.portlet.PortletPreferences getStrictPreferences(
262 long companyId, long ownerId, int ownerType, long plid,
263 String portletId)
264 throws SystemException {
265
266 boolean strict = PortletPreferencesThreadLocal.isStrict();
267
268 PortletPreferencesThreadLocal.setStrict(!PropsValues.TCK_URL);
269
270 try {
271 return getPreferences(
272 companyId, ownerId, ownerType, plid, portletId, null);
273 }
274 finally {
275 PortletPreferencesThreadLocal.setStrict(strict);
276 }
277 }
278
279 public javax.portlet.PortletPreferences getStrictPreferences(
280 PortletPreferencesIds portletPreferencesIds)
281 throws SystemException {
282
283 return getStrictPreferences(
284 portletPreferencesIds.getCompanyId(),
285 portletPreferencesIds.getOwnerId(),
286 portletPreferencesIds.getOwnerType(),
287 portletPreferencesIds.getPlid(),
288 portletPreferencesIds.getPortletId());
289 }
290
291 public PortletPreferences updatePreferences(
292 long ownerId, int ownerType, long plid, String portletId,
293 javax.portlet.PortletPreferences portletPreferences)
294 throws SystemException {
295
296 String xml = PortletPreferencesFactoryUtil.toXML(portletPreferences);
297
298 return updatePreferences(ownerId, ownerType, plid, portletId, xml);
299 }
300
301 public PortletPreferences updatePreferences(
302 long ownerId, int ownerType, long plid, String portletId,
303 String xml)
304 throws SystemException {
305
306 PortletPreferences portletPreferences =
307 portletPreferencesPersistence.fetchByO_O_P_P(
308 ownerId, ownerType, plid, portletId);
309
310 if (portletPreferences == null) {
311 long portletPreferencesId = counterLocalService.increment();
312
313 portletPreferences = portletPreferencesPersistence.create(
314 portletPreferencesId);
315
316 portletPreferences.setOwnerId(ownerId);
317 portletPreferences.setOwnerType(ownerType);
318 portletPreferences.setPlid(plid);
319 portletPreferences.setPortletId(portletId);
320 }
321
322 portletPreferences.setPreferences(xml);
323
324 portletPreferencesPersistence.update(portletPreferences, false);
325
326 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
327
328 return portletPreferences;
329 }
330
331 protected javax.portlet.PortletPreferences doGetPreferences(
332 long companyId, long ownerId, int ownerType, long plid,
333 String portletId, String defaultPreferences)
334 throws SystemException {
335
336 Map<Serializable, BasePreferencesImpl> preferencesPool =
337 PortletPreferencesLocalUtil.getPreferencesPool(ownerId, ownerType);
338
339 PreferencesKey preferencesKey = new PreferencesKey(plid, portletId);
340
341 PortletPreferencesImpl portletPreferencesImpl =
342 (PortletPreferencesImpl)preferencesPool.get(preferencesKey);
343
344 if (portletPreferencesImpl == null) {
345 Portlet portlet = portletLocalService.getPortletById(
346 companyId, portletId);
347
348 PortletPreferences portletPreferences =
349 portletPreferencesPersistence.fetchByO_O_P_P(
350 ownerId, ownerType, plid, portletId);
351
352 if (portletPreferences == null) {
353 if (PortletPreferencesThreadLocal.isStrict() &&
354 (Validator.isNull(defaultPreferences) ||
355 ((portlet != null) && portlet.isUndeployedPortlet()))) {
356
357 return new PortletPreferencesImpl();
358 }
359
360 portletPreferences =
361 portletPreferencesLocalService.addPortletPreferences(
362 companyId, ownerId, ownerType, plid, portletId, portlet,
363 defaultPreferences);
364 }
365
366 portletPreferencesImpl =
367 (PortletPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
368 companyId, ownerId, ownerType, plid, portletId,
369 portletPreferences.getPreferences());
370
371 synchronized (preferencesPool) {
372 preferencesPool.put(preferencesKey, portletPreferencesImpl);
373 }
374 }
375
376 return (PortletPreferencesImpl)portletPreferencesImpl.clone();
377 }
378
379 private static Log _log = LogFactoryUtil.getLog(
380 PortletPreferencesLocalServiceImpl.class);
381
382 private static class PreferencesKey implements Serializable {
383
384 public PreferencesKey(long plid, String portletId) {
385 _plid = plid;
386 _portletId = portletId;
387 }
388
389 @Override
390 public boolean equals(Object obj) {
391 PreferencesKey preferencesKey = (PreferencesKey)obj;
392
393 if ((preferencesKey._plid == _plid) &&
394 (preferencesKey._portletId.equals(_portletId))) {
395
396 return true;
397 }
398 else {
399 return false;
400 }
401 }
402
403 @Override
404 public int hashCode() {
405 return (int)(_plid * 11 + _portletId.hashCode());
406 }
407
408 private static final long serialVersionUID = 1L;
409
410 private final long _plid;
411 private final String _portletId;
412
413 }
414
415 }