1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.model.PortletItem;
22  import com.liferay.portal.model.PortletPreferences;
23  import com.liferay.portal.security.permission.ActionKeys;
24  import com.liferay.portal.service.base.PortletPreferencesServiceBaseImpl;
25  import com.liferay.portal.service.permission.GroupPermissionUtil;
26  import com.liferay.portal.util.PortletKeys;
27  
28  import java.io.IOException;
29  
30  import java.util.Iterator;
31  
32  import javax.portlet.ReadOnlyException;
33  import javax.portlet.ValidatorException;
34  
35  /**
36   * <a href="PortletPreferencesServiceImpl.java.html"><b><i>View Source</i></b>
37   * </a>
38   *
39   * @author Jorge Ferrer
40   */
41  public class PortletPreferencesServiceImpl
42      extends PortletPreferencesServiceBaseImpl {
43  
44      public void deleteArchivedPreferences(long portletItemId)
45          throws PortalException, SystemException {
46  
47          PortletItem portletItem = portletItemLocalService.getPortletItem(
48              portletItemId);
49  
50          GroupPermissionUtil.check(
51              getPermissionChecker(), portletItem.getGroupId(),
52              ActionKeys.MANAGE_ARCHIVED_SETUPS);
53  
54          long ownerId = portletItemId;
55          int ownerType = PortletKeys.PREFS_OWNER_TYPE_ARCHIVED;
56          long plid = 0;
57          String portletId = portletItem.getPortletId();
58  
59          portletPreferencesLocalService.deletePortletPreferences(
60              ownerId, ownerType, plid, portletId);
61  
62          portletItemLocalService.deletePortletItem(portletItemId);
63      }
64  
65      public void restoreArchivedPreferences(
66              long groupId, String name, String portletId,
67              javax.portlet.PortletPreferences preferences)
68          throws PortalException, SystemException {
69  
70          GroupPermissionUtil.check(
71              getPermissionChecker(), groupId, ActionKeys.MANAGE_ARCHIVED_SETUPS);
72  
73          PortletItem portletItem = portletItemLocalService.getPortletItem(
74              groupId, name, portletId, PortletPreferences.class.getName());
75  
76          long ownerId = portletItem.getPortletItemId();
77          int ownerType = PortletKeys.PREFS_OWNER_TYPE_ARCHIVED;
78          long plid = 0;
79  
80          javax.portlet.PortletPreferences archivedPrefs =
81              portletPreferencesLocalService.getPreferences(
82                  portletItem.getCompanyId(), ownerId, ownerType, plid,
83                  portletId);
84  
85          copyPreferences(archivedPrefs, preferences);
86      }
87  
88      public void updateArchivePreferences(
89              long userId, long groupId, String name, String portletId,
90              javax.portlet.PortletPreferences preferences)
91          throws PortalException, SystemException {
92  
93          GroupPermissionUtil.check(
94              getPermissionChecker(), groupId, ActionKeys.MANAGE_ARCHIVED_SETUPS);
95  
96          PortletItem portletItem = portletItemLocalService.updatePortletItem(
97              userId, groupId, name, portletId,
98              PortletPreferences.class.getName());
99  
100         long ownerId = portletItem.getPortletItemId();
101         int ownerType = PortletKeys.PREFS_OWNER_TYPE_ARCHIVED;
102         long plid = 0;
103 
104         javax.portlet.PortletPreferences archivedPrefs =
105             portletPreferencesLocalService.getPreferences(
106                 portletItem.getCompanyId(), ownerId, ownerType, plid,
107                 portletId);
108 
109         copyPreferences(preferences, archivedPrefs);
110     }
111 
112     protected void copyPreferences(
113             javax.portlet.PortletPreferences sourcePreferences,
114             javax.portlet.PortletPreferences targetPreferences)
115         throws SystemException {
116 
117         try {
118             Iterator<String> itr =
119                 targetPreferences.getMap().keySet().iterator();
120 
121             while (itr.hasNext()) {
122                 try {
123                     String key = itr.next();
124 
125                     targetPreferences.reset(key);
126                 }
127                 catch (ReadOnlyException roe) {
128                 }
129             }
130 
131             itr = sourcePreferences.getMap().keySet().iterator();
132 
133             while (itr.hasNext()) {
134                 try {
135                     String key = itr.next();
136 
137                     targetPreferences.setValues(
138                         key, sourcePreferences.getValues(key, new String[0]));
139                 }
140                 catch (ReadOnlyException roe) {
141                 }
142             }
143 
144             targetPreferences.store();
145         }
146         catch (IOException ioe) {
147             _log.error(ioe);
148         }
149         catch (ValidatorException ve) {
150             throw new SystemException(ve);
151         }
152     }
153 
154     private static Log _log = LogFactoryUtil.getLog(
155         PortletPreferencesServiceImpl.class);
156 
157 }