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