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 getPortletPreferences() throws SystemException {
97          return portletPreferencesPersistence.findAll();
98      }
99  
100     public List getPortletPreferences(long plid) throws SystemException {
101         return portletPreferencesPersistence.findByPlid(plid);
102     }
103 
104     public List getPortletPreferences(long plid, String portletId)
105         throws SystemException {
106 
107         return portletPreferencesPersistence.findByP_P(plid, portletId);
108     }
109 
110     public List getPortletPreferences(long ownerId, int ownerType, long plid)
111         throws PortalException, SystemException {
112 
113         return portletPreferencesPersistence.findByO_O_P(
114             ownerId, ownerType, plid);
115     }
116 
117     public PortletPreferences getPortletPreferences(
118             long ownerId, int ownerType, long plid, String portletId)
119         throws PortalException, SystemException {
120 
121         return portletPreferencesPersistence.findByO_O_P_P(
122             ownerId, ownerType, plid, portletId);
123     }
124 
125     public javax.portlet.PortletPreferences getPreferences(
126             PortletPreferencesIds portletPreferencesIds)
127         throws PortalException, SystemException {
128 
129         return getPreferences(
130             portletPreferencesIds.getCompanyId(),
131             portletPreferencesIds.getOwnerId(),
132             portletPreferencesIds.getOwnerType(),
133             portletPreferencesIds.getPlid(),
134             portletPreferencesIds.getPortletId());
135     }
136 
137     public javax.portlet.PortletPreferences getPreferences(
138             long companyId, long ownerId, int ownerType, long plid,
139             String portletId)
140         throws PortalException, SystemException {
141 
142         return getPreferences(
143             companyId, ownerId, ownerType, plid, portletId, null);
144     }
145 
146     public javax.portlet.PortletPreferences getPreferences(
147             long companyId, long ownerId, int ownerType, long plid,
148             String portletId, String defaultPreferences)
149         throws PortalException, SystemException {
150 
151         Map prefsPool = PortletPreferencesLocalUtil.getPreferencesPool(
152             ownerId, ownerType);
153 
154         String key = encodeKey(plid, portletId);
155 
156         PortletPreferencesImpl prefs =
157             (PortletPreferencesImpl)prefsPool.get(key);
158 
159         if (prefs == null) {
160             PortletPreferences portletPreferences = null;
161 
162             Portlet portlet = portletLocalService.getPortletById(
163                 companyId, portletId);
164 
165             try {
166                 portletPreferences =
167                     portletPreferencesPersistence.findByO_O_P_P(
168                         ownerId, ownerType, plid, portletId);
169             }
170             catch (NoSuchPortletPreferencesException nsppe) {
171                 long portletPreferencesId = counterLocalService.increment();
172 
173                 portletPreferences = portletPreferencesPersistence.create(
174                     portletPreferencesId);
175 
176                 portletPreferences.setOwnerId(ownerId);
177                 portletPreferences.setOwnerType(ownerType);
178                 portletPreferences.setPlid(plid);
179                 portletPreferences.setPortletId(portletId);
180 
181                 if (Validator.isNull(defaultPreferences)) {
182                     if (portlet == null) {
183                         defaultPreferences = PortletImpl.DEFAULT_PREFERENCES;
184                     }
185                     else {
186                         defaultPreferences = portlet.getDefaultPreferences();
187                     }
188                 }
189 
190                 portletPreferences.setPreferences(defaultPreferences);
191 
192                 portletPreferencesPersistence.update(portletPreferences);
193             }
194 
195             prefs = PortletPreferencesSerializer.fromXML(
196                 companyId, ownerId, ownerType, plid, portletId,
197                 portletPreferences.getPreferences());
198 
199             prefsPool.put(key, prefs);
200         }
201 
202         return (PortletPreferencesImpl)prefs.clone();
203     }
204 
205     public PortletPreferences updatePreferences(
206             long ownerId, int ownerType, long plid, String portletId,
207             javax.portlet.PortletPreferences prefs)
208         throws PortalException, SystemException {
209 
210         PortletPreferences portletPreferences = null;
211 
212         try {
213             portletPreferences = portletPreferencesPersistence.findByO_O_P_P(
214                 ownerId, ownerType, plid, portletId);
215         }
216         catch (NoSuchPortletPreferencesException nsppe) {
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);
235 
236         PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
237 
238         return portletPreferences;
239     }
240 
241     protected String encodeKey(long plid, String portletId) {
242         StringMaker sm = new StringMaker();
243 
244         sm.append(plid);
245         sm.append(StringPool.POUND);
246         sm.append(portletId);
247 
248         return sm.toString();
249     }
250 
251 }