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