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