001    /**
002     * Copyright (c) 2000-2011 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.ParamUtil;
027    import com.liferay.portal.kernel.util.PropertiesParamUtil;
028    import com.liferay.portal.kernel.util.StreamUtil;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.kernel.util.UnicodeProperties;
031    import com.liferay.portal.kernel.util.Validator;
032    import com.liferay.portal.model.Group;
033    import com.liferay.portal.model.LayoutSet;
034    import com.liferay.portal.security.auth.PrincipalException;
035    import com.liferay.portal.service.GroupLocalServiceUtil;
036    import com.liferay.portal.service.GroupServiceUtil;
037    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
038    import com.liferay.portal.service.LayoutSetServiceUtil;
039    import com.liferay.portal.theme.ThemeDisplay;
040    import com.liferay.portal.util.PortalUtil;
041    import com.liferay.portal.util.WebKeys;
042    import com.liferay.portlet.documentlibrary.FileSizeException;
043    
044    import java.io.File;
045    import java.io.InputStream;
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 mapping, ActionForm form, PortletConfig portletConfig,
066                            ActionRequest actionRequest, ActionResponse actionResponse)
067                    throws Exception {
068    
069                    try {
070                            checkPermissions(actionRequest);
071                    }
072                    catch (PrincipalException pe) {
073                            return;
074                    }
075    
076                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
077    
078                    try {
079                            if (cmd.equals(Constants.UPDATE)) {
080                                    updateLayoutSet(actionRequest, actionResponse);
081                            }
082    
083                            String closeRedirect = ParamUtil.getString(
084                                    actionRequest, "closeRedirect");
085    
086                            if (Validator.isNotNull(closeRedirect)) {
087                                    SessionMessages.add(
088                                            actionRequest,
089                                            portletConfig.getPortletName() + ".doCloseRedirect",
090                                            closeRedirect);
091                            }
092    
093                            sendRedirect(actionRequest, actionResponse);
094                    }
095                    catch (Exception e) {
096                            if (e instanceof PrincipalException ||
097                                    e instanceof SystemException) {
098    
099                                    SessionErrors.add(actionRequest, e.getClass().getName());
100    
101                                    setForward(actionRequest, "portlet.layouts_admin.error");
102                            }
103                            else if (e instanceof FileSizeException ||
104                                             e instanceof ImageTypeException ||
105                                             e instanceof UploadException) {
106    
107                                    SessionErrors.add(actionRequest, e.getClass().getName());
108                            }
109                            else {
110                                    throw e;
111                            }
112                    }
113            }
114    
115            @Override
116            public ActionForward render(
117                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
118                            RenderRequest renderRequest, RenderResponse renderResponse)
119                    throws Exception {
120    
121                    try {
122                            checkPermissions(renderRequest);
123                    }
124                    catch (PrincipalException pe) {
125                            SessionErrors.add(
126                                    renderRequest, PrincipalException.class.getName());
127    
128                            return mapping.findForward("portlet.layouts_admin.error");
129                    }
130    
131                    try {
132                            getGroup(renderRequest);
133                    }
134                    catch (Exception e) {
135                            if (e instanceof NoSuchGroupException ||
136                                    e instanceof PrincipalException) {
137    
138                                    SessionErrors.add(renderRequest, e.getClass().getName());
139    
140                                    return mapping.findForward("portlet.layouts_admin.error");
141                            }
142                            else {
143                                    throw e;
144                            }
145                    }
146    
147                    return mapping.findForward(
148                            getForward(renderRequest, "portlet.layouts_admin.edit_layouts"));
149            }
150    
151            protected void updateLayoutSet(
152                            ActionRequest actionRequest, ActionResponse actionResponse)
153                    throws Exception {
154    
155                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
156                            WebKeys.THEME_DISPLAY);
157    
158                    long layoutSetId = ParamUtil.getLong(actionRequest, "layoutSetId");
159    
160                    long liveGroupId = ParamUtil.getLong(actionRequest, "liveGroupId");
161                    long stagingGroupId = ParamUtil.getLong(
162                            actionRequest, "stagingGroupId");
163                    boolean privateLayout = ParamUtil.getBoolean(
164                            actionRequest, "privateLayout");
165    
166                    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
167                            layoutSetId);
168    
169                    updateLogo(
170                            actionRequest, liveGroupId, stagingGroupId, privateLayout,
171                            layoutSet.isLogo());
172    
173                    updateLookAndFeel(
174                            actionRequest, themeDisplay.getCompanyId(), liveGroupId,
175                            stagingGroupId, privateLayout, layoutSet.getThemeId(),
176                            layoutSet.getSettingsProperties());
177    
178                    updateMergePages(actionRequest, liveGroupId);
179    
180                    updateSettings(
181                            actionRequest, liveGroupId, stagingGroupId, privateLayout,
182                            layoutSet.getSettingsProperties());
183            }
184    
185            protected void updateLogo(
186                            ActionRequest actionRequest, long liveGroupId,
187                            long stagingGroupId, boolean privateLayout, boolean hasLogo)
188                    throws Exception {
189    
190                    UploadPortletRequest uploadPortletRequest =
191                            PortalUtil.getUploadPortletRequest(actionRequest);
192    
193                    boolean useLogo = ParamUtil.getBoolean(actionRequest, "useLogo");
194    
195                    InputStream inputStream = null;
196    
197                    try {
198                            File file = uploadPortletRequest.getFile("logoFileName");
199    
200                            if (useLogo && !file.exists()) {
201                                    if (hasLogo) {
202                                            return;
203                                    }
204    
205                                    throw new UploadException("No logo uploaded for use");
206                            }
207    
208                            if (file.exists()) {
209                                    inputStream = new ByteArrayFileInputStream(file, 1024);
210                            }
211    
212                            if (inputStream != null) {
213                                    inputStream.mark(0);
214                            }
215    
216                            LayoutSetServiceUtil.updateLogo(
217                                    liveGroupId, privateLayout, useLogo, inputStream, false);
218    
219                            if (inputStream != null) {
220                                    inputStream.reset();
221                            }
222    
223                            if (stagingGroupId > 0) {
224                                    LayoutSetServiceUtil.updateLogo(
225                                            stagingGroupId, privateLayout, useLogo, inputStream, false);
226                            }
227                    }
228                    finally {
229                            StreamUtil.cleanUp(inputStream);
230                    }
231            }
232    
233            protected void updateLookAndFeel(
234                            ActionRequest actionRequest, long companyId, long liveGroupId,
235                            long stagingGroupId, boolean privateLayout, String oldThemeId,
236                            UnicodeProperties typeSettingsProperties)
237                    throws Exception {
238    
239                    String[] devices = StringUtil.split(
240                            ParamUtil.getString(actionRequest, "devices"));
241    
242                    for (String device : devices) {
243                            String themeId = ParamUtil.getString(
244                                    actionRequest, device + "ThemeId");
245                            String colorSchemeId = ParamUtil.getString(
246                                    actionRequest, device + "ColorSchemeId");
247                            String css = ParamUtil.getString(actionRequest, device + "Css");
248                            boolean wapTheme = device.equals("wap");
249    
250                            if (Validator.isNotNull(themeId)) {
251                                    colorSchemeId = getColorSchemeId(
252                                            actionRequest, companyId, typeSettingsProperties, device,
253                                            themeId, colorSchemeId, wapTheme);
254                            }
255    
256                            long groupId = liveGroupId;
257    
258                            if (stagingGroupId > 0) {
259                                    groupId = stagingGroupId;
260                            }
261    
262                            LayoutSetServiceUtil.updateLookAndFeel(
263                                    groupId, privateLayout, themeId, colorSchemeId, css, wapTheme);
264                    }
265            }
266    
267            protected void updateMergePages(
268                            ActionRequest actionRequest, long liveGroupId)
269                    throws Exception {
270    
271                    boolean mergeGuestPublicPages = ParamUtil.getBoolean(
272                            actionRequest, "mergeGuestPublicPages");
273    
274                    Group liveGroup = GroupLocalServiceUtil.getGroup(liveGroupId);
275    
276                    UnicodeProperties typeSettingsProperties =
277                            liveGroup.getTypeSettingsProperties();
278    
279                    typeSettingsProperties.setProperty(
280                            "mergeGuestPublicPages", String.valueOf(mergeGuestPublicPages));
281    
282                    GroupServiceUtil.updateGroup(liveGroupId, liveGroup.getTypeSettings());
283            }
284    
285            protected void updateSettings(
286                            ActionRequest actionRequest, long liveGroupId, long stagingGroupId,
287                            boolean privateLayout, UnicodeProperties settingsProperties)
288                    throws Exception {
289    
290                    UnicodeProperties typeSettingsProperties =
291                            PropertiesParamUtil.getProperties(
292                                    actionRequest, "TypeSettingsProperties--");
293    
294                    settingsProperties.putAll(typeSettingsProperties);
295    
296                    LayoutSetServiceUtil.updateSettings(
297                            liveGroupId, privateLayout, settingsProperties.toString());
298    
299                    if (stagingGroupId > 0) {
300                            LayoutSetServiceUtil.updateSettings(
301                                    stagingGroupId, privateLayout,
302                                    typeSettingsProperties.toString());
303                    }
304            }
305    
306    }