001    /**
002     * Copyright (c) 2000-2012 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.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.GetterUtil;
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 mapping, ActionForm form, PortletConfig portletConfig,
073                            ActionRequest actionRequest, ActionResponse actionResponse)
074                    throws Exception {
075    
076                    try {
077                            checkPermissions(actionRequest);
078                    }
079                    catch (PrincipalException pe) {
080                            return;
081                    }
082    
083                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
084    
085                    try {
086                            if (cmd.equals(Constants.UPDATE)) {
087                                    updateLayoutSet(actionRequest, actionResponse);
088                            }
089    
090                            String closeRedirect = ParamUtil.getString(
091                                    actionRequest, "closeRedirect");
092    
093                            if (Validator.isNotNull(closeRedirect)) {
094                                    LiferayPortletConfig liferayPortletConfig =
095                                            (LiferayPortletConfig)portletConfig;
096    
097                                    SessionMessages.add(
098                                            actionRequest,
099                                            liferayPortletConfig.getPortletId() +
100                                                    SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
101                                            closeRedirect);
102                            }
103    
104                            sendRedirect(actionRequest, actionResponse);
105                    }
106                    catch (Exception e) {
107                            if (e instanceof PrincipalException ||
108                                    e instanceof SystemException) {
109    
110                                    SessionErrors.add(actionRequest, e.getClass());
111    
112                                    setForward(actionRequest, "portlet.layouts_admin.error");
113                            }
114                            else if (e instanceof FileSizeException ||
115                                             e instanceof ImageTypeException ||
116                                             e instanceof UploadException) {
117    
118                                    SessionErrors.add(actionRequest, e.getClass());
119                            }
120                            else {
121                                    throw e;
122                            }
123                    }
124            }
125    
126            @Override
127            public ActionForward render(
128                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
129                            RenderRequest renderRequest, RenderResponse renderResponse)
130                    throws Exception {
131    
132                    try {
133                            checkPermissions(renderRequest);
134                    }
135                    catch (PrincipalException pe) {
136                            SessionErrors.add(
137                                    renderRequest, PrincipalException.class.getName());
138    
139                            return mapping.findForward("portlet.layouts_admin.error");
140                    }
141    
142                    try {
143                            getGroup(renderRequest);
144                    }
145                    catch (Exception e) {
146                            if (e instanceof NoSuchGroupException ||
147                                    e instanceof PrincipalException) {
148    
149                                    SessionErrors.add(renderRequest, e.getClass());
150    
151                                    return mapping.findForward("portlet.layouts_admin.error");
152                            }
153                            else {
154                                    throw e;
155                            }
156                    }
157    
158                    return mapping.findForward(
159                            getForward(renderRequest, "portlet.layouts_admin.edit_layouts"));
160            }
161    
162            @Override
163            protected void setThemeSettingProperties(
164                    ActionRequest actionRequest, UnicodeProperties typeSettingsProperties,
165                    String device,
166                    Map<String, ThemeSetting> themeSettings) {
167    
168                    for (String key : themeSettings.keySet()) {
169                            ThemeSetting themeSetting = themeSettings.get(key);
170    
171                            String type = GetterUtil.getString(themeSetting.getType(), "text");
172    
173                            String property =
174                                    device + "ThemeSettingsProperties--" + key +
175                                            StringPool.DOUBLE_DASH;
176    
177                            String value = ParamUtil.getString(actionRequest, property);
178    
179                            if (type.equals("checkbox")) {
180                                    value = String.valueOf(GetterUtil.getBoolean(value));
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 oldThemeId,
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 themeId = ParamUtil.getString(
276                                    actionRequest, device + "ThemeId");
277                            String colorSchemeId = ParamUtil.getString(
278                                    actionRequest, device + "ColorSchemeId");
279                            String css = ParamUtil.getString(actionRequest, device + "Css");
280                            boolean wapTheme = device.equals("wap");
281    
282                            if (Validator.isNotNull(themeId)) {
283                                    colorSchemeId = getColorSchemeId(
284                                            companyId, themeId, colorSchemeId, wapTheme);
285    
286                                    updateThemeSettingsProperties(
287                                            actionRequest, companyId, typeSettingsProperties, device,
288                                            themeId, wapTheme);
289                            }
290    
291                            long groupId = liveGroupId;
292    
293                            if (stagingGroupId > 0) {
294                                    groupId = stagingGroupId;
295                            }
296    
297                            LayoutSetServiceUtil.updateLookAndFeel(
298                                    groupId, privateLayout, themeId, colorSchemeId, css, wapTheme);
299                    }
300            }
301    
302            protected void updateMergePages(
303                            ActionRequest actionRequest, long liveGroupId)
304                    throws Exception {
305    
306                    boolean mergeGuestPublicPages = ParamUtil.getBoolean(
307                            actionRequest, "mergeGuestPublicPages");
308    
309                    Group liveGroup = GroupLocalServiceUtil.getGroup(liveGroupId);
310    
311                    UnicodeProperties typeSettingsProperties =
312                            liveGroup.getTypeSettingsProperties();
313    
314                    typeSettingsProperties.setProperty(
315                            "mergeGuestPublicPages", String.valueOf(mergeGuestPublicPages));
316    
317                    GroupServiceUtil.updateGroup(liveGroupId, liveGroup.getTypeSettings());
318            }
319    
320            protected void updateSettings(
321                            ActionRequest actionRequest, long liveGroupId, long stagingGroupId,
322                            boolean privateLayout, UnicodeProperties settingsProperties)
323                    throws Exception {
324    
325                    UnicodeProperties typeSettingsProperties =
326                            PropertiesParamUtil.getProperties(
327                                    actionRequest, "TypeSettingsProperties--");
328    
329                    settingsProperties.putAll(typeSettingsProperties);
330    
331                    long groupId = liveGroupId;
332    
333                    if (stagingGroupId > 0) {
334                            groupId = stagingGroupId;
335                    }
336    
337                    LayoutSetServiceUtil.updateSettings(
338                            groupId, privateLayout, settingsProperties.toString());
339            }
340    
341    }