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