1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.model.Portlet;
27  import com.liferay.portal.model.PortletConstants;
28  import com.liferay.portal.model.PortletPreferences;
29  import com.liferay.portal.model.PortletPreferencesIds;
30  import com.liferay.portal.service.base.PortletPreferencesLocalServiceBaseImpl;
31  import com.liferay.portlet.PortletPreferencesImpl;
32  import com.liferay.portlet.PortletPreferencesSerializer;
33  
34  import java.util.List;
35  import java.util.Map;
36  
37  /**
38   * <a href="PortletPreferencesLocalServiceImpl.java.html"><b><i>View Source</i>
39   * </b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class PortletPreferencesLocalServiceImpl
45      extends PortletPreferencesLocalServiceBaseImpl {
46  
47      public void deletePortletPreferences(long portletPreferencesId)
48          throws PortalException, SystemException {
49  
50          PortletPreferences portletPreferences =
51              portletPreferencesPersistence.findByPrimaryKey(
52                  portletPreferencesId);
53  
54          long ownerId = portletPreferences.getOwnerId();
55          int ownerType = portletPreferences.getOwnerType();
56  
57          portletPreferencesPersistence.remove(portletPreferences);
58  
59          PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
60      }
61  
62      public void deletePortletPreferences(long ownerId, int ownerType, long plid)
63          throws SystemException {
64  
65          portletPreferencesPersistence.removeByO_O_P(ownerId, ownerType, plid);
66  
67          PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
68      }
69  
70      public void deletePortletPreferences(
71              long ownerId, int ownerType, long plid, String portletId)
72          throws PortalException, SystemException {
73  
74          portletPreferencesPersistence.removeByO_O_P_P(
75              ownerId, ownerType, plid, portletId);
76  
77          PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
78      }
79  
80      public javax.portlet.PortletPreferences getDefaultPreferences(
81              long companyId, String portletId)
82          throws SystemException {
83  
84          Portlet portlet = portletLocalService.getPortletById(
85              companyId, portletId);
86  
87          return PortletPreferencesSerializer.fromDefaultXML(
88              portlet.getDefaultPreferences());
89      }
90  
91      public List<PortletPreferences> getPortletPreferences()
92          throws SystemException {
93  
94          return portletPreferencesPersistence.findAll();
95      }
96  
97      public List<PortletPreferences> getPortletPreferences(
98              long plid, String portletId)
99          throws SystemException {
100 
101         return portletPreferencesPersistence.findByP_P(plid, portletId);
102     }
103 
104     public List<PortletPreferences> getPortletPreferences(
105             long ownerId, int ownerType, long plid)
106         throws SystemException {
107 
108         return portletPreferencesPersistence.findByO_O_P(
109             ownerId, ownerType, plid);
110     }
111 
112     public PortletPreferences getPortletPreferences(
113             long ownerId, int ownerType, long plid, String portletId)
114         throws PortalException, SystemException {
115 
116         return portletPreferencesPersistence.findByO_O_P_P(
117             ownerId, ownerType, plid, portletId);
118     }
119 
120     public List<PortletPreferences> getPortletPreferencesByPlid(long plid)
121         throws SystemException {
122 
123         return portletPreferencesPersistence.findByPlid(plid);
124     }
125 
126     public javax.portlet.PortletPreferences getPreferences(
127             PortletPreferencesIds portletPreferencesIds)
128         throws SystemException {
129 
130         return getPreferences(
131             portletPreferencesIds.getCompanyId(),
132             portletPreferencesIds.getOwnerId(),
133             portletPreferencesIds.getOwnerType(),
134             portletPreferencesIds.getPlid(),
135             portletPreferencesIds.getPortletId());
136     }
137 
138     public javax.portlet.PortletPreferences getPreferences(
139             long companyId, long ownerId, int ownerType, long plid,
140             String portletId)
141         throws SystemException {
142 
143         return getPreferences(
144             companyId, ownerId, ownerType, plid, portletId, null);
145     }
146 
147     public javax.portlet.PortletPreferences getPreferences(
148             long companyId, long ownerId, int ownerType, long plid,
149             String portletId, String defaultPreferences)
150         throws SystemException {
151 
152         Map<String, PortletPreferencesImpl> preferencesPool =
153             PortletPreferencesLocalUtil.getPreferencesPool(
154                 ownerId, ownerType);
155 
156         String key = encodeKey(plid, portletId);
157 
158         PortletPreferencesImpl preferences = preferencesPool.get(key);
159 
160         if (preferences == null) {
161             Portlet portlet = portletLocalService.getPortletById(
162                 companyId, portletId);
163 
164             PortletPreferences portletPreferences =
165                 portletPreferencesPersistence.fetchByO_O_P_P(
166                     ownerId, ownerType, plid, portletId);
167 
168             if (portletPreferences == null) {
169                 long portletPreferencesId = counterLocalService.increment();
170 
171                 portletPreferences = portletPreferencesPersistence.create(
172                     portletPreferencesId);
173 
174                 portletPreferences.setOwnerId(ownerId);
175                 portletPreferences.setOwnerType(ownerType);
176                 portletPreferences.setPlid(plid);
177                 portletPreferences.setPortletId(portletId);
178 
179                 if (Validator.isNull(defaultPreferences)) {
180                     if (portlet == null) {
181                         defaultPreferences =
182                             PortletConstants.DEFAULT_PREFERENCES;
183                     }
184                     else {
185                         defaultPreferences = portlet.getDefaultPreferences();
186                     }
187                 }
188 
189                 portletPreferences.setPreferences(defaultPreferences);
190 
191                 portletPreferencesPersistence.update(portletPreferences, false);
192             }
193 
194             preferences = PortletPreferencesSerializer.fromXML(
195                 companyId, ownerId, ownerType, plid, portletId,
196                 portletPreferences.getPreferences());
197 
198             preferencesPool.put(key, preferences);
199         }
200 
201         return (PortletPreferencesImpl)preferences.clone();
202     }
203 
204     public PortletPreferences updatePreferences(
205             long ownerId, int ownerType, long plid, String portletId,
206             javax.portlet.PortletPreferences preferences)
207         throws SystemException {
208 
209         PortletPreferencesImpl preferencesImpl =
210             (PortletPreferencesImpl)preferences;
211 
212         String xml = PortletPreferencesSerializer.toXML(preferencesImpl);
213 
214         return updatePreferences(ownerId, ownerType, plid, portletId, xml);
215     }
216 
217     public PortletPreferences updatePreferences(
218             long ownerId, int ownerType, long plid, String portletId,
219             String xml)
220         throws SystemException {
221 
222         PortletPreferences portletPreferences =
223             portletPreferencesPersistence.fetchByO_O_P_P(
224                 ownerId, ownerType, plid, portletId);
225 
226         if (portletPreferences == null) {
227             long portletPreferencesId = counterLocalService.increment();
228 
229             portletPreferences = portletPreferencesPersistence.create(
230                 portletPreferencesId);
231 
232             portletPreferences.setOwnerId(ownerId);
233             portletPreferences.setOwnerType(ownerType);
234             portletPreferences.setPlid(plid);
235             portletPreferences.setPortletId(portletId);
236         }
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         StringBuilder sb = new StringBuilder();
249 
250         sb.append(plid);
251         sb.append(StringPool.POUND);
252         sb.append(portletId);
253 
254         return sb.toString();
255     }
256 
257 }