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