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.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 +
190                                    "_action.jsp");
191            }
192    
193            protected void updateAction(ActionRequest actionRequest) throws Exception {
194                    long actionId = ParamUtil.getLong(actionRequest, "actionId");
195    
196                    long ruleGroupInstanceId = ParamUtil.getLong(
197                            actionRequest, "ruleGroupInstanceId");
198                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
199                            actionRequest, "name");
200                    Map<Locale, String> descriptionMap =
201                            LocalizationUtil.getLocalizationMap(
202                                    actionRequest, "description");
203                    String type = ParamUtil.getString(actionRequest, "type");
204    
205                    ActionHandler actionHandler = ActionHandlerManagerUtil.getActionHandler(
206                            type);
207    
208                    if (actionHandler == null) {
209                            SessionErrors.add(actionRequest, "typeInvalid");
210    
211                            return;
212                    }
213    
214                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties(
215                            actionRequest, actionHandler.getPropertyNames());
216    
217                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
218                            actionRequest);
219    
220                    if (actionId <= 0) {
221                            MDRActionServiceUtil.addAction(
222                                    ruleGroupInstanceId, nameMap, descriptionMap, type,
223                                    typeSettingsProperties, serviceContext);
224                    }
225                    else {
226                            MDRActionServiceUtil.updateAction(
227                                    actionId, nameMap, descriptionMap, type, typeSettingsProperties,
228                                    serviceContext);
229                    }
230            }
231    
232            private Map<Class<?>, String> _editorJSPs = new HashMap<Class<?>, String>();
233    
234    }