1
14
15 package com.liferay.portal.service.impl;
16
17 import com.liferay.portal.PortalException;
18 import com.liferay.portal.SystemException;
19 import com.liferay.portal.kernel.concurrent.LockRegistry;
20 import com.liferay.portal.kernel.dao.db.DB;
21 import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.util.StringBundler;
25 import com.liferay.portal.kernel.util.StringPool;
26 import com.liferay.portal.kernel.util.StringUtil;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.model.Portlet;
29 import com.liferay.portal.model.PortletConstants;
30 import com.liferay.portal.model.PortletPreferences;
31 import com.liferay.portal.model.PortletPreferencesIds;
32 import com.liferay.portal.service.base.PortletPreferencesLocalServiceBaseImpl;
33 import com.liferay.portlet.PortletPreferencesImpl;
34 import com.liferay.portlet.PortletPreferencesSerializer;
35 import com.liferay.portlet.PortletPreferencesThreadLocal;
36
37 import java.util.List;
38 import java.util.Map;
39 import java.util.concurrent.locks.Lock;
40
41
47 public class PortletPreferencesLocalServiceImpl
48 extends PortletPreferencesLocalServiceBaseImpl {
49
50 public PortletPreferences addPortletPreferences(
51 long companyId, long ownerId, int ownerType, long plid,
52 String portletId, Portlet portlet, String defaultPreferences)
53 throws SystemException {
54
55 long portletPreferencesId = counterLocalService.increment();
56
57 PortletPreferences portletPreferences =
58 portletPreferencesPersistence.create(portletPreferencesId);
59
60 portletPreferences.setOwnerId(ownerId);
61 portletPreferences.setOwnerType(ownerType);
62 portletPreferences.setPlid(plid);
63 portletPreferences.setPortletId(portletId);
64
65 if (Validator.isNull(defaultPreferences)) {
66 if (portlet == null) {
67 defaultPreferences =
68 PortletConstants.DEFAULT_PREFERENCES;
69 }
70 else {
71 defaultPreferences = portlet.getDefaultPreferences();
72 }
73 }
74
75 portletPreferences.setPreferences(defaultPreferences);
76
77 try {
78 portletPreferencesPersistence.update(portletPreferences, false);
79 }
80 catch (SystemException se) {
81 if (_log.isWarnEnabled()) {
82 _log.warn(
83 "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
84 ownerType + ", plid=" + plid + ", portletId=" +
85 portletId + "}");
86 }
87
88 portletPreferences = portletPreferencesPersistence.fetchByO_O_P_P(
89 ownerId, ownerType, plid, portletId, false);
90
91 if (portletPreferences == null) {
92 throw se;
93 }
94 }
95
96 return portletPreferences;
97 }
98
99 public void deletePortletPreferences(long portletPreferencesId)
100 throws PortalException, SystemException {
101
102 PortletPreferences portletPreferences =
103 portletPreferencesPersistence.findByPrimaryKey(
104 portletPreferencesId);
105
106 long ownerId = portletPreferences.getOwnerId();
107 int ownerType = portletPreferences.getOwnerType();
108
109 portletPreferencesPersistence.remove(portletPreferences);
110
111 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
112 }
113
114 public void deletePortletPreferences(long ownerId, int ownerType, long plid)
115 throws SystemException {
116
117 portletPreferencesPersistence.removeByO_O_P(ownerId, ownerType, plid);
118
119 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
120 }
121
122 public void deletePortletPreferences(
123 long ownerId, int ownerType, long plid, String portletId)
124 throws PortalException, SystemException {
125
126 portletPreferencesPersistence.removeByO_O_P_P(
127 ownerId, ownerType, plid, portletId);
128
129 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
130 }
131
132 public javax.portlet.PortletPreferences getDefaultPreferences(
133 long companyId, String portletId)
134 throws SystemException {
135
136 Portlet portlet = portletLocalService.getPortletById(
137 companyId, portletId);
138
139 return PortletPreferencesSerializer.fromDefaultXML(
140 portlet.getDefaultPreferences());
141 }
142
143 public List<PortletPreferences> getPortletPreferences()
144 throws SystemException {
145
146 return portletPreferencesPersistence.findAll();
147 }
148
149 public List<PortletPreferences> getPortletPreferences(
150 long plid, String portletId)
151 throws SystemException {
152
153 return portletPreferencesPersistence.findByP_P(plid, portletId);
154 }
155
156 public List<PortletPreferences> getPortletPreferences(
157 long ownerId, int ownerType, long plid)
158 throws SystemException {
159
160 return portletPreferencesPersistence.findByO_O_P(
161 ownerId, ownerType, plid);
162 }
163
164 public PortletPreferences getPortletPreferences(
165 long ownerId, int ownerType, long plid, String portletId)
166 throws PortalException, SystemException {
167
168 return portletPreferencesPersistence.findByO_O_P_P(
169 ownerId, ownerType, plid, portletId);
170 }
171
172 public List<PortletPreferences> getPortletPreferencesByPlid(long plid)
173 throws SystemException {
174
175 return portletPreferencesPersistence.findByPlid(plid);
176 }
177
178 public javax.portlet.PortletPreferences getPreferences(
179 PortletPreferencesIds portletPreferencesIds)
180 throws SystemException {
181
182 return getPreferences(
183 portletPreferencesIds.getCompanyId(),
184 portletPreferencesIds.getOwnerId(),
185 portletPreferencesIds.getOwnerType(),
186 portletPreferencesIds.getPlid(),
187 portletPreferencesIds.getPortletId());
188 }
189
190 public javax.portlet.PortletPreferences getPreferences(
191 long companyId, long ownerId, int ownerType, long plid,
192 String portletId)
193 throws SystemException {
194
195 return getPreferences(
196 companyId, ownerId, ownerType, plid, portletId, null);
197 }
198
199 public javax.portlet.PortletPreferences getPreferences(
200 long companyId, long ownerId, int ownerType, long plid,
201 String portletId, String defaultPreferences)
202 throws SystemException {
203
204 DB db = DBFactoryUtil.getDB();
205
206 if (!db.getType().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 PortletPreferences updatePreferences(
242 long ownerId, int ownerType, long plid, String portletId,
243 javax.portlet.PortletPreferences preferences)
244 throws SystemException {
245
246 PortletPreferencesImpl preferencesImpl =
247 (PortletPreferencesImpl)preferences;
248
249 String xml = PortletPreferencesSerializer.toXML(preferencesImpl);
250
251 return updatePreferences(ownerId, ownerType, plid, portletId, xml);
252 }
253
254 public PortletPreferences updatePreferences(
255 long ownerId, int ownerType, long plid, String portletId,
256 String xml)
257 throws SystemException {
258
259 PortletPreferences portletPreferences =
260 portletPreferencesPersistence.fetchByO_O_P_P(
261 ownerId, ownerType, plid, portletId);
262
263 if (portletPreferences == null) {
264 long portletPreferencesId = counterLocalService.increment();
265
266 portletPreferences = portletPreferencesPersistence.create(
267 portletPreferencesId);
268
269 portletPreferences.setOwnerId(ownerId);
270 portletPreferences.setOwnerType(ownerType);
271 portletPreferences.setPlid(plid);
272 portletPreferences.setPortletId(portletId);
273 }
274
275 portletPreferences.setPreferences(xml);
276
277 portletPreferencesPersistence.update(portletPreferences, false);
278
279 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
280
281 return portletPreferences;
282 }
283
284 protected javax.portlet.PortletPreferences doGetPreferences(
285 long companyId, long ownerId, int ownerType, long plid,
286 String portletId, String defaultPreferences)
287 throws SystemException {
288
289 Map<String, PortletPreferencesImpl> preferencesPool =
290 PortletPreferencesLocalUtil.getPreferencesPool(
291 ownerId, ownerType);
292
293 String key = encodeKey(plid, portletId);
294
295 PortletPreferencesImpl preferences = preferencesPool.get(key);
296
297 if (preferences == null) {
298 Portlet portlet = portletLocalService.getPortletById(
299 companyId, portletId);
300
301 PortletPreferences portletPreferences =
302 portletPreferencesPersistence.fetchByO_O_P_P(
303 ownerId, ownerType, plid, portletId);
304
305 if (portletPreferences == null) {
306 if ((portlet != null) && portlet.isUndeployedPortlet() &&
307 PortletPreferencesThreadLocal.isStrict()) {
308
309 return new PortletPreferencesImpl();
310 }
311
312 portletPreferences =
313 portletPreferencesLocalService.addPortletPreferences(
314 companyId, ownerId, ownerType, plid, portletId, portlet,
315 defaultPreferences);
316 }
317
318 preferences = PortletPreferencesSerializer.fromXML(
319 companyId, ownerId, ownerType, plid, portletId,
320 portletPreferences.getPreferences());
321
322 preferencesPool.put(key, preferences);
323 }
324
325 return (PortletPreferencesImpl)preferences.clone();
326 }
327
328 protected String encodeKey(long plid, String portletId) {
329 return StringUtil.toHexString(plid).concat(StringPool.POUND).concat(
330 portletId);
331 }
332
333 private static Log _log = LogFactoryUtil.getLog(
334 PortletPreferencesLocalServiceImpl.class);
335
336 }