001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.communities.util;
016    
017    import com.liferay.portal.events.EventsProcessorUtil;
018    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
019    import com.liferay.portal.kernel.lar.UserIdStrategy;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.LayoutSet;
025    import com.liferay.portal.model.LayoutSetPrototype;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.security.permission.ActionKeys;
028    import com.liferay.portal.security.permission.PermissionChecker;
029    import com.liferay.portal.service.LayoutLocalServiceUtil;
030    import com.liferay.portal.service.LayoutServiceUtil;
031    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
032    import com.liferay.portal.service.permission.GroupPermissionUtil;
033    import com.liferay.portal.service.permission.LayoutPermissionUtil;
034    import com.liferay.portal.theme.ThemeDisplay;
035    import com.liferay.portal.util.LayoutSettings;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.portal.util.WebKeys;
038    
039    import java.io.File;
040    
041    import java.util.LinkedHashMap;
042    import java.util.Map;
043    
044    import javax.portlet.ActionRequest;
045    import javax.portlet.ActionResponse;
046    import javax.portlet.RenderRequest;
047    import javax.portlet.RenderResponse;
048    
049    import javax.servlet.http.HttpServletRequest;
050    import javax.servlet.http.HttpServletResponse;
051    
052    /**
053     * @author Raymond Augé
054     */
055    public class CommunitiesUtil {
056    
057            public static void applyLayoutSetPrototypes(
058                            Group group, long publicLayoutSetPrototypeId,
059                            long privateLayoutSetPrototypeId)
060                    throws Exception {
061    
062                    if (publicLayoutSetPrototypeId > 0) {
063                            LayoutSetPrototype layoutSetPrototype =
064                                    LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(
065                                            publicLayoutSetPrototypeId);
066    
067                            LayoutSet publicLayoutSet = group.getPublicLayoutSet();
068    
069                            copyLayoutSet(layoutSetPrototype.getLayoutSet(), publicLayoutSet);
070                    }
071    
072                    if (privateLayoutSetPrototypeId > 0) {
073                            LayoutSetPrototype layoutSetPrototype =
074                                    LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(
075                                            privateLayoutSetPrototypeId);
076    
077                            LayoutSet privateLayoutSet = group.getPrivateLayoutSet();
078    
079                            copyLayoutSet(layoutSetPrototype.getLayoutSet(), privateLayoutSet);
080                    }
081            }
082    
083            public static void copyLayoutSet(
084                            LayoutSet sourceLayoutSet, LayoutSet targetLayoutSet)
085                    throws Exception {
086    
087                    Map<String, String[]> parameterMap = getLayoutSetPrototypeParameters();
088    
089                    File file = LayoutLocalServiceUtil.exportLayoutsAsFile(
090                            sourceLayoutSet.getGroupId(), sourceLayoutSet.isPrivateLayout(),
091                            null, parameterMap, null, null);
092    
093                    try {
094                            LayoutServiceUtil.importLayouts(
095                                    targetLayoutSet.getGroupId(), targetLayoutSet.isPrivateLayout(),
096                                    parameterMap, file);
097                    }
098                    finally {
099                            file.delete();
100                    }
101            }
102    
103            public static void deleteLayout(
104                            ActionRequest actionRequest, ActionResponse actionResponse)
105                    throws Exception {
106    
107                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
108                            actionRequest);
109                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
110                            actionResponse);
111    
112                    deleteLayout(request, response);
113            }
114    
115            public static void deleteLayout(
116                            HttpServletRequest request, HttpServletResponse response)
117                    throws Exception {
118    
119                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
120                            WebKeys.THEME_DISPLAY);
121    
122                    PermissionChecker permissionChecker =
123                            themeDisplay.getPermissionChecker();
124    
125                    long plid = ParamUtil.getLong(request, "plid");
126    
127                    long groupId = ParamUtil.getLong(request, "groupId");
128                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
129                    long layoutId = ParamUtil.getLong(request, "layoutId");
130    
131                    Layout layout = null;
132    
133                    if (plid <= 0) {
134                            layout = LayoutLocalServiceUtil.getLayout(
135                                    groupId, privateLayout, layoutId);
136                    }
137                    else {
138                            layout = LayoutLocalServiceUtil.getLayout(plid);
139    
140                            groupId = layout.getGroupId();
141                            privateLayout = layout.isPrivateLayout();
142                            layoutId = layout.getLayoutId();
143                    }
144    
145                    Group group = layout.getGroup();
146    
147                    if (group.isStagingGroup() &&
148                            !GroupPermissionUtil.contains(
149                                    permissionChecker, groupId, ActionKeys.MANAGE_STAGING) &&
150                            !GroupPermissionUtil.contains(
151                                    permissionChecker, groupId, ActionKeys.PUBLISH_STAGING)) {
152    
153                            throw new PrincipalException();
154                    }
155    
156                    if (LayoutPermissionUtil.contains(
157                                    permissionChecker, groupId, privateLayout, layoutId,
158                                    ActionKeys.DELETE)) {
159    
160                            LayoutSettings layoutSettings = LayoutSettings.getInstance(layout);
161    
162                            EventsProcessorUtil.process(
163                                    PropsKeys.LAYOUT_CONFIGURATION_ACTION_DELETE,
164                                    layoutSettings.getConfigurationActionDelete(), request,
165                                    response);
166                    }
167    
168                    LayoutServiceUtil.deleteLayout(groupId, privateLayout, layoutId);
169            }
170    
171            public static void deleteLayout(
172                            RenderRequest renderRequest, RenderResponse renderResponse)
173                    throws Exception {
174    
175                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
176                            renderRequest);
177                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
178                            renderResponse);
179    
180                    deleteLayout(request, response);
181            }
182    
183            public static Map<String, String[]> getLayoutSetPrototypeParameters() {
184                    Map<String, String[]> parameterMap =
185                            new LinkedHashMap<String, String[]>();
186    
187                    parameterMap.put(
188                            PortletDataHandlerKeys.CATEGORIES,
189                            new String[] {Boolean.TRUE.toString()});
190                    parameterMap.put(
191                            PortletDataHandlerKeys.DATA_STRATEGY,
192                            new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
193                    parameterMap.put(
194                            PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
195                            new String[] {Boolean.TRUE.toString()});
196                    parameterMap.put(
197                            PortletDataHandlerKeys.DELETE_PORTLET_DATA,
198                            new String[] {Boolean.FALSE.toString()});
199                    parameterMap.put(
200                            PortletDataHandlerKeys.PERMISSIONS,
201                            new String[] {Boolean.TRUE.toString()});
202                    parameterMap.put(
203                            PortletDataHandlerKeys.PORTLET_DATA,
204                            new String[] {Boolean.TRUE.toString()});
205                    parameterMap.put(
206                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
207                            new String[] {Boolean.TRUE.toString()});
208                    parameterMap.put(
209                            PortletDataHandlerKeys.PORTLET_SETUP,
210                            new String[] {Boolean.TRUE.toString()});
211                    parameterMap.put(
212                            PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
213                            new String[] {Boolean.TRUE.toString()});
214                    parameterMap.put(
215                            PortletDataHandlerKeys.THEME,
216                            new String[] {Boolean.FALSE.toString()});
217                    parameterMap.put(
218                            PortletDataHandlerKeys.USER_ID_STRATEGY,
219                            new String[] {UserIdStrategy.CURRENT_USER_ID});
220                    parameterMap.put(
221                            PortletDataHandlerKeys.USER_PERMISSIONS,
222                            new String[] {Boolean.FALSE.toString()});
223    
224                    return parameterMap;
225            }
226    
227    }