001
014
015 package com.liferay.portlet.mobiledevicerules.action;
016
017 import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
018 import com.liferay.portal.kernel.servlet.SessionErrors;
019 import com.liferay.portal.kernel.util.Constants;
020 import com.liferay.portal.kernel.util.LocalizationUtil;
021 import com.liferay.portal.kernel.util.ParamUtil;
022 import com.liferay.portal.security.auth.PrincipalException;
023 import com.liferay.portal.service.ServiceContext;
024 import com.liferay.portal.service.ServiceContextFactory;
025 import com.liferay.portal.struts.PortletAction;
026 import com.liferay.portal.util.WebKeys;
027 import com.liferay.portlet.mobiledevicerules.NoSuchRuleGroupException;
028 import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
029 import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupServiceUtil;
030
031 import java.util.Locale;
032 import java.util.Map;
033
034 import javax.portlet.ActionRequest;
035 import javax.portlet.ActionResponse;
036 import javax.portlet.PortletConfig;
037 import javax.portlet.PortletURL;
038 import javax.portlet.RenderRequest;
039 import javax.portlet.RenderResponse;
040
041 import org.apache.struts.action.ActionForm;
042 import org.apache.struts.action.ActionForward;
043 import org.apache.struts.action.ActionMapping;
044
045
048 public class EditRuleGroupAction extends PortletAction {
049
050 @Override
051 public void processAction(
052 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
053 ActionRequest actionRequest, ActionResponse actionResponse)
054 throws Exception {
055
056 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
057
058 try {
059 MDRRuleGroup ruleGroup = null;
060
061 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
062 ruleGroup = updateRuleGroup(actionRequest);
063 }
064 else if (cmd.equals(Constants.DELETE)) {
065 deleteRuleGroup(actionRequest);
066 }
067 else if (cmd.equals(Constants.COPY)) {
068 ruleGroup = copyRuleGroup(actionRequest);
069 }
070
071 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.COPY)) {
072 String redirect = getRedirect(
073 actionRequest, actionResponse, ruleGroup);
074
075 sendRedirect(actionRequest, actionResponse, redirect);
076 }
077 else {
078 sendRedirect(actionRequest, actionResponse);
079 }
080 }
081 catch (Exception e) {
082 if (e instanceof PrincipalException) {
083 SessionErrors.add(actionRequest, e.getClass().getName());
084
085 setForward(actionRequest, "portlet.mobile_device_rules.error");
086 }
087 else if (e instanceof NoSuchRuleGroupException) {
088 SessionErrors.add(actionRequest, e.getClass().getName());
089 }
090 else {
091 throw e;
092 }
093 }
094 }
095
096 @Override
097 public ActionForward render(
098 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
099 RenderRequest renderRequest, RenderResponse renderResponse)
100 throws Exception {
101
102 long ruleGroupId = ParamUtil.getLong(renderRequest, "ruleGroupId");
103
104 MDRRuleGroup ruleGroup = MDRRuleGroupServiceUtil.fetchRuleGroup(
105 ruleGroupId);
106
107 renderRequest.setAttribute(
108 WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP, ruleGroup);
109
110 return mapping.findForward(
111 "portlet.mobile_device_rules.edit_rule_group");
112 }
113
114 protected MDRRuleGroup copyRuleGroup(ActionRequest actionRequest)
115 throws Exception {
116
117 long ruleGroupId = ParamUtil.getLong(actionRequest, "ruleGroupId");
118
119 long groupId = ParamUtil.getLong(actionRequest, "groupId");
120
121 ServiceContext serviceContext = ServiceContextFactory.getInstance(
122 actionRequest);
123
124 return MDRRuleGroupServiceUtil.copyRuleGroup(
125 ruleGroupId, groupId, serviceContext);
126 }
127
128 protected void deleteRuleGroup(ActionRequest actionRequest)
129 throws Exception {
130
131 long ruleGroupId = ParamUtil.getLong(actionRequest, "ruleGroupId");
132
133 MDRRuleGroupServiceUtil.deleteRuleGroup(ruleGroupId);
134 }
135
136 protected String getRedirect(
137 ActionRequest actionRequest, ActionResponse actionResponse,
138 MDRRuleGroup ruleGroup) {
139
140 LiferayPortletResponse liferayPortletResponse =
141 (LiferayPortletResponse)actionResponse;
142
143 PortletURL portletURL = liferayPortletResponse.createRenderURL();
144
145 portletURL.setParameter(
146 "struts_action", "/mobile_device_rules/edit_rule_group");
147
148 String redirect = ParamUtil.getString(actionRequest, "redirect");
149
150 portletURL.setParameter("redirect", redirect);
151
152 portletURL.setParameter(
153 "ruleGroupId", String.valueOf(ruleGroup.getRuleGroupId()));
154
155 return portletURL.toString();
156 }
157
158 protected MDRRuleGroup updateRuleGroup(ActionRequest actionRequest)
159 throws Exception {
160
161 long ruleGroupId = ParamUtil.getLong(actionRequest, "ruleGroupId");
162
163 long groupId = ParamUtil.getLong(actionRequest, "groupId");
164 Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
165 actionRequest, "name");
166 Map<Locale, String> descriptionMap =
167 LocalizationUtil.getLocalizationMap(actionRequest, "description");
168
169 ServiceContext serviceContext = ServiceContextFactory.getInstance(
170 actionRequest);
171
172 MDRRuleGroup ruleGroup = null;
173
174 if (ruleGroupId <= 0) {
175 ruleGroup = MDRRuleGroupServiceUtil.addRuleGroup(
176 groupId, nameMap, descriptionMap, serviceContext);
177 }
178 else {
179 ruleGroup = MDRRuleGroupServiceUtil.updateRuleGroup(
180 ruleGroupId, nameMap, descriptionMap, serviceContext);
181 }
182
183 return ruleGroup;
184 }
185
186 }