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