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 value = null;
174    
175                            if (!themeId.equals(deviceThemeId)) {
176                                    value = themeSetting.getValue();
177                            }
178                            else {
179                                    String property =
180                                            device + "ThemeSettingsProperties--" + key +
181                                                    StringPool.DOUBLE_DASH;
182    
183                                    value = ParamUtil.getString(actionRequest, property);
184                            }
185    
186                            if (!value.equals(themeSetting.getValue())) {
187                                    typeSettingsProperties.setProperty(
188                                            ThemeSettingImpl.namespaceProperty(device, key), value);
189                            }
190                    }
191            }
192    
193            protected void updateLayoutSet(
194                            ActionRequest actionRequest, ActionResponse actionResponse)
195                    throws Exception {
196    
197                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
198                            WebKeys.THEME_DISPLAY);
199    
200                    long layoutSetId = ParamUtil.getLong(actionRequest, "layoutSetId");
201    
202                    long liveGroupId = ParamUtil.getLong(actionRequest, "liveGroupId");
203                    long stagingGroupId = ParamUtil.getLong(
204                            actionRequest, "stagingGroupId");
205                    boolean privateLayout = ParamUtil.getBoolean(
206                            actionRequest, "privateLayout");
207    
208                    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
209                            layoutSetId);
210    
211                    updateLogo(
212                            actionRequest, liveGroupId, stagingGroupId, privateLayout,
213                            layoutSet.isLogo());
214    
215                    updateLookAndFeel(
216                            actionRequest, themeDisplay.getCompanyId(), liveGroupId,
217                            stagingGroupId, privateLayout, layoutSet.getThemeId(),
218                            layoutSet.getSettingsProperties());
219    
220                    updateMergePages(actionRequest, liveGroupId);
221    
222                    updateSettings(
223                            actionRequest, liveGroupId, stagingGroupId, privateLayout,
224                            layoutSet.getSettingsProperties());
225            }
226    
227            protected void updateLogo(
228                            ActionRequest actionRequest, long liveGroupId, long stagingGroupId,
229                            boolean privateLayout, boolean hasLogo)
230                    throws Exception {
231    
232                    UploadPortletRequest uploadPortletRequest =
233                            PortalUtil.getUploadPortletRequest(actionRequest);
234    
235                    boolean useLogo = ParamUtil.getBoolean(actionRequest, "useLogo");
236    
237                    InputStream inputStream = null;
238    
239                    try {
240                            File file = uploadPortletRequest.getFile("logoFileName");
241    
242                            if (useLogo && !file.exists()) {
243                                    if (hasLogo) {
244                                            return;
245                                    }
246    
247                                    throw new UploadException("No logo uploaded for use");
248                            }
249    
250                            if ((file != null) && file.exists()) {
251                                    inputStream = new ByteArrayFileInputStream(file, 1024);
252                            }
253    
254                            long groupId = liveGroupId;
255    
256                            if (stagingGroupId > 0) {
257                                    groupId = stagingGroupId;
258                            }
259    
260                            LayoutSetServiceUtil.updateLogo(
261                                    groupId, privateLayout, useLogo, inputStream, false);
262                    }
263                    finally {
264                            StreamUtil.cleanUp(inputStream);
265                    }
266            }
267    
268            protected void updateLookAndFeel(
269                            ActionRequest actionRequest, long companyId, long liveGroupId,
270                            long stagingGroupId, boolean privateLayout, String themeId,
271                            UnicodeProperties typeSettingsProperties)
272                    throws Exception {
273    
274                    String[] devices = StringUtil.split(
275                            ParamUtil.getString(actionRequest, "devices"));
276    
277                    for (String device : devices) {
278                            String deviceThemeId = ParamUtil.getString(
279                                    actionRequest, device + "ThemeId");
280                            String deviceColorSchemeId = ParamUtil.getString(
281                                    actionRequest, device + "ColorSchemeId");
282                            String deviceCss = ParamUtil.getString(
283                                    actionRequest, device + "Css");
284                            boolean deviceWapTheme = device.equals("wap");
285    
286                            if (Validator.isNotNull(deviceThemeId)) {
287                                    deviceColorSchemeId = getColorSchemeId(
288                                            companyId, deviceThemeId, deviceColorSchemeId,
289                                            deviceWapTheme);
290    
291                                    updateThemeSettingsProperties(
292                                            actionRequest, companyId, typeSettingsProperties, themeId,
293                                            device, deviceThemeId, deviceWapTheme);
294                            }
295    
296                            long groupId = liveGroupId;
297    
298                            if (stagingGroupId > 0) {
299                                    groupId = stagingGroupId;
300                            }
301    
302                            LayoutSetServiceUtil.updateLookAndFeel(
303                                    groupId, privateLayout, deviceThemeId, deviceColorSchemeId,
304                                    deviceCss, deviceWapTheme);
305                    }
306            }
307    
308            protected void updateMergePages(
309                            ActionRequest actionRequest, long liveGroupId)
310                    throws Exception {
311    
312                    boolean mergeGuestPublicPages = ParamUtil.getBoolean(
313                            actionRequest, "mergeGuestPublicPages");
314    
315                    Group liveGroup = GroupLocalServiceUtil.getGroup(liveGroupId);
316    
317                    UnicodeProperties typeSettingsProperties =
318                            liveGroup.getTypeSettingsProperties();
319    
320                    typeSettingsProperties.setProperty(
321                            "mergeGuestPublicPages", String.valueOf(mergeGuestPublicPages));
322    
323                    GroupServiceUtil.updateGroup(liveGroupId, liveGroup.getTypeSettings());
324            }
325    
326            protected void updateSettings(
327                            ActionRequest actionRequest, long liveGroupId, long stagingGroupId,
328                            boolean privateLayout, UnicodeProperties settingsProperties)
329                    throws Exception {
330    
331                    UnicodeProperties typeSettingsProperties =
332                            PropertiesParamUtil.getProperties(
333                                    actionRequest, "TypeSettingsProperties--");
334    
335                    settingsProperties.putAll(typeSettingsProperties);
336    
337                    long groupId = liveGroupId;
338    
339                    if (stagingGroupId > 0) {
340                            groupId = stagingGroupId;
341                    }
342    
343                    LayoutSetServiceUtil.updateSettings(
344                            groupId, privateLayout, settingsProperties.toString());
345            }
346    
347    }