1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Portlet;
31  import com.liferay.portal.model.PortletConstants;
32  import com.liferay.portal.model.PortletPreferences;
33  import com.liferay.portal.model.PortletPreferencesIds;
34  import com.liferay.portal.service.base.PortletPreferencesLocalServiceBaseImpl;
35  import com.liferay.portlet.PortletPreferencesImpl;
36  import com.liferay.portlet.PortletPreferencesSerializer;
37  
38  import java.util.List;
39  import java.util.Map;
40  
41  /**
42   * <a href="PortletPreferencesLocalServiceImpl.java.html"><b><i>View Source</i>
43   * </b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class PortletPreferencesLocalServiceImpl
49      extends PortletPreferencesLocalServiceBaseImpl {
50  
51      public void deletePortletPreferences(long portletPreferencesId)
52          throws PortalException, SystemException {
53  
54          PortletPreferences portletPreferences =
55              portletPreferencesPersistence.findByPrimaryKey(
56                  portletPreferencesId);
57  
58          long ownerId = portletPreferences.getOwnerId();
59          int ownerType = portletPreferences.getOwnerType();
60  
61          portletPreferencesPersistence.remove(portletPreferences);
62  
63          PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
64      }
65  
66      public void deletePortletPreferences(long ownerId, int ownerType, long plid)
67          throws SystemException {
68  
69          portletPreferencesPersistence.removeByO_O_P(ownerId, ownerType, plid);
70  
71          PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
72      }
73  
74      public void deletePortletPreferences(
75              long ownerId, int ownerType, long plid, String portletId)
76          throws PortalException, SystemException {
77  
78          portletPreferencesPersistence.removeByO_O_P_P(
79              ownerId, ownerType, plid, portletId);
80  
81          PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
82      }
83  
84      public javax.portlet.PortletPreferences getDefaultPreferences(
85              long companyId, String portletId)
86          throws PortalException, SystemException {
87  
88          Portlet portlet = portletLocalService.getPortletById(
89              companyId, portletId);
90  
91          return PortletPreferencesSerializer.fromDefaultXML(
92              portlet.getDefaultPreferences());
93      }
94  
95      public List<PortletPreferences> getPortletPreferences()
96          throws SystemException {
97  
98          return portletPreferencesPersistence.findAll();
99      }
100 
101     public List<PortletPreferences> getPortletPreferences(
102             long plid, String portletId)
103         throws SystemException {
104 
105         return portletPreferencesPersistence.findByP_P(plid, portletId);
106     }
107 
108     public List<PortletPreferences> getPortletPreferences(
109             long ownerId, int ownerType, long plid)
110         throws SystemException {
111 
112         return portletPreferencesPersistence.findByO_O_P(
113             ownerId, ownerType, plid);
114     }
115 
116     public PortletPreferences getPortletPreferences(
117             long ownerId, int ownerType, long plid, String portletId)
118         throws PortalException, SystemException {
119 
120         return portletPreferencesPersistence.findByO_O_P_P(
121             ownerId, ownerType, plid, portletId);
122     }
123 
124     public List<PortletPreferences> getPortletPreferencesByPlid(long plid)
125         throws SystemException {
126 
127         return portletPreferencesPersistence.findByPlid(plid);
128     }
129 
130     public javax.portlet.PortletPreferences getPreferences(
131             PortletPreferencesIds portletPreferencesIds)
132         throws PortalException, SystemException {
133 
134         return getPreferences(
135             portletPreferencesIds.getCompanyId(),
136             portletPreferencesIds.getOwnerId(),
137             portletPreferencesIds.getOwnerType(),
138             portletPreferencesIds.getPlid(),
139             portletPreferencesIds.getPortletId());
140     }
141 
142     public javax.portlet.PortletPreferences getPreferences(
143             long companyId, long ownerId, int ownerType, long plid,
144             String portletId)
145         throws PortalException, SystemException {
146 
147         return getPreferences(
148             companyId, ownerId, ownerType, plid, portletId, null);
149     }
150 
151     public javax.portlet.PortletPreferences getPreferences(
152             long companyId, long ownerId, int ownerType, long plid,
153             String portletId, String defaultPreferences)
154         throws PortalException, SystemException {
155 
156         Map<String, PortletPreferencesImpl> prefsPool =
157             PortletPreferencesLocalUtil.getPreferencesPool(
158                 ownerId, ownerType);
159 
160         String key = encodeKey(plid, portletId);
161 
162         PortletPreferencesImpl prefs = prefsPool.get(key);
163 
164         if (prefs == null) {
165             PortletPreferences portletPreferences = null;
166 
167             Portlet portlet = portletLocalService.getPortletById(
168                 companyId, portletId);
169 
170             try {
171                 portletPreferences =
172                     portletPreferencesPersistence.findByO_O_P_P(
173                         ownerId, ownerType, plid, portletId);
174             }
175             catch (NoSuchPortletPreferencesException nsppe) {
176                 long portletPreferencesId = counterLocalService.increment();
177 
178                 portletPreferences = portletPreferencesPersistence.create(
179                     portletPreferencesId);
180 
181                 portletPreferences.setOwnerId(ownerId);
182                 portletPreferences.setOwnerType(ownerType);
183                 portletPreferences.setPlid(plid);
184                 portletPreferences.setPortletId(portletId);
185 
186                 if (Validator.isNull(defaultPreferences)) {
187                     if (portlet == null) {
188                         defaultPreferences =
189                             PortletConstants.DEFAULT_PREFERENCES;
190                     }
191                     else {
192                         defaultPreferences = portlet.getDefaultPreferences();
193                     }
194                 }
195 
196                 portletPreferences.setPreferences(defaultPreferences);
197 
198                 portletPreferencesPersistence.update(portletPreferences, false);
199             }
200 
201             prefs = PortletPreferencesSerializer.fromXML(
202                 companyId, ownerId, ownerType, plid, portletId,
203                 portletPreferences.getPreferences());
204 
205             prefsPool.put(key, prefs);
206         }
207 
208         return (PortletPreferencesImpl)prefs.clone();
209     }
210 
211     public PortletPreferences updatePreferences(
212             long ownerId, int ownerType, long plid, String portletId,
213             javax.portlet.PortletPreferences prefs)
214         throws SystemException {
215 
216         PortletPreferences portletPreferences = null;
217 
218         try {
219             portletPreferences = portletPreferencesPersistence.findByO_O_P_P(
220                 ownerId, ownerType, plid, portletId);
221         }
222         catch (NoSuchPortletPreferencesException nsppe) {
223             long portletPreferencesId = counterLocalService.increment();
224 
225             portletPreferences = portletPreferencesPersistence.create(
226                 portletPreferencesId);
227 
228             portletPreferences.setOwnerId(ownerId);
229             portletPreferences.setOwnerType(ownerType);
230             portletPreferences.setPlid(plid);
231             portletPreferences.setPortletId(portletId);
232         }
233 
234         PortletPreferencesImpl prefsImpl = (PortletPreferencesImpl)prefs;
235 
236         String xml = PortletPreferencesSerializer.toXML(prefsImpl);
237 
238         portletPreferences.setPreferences(xml);
239 
240         portletPreferencesPersistence.update(portletPreferences, false);
241 
242         PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
243 
244         return portletPreferences;
245     }
246 
247     protected String encodeKey(long plid, String portletId) {
248         StringBuilder sb = new StringBuilder();
249 
250         sb.append(plid);
251         sb.append(StringPool.POUND);
252         sb.append(portletId);
253 
254         return sb.toString();
255     }
256 
257 }