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.portlet.communities.util;
24  
25  import com.germinus.easyconf.Filter;
26  
27  import com.liferay.portal.events.EventsProcessor;
28  import com.liferay.portal.kernel.security.permission.ActionKeys;
29  import com.liferay.portal.kernel.security.permission.PermissionChecker;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.model.Layout;
33  import com.liferay.portal.service.LayoutLocalServiceUtil;
34  import com.liferay.portal.service.LayoutServiceUtil;
35  import com.liferay.portal.service.permission.LayoutPermissionUtil;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.PropsUtil;
38  import com.liferay.portal.util.WebKeys;
39  import com.liferay.portlet.ActionRequestImpl;
40  import com.liferay.portlet.ActionResponseImpl;
41  import com.liferay.portlet.RenderRequestImpl;
42  import com.liferay.portlet.RenderResponseImpl;
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   */
58  public class CommunitiesUtil {
59  
60      public static void deleteLayout(ActionRequest req, ActionResponse res)
61          throws Exception {
62  
63          HttpServletRequest httpReq = (HttpServletRequest)
64              ((ActionRequestImpl)req).getHttpServletRequest();
65          HttpServletResponse httpRes = (HttpServletResponse)
66              ((ActionResponseImpl)res).getHttpServletResponse();
67  
68          deleteLayout(httpReq, httpRes);
69      }
70  
71      public static void deleteLayout(RenderRequest req, RenderResponse res)
72          throws Exception {
73  
74          HttpServletRequest httpReq = (HttpServletRequest)
75              ((RenderRequestImpl)req).getHttpServletRequest();
76          HttpServletResponse httpRes = (HttpServletResponse)
77              ((RenderResponseImpl)res).getHttpServletResponse();
78  
79          deleteLayout(httpReq, httpRes);
80      }
81  
82      public static void deleteLayout(
83              HttpServletRequest req, HttpServletResponse res)
84          throws Exception {
85  
86          ThemeDisplay themeDisplay =
87              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
88  
89          PermissionChecker permissionChecker =
90              themeDisplay.getPermissionChecker();
91  
92          long plid = ParamUtil.getLong(req, "plid");
93  
94          long groupId = ParamUtil.getLong(req, "groupId");
95          boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
96          long layoutId = ParamUtil.getLong(req, "layoutId");
97  
98          Layout layout = null;
99  
100         if (plid <= 0) {
101             layout = LayoutLocalServiceUtil.getLayout(
102                 groupId, privateLayout, layoutId);
103         }
104         else {
105             layout = LayoutLocalServiceUtil.getLayout(plid);
106 
107             groupId = layout.getGroupId();
108             privateLayout = layout.isPrivateLayout();
109             layoutId = layout.getLayoutId();
110         }
111 
112         if (LayoutPermissionUtil.contains(
113                 permissionChecker, groupId, privateLayout, layoutId,
114                 ActionKeys.DELETE)) {
115 
116             String[] eventClasses = StringUtil.split(
117                 PropsUtil.getComponentProperties().getString(
118                     PropsUtil.LAYOUT_CONFIGURATION_ACTION_DELETE,
119                     Filter.by(layout.getType())));
120 
121             EventsProcessor.process(eventClasses, req, res);
122         }
123 
124         LayoutServiceUtil.deleteLayout(groupId, privateLayout, layoutId);
125     }
126 
127 }