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