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