001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.servlet.SessionErrors;
022    import com.liferay.portal.kernel.servlet.SessionMessages;
023    import com.liferay.portal.kernel.upload.UploadException;
024    import com.liferay.portal.kernel.upload.UploadPortletRequest;
025    import com.liferay.portal.kernel.util.Constants;
026    import com.liferay.portal.kernel.util.HtmlUtil;
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                                    SessionMessages.add(
100                                            actionRequest,
101                                            PortalUtil.getPortletId(actionRequest) +
102                                                    SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
103                                            closeRedirect);
104                            }
105    
106                            sendRedirect(actionRequest, actionResponse, redirect);
107                    }
108                    catch (Exception e) {
109                            if (e instanceof PrincipalException ||
110                                    e instanceof SystemException) {
111    
112                                    SessionErrors.add(actionRequest, e.getClass());
113    
114                                    setForward(actionRequest, "portlet.layouts_admin.error");
115                            }
116                            else if (e instanceof FileSizeException ||
117                                             e instanceof ImageTypeException ||
118                                             e instanceof UploadException) {
119    
120                                    SessionErrors.add(actionRequest, e.getClass());
121                            }
122                            else {
123                                    throw e;
124                            }
125                    }
126            }
127    
128            @Override
129            public ActionForward render(
130                            ActionMapping actionMapping, ActionForm actionForm,
131                            PortletConfig portletConfig, RenderRequest renderRequest,
132                            RenderResponse renderResponse)
133                    throws Exception {
134    
135                    try {
136                            checkPermissions(renderRequest);
137                    }
138                    catch (PrincipalException pe) {
139                            SessionErrors.add(
140                                    renderRequest, PrincipalException.class.getName());
141    
142                            return actionMapping.findForward("portlet.layouts_admin.error");
143                    }
144    
145                    try {
146                            getGroup(renderRequest);
147                    }
148                    catch (Exception e) {
149                            if (e instanceof NoSuchGroupException ||
150                                    e instanceof PrincipalException) {
151    
152                                    SessionErrors.add(renderRequest, e.getClass());
153    
154                                    return actionMapping.findForward("portlet.layouts_admin.error");
155                            }
156                            else {
157                                    throw e;
158                            }
159                    }
160    
161                    return actionMapping.findForward(
162                            getForward(renderRequest, "portlet.layouts_admin.edit_layouts"));
163            }
164    
165            @Override
166            protected void setThemeSettingProperties(
167                    ActionRequest actionRequest, UnicodeProperties typeSettingsProperties,
168                    String themeId, Map<String, ThemeSetting> themeSettings, String device,
169                    String deviceThemeId) {
170    
171                    for (String key : themeSettings.keySet()) {
172                            ThemeSetting themeSetting = themeSettings.get(key);
173    
174                            String property =
175                                    device + "ThemeSettingsProperties--" + key +
176                                            StringPool.DOUBLE_DASH;
177    
178                            String value = ParamUtil.getString(
179                                    actionRequest, HtmlUtil.escapeAttribute(property));
180    
181                            if (!value.equals(themeSetting.getValue())) {
182                                    typeSettingsProperties.setProperty(
183                                            ThemeSettingImpl.namespaceProperty(device, key), value);
184                            }
185                    }
186            }
187    
188            protected void updateLayoutSet(
189                            ActionRequest actionRequest, ActionResponse actionResponse)
190                    throws Exception {
191    
192                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
193                            WebKeys.THEME_DISPLAY);
194    
195                    long layoutSetId = ParamUtil.getLong(actionRequest, "layoutSetId");
196    
197                    long liveGroupId = ParamUtil.getLong(actionRequest, "liveGroupId");
198                    long stagingGroupId = ParamUtil.getLong(
199                            actionRequest, "stagingGroupId");
200                    boolean privateLayout = ParamUtil.getBoolean(
201                            actionRequest, "privateLayout");
202    
203                    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
204                            layoutSetId);
205    
206                    updateLogo(
207                            actionRequest, liveGroupId, stagingGroupId, privateLayout,
208                            layoutSet.isLogo());
209    
210                    updateLookAndFeel(
211                            actionRequest, themeDisplay.getCompanyId(), liveGroupId,
212                            stagingGroupId, privateLayout, layoutSet.getThemeId(),
213                            layoutSet.getSettingsProperties());
214    
215                    updateMergePages(actionRequest, liveGroupId);
216    
217                    updateSettings(
218                            actionRequest, liveGroupId, stagingGroupId, privateLayout,
219                            layoutSet.getSettingsProperties());
220            }
221    
222            protected void updateLogo(
223                            ActionRequest actionRequest, long liveGroupId, long stagingGroupId,
224                            boolean privateLayout, boolean hasLogo)
225                    throws Exception {
226    
227                    UploadPortletRequest uploadPortletRequest =
228                            PortalUtil.getUploadPortletRequest(actionRequest);
229    
230                    boolean useLogo = ParamUtil.getBoolean(actionRequest, "useLogo");
231    
232                    InputStream inputStream = null;
233    
234                    try {
235                            File file = uploadPortletRequest.getFile("logoFileName");
236    
237                            if (useLogo && !file.exists()) {
238                                    if (hasLogo) {
239                                            return;
240                                    }
241    
242                                    throw new UploadException("No logo uploaded for use");
243                            }
244    
245                            if ((file != null) && file.exists()) {
246                                    inputStream = new ByteArrayFileInputStream(file, 1024);
247                            }
248    
249                            long groupId = liveGroupId;
250    
251                            if (stagingGroupId > 0) {
252                                    groupId = stagingGroupId;
253                            }
254    
255                            LayoutSetServiceUtil.updateLogo(
256                                    groupId, privateLayout, useLogo, inputStream, false);
257                    }
258                    finally {
259                            StreamUtil.cleanUp(inputStream);
260                    }
261            }
262    
263            protected void updateLookAndFeel(
264                            ActionRequest actionRequest, long companyId, long liveGroupId,
265                            long stagingGroupId, boolean privateLayout, String themeId,
266                            UnicodeProperties typeSettingsProperties)
267                    throws Exception {
268    
269                    String[] devices = StringUtil.split(
270                            ParamUtil.getString(actionRequest, "devices"));
271    
272                    for (String device : devices) {
273                            String deviceThemeId = ParamUtil.getString(
274                                    actionRequest, device + "ThemeId");
275                            String deviceColorSchemeId = ParamUtil.getString(
276                                    actionRequest, device + "ColorSchemeId");
277                            String deviceCss = ParamUtil.getString(
278                                    actionRequest, device + "Css");
279                            boolean deviceWapTheme = device.equals("wap");
280    
281                            if (Validator.isNotNull(deviceThemeId)) {
282                                    deviceColorSchemeId = getColorSchemeId(
283                                            companyId, deviceThemeId, deviceColorSchemeId,
284                                            deviceWapTheme);
285    
286                                    updateThemeSettingsProperties(
287                                            actionRequest, companyId, typeSettingsProperties, themeId,
288                                            device, deviceThemeId, deviceWapTheme);
289                            }
290    
291                            long groupId = liveGroupId;
292    
293                            if (stagingGroupId > 0) {
294                                    groupId = stagingGroupId;
295                            }
296    
297                            LayoutSetServiceUtil.updateLookAndFeel(
298                                    groupId, privateLayout, deviceThemeId, deviceColorSchemeId,
299                                    deviceCss, deviceWapTheme);
300                    }
301            }
302    
303            protected void updateMergePages(
304                            ActionRequest actionRequest, long liveGroupId)
305                    throws Exception {
306    
307                    boolean mergeGuestPublicPages = ParamUtil.getBoolean(
308                            actionRequest, "mergeGuestPublicPages");
309    
310                    Group liveGroup = GroupLocalServiceUtil.getGroup(liveGroupId);
311    
312                    UnicodeProperties typeSettingsProperties =
313                            liveGroup.getTypeSettingsProperties();
314    
315                    typeSettingsProperties.setProperty(
316                            "mergeGuestPublicPages", String.valueOf(mergeGuestPublicPages));
317    
318                    GroupServiceUtil.updateGroup(liveGroupId, liveGroup.getTypeSettings());
319            }
320    
321            protected void updateSettings(
322                            ActionRequest actionRequest, long liveGroupId, long stagingGroupId,
323                            boolean privateLayout, UnicodeProperties settingsProperties)
324                    throws Exception {
325    
326                    UnicodeProperties typeSettingsProperties =
327                            PropertiesParamUtil.getProperties(
328                                    actionRequest, "TypeSettingsProperties--");
329    
330                    settingsProperties.putAll(typeSettingsProperties);
331    
332                    long groupId = liveGroupId;
333    
334                    if (stagingGroupId > 0) {
335                            groupId = stagingGroupId;
336                    }
337    
338                    LayoutSetServiceUtil.updateSettings(
339                            groupId, privateLayout, settingsProperties.toString());
340            }
341    
342    }