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.kernel.util.StringUtil;
023 import com.liferay.portal.security.auth.PrincipalException;
024 import com.liferay.portal.service.ServiceContext;
025 import com.liferay.portal.service.ServiceContextFactory;
026 import com.liferay.portal.struts.PortletAction;
027 import com.liferay.portal.util.WebKeys;
028 import com.liferay.portlet.mobiledevicerules.NoSuchRuleGroupException;
029 import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
030 import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupServiceUtil;
031
032 import java.util.Locale;
033 import java.util.Map;
034
035 import javax.portlet.ActionRequest;
036 import javax.portlet.ActionResponse;
037 import javax.portlet.PortletConfig;
038 import javax.portlet.PortletURL;
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 EditRuleGroupAction 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 MDRRuleGroup ruleGroup = null;
062
063 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
064 ruleGroup = updateRuleGroup(actionRequest);
065 }
066 else if (cmd.equals(Constants.DELETE)) {
067 deleteRuleGroups(actionRequest);
068 }
069 else if (cmd.equals(Constants.COPY)) {
070 ruleGroup = copyRuleGroup(actionRequest);
071 }
072
073 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.COPY)) {
074 String redirect = getRedirect(
075 actionRequest, actionResponse, ruleGroup);
076
077 sendRedirect(actionRequest, actionResponse, redirect);
078 }
079 else {
080 sendRedirect(actionRequest, actionResponse);
081 }
082 }
083 catch (Exception e) {
084 if (e instanceof PrincipalException) {
085 SessionErrors.add(actionRequest, e.getClass());
086
087 setForward(actionRequest, "portlet.mobile_device_rules.error");
088 }
089 else if (e instanceof NoSuchRuleGroupException) {
090 SessionErrors.add(actionRequest, e.getClass());
091 }
092 else {
093 throw e;
094 }
095 }
096 }
097
098 @Override
099 public ActionForward render(
100 ActionMapping actionMapping, ActionForm actionForm,
101 PortletConfig portletConfig, RenderRequest renderRequest,
102 RenderResponse renderResponse)
103 throws Exception {
104
105 long ruleGroupId = ParamUtil.getLong(renderRequest, "ruleGroupId");
106
107 MDRRuleGroup ruleGroup = MDRRuleGroupServiceUtil.fetchRuleGroup(
108 ruleGroupId);
109
110 renderRequest.setAttribute(
111 WebKeys.MOBILE_DEVICE_RULES_RULE_GROUP, ruleGroup);
112
113 return actionMapping.findForward(
114 "portlet.mobile_device_rules.edit_rule_group");
115 }
116
117 protected MDRRuleGroup copyRuleGroup(ActionRequest actionRequest)
118 throws Exception {
119
120 long ruleGroupId = ParamUtil.getLong(actionRequest, "ruleGroupId");
121
122 long groupId = ParamUtil.getLong(actionRequest, "groupId");
123
124 ServiceContext serviceContext = ServiceContextFactory.getInstance(
125 actionRequest);
126
127 return MDRRuleGroupServiceUtil.copyRuleGroup(
128 ruleGroupId, groupId, serviceContext);
129 }
130
131 protected void deleteRuleGroups(ActionRequest actionRequest)
132 throws Exception {
133
134 long[] deleteRuleGroupIds = null;
135
136 long ruleGroupId = ParamUtil.getLong(actionRequest, "ruleGroupId");
137
138 if (ruleGroupId > 0) {
139 deleteRuleGroupIds = new long[] {ruleGroupId};
140 }
141 else {
142 deleteRuleGroupIds = StringUtil.split(
143 ParamUtil.getString(actionRequest, "ruleGroupIds"), 0L);
144 }
145
146 for (long deleteRuleGroupId : deleteRuleGroupIds) {
147 MDRRuleGroupServiceUtil.deleteRuleGroup(deleteRuleGroupId);
148 }
149 }
150
151 protected String getRedirect(
152 ActionRequest actionRequest, ActionResponse actionResponse,
153 MDRRuleGroup ruleGroup) {
154
155 LiferayPortletResponse liferayPortletResponse =
156 (LiferayPortletResponse)actionResponse;
157
158 PortletURL portletURL = liferayPortletResponse.createRenderURL();
159
160 portletURL.setParameter(
161 "struts_action", "/mobile_device_rules/edit_rule_group");
162
163 String redirect = ParamUtil.getString(actionRequest, "redirect");
164
165 portletURL.setParameter("redirect", redirect);
166
167 portletURL.setParameter(
168 "ruleGroupId", String.valueOf(ruleGroup.getRuleGroupId()));
169
170 return portletURL.toString();
171 }
172
173 protected MDRRuleGroup updateRuleGroup(ActionRequest actionRequest)
174 throws Exception {
175
176 long ruleGroupId = ParamUtil.getLong(actionRequest, "ruleGroupId");
177
178 long groupId = ParamUtil.getLong(actionRequest, "groupId");
179 Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
180 actionRequest, "name");
181 Map<Locale, String> descriptionMap =
182 LocalizationUtil.getLocalizationMap(actionRequest, "description");
183
184 ServiceContext serviceContext = ServiceContextFactory.getInstance(
185 actionRequest);
186
187 MDRRuleGroup ruleGroup = null;
188
189 if (ruleGroupId <= 0) {
190 ruleGroup = MDRRuleGroupServiceUtil.addRuleGroup(
191 groupId, nameMap, descriptionMap, serviceContext);
192 }
193 else {
194 ruleGroup = MDRRuleGroupServiceUtil.updateRuleGroup(
195 ruleGroupId, nameMap, descriptionMap, serviceContext);
196 }
197
198 return ruleGroup;
199 }
200
201 }