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