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