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