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