1
19
20 package com.liferay.portal.service.impl;
21
22 import com.liferay.portal.PortalException;
23 import com.liferay.portal.SystemException;
24 import com.liferay.portal.kernel.log.Log;
25 import com.liferay.portal.kernel.log.LogFactoryUtil;
26 import com.liferay.portal.kernel.util.StringPool;
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
36 import java.util.List;
37 import java.util.Map;
38
39
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 Map<String, PortletPreferencesImpl> preferencesPool =
204 PortletPreferencesLocalUtil.getPreferencesPool(
205 ownerId, ownerType);
206
207 String key = encodeKey(plid, portletId);
208
209 PortletPreferencesImpl preferences = preferencesPool.get(key);
210
211 if (preferences == null) {
212 Portlet portlet = portletLocalService.getPortletById(
213 companyId, portletId);
214
215 PortletPreferences portletPreferences =
216 portletPreferencesPersistence.fetchByO_O_P_P(
217 ownerId, ownerType, plid, portletId);
218
219 if (portletPreferences == null) {
220 portletPreferences =
221 portletPreferencesLocalService.addPortletPreferences(
222 companyId, ownerId, ownerType, plid, portletId, portlet,
223 defaultPreferences);
224 }
225
226 preferences = PortletPreferencesSerializer.fromXML(
227 companyId, ownerId, ownerType, plid, portletId,
228 portletPreferences.getPreferences());
229
230 preferencesPool.put(key, preferences);
231 }
232
233 return (PortletPreferencesImpl)preferences.clone();
234 }
235
236 public PortletPreferences updatePreferences(
237 long ownerId, int ownerType, long plid, String portletId,
238 javax.portlet.PortletPreferences preferences)
239 throws SystemException {
240
241 PortletPreferencesImpl preferencesImpl =
242 (PortletPreferencesImpl)preferences;
243
244 String xml = PortletPreferencesSerializer.toXML(preferencesImpl);
245
246 return updatePreferences(ownerId, ownerType, plid, portletId, xml);
247 }
248
249 public PortletPreferences updatePreferences(
250 long ownerId, int ownerType, long plid, String portletId,
251 String xml)
252 throws SystemException {
253
254 PortletPreferences portletPreferences =
255 portletPreferencesPersistence.fetchByO_O_P_P(
256 ownerId, ownerType, plid, portletId);
257
258 if (portletPreferences == null) {
259 long portletPreferencesId = counterLocalService.increment();
260
261 portletPreferences = portletPreferencesPersistence.create(
262 portletPreferencesId);
263
264 portletPreferences.setOwnerId(ownerId);
265 portletPreferences.setOwnerType(ownerType);
266 portletPreferences.setPlid(plid);
267 portletPreferences.setPortletId(portletId);
268 }
269
270 portletPreferences.setPreferences(xml);
271
272 portletPreferencesPersistence.update(portletPreferences, false);
273
274 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
275
276 return portletPreferences;
277 }
278
279 protected String encodeKey(long plid, String portletId) {
280 StringBuilder sb = new StringBuilder();
281
282 sb.append(plid);
283 sb.append(StringPool.POUND);
284 sb.append(portletId);
285
286 return sb.toString();
287 }
288
289 private static Log _log =
290 LogFactoryUtil.getLog(PortletPreferencesLocalServiceImpl.class);
291
292 }