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.RuleGroupProcessorUtil;
020    import com.liferay.portal.kernel.mobile.device.rulegroup.rule.RuleHandler;
021    import com.liferay.portal.kernel.mobile.device.rulegroup.rule.UnknownRuleHandlerException;
022    import com.liferay.portal.kernel.servlet.SessionErrors;
023    import com.liferay.portal.kernel.util.Constants;
024    import com.liferay.portal.kernel.util.LocalizationUtil;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.UnicodeProperties;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.mobile.device.rulegroup.rule.impl.SimpleRuleHandler;
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.struts.PortletAction;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.portlet.mobiledevicerules.NoSuchActionException;
037    import com.liferay.portlet.mobiledevicerules.NoSuchRuleGroupException;
038    import com.liferay.portlet.mobiledevicerules.model.MDRRule;
039    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
040    import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupServiceUtil;
041    import com.liferay.portlet.mobiledevicerules.service.MDRRuleServiceUtil;
042    
043    import java.util.Collection;
044    import java.util.Locale;
045    import java.util.Map;
046    
047    import javax.portlet.ActionRequest;
048    import javax.portlet.ActionResponse;
049    import javax.portlet.PortletConfig;
050    import javax.portlet.PortletContext;
051    import javax.portlet.PortletRequestDispatcher;
052    import javax.portlet.RenderRequest;
053    import javax.portlet.RenderResponse;
054    import javax.portlet.ResourceRequest;
055    import javax.portlet.ResourceResponse;
056    
057    import org.apache.struts.action.ActionForm;
058    import org.apache.struts.action.ActionForward;
059    import org.apache.struts.action.ActionMapping;
060    
061    /**
062     * @author Edward Han
063     */
064    public class EditRuleAction extends PortletAction {
065    
066            @Override
067            public void processAction(
068                            ActionMapping actionMapping, ActionForm actionForm,
069                            PortletConfig portletConfig, ActionRequest actionRequest,
070                            ActionResponse actionResponse)
071                    throws Exception {
072    
073                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
074    
075                    try {
076                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
077                                    updateRule(actionRequest);
078                            }
079                            else if (cmd.equals(Constants.DELETE)) {
080                                    deleteRule(actionRequest);
081                            }
082    
083                            sendRedirect(actionRequest, actionResponse);
084                    }
085                    catch (Exception e) {
086                            if (e instanceof PrincipalException) {
087                                    SessionErrors.add(actionRequest, e.getClass());
088    
089                                    setForward(actionRequest, "portlet.mobile_device_rules.error");
090                            }
091                            else if (e instanceof NoSuchActionException ||
092                                             e instanceof NoSuchRuleGroupException ||
093                                             e instanceof UnknownRuleHandlerException) {
094    
095                                    SessionErrors.add(actionRequest, e.getClass());
096                            }
097                            else {
098                                    throw e;
099                            }
100                    }
101            }
102    
103            @Override
104            public ActionForward render(
105                            ActionMapping actionMapping, ActionForm actionForm,
106                            PortletConfig portletConfig, RenderRequest renderRequest,
107                            RenderResponse renderResponse)
108                    throws Exception {
109    
110                    long ruleId = ParamUtil.getLong(renderRequest, "ruleId");
111    
112                    MDRRule rule = MDRRuleServiceUtil.fetchRule(ruleId);
113    
114                    renderRequest.setAttribute(WebKeys.MOBILE_DEVICE_RULES_RULE, rule);
115    
116                    String type = BeanPropertiesUtil.getString(rule, "type");
117    
118                    renderRequest.setAttribute(WebKeys.MOBILE_DEVICE_RULES_RULE_TYPE, type);
119    
120                    String editorJSP = getEditorJSP(type);
121    
122                    renderRequest.setAttribute(
123                            WebKeys.MOBILE_DEVICE_RULES_RULE_EDITOR_JSP, editorJSP);
124    
125                    long ruleGroupId = BeanParamUtil.getLong(
126                            rule, renderRequest, "ruleGroupId");
127    
128                    MDRRuleGroup ruleGroup = MDRRuleGroupServiceUtil.getRuleGroup(
129                            ruleGroupId);
130    
131                    renderRequest.setAttribute(
132                            WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP, ruleGroup);
133    
134                    return actionMapping.findForward(
135                            "portlet.mobile_device_rules.edit_rule");
136            }
137    
138            @Override
139            public void serveResource(
140                            ActionMapping actionMapping, ActionForm actionForm,
141                            PortletConfig portletConfig, ResourceRequest resourceRequest,
142                            ResourceResponse resourceResponse)
143                    throws Exception {
144    
145                    long ruleId = ParamUtil.getLong(resourceRequest, "ruleId");
146    
147                    if (ruleId > 0) {
148                            MDRRule rule = MDRRuleServiceUtil.fetchRule(ruleId);
149    
150                            resourceRequest.setAttribute(
151                                    WebKeys.MOBILE_DEVICE_RULES_RULE, rule);
152                    }
153    
154                    String type = ParamUtil.getString(resourceRequest, "type");
155    
156                    includeEditorJSP(
157                            portletConfig, resourceRequest, resourceResponse, type);
158            }
159    
160            protected void deleteRule(ActionRequest request) throws Exception {
161                    long ruleId = ParamUtil.getLong(request, "ruleId");
162    
163                    MDRRuleServiceUtil.deleteRule(ruleId);
164            }
165    
166            protected String getEditorJSP(String ruleType) {
167                    if (ruleType.equals(SimpleRuleHandler.getHandlerType())) {
168                            return _SIMPLE_RULE_EDIT_RJSP;
169                    }
170    
171                    return StringPool.BLANK;
172            }
173    
174            protected UnicodeProperties getTypeSettingsProperties(
175                    ActionRequest actionRequest, Collection<String> propertyNames) {
176    
177                    UnicodeProperties typeSettingsProperties = new UnicodeProperties();
178    
179                    for (String propertyName : propertyNames) {
180                            String[] values = ParamUtil.getParameterValues(
181                                    actionRequest, propertyName);
182    
183                            String merged = StringUtil.merge(values);
184    
185                            typeSettingsProperties.setProperty(propertyName, merged);
186                    }
187    
188                    return typeSettingsProperties;
189            }
190    
191            protected void includeEditorJSP(
192                            PortletConfig portletConfig, ResourceRequest resourceRequest,
193                            ResourceResponse resourceResponse, String type)
194                    throws Exception {
195    
196                    String editorJSP = getEditorJSP(type);
197    
198                    if (Validator.isNull(editorJSP)) {
199                            return;
200                    }
201    
202                    PortletContext portletContext = portletConfig.getPortletContext();
203    
204                    PortletRequestDispatcher portletRequestDispatcher =
205                            portletContext.getRequestDispatcher(editorJSP);
206    
207                    portletRequestDispatcher.include(resourceRequest, resourceResponse);
208            }
209    
210            protected void updateRule(ActionRequest actionRequest) throws Exception {
211                    long ruleId = ParamUtil.getLong(actionRequest, "ruleId");
212    
213                    long ruleGroupId = ParamUtil.getLong(actionRequest, "ruleGroupId");
214                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
215                            actionRequest, "name");
216                    Map<Locale, String> descriptionMap =
217                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
218                    String type = ParamUtil.getString(actionRequest, "type");
219    
220                    RuleHandler ruleHandler = RuleGroupProcessorUtil.getRuleHandler(type);
221    
222                    if (ruleHandler == null) {
223                            throw new UnknownRuleHandlerException(type);
224                    }
225    
226                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties(
227                            actionRequest, ruleHandler.getPropertyNames());
228    
229                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
230                            actionRequest);
231    
232                    if (ruleId <= 0) {
233                            MDRRuleServiceUtil.addRule(
234                                    ruleGroupId, nameMap, descriptionMap, type,
235                                    typeSettingsProperties, serviceContext);
236                    }
237                    else {
238                            MDRRuleServiceUtil.updateRule(
239                                    ruleId, nameMap, descriptionMap, type, typeSettingsProperties,
240                                    serviceContext);
241                    }
242            }
243    
244            private static final String _SIMPLE_RULE_EDIT_RJSP =
245                    "/html/portlet/mobile_device_rules/rule/simple_rule.jsp";
246    
247    }