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