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
39 import java.util.List;
40 import java.util.Map;
41
42
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 portletPreferences =
224 portletPreferencesLocalService.addPortletPreferences(
225 companyId, ownerId, ownerType, plid, portletId, portlet,
226 defaultPreferences);
227 }
228
229 preferences = PortletPreferencesSerializer.fromXML(
230 companyId, ownerId, ownerType, plid, portletId,
231 portletPreferences.getPreferences());
232
233 preferencesPool.put(key, preferences);
234 }
235
236 return (PortletPreferencesImpl)preferences.clone();
237 }
238
239 public PortletPreferences updatePreferences(
240 long ownerId, int ownerType, long plid, String portletId,
241 javax.portlet.PortletPreferences preferences)
242 throws SystemException {
243
244 PortletPreferencesImpl preferencesImpl =
245 (PortletPreferencesImpl)preferences;
246
247 String xml = PortletPreferencesSerializer.toXML(preferencesImpl);
248
249 return updatePreferences(ownerId, ownerType, plid, portletId, xml);
250 }
251
252 public PortletPreferences updatePreferences(
253 long ownerId, int ownerType, long plid, String portletId,
254 String xml)
255 throws SystemException {
256
257 PortletPreferences portletPreferences =
258 portletPreferencesPersistence.fetchByO_O_P_P(
259 ownerId, ownerType, plid, portletId);
260
261 if (portletPreferences == null) {
262 long portletPreferencesId = counterLocalService.increment();
263
264 portletPreferences = portletPreferencesPersistence.create(
265 portletPreferencesId);
266
267 portletPreferences.setOwnerId(ownerId);
268 portletPreferences.setOwnerType(ownerType);
269 portletPreferences.setPlid(plid);
270 portletPreferences.setPortletId(portletId);
271 }
272
273 portletPreferences.setPreferences(xml);
274
275 portletPreferencesPersistence.update(portletPreferences, false);
276
277 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
278
279 return portletPreferences;
280 }
281
282 protected String encodeKey(long plid, String portletId) {
283 StringBuilder sb = new StringBuilder();
284
285 sb.append(plid);
286 sb.append(StringPool.POUND);
287 sb.append(portletId);
288
289 return sb.toString();
290 }
291
292 private static Log _log =
293 LogFactoryUtil.getLog(PortletPreferencesLocalServiceImpl.class);
294
295 }