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.portlet.communities.util;
16  
17  import com.liferay.portal.events.EventsProcessorUtil;
18  import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
19  import com.liferay.portal.kernel.lar.UserIdStrategy;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.util.PropsKeys;
22  import com.liferay.portal.model.Group;
23  import com.liferay.portal.model.Layout;
24  import com.liferay.portal.model.LayoutSet;
25  import com.liferay.portal.model.LayoutSetPrototype;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.security.permission.ActionKeys;
28  import com.liferay.portal.security.permission.PermissionChecker;
29  import com.liferay.portal.service.LayoutLocalServiceUtil;
30  import com.liferay.portal.service.LayoutServiceUtil;
31  import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
32  import com.liferay.portal.service.permission.GroupPermissionUtil;
33  import com.liferay.portal.service.permission.LayoutPermissionUtil;
34  import com.liferay.portal.theme.ThemeDisplay;
35  import com.liferay.portal.util.LayoutSettings;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portal.util.WebKeys;
38  
39  import java.io.File;
40  
41  import java.util.LinkedHashMap;
42  import java.util.Map;
43  
44  import javax.portlet.ActionRequest;
45  import javax.portlet.ActionResponse;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  
49  import javax.servlet.http.HttpServletRequest;
50  import javax.servlet.http.HttpServletResponse;
51  
52  /**
53   * <a href="CommunitiesUtil.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Raymond Augé
56   */
57  public class CommunitiesUtil {
58  
59      public static void applyLayoutSetPrototypes(
60              Group group, long publicLayoutSetPrototypeId,
61              long privateLayoutSetPrototypeId)
62          throws Exception {
63  
64          if (publicLayoutSetPrototypeId > 0) {
65              LayoutSetPrototype layoutSetPrototype =
66                  LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(
67                      publicLayoutSetPrototypeId);
68  
69              LayoutSet publicLayoutSet = group.getPublicLayoutSet();
70  
71              copyLayoutSet(layoutSetPrototype.getLayoutSet(), publicLayoutSet);
72          }
73  
74          if (privateLayoutSetPrototypeId > 0) {
75              LayoutSetPrototype layoutSetPrototype =
76                  LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(
77                      privateLayoutSetPrototypeId);
78  
79              LayoutSet privateLayoutSet = group.getPrivateLayoutSet();
80  
81              copyLayoutSet(layoutSetPrototype.getLayoutSet(), privateLayoutSet);
82          }
83      }
84  
85      public static void copyLayoutSet(
86              LayoutSet sourceLayoutSet, LayoutSet targetLayoutSet)
87          throws Exception {
88  
89          Map<String, String[]> parameterMap = getLayoutSetPrototypeParameters();
90  
91          File file = LayoutLocalServiceUtil.exportLayoutsAsFile(
92              sourceLayoutSet.getGroupId(), sourceLayoutSet.isPrivateLayout(),
93              null, parameterMap, null, null);
94  
95          try {
96              LayoutServiceUtil.importLayouts(
97                  targetLayoutSet.getGroupId(), targetLayoutSet.isPrivateLayout(),
98                  parameterMap, file);
99          }
100         finally {
101             file.delete();
102         }
103     }
104 
105     public static void deleteLayout(
106             ActionRequest actionRequest, ActionResponse actionResponse)
107         throws Exception {
108 
109         HttpServletRequest request = PortalUtil.getHttpServletRequest(
110             actionRequest);
111         HttpServletResponse response = PortalUtil.getHttpServletResponse(
112             actionResponse);
113 
114         deleteLayout(request, response);
115     }
116 
117     public static void deleteLayout(
118             HttpServletRequest request, HttpServletResponse response)
119         throws Exception {
120 
121         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
122             WebKeys.THEME_DISPLAY);
123 
124         PermissionChecker permissionChecker =
125             themeDisplay.getPermissionChecker();
126 
127         long plid = ParamUtil.getLong(request, "plid");
128 
129         long groupId = ParamUtil.getLong(request, "groupId");
130         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
131         long layoutId = ParamUtil.getLong(request, "layoutId");
132 
133         Layout layout = null;
134 
135         if (plid <= 0) {
136             layout = LayoutLocalServiceUtil.getLayout(
137                 groupId, privateLayout, layoutId);
138         }
139         else {
140             layout = LayoutLocalServiceUtil.getLayout(plid);
141 
142             groupId = layout.getGroupId();
143             privateLayout = layout.isPrivateLayout();
144             layoutId = layout.getLayoutId();
145         }
146 
147         Group group = layout.getGroup();
148 
149         if (group.isStagingGroup() &&
150             !GroupPermissionUtil.contains(
151                 permissionChecker, groupId, ActionKeys.MANAGE_STAGING) &&
152             !GroupPermissionUtil.contains(
153                 permissionChecker, groupId, ActionKeys.PUBLISH_STAGING)) {
154 
155             throw new PrincipalException();
156         }
157 
158         if (LayoutPermissionUtil.contains(
159                 permissionChecker, groupId, privateLayout, layoutId,
160                 ActionKeys.DELETE)) {
161 
162             LayoutSettings layoutSettings = LayoutSettings.getInstance(layout);
163 
164             EventsProcessorUtil.process(
165                 PropsKeys.LAYOUT_CONFIGURATION_ACTION_DELETE,
166                 layoutSettings.getConfigurationActionDelete(), request,
167                 response);
168         }
169 
170         LayoutServiceUtil.deleteLayout(groupId, privateLayout, layoutId);
171     }
172 
173     public static void deleteLayout(
174             RenderRequest renderRequest, RenderResponse renderResponse)
175         throws Exception {
176 
177         HttpServletRequest request = PortalUtil.getHttpServletRequest(
178             renderRequest);
179         HttpServletResponse response = PortalUtil.getHttpServletResponse(
180             renderResponse);
181 
182         deleteLayout(request, response);
183     }
184 
185     public static Map<String, String[]> getLayoutSetPrototypeParameters() {
186         Map<String, String[]> parameterMap =
187             new LinkedHashMap<String, String[]>();
188 
189         parameterMap.put(
190             PortletDataHandlerKeys.CATEGORIES,
191             new String[] {Boolean.TRUE.toString()});
192         parameterMap.put(
193             PortletDataHandlerKeys.DATA_STRATEGY,
194             new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
195         parameterMap.put(
196             PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
197             new String[] {Boolean.TRUE.toString()});
198         parameterMap.put(
199             PortletDataHandlerKeys.DELETE_PORTLET_DATA,
200             new String[] {Boolean.FALSE.toString()});
201         parameterMap.put(
202             PortletDataHandlerKeys.PERMISSIONS,
203             new String[] {Boolean.TRUE.toString()});
204         parameterMap.put(
205             PortletDataHandlerKeys.PORTLET_DATA,
206             new String[] {Boolean.TRUE.toString()});
207         parameterMap.put(
208             PortletDataHandlerKeys.PORTLET_DATA_ALL,
209             new String[] {Boolean.TRUE.toString()});
210         parameterMap.put(
211             PortletDataHandlerKeys.PORTLET_SETUP,
212             new String[] {Boolean.TRUE.toString()});
213         parameterMap.put(
214             PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
215             new String[] {Boolean.TRUE.toString()});
216         parameterMap.put(
217             PortletDataHandlerKeys.THEME,
218             new String[] {Boolean.FALSE.toString()});
219         parameterMap.put(
220             PortletDataHandlerKeys.USER_ID_STRATEGY,
221             new String[] {UserIdStrategy.CURRENT_USER_ID});
222         parameterMap.put(
223             PortletDataHandlerKeys.USER_PERMISSIONS,
224             new String[] {Boolean.FALSE.toString()});
225 
226         return parameterMap;
227     }
228 
229 }