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.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  /**
43   * <a href="PortletPreferencesLocalServiceImpl.java.html"><b><i>View Source</i>
44   * </b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class PortletPreferencesLocalServiceImpl
50      extends PortletPreferencesLocalServiceBaseImpl {
51  
52      public void deletePortletPreferences(long portletPreferencesId)
53          throws PortalException, SystemException {
54  
55          PortletPreferences portletPreferences =
56              portletPreferencesPersistence.findByPrimaryKey(
57                  portletPreferencesId);
58  
59          long ownerId = portletPreferences.getOwnerId();
60          int ownerType = portletPreferences.getOwnerType();
61  
62          portletPreferencesPersistence.remove(portletPreferences);
63  
64          PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
65      }
66  
67      public void deletePortletPreferences(long ownerId, int ownerType, long plid)
68          throws PortalException, SystemException {
69  
70          portletPreferencesPersistence.removeByO_O_P(ownerId, ownerType, plid);
71  
72          PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
73      }
74  
75      public void deletePortletPreferences(
76              long ownerId, int ownerType, long plid, String portletId)
77          throws PortalException, SystemException {
78  
79          portletPreferencesPersistence.removeByO_O_P_P(
80              ownerId, ownerType, plid, portletId);
81  
82          PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
83      }
84  
85      public javax.portlet.PortletPreferences getDefaultPreferences(
86              long companyId, String portletId)
87          throws PortalException, SystemException {
88  
89          Portlet portlet = portletLocalService.getPortletById(
90              companyId, portletId);
91  
92          return PortletPreferencesSerializer.fromDefaultXML(
93              portlet.getDefaultPreferences());
94      }
95  
96      public List<PortletPreferences> getPortletPreferences()
97          throws SystemException {
98  
99          return portletPreferencesPersistence.findAll();
100     }
101 
102     public List<PortletPreferences> getPortletPreferences(long plid)
103         throws SystemException {
104 
105         return portletPreferencesPersistence.findByPlid(plid);
106     }
107 
108     public List<PortletPreferences> getPortletPreferences(
109             long plid, String portletId)
110         throws SystemException {
111 
112         return portletPreferencesPersistence.findByP_P(plid, portletId);
113     }
114 
115     public List<PortletPreferences> getPortletPreferences(
116             long ownerId, int ownerType, long plid)
117         throws PortalException, SystemException {
118 
119         return portletPreferencesPersistence.findByO_O_P(
120             ownerId, ownerType, plid);
121     }
122 
123     public PortletPreferences getPortletPreferences(
124             long ownerId, int ownerType, long plid, String portletId)
125         throws PortalException, SystemException {
126 
127         return portletPreferencesPersistence.findByO_O_P_P(
128             ownerId, ownerType, plid, portletId);
129     }
130 
131     public javax.portlet.PortletPreferences getPreferences(
132             PortletPreferencesIds portletPreferencesIds)
133         throws PortalException, SystemException {
134 
135         return getPreferences(
136             portletPreferencesIds.getCompanyId(),
137             portletPreferencesIds.getOwnerId(),
138             portletPreferencesIds.getOwnerType(),
139             portletPreferencesIds.getPlid(),
140             portletPreferencesIds.getPortletId());
141     }
142 
143     public javax.portlet.PortletPreferences getPreferences(
144             long companyId, long ownerId, int ownerType, long plid,
145             String portletId)
146         throws PortalException, SystemException {
147 
148         return getPreferences(
149             companyId, ownerId, ownerType, plid, portletId, null);
150     }
151 
152     public javax.portlet.PortletPreferences getPreferences(
153             long companyId, long ownerId, int ownerType, long plid,
154             String portletId, String defaultPreferences)
155         throws PortalException, SystemException {
156 
157         Map<String, PortletPreferencesImpl> prefsPool =
158             PortletPreferencesLocalUtil.getPreferencesPool(
159                 ownerId, ownerType);
160 
161         String key = encodeKey(plid, portletId);
162 
163         PortletPreferencesImpl prefs = prefsPool.get(key);
164 
165         if (prefs == null) {
166             PortletPreferences portletPreferences = null;
167 
168             Portlet portlet = portletLocalService.getPortletById(
169                 companyId, portletId);
170 
171             try {
172                 portletPreferences =
173                     portletPreferencesPersistence.findByO_O_P_P(
174                         ownerId, ownerType, plid, portletId);
175             }
176             catch (NoSuchPortletPreferencesException nsppe) {
177                 long portletPreferencesId = counterLocalService.increment();
178 
179                 portletPreferences = portletPreferencesPersistence.create(
180                     portletPreferencesId);
181 
182                 portletPreferences.setOwnerId(ownerId);
183                 portletPreferences.setOwnerType(ownerType);
184                 portletPreferences.setPlid(plid);
185                 portletPreferences.setPortletId(portletId);
186 
187                 if (Validator.isNull(defaultPreferences)) {
188                     if (portlet == null) {
189                         defaultPreferences = PortletImpl.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 PortalException, 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         StringMaker sm = new StringMaker();
249 
250         sm.append(plid);
251         sm.append(StringPool.POUND);
252         sm.append(portletId);
253 
254         return sm.toString();
255     }
256 
257 }