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