001
014
015 package com.liferay.portlet.mobiledevicerules.action;
016
017 import com.liferay.portal.kernel.bean.BeanParamUtil;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.json.JSONArray;
021 import com.liferay.portal.kernel.json.JSONFactoryUtil;
022 import com.liferay.portal.kernel.json.JSONObject;
023 import com.liferay.portal.kernel.servlet.SessionErrors;
024 import com.liferay.portal.kernel.util.Constants;
025 import com.liferay.portal.kernel.util.ParamUtil;
026 import com.liferay.portal.security.auth.PrincipalException;
027 import com.liferay.portal.struts.PortletAction;
028 import com.liferay.portal.util.WebKeys;
029 import com.liferay.portlet.mobiledevicerules.NoSuchRuleGroupException;
030 import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
031 import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroupInstance;
032 import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupInstanceLocalServiceUtil;
033 import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupInstanceServiceUtil;
034 import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupLocalServiceUtil;
035
036 import javax.portlet.ActionRequest;
037 import javax.portlet.ActionResponse;
038 import javax.portlet.PortletConfig;
039 import javax.portlet.RenderRequest;
040 import javax.portlet.RenderResponse;
041
042 import org.apache.struts.action.ActionForm;
043 import org.apache.struts.action.ActionForward;
044 import org.apache.struts.action.ActionMapping;
045
046
049 public class EditRuleGroupInstanceAction extends PortletAction {
050
051 @Override
052 public void processAction(
053 ActionMapping actionMapping, ActionForm actionForm,
054 PortletConfig portletConfig, ActionRequest actionRequest,
055 ActionResponse actionResponse)
056 throws Exception {
057
058 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
059
060 try {
061 if (cmd.equals(Constants.DELETE)) {
062 deleteRuleGroupInstance(actionRequest);
063 }
064 else if (cmd.equals(Constants.UPDATE)) {
065 updateRuleGroupInstancesPriorities(actionRequest);
066 }
067
068 sendRedirect(actionRequest, actionResponse);
069 }
070 catch (Exception e) {
071 if (e instanceof PrincipalException) {
072 SessionErrors.add(actionRequest, e.getClass());
073
074 setForward(actionRequest, "portlet.mobile_device_rules.error");
075 }
076 else if (e instanceof NoSuchRuleGroupException) {
077 SessionErrors.add(actionRequest, e.getClass());
078 }
079 else {
080 throw e;
081 }
082 }
083 }
084
085 @Override
086 public ActionForward render(
087 ActionMapping actionMapping, ActionForm actionForm,
088 PortletConfig portletConfig, RenderRequest renderRequest,
089 RenderResponse renderResponse)
090 throws Exception {
091
092 long ruleGroupInstanceId = ParamUtil.getLong(
093 renderRequest, "ruleGroupInstanceId");
094
095 MDRRuleGroupInstance ruleGroupInstance =
096 MDRRuleGroupInstanceLocalServiceUtil.fetchRuleGroupInstance(
097 ruleGroupInstanceId);
098
099 renderRequest.setAttribute(
100 WebKeys.MOBILE_DEVICE_RULES_RULE_INSTANCE, ruleGroupInstance);
101
102 long ruleGroupId = BeanParamUtil.getLong(
103 ruleGroupInstance, renderRequest, "ruleGroupId");
104
105 MDRRuleGroup ruleGroup = MDRRuleGroupLocalServiceUtil.fetchRuleGroup(
106 ruleGroupId);
107
108 renderRequest.setAttribute(
109 WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP, ruleGroup);
110
111 return actionMapping.findForward(
112 "portlet.mobile_device_rules.edit_rule_group_instance_priorities");
113 }
114
115 protected void deleteRuleGroupInstance(ActionRequest actionRequest)
116 throws PortalException, SystemException {
117
118 long ruleGroupInstanceId = ParamUtil.getLong(
119 actionRequest, "ruleGroupInstanceId");
120
121 MDRRuleGroupInstanceServiceUtil.deleteRuleGroupInstance(
122 ruleGroupInstanceId);
123 }
124
125 protected void updateRuleGroupInstancesPriorities(
126 ActionRequest actionRequest)
127 throws PortalException, SystemException {
128
129 String ruleGroupsInstancesJSON = ParamUtil.getString(
130 actionRequest, "ruleGroupsInstancesJSON");
131
132 JSONArray ruleGroupsInstancesJSONArray =
133 JSONFactoryUtil.createJSONArray(ruleGroupsInstancesJSON);
134
135 for (int i = 0; i < ruleGroupsInstancesJSONArray.length(); i++) {
136 JSONObject ruleGroupInstanceJSONObject =
137 ruleGroupsInstancesJSONArray.getJSONObject(i);
138
139 long ruleGroupInstanceId = ruleGroupInstanceJSONObject.getLong(
140 "ruleGroupInstanceId");
141
142 int priority = ruleGroupInstanceJSONObject.getInt("priority");
143
144 MDRRuleGroupInstanceServiceUtil.updateRuleGroupInstance(
145 ruleGroupInstanceId, priority);
146 }
147 }
148
149 }