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,
188                            long stagingGroupId, 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                            if (inputStream != null) {
214                                    inputStream.mark(0);
215                            }
216    
217                            LayoutSetServiceUtil.updateLogo(
218                                    liveGroupId, privateLayout, useLogo, inputStream, false);
219    
220                            if (inputStream != null) {
221                                    inputStream.reset();
222                            }
223    
224                            if (stagingGroupId > 0) {
225                                    LayoutSetServiceUtil.updateLogo(
226                                            stagingGroupId, privateLayout, useLogo, inputStream, false);
227                            }
228                    }
229                    finally {
230                            StreamUtil.cleanUp(inputStream);
231                    }
232            }
233    
234            protected void updateLookAndFeel(
235                            ActionRequest actionRequest, long companyId, long liveGroupId,
236                            long stagingGroupId, boolean privateLayout, String oldThemeId,
237                            UnicodeProperties typeSettingsProperties)
238                    throws Exception {
239    
240                    String[] devices = StringUtil.split(
241                            ParamUtil.getString(actionRequest, "devices"));
242    
243                    for (String device : devices) {
244                            String themeId = ParamUtil.getString(
245                                    actionRequest, device + "ThemeId");
246                            String colorSchemeId = ParamUtil.getString(
247                                    actionRequest, device + "ColorSchemeId");
248                            String css = ParamUtil.getString(actionRequest, device + "Css");
249                            boolean wapTheme = device.equals("wap");
250    
251                            if (Validator.isNotNull(themeId)) {
252                                    colorSchemeId = getColorSchemeId(
253                                            actionRequest, companyId, typeSettingsProperties, device,
254                                            themeId, colorSchemeId, wapTheme);
255                            }
256    
257                            long groupId = liveGroupId;
258    
259                            if (stagingGroupId > 0) {
260                                    groupId = stagingGroupId;
261                            }
262    
263                            LayoutSetServiceUtil.updateLookAndFeel(
264                                    groupId, privateLayout, themeId, colorSchemeId, css, wapTheme);
265                    }
266            }
267    
268            protected void updateMergePages(
269                            ActionRequest actionRequest, long liveGroupId)
270                    throws Exception {
271    
272                    boolean mergeGuestPublicPages = ParamUtil.getBoolean(
273                            actionRequest, "mergeGuestPublicPages");
274    
275                    Group liveGroup = GroupLocalServiceUtil.getGroup(liveGroupId);
276    
277                    UnicodeProperties typeSettingsProperties =
278                            liveGroup.getTypeSettingsProperties();
279    
280                    typeSettingsProperties.setProperty(
281                            "mergeGuestPublicPages", String.valueOf(mergeGuestPublicPages));
282    
283                    GroupServiceUtil.updateGroup(liveGroupId, liveGroup.getTypeSettings());
284            }
285    
286            protected void updateSettings(
287                            ActionRequest actionRequest, long liveGroupId, long stagingGroupId,
288                            boolean privateLayout, UnicodeProperties settingsProperties)
289                    throws Exception {
290    
291                    UnicodeProperties typeSettingsProperties =
292                            PropertiesParamUtil.getProperties(
293                                    actionRequest, "TypeSettingsProperties--");
294    
295                    settingsProperties.putAll(typeSettingsProperties);
296    
297                    boolean showSiteName = ParamUtil.getBoolean(
298                            actionRequest, "showSiteName");
299    
300                    settingsProperties.put("showSiteName", Boolean.toString(showSiteName));
301    
302                    LayoutSetServiceUtil.updateSettings(
303                            liveGroupId, privateLayout, settingsProperties.toString());
304    
305                    if (stagingGroupId > 0) {
306                            LayoutSetServiceUtil.updateSettings(
307                                    stagingGroupId, privateLayout,
308                                    typeSettingsProperties.toString());
309                    }
310            }
311    
312    }