001    /**
002     * Copyright (c) 2000-2012 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.layoutsadmin.action;
016    
017    import com.liferay.portal.ImageTypeException;
018    import com.liferay.portal.NoSuchGroupException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.io.ByteArrayFileInputStream;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.servlet.SessionMessages;
023    import com.liferay.portal.kernel.upload.UploadException;
024    import com.liferay.portal.kernel.upload.UploadPortletRequest;
025    import com.liferay.portal.kernel.util.Constants;
026    import com.liferay.portal.kernel.util.ParamUtil;
027    import com.liferay.portal.kernel.util.PropertiesParamUtil;
028    import com.liferay.portal.kernel.util.StreamUtil;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.kernel.util.UnicodeProperties;
031    import com.liferay.portal.kernel.util.Validator;
032    import com.liferay.portal.model.Group;
033    import com.liferay.portal.model.LayoutSet;
034    import com.liferay.portal.security.auth.PrincipalException;
035    import com.liferay.portal.service.GroupLocalServiceUtil;
036    import com.liferay.portal.service.GroupServiceUtil;
037    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
038    import com.liferay.portal.service.LayoutSetServiceUtil;
039    import com.liferay.portal.theme.ThemeDisplay;
040    import com.liferay.portal.util.PortalUtil;
041    import com.liferay.portal.util.WebKeys;
042    import com.liferay.portlet.documentlibrary.FileSizeException;
043    
044    import java.io.File;
045    import java.io.InputStream;
046    
047    import javax.portlet.ActionRequest;
048    import javax.portlet.ActionResponse;
049    import javax.portlet.PortletConfig;
050    import javax.portlet.RenderRequest;
051    import javax.portlet.RenderResponse;
052    
053    import org.apache.struts.action.ActionForm;
054    import org.apache.struts.action.ActionForward;
055    import org.apache.struts.action.ActionMapping;
056    
057    /**
058     * @author Brian Wing Shun Chan
059     * @author Julio Camarero
060     */
061    public class EditLayoutSetAction extends EditLayoutsAction {
062    
063            @Override
064            public void processAction(
065                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
066                            ActionRequest actionRequest, ActionResponse actionResponse)
067                    throws Exception {
068    
069                    try {
070                            checkPermissions(actionRequest);
071                    }
072                    catch (PrincipalException pe) {
073                            return;
074                    }
075    
076                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
077    
078                    try {
079                            if (cmd.equals(Constants.UPDATE)) {
080                                    updateLayoutSet(actionRequest, actionResponse);
081                            }
082    
083                            String closeRedirect = ParamUtil.getString(
084                                    actionRequest, "closeRedirect");
085    
086                            if (Validator.isNotNull(closeRedirect)) {
087                                    SessionMessages.add(
088                                            actionRequest,
089                                            portletConfig.getPortletName() +
090                                                    SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
091                                            closeRedirect);
092                            }
093    
094                            sendRedirect(actionRequest, actionResponse);
095                    }
096                    catch (Exception e) {
097                            if (e instanceof PrincipalException ||
098                                    e instanceof SystemException) {
099    
100                                    SessionErrors.add(actionRequest, e.getClass().getName());
101    
102                                    setForward(actionRequest, "portlet.layouts_admin.error");
103                            }
104                            else if (e instanceof FileSizeException ||
105                                             e instanceof ImageTypeException ||
106                                             e instanceof UploadException) {
107    
108                                    SessionErrors.add(actionRequest, e.getClass().getName());
109                            }
110                            else {
111                                    throw e;
112                            }
113                    }
114            }
115    
116            @Override
117            public ActionForward render(
118                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
119                            RenderRequest renderRequest, RenderResponse renderResponse)
120                    throws Exception {
121    
122                    try {
123                            checkPermissions(renderRequest);
124                    }
125                    catch (PrincipalException pe) {
126                            SessionErrors.add(
127                                    renderRequest, PrincipalException.class.getName());
128    
129                            return mapping.findForward("portlet.layouts_admin.error");
130                    }
131    
132                    try {
133                            getGroup(renderRequest);
134                    }
135                    catch (Exception e) {
136                            if (e instanceof NoSuchGroupException ||
137                                    e instanceof PrincipalException) {
138    
139                                    SessionErrors.add(renderRequest, e.getClass().getName());
140    
141                                    return mapping.findForward("portlet.layouts_admin.error");
142                            }
143                            else {
144                                    throw e;
145                            }
146                    }
147    
148                    return mapping.findForward(
149                            getForward(renderRequest, "portlet.layouts_admin.edit_layouts"));
150            }
151    
152            protected void updateLayoutSet(
153                            ActionRequest actionRequest, ActionResponse actionResponse)
154                    throws Exception {
155    
156                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
157                            WebKeys.THEME_DISPLAY);
158    
159                    long layoutSetId = ParamUtil.getLong(actionRequest, "layoutSetId");
160    
161                    long liveGroupId = ParamUtil.getLong(actionRequest, "liveGroupId");
162                    long stagingGroupId = ParamUtil.getLong(
163                            actionRequest, "stagingGroupId");
164                    boolean privateLayout = ParamUtil.getBoolean(
165                            actionRequest, "privateLayout");
166    
167                    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
168                            layoutSetId);
169    
170                    updateLogo(
171                            actionRequest, liveGroupId, stagingGroupId, privateLayout,
172                            layoutSet.isLogo());
173    
174                    updateLookAndFeel(
175                            actionRequest, themeDisplay.getCompanyId(), liveGroupId,
176                            stagingGroupId, privateLayout, layoutSet.getThemeId(),
177                            layoutSet.getSettingsProperties());
178    
179                    updateMergePages(actionRequest, liveGroupId);
180    
181                    updateSettings(
182                            actionRequest, liveGroupId, stagingGroupId, privateLayout,
183                            layoutSet.getSettingsProperties());
184            }
185    
186            protected void updateLogo(
187                            ActionRequest actionRequest, long liveGroupId, long stagingGroupId,
188                            boolean privateLayout, boolean hasLogo)
189                    throws Exception {
190    
191                    UploadPortletRequest uploadPortletRequest =
192                            PortalUtil.getUploadPortletRequest(actionRequest);
193    
194                    boolean useLogo = ParamUtil.getBoolean(actionRequest, "useLogo");
195    
196                    InputStream inputStream = null;
197    
198                    try {
199                            File file = uploadPortletRequest.getFile("logoFileName");
200    
201                            if (useLogo && !file.exists()) {
202                                    if (hasLogo) {
203                                            return;
204                                    }
205    
206                                    throw new UploadException("No logo uploaded for use");
207                            }
208    
209                            if (file.exists()) {
210                                    inputStream = new ByteArrayFileInputStream(file, 1024);
211                            }
212    
213                            long groupId = liveGroupId;
214    
215                            if (stagingGroupId > 0) {
216                                    groupId = stagingGroupId;
217                            }
218    
219                            LayoutSetServiceUtil.updateLogo(
220                                    groupId, privateLayout, useLogo, inputStream, false);
221                    }
222                    finally {
223                            StreamUtil.cleanUp(inputStream);
224                    }
225            }
226    
227            protected void updateLookAndFeel(
228                            ActionRequest actionRequest, long companyId, long liveGroupId,
229                            long stagingGroupId, boolean privateLayout, String oldThemeId,
230                            UnicodeProperties typeSettingsProperties)
231                    throws Exception {
232    
233                    String[] devices = StringUtil.split(
234                            ParamUtil.getString(actionRequest, "devices"));
235    
236                    for (String device : devices) {
237                            String themeId = ParamUtil.getString(
238                                    actionRequest, device + "ThemeId");
239                            String colorSchemeId = ParamUtil.getString(
240                                    actionRequest, device + "ColorSchemeId");
241                            String css = ParamUtil.getString(actionRequest, device + "Css");
242                            boolean wapTheme = device.equals("wap");
243    
244                            if (Validator.isNotNull(themeId)) {
245                                    colorSchemeId = getColorSchemeId(
246                                            companyId, themeId, colorSchemeId, wapTheme);
247    
248                                    getThemeSettingsProperties(
249                                            actionRequest, companyId, typeSettingsProperties, device,
250                                            themeId, wapTheme);
251                            }
252    
253                            long groupId = liveGroupId;
254    
255                            if (stagingGroupId > 0) {
256                                    groupId = stagingGroupId;
257                            }
258    
259                            LayoutSetServiceUtil.updateLookAndFeel(
260                                    groupId, privateLayout, themeId, colorSchemeId, css, wapTheme);
261                    }
262            }
263    
264            protected void updateMergePages(
265                            ActionRequest actionRequest, long liveGroupId)
266                    throws Exception {
267    
268                    boolean mergeGuestPublicPages = ParamUtil.getBoolean(
269                            actionRequest, "mergeGuestPublicPages");
270    
271                    Group liveGroup = GroupLocalServiceUtil.getGroup(liveGroupId);
272    
273                    UnicodeProperties typeSettingsProperties =
274                            liveGroup.getTypeSettingsProperties();
275    
276                    typeSettingsProperties.setProperty(
277                            "mergeGuestPublicPages", String.valueOf(mergeGuestPublicPages));
278    
279                    GroupServiceUtil.updateGroup(liveGroupId, liveGroup.getTypeSettings());
280            }
281    
282            protected void updateSettings(
283                            ActionRequest actionRequest, long liveGroupId, long stagingGroupId,
284                            boolean privateLayout, UnicodeProperties settingsProperties)
285                    throws Exception {
286    
287                    UnicodeProperties typeSettingsProperties =
288                            PropertiesParamUtil.getProperties(
289                                    actionRequest, "TypeSettingsProperties--");
290    
291                    settingsProperties.putAll(typeSettingsProperties);
292    
293                    long groupId = liveGroupId;
294    
295                    if (stagingGroupId > 0) {
296                            groupId = stagingGroupId;
297                    }
298    
299                    LayoutSetServiceUtil.updateSettings(
300                            groupId, privateLayout, settingsProperties.toString());
301            }
302    
303    }