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.layoutsetprototypes.action;
016    
017    import com.liferay.portal.NoSuchLayoutSetPrototypeException;
018    import com.liferay.portal.RequiredLayoutSetPrototypeException;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.LocalizationUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.UnicodeProperties;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.LayoutSetPrototype;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.service.LayoutSetPrototypeServiceUtil;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.service.ServiceContextFactory;
031    import com.liferay.portal.struts.PortletAction;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portal.util.PortletKeys;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.portlet.sites.util.SitesUtil;
037    
038    import java.util.Locale;
039    import java.util.Map;
040    
041    import javax.portlet.ActionRequest;
042    import javax.portlet.ActionResponse;
043    import javax.portlet.PortletConfig;
044    import javax.portlet.PortletURL;
045    import javax.portlet.RenderRequest;
046    import javax.portlet.RenderResponse;
047    
048    import org.apache.struts.action.ActionForm;
049    import org.apache.struts.action.ActionForward;
050    import org.apache.struts.action.ActionMapping;
051    
052    /**
053     * @author Brian Wing Shun Chan
054     * @author Ryan Park
055     * @author M??t?? Thurz??
056     * @author Josef Sustacek
057     */
058    public class EditLayoutSetPrototypeAction extends PortletAction {
059    
060            @Override
061            public void processAction(
062                            ActionMapping actionMapping, ActionForm actionForm,
063                            PortletConfig portletConfig, ActionRequest actionRequest,
064                            ActionResponse actionResponse)
065                    throws Exception {
066    
067                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
068    
069                    String redirect = ParamUtil.getString(actionRequest, "redirect");
070    
071                    try {
072                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
073                                    LayoutSetPrototype layoutSetPrototype =
074                                            updateLayoutSetPrototype(actionRequest);
075    
076                                    ThemeDisplay themeDisplay =
077                                            (ThemeDisplay)actionRequest.getAttribute(
078                                                    WebKeys.THEME_DISPLAY);
079    
080                                    ThemeDisplay siteThemeDisplay =
081                                            (ThemeDisplay)themeDisplay.clone();
082    
083                                    siteThemeDisplay.setScopeGroupId(
084                                            layoutSetPrototype.getGroupId());
085    
086                                    PortletURL siteAdministrationURL =
087                                            PortalUtil.getSiteAdministrationURL(
088                                                    actionResponse, siteThemeDisplay,
089                                                    PortletKeys.SITE_TEMPLATE_SETTINGS);
090    
091                                    redirect = siteAdministrationURL.toString();
092                            }
093                            else if (cmd.equals(Constants.DELETE)) {
094                                    deleteLayoutSetPrototypes(actionRequest);
095                            }
096                            else if (cmd.equals("reset_merge_fail_count")) {
097                                    resetMergeFailCount(actionRequest);
098                            }
099    
100                            sendRedirect(actionRequest, actionResponse, redirect);
101                    }
102                    catch (Exception e) {
103                            if (e instanceof PrincipalException) {
104                                    SessionErrors.add(actionRequest, e.getClass());
105    
106                                    setForward(
107                                            actionRequest, "portlet.layout_set_prototypes.error");
108                            }
109                            else if (e instanceof RequiredLayoutSetPrototypeException) {
110                                    SessionErrors.add(actionRequest, e.getClass());
111    
112                                    redirect = PortalUtil.escapeRedirect(redirect);
113    
114                                    if (Validator.isNotNull(redirect)) {
115                                            actionResponse.sendRedirect(redirect);
116                                    }
117                            }
118                            else {
119                                    throw e;
120                            }
121                    }
122            }
123    
124            @Override
125            public ActionForward render(
126                            ActionMapping actionMapping, ActionForm actionForm,
127                            PortletConfig portletConfig, RenderRequest renderRequest,
128                            RenderResponse renderResponse)
129                    throws Exception {
130    
131                    try {
132                            ActionUtil.getLayoutSetPrototype(renderRequest);
133                    }
134                    catch (Exception e) {
135                            if (e instanceof NoSuchLayoutSetPrototypeException ||
136                                    e instanceof PrincipalException) {
137    
138                                    SessionErrors.add(renderRequest, e.getClass());
139    
140                                    return actionMapping.findForward(
141                                            "portlet.layout_set_prototypes.error");
142                            }
143                            else {
144                                    throw e;
145                            }
146                    }
147    
148                    return actionMapping.findForward(
149                            getForward(
150                                    renderRequest,
151                                    "portlet.layout_set_prototypes.edit_layout_set_prototype"));
152            }
153    
154            protected void deleteLayoutSetPrototypes(ActionRequest actionRequest)
155                    throws Exception {
156    
157                    long[] layoutSetPrototypeIds = StringUtil.split(
158                            ParamUtil.getString(actionRequest, "layoutSetPrototypeIds"), 0L);
159    
160                    for (long layoutSetPrototypeId : layoutSetPrototypeIds) {
161                            LayoutSetPrototypeServiceUtil.deleteLayoutSetPrototype(
162                                    layoutSetPrototypeId);
163                    }
164            }
165    
166            @Override
167            protected boolean isCheckMethodOnProcessAction() {
168                    return _CHECK_METHOD_ON_PROCESS_ACTION;
169            }
170    
171            protected void resetMergeFailCount(ActionRequest actionRequest)
172                    throws Exception {
173    
174                    long layoutSetPrototypeId = ParamUtil.getLong(
175                            actionRequest, "layoutSetPrototypeId");
176    
177                    LayoutSetPrototype layoutSetPrototype =
178                            LayoutSetPrototypeServiceUtil.getLayoutSetPrototype(
179                                    layoutSetPrototypeId);
180    
181                    SitesUtil.setMergeFailCount(layoutSetPrototype, 0);
182            }
183    
184            protected LayoutSetPrototype updateLayoutSetPrototype(
185                            ActionRequest actionRequest)
186                    throws Exception {
187    
188                    long layoutSetPrototypeId = ParamUtil.getLong(
189                            actionRequest, "layoutSetPrototypeId");
190    
191                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
192                            actionRequest, "name");
193                    String description = ParamUtil.getString(actionRequest, "description");
194                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
195                    boolean layoutsUpdateable = ParamUtil.getBoolean(
196                            actionRequest, "layoutsUpdateable");
197    
198                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
199                            actionRequest);
200    
201                    LayoutSetPrototype layoutSetPrototype = null;
202    
203                    if (layoutSetPrototypeId <= 0) {
204    
205                            // Add layout prototoype
206    
207                            layoutSetPrototype =
208                                    LayoutSetPrototypeServiceUtil.addLayoutSetPrototype(
209                                            nameMap, description, active, layoutsUpdateable,
210                                            serviceContext);
211                    }
212                    else {
213    
214                            // Update layout prototoype
215    
216                            layoutSetPrototype =
217                                    LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
218                                            layoutSetPrototypeId, nameMap, description, active,
219                                            layoutsUpdateable, serviceContext);
220                    }
221    
222                    // Custom JSPs
223    
224                    String customJspServletContextName = ParamUtil.getString(
225                            actionRequest, "customJspServletContextName");
226    
227                    UnicodeProperties settingsProperties =
228                            layoutSetPrototype.getSettingsProperties();
229    
230                    settingsProperties.setProperty(
231                            "customJspServletContextName", customJspServletContextName);
232    
233                    return LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
234                            layoutSetPrototype.getLayoutSetPrototypeId(),
235                            settingsProperties.toString());
236            }
237    
238            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
239    
240    }