1   /**
2    * Copyright (c) 2000-2009 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.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Portlet;
30  import com.liferay.portal.model.PortletConstants;
31  import com.liferay.portal.model.PortletPreferences;
32  import com.liferay.portal.model.PortletPreferencesIds;
33  import com.liferay.portal.service.base.PortletPreferencesLocalServiceBaseImpl;
34  import com.liferay.portlet.PortletPreferencesImpl;
35  import com.liferay.portlet.PortletPreferencesSerializer;
36  
37  import java.util.List;
38  import java.util.Map;
39  
40  /**
41   * <a href="PortletPreferencesLocalServiceImpl.java.html"><b><i>View Source</i>
42   * </b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class PortletPreferencesLocalServiceImpl
48      extends PortletPreferencesLocalServiceBaseImpl {
49  
50      public void deletePortletPreferences(long portletPreferencesId)
51          throws PortalException, SystemException {
52  
53          PortletPreferences portletPreferences =
54              portletPreferencesPersistence.findByPrimaryKey(
55                  portletPreferencesId);
56  
57          long ownerId = portletPreferences.getOwnerId();
58          int ownerType = portletPreferences.getOwnerType();
59  
60          portletPreferencesPersistence.remove(portletPreferences);
61  
62          PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
63      }
64  
65      public void deletePortletPreferences(long ownerId, int ownerType, long plid)
66          throws SystemException {
67  
68          portletPreferencesPersistence.removeByO_O_P(ownerId, ownerType, plid);
69  
70          PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
71      }
72  
73      public void deletePortletPreferences(
74              long ownerId, int ownerType, long plid, String portletId)
75          throws PortalException, SystemException {
76  
77          portletPreferencesPersistence.removeByO_O_P_P(
78              ownerId, ownerType, plid, portletId);
79  
80          PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
81      }
82  
83      public javax.portlet.PortletPreferences getDefaultPreferences(
84              long companyId, String portletId)
85          throws SystemException {
86  
87          Portlet portlet = portletLocalService.getPortletById(
88              companyId, portletId);
89  
90          return PortletPreferencesSerializer.fromDefaultXML(
91              portlet.getDefaultPreferences());
92      }
93  
94      public List<PortletPreferences> getPortletPreferences()
95          throws SystemException {
96  
97          return portletPreferencesPersistence.findAll();
98      }
99  
100     public List<PortletPreferences> getPortletPreferences(
101             long plid, String portletId)
102         throws SystemException {
103 
104         return portletPreferencesPersistence.findByP_P(plid, portletId);
105     }
106 
107     public List<PortletPreferences> getPortletPreferences(
108             long ownerId, int ownerType, long plid)
109         throws SystemException {
110 
111         return portletPreferencesPersistence.findByO_O_P(
112             ownerId, ownerType, plid);
113     }
114 
115     public PortletPreferences getPortletPreferences(
116             long ownerId, int ownerType, long plid, String portletId)
117         throws PortalException, SystemException {
118 
119         return portletPreferencesPersistence.findByO_O_P_P(
120             ownerId, ownerType, plid, portletId);
121     }
122 
123     public List<PortletPreferences> getPortletPreferencesByPlid(long plid)
124         throws SystemException {
125 
126         return portletPreferencesPersistence.findByPlid(plid);
127     }
128 
129     public javax.portlet.PortletPreferences getPreferences(
130             PortletPreferencesIds portletPreferencesIds)
131         throws SystemException {
132 
133         return getPreferences(
134             portletPreferencesIds.getCompanyId(),
135             portletPreferencesIds.getOwnerId(),
136             portletPreferencesIds.getOwnerType(),
137             portletPreferencesIds.getPlid(),
138             portletPreferencesIds.getPortletId());
139     }
140 
141     public javax.portlet.PortletPreferences getPreferences(
142             long companyId, long ownerId, int ownerType, long plid,
143             String portletId)
144         throws SystemException {
145 
146         return getPreferences(
147             companyId, ownerId, ownerType, plid, portletId, null);
148     }
149 
150     public javax.portlet.PortletPreferences getPreferences(
151             long companyId, long ownerId, int ownerType, long plid,
152             String portletId, String defaultPreferences)
153         throws SystemException {
154 
155         Map<String, PortletPreferencesImpl> preferencesPool =
156             PortletPreferencesLocalUtil.getPreferencesPool(
157                 ownerId, ownerType);
158 
159         String key = encodeKey(plid, portletId);
160 
161         PortletPreferencesImpl preferences = preferencesPool.get(key);
162 
163         if (preferences == null) {
164             Portlet portlet = portletLocalService.getPortletById(
165                 companyId, portletId);
166 
167             PortletPreferences portletPreferences =
168                 portletPreferencesPersistence.fetchByO_O_P_P(
169                     ownerId, ownerType, plid, portletId);
170 
171             if (portletPreferences == null) {
172                 long portletPreferencesId = counterLocalService.increment();
173 
174                 portletPreferences = portletPreferencesPersistence.create(
175                     portletPreferencesId);
176 
177                 portletPreferences.setOwnerId(ownerId);
178                 portletPreferences.setOwnerType(ownerType);
179                 portletPreferences.setPlid(plid);
180                 portletPreferences.setPortletId(portletId);
181 
182                 if (Validator.isNull(defaultPreferences)) {
183                     if (portlet == null) {
184                         defaultPreferences =
185                             PortletConstants.DEFAULT_PREFERENCES;
186                     }
187                     else {
188                         defaultPreferences = portlet.getDefaultPreferences();
189                     }
190                 }
191 
192                 portletPreferences.setPreferences(defaultPreferences);
193 
194                 portletPreferencesPersistence.update(portletPreferences, false);
195             }
196 
197             preferences = PortletPreferencesSerializer.fromXML(
198                 companyId, ownerId, ownerType, plid, portletId,
199                 portletPreferences.getPreferences());
200 
201             preferencesPool.put(key, preferences);
202         }
203 
204         return (PortletPreferencesImpl)preferences.clone();
205     }
206 
207     public PortletPreferences updatePreferences(
208             long ownerId, int ownerType, long plid, String portletId,
209             javax.portlet.PortletPreferences preferences)
210         throws SystemException {
211 
212         PortletPreferencesImpl preferencesImpl =
213             (PortletPreferencesImpl)preferences;
214 
215         String xml = PortletPreferencesSerializer.toXML(preferencesImpl);
216 
217         return updatePreferences(ownerId, ownerType, plid, portletId, xml);
218     }
219 
220     public PortletPreferences updatePreferences(
221             long ownerId, int ownerType, long plid, String portletId,
222             String xml)
223         throws SystemException {
224 
225         PortletPreferences portletPreferences =
226             portletPreferencesPersistence.fetchByO_O_P_P(
227                 ownerId, ownerType, plid, portletId);
228 
229         if (portletPreferences == null) {
230             long portletPreferencesId = counterLocalService.increment();
231 
232             portletPreferences = portletPreferencesPersistence.create(
233                 portletPreferencesId);
234 
235             portletPreferences.setOwnerId(ownerId);
236             portletPreferences.setOwnerType(ownerType);
237             portletPreferences.setPlid(plid);
238             portletPreferences.setPortletId(portletId);
239         }
240 
241         portletPreferences.setPreferences(xml);
242 
243         portletPreferencesPersistence.update(portletPreferences, false);
244 
245         PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
246 
247         return portletPreferences;
248     }
249 
250     protected String encodeKey(long plid, String portletId) {
251         StringBuilder sb = new StringBuilder();
252 
253         sb.append(plid);
254         sb.append(StringPool.POUND);
255         sb.append(portletId);
256 
257         return sb.toString();
258     }
259 
260 }