001    /**
002     * Copyright (c) 2000-2012 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 mapping, ActionForm form, PortletConfig portletConfig,
074                            ActionRequest actionRequest, ActionResponse actionResponse)
075                    throws Exception {
076    
077                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
078    
079                    try {
080                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
081                                    updateAction(actionRequest);
082                            }
083                            else if (cmd.equals(Constants.DELETE)) {
084                                    deleteAction(actionRequest);
085                            }
086    
087                            sendRedirect(actionRequest, actionResponse);
088                    }
089                    catch (Exception e) {
090                            if (e instanceof PrincipalException) {
091                                    SessionErrors.add(actionRequest, e.getClass().getName());
092    
093                                    setForward(actionRequest, "portlet.mobile_device_rules.error");
094                            }
095                            else if (e instanceof NoSuchActionException ||
096                                             e instanceof NoSuchRuleGroupException) {
097    
098                                    SessionErrors.add(actionRequest, e.getClass().getName());
099                            }
100                            else {
101                                    throw e;
102                            }
103                    }
104            }
105    
106            @Override
107            public ActionForward render(
108                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
109                            RenderRequest renderRequest, RenderResponse renderResponse)
110                    throws Exception {
111    
112                    long actionId = ParamUtil.getLong(renderRequest, "actionId");
113    
114                    MDRAction action = MDRActionServiceUtil.fetchAction(actionId);
115    
116                    renderRequest.setAttribute(
117                            WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP_ACTION, action);
118    
119                    String type = BeanPropertiesUtil.getString(action, "type");
120    
121                    renderRequest.setAttribute(
122                            WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP_ACTION_TYPE, type);
123    
124                    String editorJSP = getEditorJSP(type);
125    
126                    renderRequest.setAttribute(
127                            WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP_ACTION_EDITOR_JSP,
128                            editorJSP);
129    
130                    long ruleGroupInstanceId = BeanParamUtil.getLong(
131                            action, renderRequest, "ruleGroupInstanceId");
132    
133                    MDRRuleGroupInstance ruleGroupInstance =
134                            MDRRuleGroupInstanceLocalServiceUtil.getMDRRuleGroupInstance(
135                                    ruleGroupInstanceId);
136    
137                    renderRequest.setAttribute(
138                            WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP_INSTANCE, ruleGroupInstance);
139    
140                    return mapping.findForward("portlet.mobile_device_rules.edit_action");
141            }
142    
143            @Override
144            public void serveResource(
145                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
146                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
147                    throws Exception {
148    
149                    long actionId = ParamUtil.getLong(resourceRequest, "actionId");
150    
151                    MDRAction action = MDRActionServiceUtil.fetchAction(actionId);
152    
153                    resourceRequest.setAttribute(
154                            WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP_ACTION, action);
155    
156                    String type = ParamUtil.getString(resourceRequest, "type");
157    
158                    includeEditorJSP(
159                            portletConfig, resourceRequest, resourceResponse, type);
160            }
161    
162            protected void deleteAction(ActionRequest actionRequest) throws Exception {
163                    long actionId = ParamUtil.getLong(actionRequest, "actionId");
164    
165                    MDRActionServiceUtil.deleteAction(actionId);
166            }
167    
168            @Override
169            protected String getEditorJSP(String type) {
170                    ActionHandler actionHandler = ActionHandlerManagerUtil.getActionHandler(
171                            type);
172    
173                    String editorJSP = null;
174    
175                    if (actionHandler != null) {
176                            editorJSP = _editorJSPs.get(actionHandler.getClass());
177                    }
178    
179                    if (editorJSP == null) {
180                            editorJSP = StringPool.BLANK;
181                    }
182    
183                    return editorJSP;
184            }
185    
186            protected void registerEditorJSP(Class<?> clazz, String jspPrefix) {
187                    _editorJSPs.put(
188                            clazz,
189                            "/html/portlet/mobile_device_rules/action/" + jspPrefix + ".jsp");
190            }
191    
192            protected void updateAction(ActionRequest actionRequest) throws Exception {
193                    long actionId = ParamUtil.getLong(actionRequest, "actionId");
194    
195                    long ruleGroupInstanceId = ParamUtil.getLong(
196                            actionRequest, "ruleGroupInstanceId");
197                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
198                            actionRequest, "name");
199                    Map<Locale, String> descriptionMap =
200                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
201                    String type = ParamUtil.getString(actionRequest, "type");
202    
203                    ActionHandler actionHandler = ActionHandlerManagerUtil.getActionHandler(
204                            type);
205    
206                    if (actionHandler == null) {
207                            SessionErrors.add(actionRequest, "typeInvalid");
208    
209                            return;
210                    }
211    
212                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties(
213                            actionRequest, actionHandler.getPropertyNames());
214    
215                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
216                            actionRequest);
217    
218                    if (actionId <= 0) {
219                            MDRActionServiceUtil.addAction(
220                                    ruleGroupInstanceId, nameMap, descriptionMap, type,
221                                    typeSettingsProperties, serviceContext);
222                    }
223                    else {
224                            MDRActionServiceUtil.updateAction(
225                                    actionId, nameMap, descriptionMap, type, typeSettingsProperties,
226                                    serviceContext);
227                    }
228            }
229    
230            private Map<Class<?>, String> _editorJSPs = new HashMap<Class<?>, String>();
231    
232    }