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  public class PortletPreferencesServiceImpl
50      extends PortletPreferencesServiceBaseImpl {
51  
52      public void deleteArchivedPreferences(long portletItemId)
53          throws PortalException, SystemException {
54  
55          PortletItem portletItem = portletItemLocalService.getPortletItem(
56              portletItemId);
57  
58          GroupPermissionUtil.check(
59              getPermissionChecker(), portletItem.getGroupId(),
60              ActionKeys.MANAGE_ARCHIVED_SETUPS);
61  
62          long ownerId = portletItemId;
63          int ownerType = PortletKeys.PREFS_OWNER_TYPE_ARCHIVED;
64          long plid = 0;
65          String portletId = portletItem.getPortletId();
66  
67          portletPreferencesLocalService.deletePortletPreferences(
68              ownerId, ownerType, plid, portletId);
69  
70          portletItemLocalService.deletePortletItem(portletItemId);
71      }
72  
73      public void restoreArchivedPreferences(
74              long groupId, String name, String portletId,
75              javax.portlet.PortletPreferences prefs)
76          throws PortalException, SystemException {
77  
78          GroupPermissionUtil.check(
79              getPermissionChecker(), groupId, ActionKeys.MANAGE_ARCHIVED_SETUPS);
80  
81          PortletItem portletItem = portletItemLocalService.getPortletItem(
82              groupId, name, portletId, PortletPreferences.class.getName());
83  
84          long ownerId = portletItem.getPortletItemId();
85          int ownerType = PortletKeys.PREFS_OWNER_TYPE_ARCHIVED;
86          long plid = 0;
87  
88          javax.portlet.PortletPreferences archivedPrefs =
89              portletPreferencesLocalService.getPreferences(
90                  portletItem.getCompanyId(), ownerId, ownerType, plid,
91                  portletId);
92  
93          copyPreferences(archivedPrefs, prefs);
94      }
95  
96      public void updateArchivePreferences(
97              long userId, long groupId, String name, String portletId,
98              javax.portlet.PortletPreferences prefs)
99          throws PortalException, SystemException {
100 
101         GroupPermissionUtil.check(
102             getPermissionChecker(), groupId, ActionKeys.MANAGE_ARCHIVED_SETUPS);
103 
104         PortletItem portletItem = portletItemLocalService.updatePortletItem(
105             userId, groupId, name, portletId,
106             PortletPreferences.class.getName());
107 
108         long ownerId = portletItem.getPortletItemId();
109         int ownerType = PortletKeys.PREFS_OWNER_TYPE_ARCHIVED;
110         long plid = 0;
111 
112         javax.portlet.PortletPreferences archivedPrefs =
113             portletPreferencesLocalService.getPreferences(
114                 portletItem.getCompanyId(), ownerId, ownerType, plid,
115                 portletId);
116 
117         copyPreferences(prefs, archivedPrefs);
118     }
119 
120     protected void copyPreferences(
121             javax.portlet.PortletPreferences sourcePreferences,
122             javax.portlet.PortletPreferences targetPreferences)
123         throws SystemException {
124 
125         try {
126             Iterator<String> itr =
127                 targetPreferences.getMap().keySet().iterator();
128 
129             while (itr.hasNext()) {
130                 try {
131                     String key = itr.next();
132 
133                     targetPreferences.reset(key);
134                 }
135                 catch (ReadOnlyException roe) {
136                 }
137             }
138 
139             itr = sourcePreferences.getMap().keySet().iterator();
140 
141             while (itr.hasNext()) {
142                 try {
143                     String key = itr.next();
144 
145                     targetPreferences.setValues(
146                         key, sourcePreferences.getValues(key, new String[0]));
147                 }
148                 catch (ReadOnlyException roe) {
149                 }
150             }
151 
152             targetPreferences.store();
153         }
154         catch (IOException ioe) {
155             _log.error(ioe);
156         }
157         catch (ValidatorException ve) {
158             throw new SystemException(ve);
159         }
160     }
161 
162     private static Log _log =
163         LogFactoryUtil.getLog(PortletPreferencesServiceImpl.class);
164 
165 }