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 int ownerType, long plid, String portletId)
160 throws SystemException {
161
162 return portletPreferencesPersistence.findByO_P_P(
163 ownerType, plid, portletId);
164 }
165
166 public List<PortletPreferences> getPortletPreferences(
167 long ownerId, int ownerType, long plid)
168 throws SystemException {
169
170 return portletPreferencesPersistence.findByO_O_P(
171 ownerId, ownerType, plid);
172 }
173
174 public PortletPreferences getPortletPreferences(
175 long ownerId, int ownerType, long plid, String portletId)
176 throws PortalException, SystemException {
177
178 return portletPreferencesPersistence.findByO_O_P_P(
179 ownerId, ownerType, plid, portletId);
180 }
181
182 public List<PortletPreferences> getPortletPreferences(
183 long companyId, long groupId, long ownerId, int ownerType,
184 String portletId, boolean privateLayout)
185 throws SystemException {
186
187 return portletPreferencesFinder.findByC_G_O_O_P_P(
188 companyId, groupId, ownerId, ownerType, portletId, privateLayout);
189 }
190
191 public List<PortletPreferences> getPortletPreferences(
192 long plid, String portletId)
193 throws SystemException {
194
195 return portletPreferencesPersistence.findByP_P(plid, portletId);
196 }
197
198 public List<PortletPreferences> getPortletPreferencesByPlid(long plid)
199 throws SystemException {
200
201 return portletPreferencesPersistence.findByPlid(plid);
202 }
203
204 public javax.portlet.PortletPreferences getPreferences(
205 long companyId, long ownerId, int ownerType, long plid,
206 String portletId)
207 throws SystemException {
208
209 return getPreferences(
210 companyId, ownerId, ownerType, plid, portletId, null);
211 }
212
213 public javax.portlet.PortletPreferences getPreferences(
214 long companyId, long ownerId, int ownerType, long plid,
215 String portletId, String defaultPreferences)
216 throws SystemException {
217
218 DB db = DBFactoryUtil.getDB();
219
220 String dbType = db.getType();
221
222 if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
223 return doGetPreferences(
224 companyId, ownerId, ownerType, plid, portletId,
225 defaultPreferences);
226 }
227
228 StringBundler sb = new StringBundler(7);
229
230 sb.append(ownerId);
231 sb.append(StringPool.POUND);
232 sb.append(ownerType);
233 sb.append(StringPool.POUND);
234 sb.append(plid);
235 sb.append(StringPool.POUND);
236 sb.append(portletId);
237
238 String groupName = getClass().getName();
239 String key = sb.toString();
240
241 Lock lock = LockRegistry.allocateLock(groupName, key);
242
243 lock.lock();
244
245 try {
246 return doGetPreferences(
247 companyId, ownerId, ownerType, plid, portletId,
248 defaultPreferences);
249 }
250 finally {
251 lock.unlock();
252
253 LockRegistry.freeLock(groupName, key);
254 }
255 }
256
257 public javax.portlet.PortletPreferences getPreferences(
258 PortletPreferencesIds portletPreferencesIds)
259 throws SystemException {
260
261 return getPreferences(
262 portletPreferencesIds.getCompanyId(),
263 portletPreferencesIds.getOwnerId(),
264 portletPreferencesIds.getOwnerType(),
265 portletPreferencesIds.getPlid(),
266 portletPreferencesIds.getPortletId());
267 }
268
269 public javax.portlet.PortletPreferences getStrictPreferences(
270 long companyId, long ownerId, int ownerType, long plid,
271 String portletId)
272 throws SystemException {
273
274 boolean strict = PortletPreferencesThreadLocal.isStrict();
275
276 PortletPreferencesThreadLocal.setStrict(!PropsValues.TCK_URL);
277
278 try {
279 return getPreferences(
280 companyId, ownerId, ownerType, plid, portletId, null);
281 }
282 finally {
283 PortletPreferencesThreadLocal.setStrict(strict);
284 }
285 }
286
287 public javax.portlet.PortletPreferences getStrictPreferences(
288 PortletPreferencesIds portletPreferencesIds)
289 throws SystemException {
290
291 return getStrictPreferences(
292 portletPreferencesIds.getCompanyId(),
293 portletPreferencesIds.getOwnerId(),
294 portletPreferencesIds.getOwnerType(),
295 portletPreferencesIds.getPlid(),
296 portletPreferencesIds.getPortletId());
297 }
298
299 public PortletPreferences updatePreferences(
300 long ownerId, int ownerType, long plid, String portletId,
301 javax.portlet.PortletPreferences portletPreferences)
302 throws SystemException {
303
304 String xml = PortletPreferencesFactoryUtil.toXML(portletPreferences);
305
306 return updatePreferences(ownerId, ownerType, plid, portletId, xml);
307 }
308
309 public PortletPreferences updatePreferences(
310 long ownerId, int ownerType, long plid, String portletId,
311 String xml)
312 throws SystemException {
313
314 PortletPreferences portletPreferences =
315 portletPreferencesPersistence.fetchByO_O_P_P(
316 ownerId, ownerType, plid, portletId);
317
318 if (portletPreferences == null) {
319 long portletPreferencesId = counterLocalService.increment();
320
321 portletPreferences = portletPreferencesPersistence.create(
322 portletPreferencesId);
323
324 portletPreferences.setOwnerId(ownerId);
325 portletPreferences.setOwnerType(ownerType);
326 portletPreferences.setPlid(plid);
327 portletPreferences.setPortletId(portletId);
328 }
329
330 portletPreferences.setPreferences(xml);
331
332 portletPreferencesPersistence.update(portletPreferences, false);
333
334 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
335
336 return portletPreferences;
337 }
338
339 protected javax.portlet.PortletPreferences doGetPreferences(
340 long companyId, long ownerId, int ownerType, long plid,
341 String portletId, String defaultPreferences)
342 throws SystemException {
343
344 Map<Serializable, BasePreferencesImpl> preferencesPool =
345 PortletPreferencesLocalUtil.getPreferencesPool(ownerId, ownerType);
346
347 PreferencesKey preferencesKey = new PreferencesKey(plid, portletId);
348
349 PortletPreferencesImpl portletPreferencesImpl =
350 (PortletPreferencesImpl)preferencesPool.get(preferencesKey);
351
352 if (portletPreferencesImpl == null) {
353 Portlet portlet = portletLocalService.getPortletById(
354 companyId, portletId);
355
356 PortletPreferences portletPreferences =
357 portletPreferencesPersistence.fetchByO_O_P_P(
358 ownerId, ownerType, plid, portletId);
359
360 if (portletPreferences == null) {
361 if (PortletPreferencesThreadLocal.isStrict() &&
362 (Validator.isNull(defaultPreferences) ||
363 ((portlet != null) && portlet.isUndeployedPortlet()))) {
364
365 return new PortletPreferencesImpl();
366 }
367
368 portletPreferences =
369 portletPreferencesLocalService.addPortletPreferences(
370 companyId, ownerId, ownerType, plid, portletId, portlet,
371 defaultPreferences);
372 }
373
374 portletPreferencesImpl =
375 (PortletPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
376 companyId, ownerId, ownerType, plid, portletId,
377 portletPreferences.getPreferences());
378
379 synchronized (preferencesPool) {
380 preferencesPool.put(preferencesKey, portletPreferencesImpl);
381 }
382 }
383
384 return (PortletPreferencesImpl)portletPreferencesImpl.clone();
385 }
386
387 private static Log _log = LogFactoryUtil.getLog(
388 PortletPreferencesLocalServiceImpl.class);
389
390 private class PreferencesKey implements Serializable {
391
392 public PreferencesKey(long plid, String portletId) {
393 _plid = plid;
394 _portletId = portletId;
395 }
396
397 @Override
398 public boolean equals(Object obj) {
399 PreferencesKey preferencesKey = (PreferencesKey)obj;
400
401 if ((preferencesKey._plid == _plid) &&
402 (preferencesKey._portletId.equals(_portletId))) {
403
404 return true;
405 }
406 else {
407 return false;
408 }
409 }
410
411 @Override
412 public int hashCode() {
413 return (int)(_plid * 11 + _portletId.hashCode());
414 }
415
416 private static final long serialVersionUID = 1L;
417
418 private final long _plid;
419 private final String _portletId;
420
421 }
422
423 }