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