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 actionMapping, ActionForm actionForm,
053 PortletConfig portletConfig, ActionRequest actionRequest,
054 ActionResponse actionResponse)
055 throws Exception {
056
057 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
058
059 try {
060 MDRRuleGroup ruleGroup = null;
061
062 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
063 ruleGroup = updateRuleGroup(actionRequest);
064 }
065 else if (cmd.equals(Constants.DELETE)) {
066 deleteRuleGroup(actionRequest);
067 }
068 else if (cmd.equals(Constants.COPY)) {
069 ruleGroup = copyRuleGroup(actionRequest);
070 }
071
072 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.COPY)) {
073 String redirect = getRedirect(
074 actionRequest, actionResponse, ruleGroup);
075
076 sendRedirect(actionRequest, actionResponse, redirect);
077 }
078 else {
079 sendRedirect(actionRequest, actionResponse);
080 }
081 }
082 catch (Exception e) {
083 if (e instanceof PrincipalException) {
084 SessionErrors.add(actionRequest, e.getClass());
085
086 setForward(actionRequest, "portlet.mobile_device_rules.error");
087 }
088 else if (e instanceof NoSuchRuleGroupException) {
089 SessionErrors.add(actionRequest, e.getClass());
090 }
091 else {
092 throw e;
093 }
094 }
095 }
096
097 @Override
098 public ActionForward render(
099 ActionMapping actionMapping, ActionForm actionForm,
100 PortletConfig portletConfig, RenderRequest renderRequest,
101 RenderResponse renderResponse)
102 throws Exception {
103
104 long ruleGroupId = ParamUtil.getLong(renderRequest, "ruleGroupId");
105
106 MDRRuleGroup ruleGroup = MDRRuleGroupServiceUtil.fetchRuleGroup(
107 ruleGroupId);
108
109 renderRequest.setAttribute(
110 WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP, ruleGroup);
111
112 return actionMapping.findForward(
113 "portlet.mobile_device_rules.edit_rule_group");
114 }
115
116 protected MDRRuleGroup copyRuleGroup(ActionRequest actionRequest)
117 throws Exception {
118
119 long ruleGroupId = ParamUtil.getLong(actionRequest, "ruleGroupId");
120
121 long groupId = ParamUtil.getLong(actionRequest, "groupId");
122
123 ServiceContext serviceContext = ServiceContextFactory.getInstance(
124 actionRequest);
125
126 return MDRRuleGroupServiceUtil.copyRuleGroup(
127 ruleGroupId, groupId, serviceContext);
128 }
129
130 protected void deleteRuleGroup(ActionRequest actionRequest)
131 throws Exception {
132
133 long ruleGroupId = ParamUtil.getLong(actionRequest, "ruleGroupId");
134
135 MDRRuleGroupServiceUtil.deleteRuleGroup(ruleGroupId);
136 }
137
138 protected String getRedirect(
139 ActionRequest actionRequest, ActionResponse actionResponse,
140 MDRRuleGroup ruleGroup) {
141
142 LiferayPortletResponse liferayPortletResponse =
143 (LiferayPortletResponse)actionResponse;
144
145 PortletURL portletURL = liferayPortletResponse.createRenderURL();
146
147 portletURL.setParameter(
148 "struts_action", "/mobile_device_rules/edit_rule_group");
149
150 String redirect = ParamUtil.getString(actionRequest, "redirect");
151
152 portletURL.setParameter("redirect", redirect);
153
154 portletURL.setParameter(
155 "ruleGroupId", String.valueOf(ruleGroup.getRuleGroupId()));
156
157 return portletURL.toString();
158 }
159
160 protected MDRRuleGroup updateRuleGroup(ActionRequest actionRequest)
161 throws Exception {
162
163 long ruleGroupId = ParamUtil.getLong(actionRequest, "ruleGroupId");
164
165 long groupId = ParamUtil.getLong(actionRequest, "groupId");
166 Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
167 actionRequest, "name");
168 Map<Locale, String> descriptionMap =
169 LocalizationUtil.getLocalizationMap(actionRequest, "description");
170
171 ServiceContext serviceContext = ServiceContextFactory.getInstance(
172 actionRequest);
173
174 MDRRuleGroup ruleGroup = null;
175
176 if (ruleGroupId <= 0) {
177 ruleGroup = MDRRuleGroupServiceUtil.addRuleGroup(
178 groupId, nameMap, descriptionMap, serviceContext);
179 }
180 else {
181 ruleGroup = MDRRuleGroupServiceUtil.updateRuleGroup(
182 ruleGroupId, nameMap, descriptionMap, serviceContext);
183 }
184
185 return ruleGroup;
186 }
187
188 }