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.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.model.PortletItem;
30  import com.liferay.portal.model.PortletPreferences;
31  import com.liferay.portal.security.permission.ActionKeys;
32  import com.liferay.portal.service.base.PortletPreferencesServiceBaseImpl;
33  import com.liferay.portal.service.permission.GroupPermissionUtil;
34  import com.liferay.portal.util.PortletKeys;
35  
36  import java.io.IOException;
37  
38  import java.util.Iterator;
39  
40  import javax.portlet.ReadOnlyException;
41  import javax.portlet.ValidatorException;
42  
43  /**
44   * <a href="PortletPreferencesServiceImpl.java.html"><b><i>View Source</i></b>
45   * </a>
46   *
47   * @author Jorge Ferrer
48   *
49   */
50  public class PortletPreferencesServiceImpl
51      extends PortletPreferencesServiceBaseImpl {
52  
53      public void deleteArchivedPreferences(long portletItemId)
54          throws PortalException, SystemException {
55  
56          PortletItem portletItem = portletItemLocalService.getPortletItem(
57              portletItemId);
58  
59          GroupPermissionUtil.check(
60              getPermissionChecker(), portletItem.getGroupId(),
61              ActionKeys.MANAGE_ARCHIVED_SETUPS);
62  
63          long ownerId = portletItemId;
64          int ownerType = PortletKeys.PREFS_OWNER_TYPE_ARCHIVED;
65          long plid = 0;
66          String portletId = portletItem.getPortletId();
67  
68          portletPreferencesLocalService.deletePortletPreferences(
69              ownerId, ownerType, plid, portletId);
70  
71          portletItemLocalService.deletePortletItem(portletItemId);
72      }
73  
74      public void restoreArchivedPreferences(
75              long groupId, String name, String portletId,
76              javax.portlet.PortletPreferences preferences)
77          throws PortalException, SystemException {
78  
79          GroupPermissionUtil.check(
80              getPermissionChecker(), groupId, ActionKeys.MANAGE_ARCHIVED_SETUPS);
81  
82          PortletItem portletItem = portletItemLocalService.getPortletItem(
83              groupId, name, portletId, PortletPreferences.class.getName());
84  
85          long ownerId = portletItem.getPortletItemId();
86          int ownerType = PortletKeys.PREFS_OWNER_TYPE_ARCHIVED;
87          long plid = 0;
88  
89          javax.portlet.PortletPreferences archivedPrefs =
90              portletPreferencesLocalService.getPreferences(
91                  portletItem.getCompanyId(), ownerId, ownerType, plid,
92                  portletId);
93  
94          copyPreferences(archivedPrefs, preferences);
95      }
96  
97      public void updateArchivePreferences(
98              long userId, long groupId, String name, String portletId,
99              javax.portlet.PortletPreferences preferences)
100         throws PortalException, SystemException {
101 
102         GroupPermissionUtil.check(
103             getPermissionChecker(), groupId, ActionKeys.MANAGE_ARCHIVED_SETUPS);
104 
105         PortletItem portletItem = portletItemLocalService.updatePortletItem(
106             userId, groupId, name, portletId,
107             PortletPreferences.class.getName());
108 
109         long ownerId = portletItem.getPortletItemId();
110         int ownerType = PortletKeys.PREFS_OWNER_TYPE_ARCHIVED;
111         long plid = 0;
112 
113         javax.portlet.PortletPreferences archivedPrefs =
114             portletPreferencesLocalService.getPreferences(
115                 portletItem.getCompanyId(), ownerId, ownerType, plid,
116                 portletId);
117 
118         copyPreferences(preferences, archivedPrefs);
119     }
120 
121     protected void copyPreferences(
122             javax.portlet.PortletPreferences sourcePreferences,
123             javax.portlet.PortletPreferences targetPreferences)
124         throws SystemException {
125 
126         try {
127             Iterator<String> itr =
128                 targetPreferences.getMap().keySet().iterator();
129 
130             while (itr.hasNext()) {
131                 String key = itr.next();
132 
133                 targetPreferences.reset(key);
134             }
135 
136             itr = sourcePreferences.getMap().keySet().iterator();
137 
138             while (itr.hasNext()) {
139                 String key = itr.next();
140 
141                 targetPreferences.setValues(
142                     key, sourcePreferences.getValues(key, new String[0]));
143             }
144 
145             targetPreferences.store();
146         }
147         catch (IOException ioe) {
148             _log.error(ioe);
149         }
150         catch (ReadOnlyException roe) {
151         }
152         catch (ValidatorException ve) {
153             throw new SystemException(ve);
154         }
155     }
156 
157     private static Log _log =
158         LogFactoryUtil.getLog(PortletPreferencesServiceImpl.class);
159 
160 }