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