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.portlet.PortletPreferencesImpl;
033 import com.liferay.portlet.PortletPreferencesSerializer;
034 import com.liferay.portlet.PortletPreferencesThreadLocal;
035
036 import java.util.List;
037 import java.util.Map;
038 import java.util.concurrent.locks.Lock;
039
040
043 public class PortletPreferencesLocalServiceImpl
044 extends PortletPreferencesLocalServiceBaseImpl {
045
046 public PortletPreferences addPortletPreferences(
047 long companyId, long ownerId, int ownerType, long plid,
048 String portletId, Portlet portlet, String defaultPreferences)
049 throws SystemException {
050
051 long portletPreferencesId = counterLocalService.increment();
052
053 PortletPreferences portletPreferences =
054 portletPreferencesPersistence.create(portletPreferencesId);
055
056 portletPreferences.setOwnerId(ownerId);
057 portletPreferences.setOwnerType(ownerType);
058 portletPreferences.setPlid(plid);
059 portletPreferences.setPortletId(portletId);
060
061 if (Validator.isNull(defaultPreferences)) {
062 if (portlet == null) {
063 defaultPreferences =
064 PortletConstants.DEFAULT_PREFERENCES;
065 }
066 else {
067 defaultPreferences = portlet.getDefaultPreferences();
068 }
069 }
070
071 portletPreferences.setPreferences(defaultPreferences);
072
073 try {
074 portletPreferencesPersistence.update(portletPreferences, false);
075 }
076 catch (SystemException se) {
077 if (_log.isWarnEnabled()) {
078 _log.warn(
079 "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
080 ownerType + ", plid=" + plid + ", portletId=" +
081 portletId + "}");
082 }
083
084 portletPreferences = portletPreferencesPersistence.fetchByO_O_P_P(
085 ownerId, ownerType, plid, portletId, false);
086
087 if (portletPreferences == null) {
088 throw se;
089 }
090 }
091
092 return portletPreferences;
093 }
094
095 public void deletePortletPreferences(long portletPreferencesId)
096 throws PortalException, SystemException {
097
098 PortletPreferences portletPreferences =
099 portletPreferencesPersistence.findByPrimaryKey(
100 portletPreferencesId);
101
102 long ownerId = portletPreferences.getOwnerId();
103 int ownerType = portletPreferences.getOwnerType();
104
105 portletPreferencesPersistence.remove(portletPreferences);
106
107 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
108 }
109
110 public void deletePortletPreferences(long ownerId, int ownerType, long plid)
111 throws SystemException {
112
113 portletPreferencesPersistence.removeByO_O_P(ownerId, ownerType, plid);
114
115 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
116 }
117
118 public void deletePortletPreferences(
119 long ownerId, int ownerType, long plid, String portletId)
120 throws PortalException, SystemException {
121
122 portletPreferencesPersistence.removeByO_O_P_P(
123 ownerId, ownerType, plid, portletId);
124
125 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
126 }
127
128 public javax.portlet.PortletPreferences getDefaultPreferences(
129 long companyId, String portletId)
130 throws SystemException {
131
132 Portlet portlet = portletLocalService.getPortletById(
133 companyId, portletId);
134
135 return PortletPreferencesSerializer.fromDefaultXML(
136 portlet.getDefaultPreferences());
137 }
138
139 public List<PortletPreferences> getPortletPreferences()
140 throws SystemException {
141
142 return portletPreferencesPersistence.findAll();
143 }
144
145 public List<PortletPreferences> getPortletPreferences(
146 long plid, String portletId)
147 throws SystemException {
148
149 return portletPreferencesPersistence.findByP_P(plid, portletId);
150 }
151
152 public List<PortletPreferences> getPortletPreferences(
153 long ownerId, int ownerType, long plid)
154 throws SystemException {
155
156 return portletPreferencesPersistence.findByO_O_P(
157 ownerId, ownerType, plid);
158 }
159
160 public PortletPreferences getPortletPreferences(
161 long ownerId, int ownerType, long plid, String portletId)
162 throws PortalException, SystemException {
163
164 return portletPreferencesPersistence.findByO_O_P_P(
165 ownerId, ownerType, plid, portletId);
166 }
167
168 public List<PortletPreferences> getPortletPreferencesByPlid(long plid)
169 throws SystemException {
170
171 return portletPreferencesPersistence.findByPlid(plid);
172 }
173
174 public javax.portlet.PortletPreferences getPreferences(
175 PortletPreferencesIds portletPreferencesIds)
176 throws SystemException {
177
178 return getPreferences(
179 portletPreferencesIds.getCompanyId(),
180 portletPreferencesIds.getOwnerId(),
181 portletPreferencesIds.getOwnerType(),
182 portletPreferencesIds.getPlid(),
183 portletPreferencesIds.getPortletId());
184 }
185
186 public javax.portlet.PortletPreferences getPreferences(
187 long companyId, long ownerId, int ownerType, long plid,
188 String portletId)
189 throws SystemException {
190
191 return getPreferences(
192 companyId, ownerId, ownerType, plid, portletId, null);
193 }
194
195 public javax.portlet.PortletPreferences getPreferences(
196 long companyId, long ownerId, int ownerType, long plid,
197 String portletId, String defaultPreferences)
198 throws SystemException {
199
200 DB db = DBFactoryUtil.getDB();
201
202 if (!db.getType().equals(DB.TYPE_HYPERSONIC)) {
203 return doGetPreferences(
204 companyId, ownerId, ownerType, plid, portletId,
205 defaultPreferences);
206 }
207
208 StringBundler sb = new StringBundler(7);
209
210 sb.append(ownerId);
211 sb.append(StringPool.POUND);
212 sb.append(ownerType);
213 sb.append(StringPool.POUND);
214 sb.append(plid);
215 sb.append(StringPool.POUND);
216 sb.append(portletId);
217
218 String groupName = getClass().getName();
219 String key = sb.toString();
220
221 Lock lock = LockRegistry.allocateLock(groupName, key);
222
223 lock.lock();
224
225 try {
226 return doGetPreferences(
227 companyId, ownerId, ownerType, plid, portletId,
228 defaultPreferences);
229 }
230 finally {
231 lock.unlock();
232
233 LockRegistry.freeLock(groupName, key);
234 }
235 }
236
237 public PortletPreferences updatePreferences(
238 long ownerId, int ownerType, long plid, String portletId,
239 javax.portlet.PortletPreferences preferences)
240 throws SystemException {
241
242 PortletPreferencesImpl preferencesImpl =
243 (PortletPreferencesImpl)preferences;
244
245 String xml = PortletPreferencesSerializer.toXML(preferencesImpl);
246
247 return updatePreferences(ownerId, ownerType, plid, portletId, xml);
248 }
249
250 public PortletPreferences updatePreferences(
251 long ownerId, int ownerType, long plid, String portletId,
252 String xml)
253 throws SystemException {
254
255 PortletPreferences portletPreferences =
256 portletPreferencesPersistence.fetchByO_O_P_P(
257 ownerId, ownerType, plid, portletId);
258
259 if (portletPreferences == null) {
260 long portletPreferencesId = counterLocalService.increment();
261
262 portletPreferences = portletPreferencesPersistence.create(
263 portletPreferencesId);
264
265 portletPreferences.setOwnerId(ownerId);
266 portletPreferences.setOwnerType(ownerType);
267 portletPreferences.setPlid(plid);
268 portletPreferences.setPortletId(portletId);
269 }
270
271 portletPreferences.setPreferences(xml);
272
273 portletPreferencesPersistence.update(portletPreferences, false);
274
275 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
276
277 return portletPreferences;
278 }
279
280 protected javax.portlet.PortletPreferences doGetPreferences(
281 long companyId, long ownerId, int ownerType, long plid,
282 String portletId, String defaultPreferences)
283 throws SystemException {
284
285 Map<String, PortletPreferencesImpl> preferencesPool =
286 PortletPreferencesLocalUtil.getPreferencesPool(
287 ownerId, ownerType);
288
289 String key = encodeKey(plid, portletId);
290
291 PortletPreferencesImpl preferences = preferencesPool.get(key);
292
293 if (preferences == null) {
294 Portlet portlet = portletLocalService.getPortletById(
295 companyId, portletId);
296
297 PortletPreferences portletPreferences =
298 portletPreferencesPersistence.fetchByO_O_P_P(
299 ownerId, ownerType, plid, portletId);
300
301 if (portletPreferences == null) {
302 if ((portlet != null) && portlet.isUndeployedPortlet() &&
303 PortletPreferencesThreadLocal.isStrict()) {
304
305 return new PortletPreferencesImpl();
306 }
307
308 portletPreferences =
309 portletPreferencesLocalService.addPortletPreferences(
310 companyId, ownerId, ownerType, plid, portletId, portlet,
311 defaultPreferences);
312 }
313
314 preferences = PortletPreferencesSerializer.fromXML(
315 companyId, ownerId, ownerType, plid, portletId,
316 portletPreferences.getPreferences());
317
318 preferencesPool.put(key, preferences);
319 }
320
321 return (PortletPreferencesImpl)preferences.clone();
322 }
323
324 protected String encodeKey(long plid, String portletId) {
325 return String.valueOf(plid).concat(StringPool.POUND).concat(portletId);
326 }
327
328 private static Log _log = LogFactoryUtil.getLog(
329 PortletPreferencesLocalServiceImpl.class);
330
331 }