1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.model.Portlet;
24  import com.liferay.portal.model.PortletConstants;
25  import com.liferay.portal.model.PortletPreferences;
26  import com.liferay.portal.model.PortletPreferencesIds;
27  import com.liferay.portal.service.base.PortletPreferencesLocalServiceBaseImpl;
28  import com.liferay.portlet.PortletPreferencesImpl;
29  import com.liferay.portlet.PortletPreferencesSerializer;
30  import com.liferay.portlet.PortletPreferencesThreadLocal;
31  
32  import java.util.List;
33  import java.util.Map;
34  
35  /**
36   * <a href="PortletPreferencesLocalServiceImpl.java.html"><b><i>View Source</i>
37   * </b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class PortletPreferencesLocalServiceImpl
42      extends PortletPreferencesLocalServiceBaseImpl {
43  
44      public PortletPreferences addPortletPreferences(
45              long companyId, long ownerId, int ownerType, long plid,
46              String portletId, Portlet portlet, String defaultPreferences)
47          throws SystemException {
48  
49          long portletPreferencesId = counterLocalService.increment();
50  
51          PortletPreferences portletPreferences =
52              portletPreferencesPersistence.create(portletPreferencesId);
53  
54          portletPreferences.setOwnerId(ownerId);
55          portletPreferences.setOwnerType(ownerType);
56          portletPreferences.setPlid(plid);
57          portletPreferences.setPortletId(portletId);
58  
59          if (Validator.isNull(defaultPreferences)) {
60              if (portlet == null) {
61                  defaultPreferences =
62                      PortletConstants.DEFAULT_PREFERENCES;
63              }
64              else {
65                  defaultPreferences = portlet.getDefaultPreferences();
66              }
67          }
68  
69          portletPreferences.setPreferences(defaultPreferences);
70  
71          try {
72              portletPreferencesPersistence.update(portletPreferences, false);
73          }
74          catch (SystemException se) {
75              if (_log.isWarnEnabled()) {
76                  _log.warn(
77                      "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
78                          ownerType + ", plid=" + plid + ", portletId=" +
79                              portletId + "}");
80              }
81  
82              portletPreferences = portletPreferencesPersistence.fetchByO_O_P_P(
83                  ownerId, ownerType, plid, portletId, false);
84  
85              if (portletPreferences == null) {
86                  throw se;
87              }
88          }
89  
90          return portletPreferences;
91      }
92  
93      public void deletePortletPreferences(long portletPreferencesId)
94          throws PortalException, SystemException {
95  
96          PortletPreferences portletPreferences =
97              portletPreferencesPersistence.findByPrimaryKey(
98                  portletPreferencesId);
99  
100         long ownerId = portletPreferences.getOwnerId();
101         int ownerType = portletPreferences.getOwnerType();
102 
103         portletPreferencesPersistence.remove(portletPreferences);
104 
105         PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
106     }
107 
108     public void deletePortletPreferences(long ownerId, int ownerType, long plid)
109         throws SystemException {
110 
111         portletPreferencesPersistence.removeByO_O_P(ownerId, ownerType, plid);
112 
113         PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
114     }
115 
116     public void deletePortletPreferences(
117             long ownerId, int ownerType, long plid, String portletId)
118         throws PortalException, SystemException {
119 
120         portletPreferencesPersistence.removeByO_O_P_P(
121             ownerId, ownerType, plid, portletId);
122 
123         PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
124     }
125 
126     public javax.portlet.PortletPreferences getDefaultPreferences(
127             long companyId, String portletId)
128         throws SystemException {
129 
130         Portlet portlet = portletLocalService.getPortletById(
131             companyId, portletId);
132 
133         return PortletPreferencesSerializer.fromDefaultXML(
134             portlet.getDefaultPreferences());
135     }
136 
137     public List<PortletPreferences> getPortletPreferences()
138         throws SystemException {
139 
140         return portletPreferencesPersistence.findAll();
141     }
142 
143     public List<PortletPreferences> getPortletPreferences(
144             long plid, String portletId)
145         throws SystemException {
146 
147         return portletPreferencesPersistence.findByP_P(plid, portletId);
148     }
149 
150     public List<PortletPreferences> getPortletPreferences(
151             long ownerId, int ownerType, long plid)
152         throws SystemException {
153 
154         return portletPreferencesPersistence.findByO_O_P(
155             ownerId, ownerType, plid);
156     }
157 
158     public PortletPreferences getPortletPreferences(
159             long ownerId, int ownerType, long plid, String portletId)
160         throws PortalException, SystemException {
161 
162         return portletPreferencesPersistence.findByO_O_P_P(
163             ownerId, ownerType, plid, portletId);
164     }
165 
166     public List<PortletPreferences> getPortletPreferencesByPlid(long plid)
167         throws SystemException {
168 
169         return portletPreferencesPersistence.findByPlid(plid);
170     }
171 
172     public javax.portlet.PortletPreferences getPreferences(
173             PortletPreferencesIds portletPreferencesIds)
174         throws SystemException {
175 
176         return getPreferences(
177             portletPreferencesIds.getCompanyId(),
178             portletPreferencesIds.getOwnerId(),
179             portletPreferencesIds.getOwnerType(),
180             portletPreferencesIds.getPlid(),
181             portletPreferencesIds.getPortletId());
182     }
183 
184     public javax.portlet.PortletPreferences getPreferences(
185             long companyId, long ownerId, int ownerType, long plid,
186             String portletId)
187         throws SystemException {
188 
189         return getPreferences(
190             companyId, ownerId, ownerType, plid, portletId, null);
191     }
192 
193     public javax.portlet.PortletPreferences getPreferences(
194             long companyId, long ownerId, int ownerType, long plid,
195             String portletId, String defaultPreferences)
196         throws SystemException {
197 
198         Map<String, PortletPreferencesImpl> preferencesPool =
199             PortletPreferencesLocalUtil.getPreferencesPool(
200                 ownerId, ownerType);
201 
202         String key = encodeKey(plid, portletId);
203 
204         PortletPreferencesImpl preferences = preferencesPool.get(key);
205 
206         if (preferences == null) {
207             Portlet portlet = portletLocalService.getPortletById(
208                 companyId, portletId);
209 
210             PortletPreferences portletPreferences =
211                 portletPreferencesPersistence.fetchByO_O_P_P(
212                     ownerId, ownerType, plid, portletId);
213 
214             if (portletPreferences == null) {
215                 if ((portlet != null) && portlet.isUndeployedPortlet() &&
216                     PortletPreferencesThreadLocal.isStrict()) {
217 
218                     return new PortletPreferencesImpl();
219                 }
220 
221                 portletPreferences =
222                     portletPreferencesLocalService.addPortletPreferences(
223                         companyId, ownerId, ownerType, plid, portletId, portlet,
224                         defaultPreferences);
225             }
226 
227             preferences = PortletPreferencesSerializer.fromXML(
228                 companyId, ownerId, ownerType, plid, portletId,
229                 portletPreferences.getPreferences());
230 
231             preferencesPool.put(key, preferences);
232         }
233 
234         return (PortletPreferencesImpl)preferences.clone();
235     }
236 
237     public PortletPreferences updatePreferences(
238             long ownerId, int ownerType, long plid, String portletId,
239             javax.portlet.PortletPreferences preferences)
240         throws SystemException {
241 
242         PortletPreferencesImpl preferencesImpl =
243             (PortletPreferencesImpl)preferences;
244 
245         String xml = PortletPreferencesSerializer.toXML(preferencesImpl);
246 
247         return updatePreferences(ownerId, ownerType, plid, portletId, xml);
248     }
249 
250     public PortletPreferences updatePreferences(
251             long ownerId, int ownerType, long plid, String portletId,
252             String xml)
253         throws SystemException {
254 
255         PortletPreferences portletPreferences =
256             portletPreferencesPersistence.fetchByO_O_P_P(
257                 ownerId, ownerType, plid, portletId);
258 
259         if (portletPreferences == null) {
260             long portletPreferencesId = counterLocalService.increment();
261 
262             portletPreferences = portletPreferencesPersistence.create(
263                 portletPreferencesId);
264 
265             portletPreferences.setOwnerId(ownerId);
266             portletPreferences.setOwnerType(ownerType);
267             portletPreferences.setPlid(plid);
268             portletPreferences.setPortletId(portletId);
269         }
270 
271         portletPreferences.setPreferences(xml);
272 
273         portletPreferencesPersistence.update(portletPreferences, false);
274 
275         PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
276 
277         return portletPreferences;
278     }
279 
280     protected String encodeKey(long plid, String portletId) {
281         return String.valueOf(plid).concat(StringPool.POUND).concat(portletId);
282     }
283 
284     private static Log _log = LogFactoryUtil.getLog(
285         PortletPreferencesLocalServiceImpl.class);
286 
287 }