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.util.StringPool;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.model.Portlet;
30 import com.liferay.portal.model.PortletConstants;
31 import com.liferay.portal.model.PortletPreferences;
32 import com.liferay.portal.model.PortletPreferencesIds;
33 import com.liferay.portal.service.base.PortletPreferencesLocalServiceBaseImpl;
34 import com.liferay.portlet.PortletPreferencesImpl;
35 import com.liferay.portlet.PortletPreferencesSerializer;
36
37 import java.util.List;
38 import java.util.Map;
39
40
47 public class PortletPreferencesLocalServiceImpl
48 extends PortletPreferencesLocalServiceBaseImpl {
49
50 public void deletePortletPreferences(long portletPreferencesId)
51 throws PortalException, SystemException {
52
53 PortletPreferences portletPreferences =
54 portletPreferencesPersistence.findByPrimaryKey(
55 portletPreferencesId);
56
57 long ownerId = portletPreferences.getOwnerId();
58 int ownerType = portletPreferences.getOwnerType();
59
60 portletPreferencesPersistence.remove(portletPreferences);
61
62 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
63 }
64
65 public void deletePortletPreferences(long ownerId, int ownerType, long plid)
66 throws SystemException {
67
68 portletPreferencesPersistence.removeByO_O_P(ownerId, ownerType, plid);
69
70 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
71 }
72
73 public void deletePortletPreferences(
74 long ownerId, int ownerType, long plid, String portletId)
75 throws PortalException, SystemException {
76
77 portletPreferencesPersistence.removeByO_O_P_P(
78 ownerId, ownerType, plid, portletId);
79
80 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
81 }
82
83 public javax.portlet.PortletPreferences getDefaultPreferences(
84 long companyId, String portletId)
85 throws SystemException {
86
87 Portlet portlet = portletLocalService.getPortletById(
88 companyId, portletId);
89
90 return PortletPreferencesSerializer.fromDefaultXML(
91 portlet.getDefaultPreferences());
92 }
93
94 public List<PortletPreferences> getPortletPreferences()
95 throws SystemException {
96
97 return portletPreferencesPersistence.findAll();
98 }
99
100 public List<PortletPreferences> getPortletPreferences(
101 long plid, String portletId)
102 throws SystemException {
103
104 return portletPreferencesPersistence.findByP_P(plid, portletId);
105 }
106
107 public List<PortletPreferences> getPortletPreferences(
108 long ownerId, int ownerType, long plid)
109 throws SystemException {
110
111 return portletPreferencesPersistence.findByO_O_P(
112 ownerId, ownerType, plid);
113 }
114
115 public PortletPreferences getPortletPreferences(
116 long ownerId, int ownerType, long plid, String portletId)
117 throws PortalException, SystemException {
118
119 return portletPreferencesPersistence.findByO_O_P_P(
120 ownerId, ownerType, plid, portletId);
121 }
122
123 public List<PortletPreferences> getPortletPreferencesByPlid(long plid)
124 throws SystemException {
125
126 return portletPreferencesPersistence.findByPlid(plid);
127 }
128
129 public javax.portlet.PortletPreferences getPreferences(
130 PortletPreferencesIds portletPreferencesIds)
131 throws SystemException {
132
133 return getPreferences(
134 portletPreferencesIds.getCompanyId(),
135 portletPreferencesIds.getOwnerId(),
136 portletPreferencesIds.getOwnerType(),
137 portletPreferencesIds.getPlid(),
138 portletPreferencesIds.getPortletId());
139 }
140
141 public javax.portlet.PortletPreferences getPreferences(
142 long companyId, long ownerId, int ownerType, long plid,
143 String portletId)
144 throws SystemException {
145
146 return getPreferences(
147 companyId, ownerId, ownerType, plid, portletId, null);
148 }
149
150 public javax.portlet.PortletPreferences getPreferences(
151 long companyId, long ownerId, int ownerType, long plid,
152 String portletId, String defaultPreferences)
153 throws SystemException {
154
155 Map<String, PortletPreferencesImpl> prefsPool =
156 PortletPreferencesLocalUtil.getPreferencesPool(
157 ownerId, ownerType);
158
159 String key = encodeKey(plid, portletId);
160
161 PortletPreferencesImpl prefs = prefsPool.get(key);
162
163 if (prefs == null) {
164 Portlet portlet = portletLocalService.getPortletById(
165 companyId, portletId);
166
167 PortletPreferences portletPreferences =
168 portletPreferencesPersistence.fetchByO_O_P_P(
169 ownerId, ownerType, plid, portletId);
170
171 if (portletPreferences == null) {
172 long portletPreferencesId = counterLocalService.increment();
173
174 portletPreferences = portletPreferencesPersistence.create(
175 portletPreferencesId);
176
177 portletPreferences.setOwnerId(ownerId);
178 portletPreferences.setOwnerType(ownerType);
179 portletPreferences.setPlid(plid);
180 portletPreferences.setPortletId(portletId);
181
182 if (Validator.isNull(defaultPreferences)) {
183 if (portlet == null) {
184 defaultPreferences =
185 PortletConstants.DEFAULT_PREFERENCES;
186 }
187 else {
188 defaultPreferences = portlet.getDefaultPreferences();
189 }
190 }
191
192 portletPreferences.setPreferences(defaultPreferences);
193
194 portletPreferencesPersistence.update(portletPreferences, false);
195 }
196
197 prefs = PortletPreferencesSerializer.fromXML(
198 companyId, ownerId, ownerType, plid, portletId,
199 portletPreferences.getPreferences());
200
201 prefsPool.put(key, prefs);
202 }
203
204 return (PortletPreferencesImpl)prefs.clone();
205 }
206
207 public PortletPreferences updatePreferences(
208 long ownerId, int ownerType, long plid, String portletId,
209 javax.portlet.PortletPreferences prefs)
210 throws SystemException {
211
212 PortletPreferences portletPreferences =
213 portletPreferencesPersistence.fetchByO_O_P_P(
214 ownerId, ownerType, plid, portletId);
215
216 if (portletPreferences == null) {
217 long portletPreferencesId = counterLocalService.increment();
218
219 portletPreferences = portletPreferencesPersistence.create(
220 portletPreferencesId);
221
222 portletPreferences.setOwnerId(ownerId);
223 portletPreferences.setOwnerType(ownerType);
224 portletPreferences.setPlid(plid);
225 portletPreferences.setPortletId(portletId);
226 }
227
228 PortletPreferencesImpl prefsImpl = (PortletPreferencesImpl)prefs;
229
230 String xml = PortletPreferencesSerializer.toXML(prefsImpl);
231
232 portletPreferences.setPreferences(xml);
233
234 portletPreferencesPersistence.update(portletPreferences, false);
235
236 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
237
238 return portletPreferences;
239 }
240
241 protected String encodeKey(long plid, String portletId) {
242 StringBuilder sb = new StringBuilder();
243
244 sb.append(plid);
245 sb.append(StringPool.POUND);
246 sb.append(portletId);
247
248 return sb.toString();
249 }
250
251 }