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.mobiledevicerules.action;
016    
017    import com.liferay.portal.kernel.bean.BeanParamUtil;
018    import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
019    import com.liferay.portal.kernel.mobile.device.rulegroup.ActionHandlerManagerUtil;
020    import com.liferay.portal.kernel.mobile.device.rulegroup.action.ActionHandler;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.LocalizationUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.UnicodeProperties;
027    import com.liferay.portal.mobile.device.rulegroup.action.impl.LayoutTemplateModificationActionHandler;
028    import com.liferay.portal.mobile.device.rulegroup.action.impl.SimpleRedirectActionHandler;
029    import com.liferay.portal.mobile.device.rulegroup.action.impl.SiteRedirectActionHandler;
030    import com.liferay.portal.mobile.device.rulegroup.action.impl.ThemeModificationActionHandler;
031    import com.liferay.portal.security.auth.PrincipalException;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portal.service.ServiceContextFactory;
034    import com.liferay.portal.util.WebKeys;
035    import com.liferay.portlet.mobiledevicerules.NoSuchActionException;
036    import com.liferay.portlet.mobiledevicerules.NoSuchRuleGroupException;
037    import com.liferay.portlet.mobiledevicerules.model.MDRAction;
038    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroupInstance;
039    import com.liferay.portlet.mobiledevicerules.service.MDRActionServiceUtil;
040    import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupInstanceLocalServiceUtil;
041    
042    import java.util.HashMap;
043    import java.util.Locale;
044    import java.util.Map;
045    
046    import javax.portlet.ActionRequest;
047    import javax.portlet.ActionResponse;
048    import javax.portlet.PortletConfig;
049    import javax.portlet.RenderRequest;
050    import javax.portlet.RenderResponse;
051    import javax.portlet.ResourceRequest;
052    import javax.portlet.ResourceResponse;
053    
054    import org.apache.struts.action.ActionForm;
055    import org.apache.struts.action.ActionForward;
056    import org.apache.struts.action.ActionMapping;
057    
058    /**
059     * @author Edward Han
060     */
061    public class EditActionAction extends EditRuleAction {
062    
063            public EditActionAction() {
064                    registerEditorJSP(
065                            LayoutTemplateModificationActionHandler.class, "layout_tpl");
066                    registerEditorJSP(SimpleRedirectActionHandler.class, "simple_url");
067                    registerEditorJSP(SiteRedirectActionHandler.class, "site_url");
068                    registerEditorJSP(ThemeModificationActionHandler.class, "theme");
069            }
070    
071            @Override
072            public void processAction(
073                            ActionMapping actionMapping, ActionForm actionForm,
074                            PortletConfig portletConfig, ActionRequest actionRequest,
075                            ActionResponse actionResponse)
076                    throws Exception {
077    
078                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
079    
080                    try {
081                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
082                                    updateAction(actionRequest);
083                            }
084                            else if (cmd.equals(Constants.DELETE)) {
085                                    deleteAction(actionRequest);
086                            }
087    
088                            sendRedirect(actionRequest, actionResponse);
089                    }
090                    catch (Exception e) {
091                            if (e instanceof PrincipalException) {
092                                    SessionErrors.add(actionRequest, e.getClass());
093    
094                                    setForward(actionRequest, "portlet.mobile_device_rules.error");
095                            }
096                            else if (e instanceof NoSuchActionException ||
097                                             e instanceof NoSuchRuleGroupException) {
098    
099                                    SessionErrors.add(actionRequest, e.getClass());
100                            }
101                            else {
102                                    throw e;
103                            }
104                    }
105            }
106    
107            @Override
108            public ActionForward render(
109                            ActionMapping actionMapping, ActionForm actionForm,
110                            PortletConfig portletConfig, RenderRequest renderRequest,
111                            RenderResponse renderResponse)
112                    throws Exception {
113    
114                    long actionId = ParamUtil.getLong(renderRequest, "actionId");
115    
116                    MDRAction action = MDRActionServiceUtil.fetchAction(actionId);
117    
118                    renderRequest.setAttribute(
119                            WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP_ACTION, action);
120    
121                    String type = BeanPropertiesUtil.getString(action, "type");
122    
123                    renderRequest.setAttribute(
124                            WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP_ACTION_TYPE, type);
125    
126                    String editorJSP = getEditorJSP(type);
127    
128                    renderRequest.setAttribute(
129                            WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP_ACTION_EDITOR_JSP,
130                            editorJSP);
131    
132                    long ruleGroupInstanceId = BeanParamUtil.getLong(
133                            action, renderRequest, "ruleGroupInstanceId");
134    
135                    MDRRuleGroupInstance ruleGroupInstance =
136                            MDRRuleGroupInstanceLocalServiceUtil.getMDRRuleGroupInstance(
137                                    ruleGroupInstanceId);
138    
139                    renderRequest.setAttribute(
140                            WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP_INSTANCE, ruleGroupInstance);
141    
142                    return actionMapping.findForward(
143                            "portlet.mobile_device_rules.edit_action");
144            }
145    
146            @Override
147            public void serveResource(
148                            ActionMapping actionMapping, ActionForm actionForm,
149                            PortletConfig portletConfig, ResourceRequest resourceRequest,
150                            ResourceResponse resourceResponse)
151                    throws Exception {
152    
153                    long actionId = ParamUtil.getLong(resourceRequest, "actionId");
154    
155                    MDRAction action = MDRActionServiceUtil.fetchAction(actionId);
156    
157                    resourceRequest.setAttribute(
158                            WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP_ACTION, action);
159    
160                    String type = ParamUtil.getString(resourceRequest, "type");
161    
162                    includeEditorJSP(
163                            portletConfig, resourceRequest, resourceResponse, type);
164            }
165    
166            protected void deleteAction(ActionRequest actionRequest) throws Exception {
167                    long actionId = ParamUtil.getLong(actionRequest, "actionId");
168    
169                    MDRActionServiceUtil.deleteAction(actionId);
170            }
171    
172            @Override
173            protected String getEditorJSP(String type) {
174                    ActionHandler actionHandler = ActionHandlerManagerUtil.getActionHandler(
175                            type);
176    
177                    String editorJSP = null;
178    
179                    if (actionHandler != null) {
180                            editorJSP = _editorJSPs.get(actionHandler.getClass());
181                    }
182    
183                    if (editorJSP == null) {
184                            editorJSP = StringPool.BLANK;
185                    }
186    
187                    return editorJSP;
188            }
189    
190            protected void registerEditorJSP(Class<?> clazz, String jspPrefix) {
191                    _editorJSPs.put(
192                            clazz,
193                            "/html/portlet/mobile_device_rules/action/" + jspPrefix + ".jsp");
194            }
195    
196            protected void updateAction(ActionRequest actionRequest) throws Exception {
197                    long actionId = ParamUtil.getLong(actionRequest, "actionId");
198    
199                    long ruleGroupInstanceId = ParamUtil.getLong(
200                            actionRequest, "ruleGroupInstanceId");
201                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
202                            actionRequest, "name");
203                    Map<Locale, String> descriptionMap =
204                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
205                    String type = ParamUtil.getString(actionRequest, "type");
206    
207                    ActionHandler actionHandler = ActionHandlerManagerUtil.getActionHandler(
208                            type);
209    
210                    if (actionHandler == null) {
211                            SessionErrors.add(actionRequest, "typeInvalid");
212    
213                            return;
214                    }
215    
216                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties(
217                            actionRequest, actionHandler.getPropertyNames());
218    
219                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
220                            actionRequest);
221    
222                    if (actionId <= 0) {
223                            MDRActionServiceUtil.addAction(
224                                    ruleGroupInstanceId, nameMap, descriptionMap, type,
225                                    typeSettingsProperties, serviceContext);
226                    }
227                    else {
228                            MDRActionServiceUtil.updateAction(
229                                    actionId, nameMap, descriptionMap, type, typeSettingsProperties,
230                                    serviceContext);
231                    }
232            }
233    
234            private Map<Class<?>, String> _editorJSPs = new HashMap<Class<?>, String>();
235    
236    }