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 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 }