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