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