1
22
23 package com.liferay.portlet.messageboards.action;
24
25 import com.liferay.portal.captcha.CaptchaTextException;
26 import com.liferay.portal.captcha.CaptchaUtil;
27 import com.liferay.portal.kernel.servlet.SessionErrors;
28 import com.liferay.portal.kernel.util.Constants;
29 import com.liferay.portal.kernel.util.ParamUtil;
30 import com.liferay.portal.model.Layout;
31 import com.liferay.portal.security.auth.PrincipalException;
32 import com.liferay.portal.struts.PortletAction;
33 import com.liferay.portal.util.PropsValues;
34 import com.liferay.portal.util.WebKeys;
35 import com.liferay.portlet.messageboards.CategoryNameException;
36 import com.liferay.portlet.messageboards.NoSuchCategoryException;
37 import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
38
39 import javax.portlet.ActionRequest;
40 import javax.portlet.ActionResponse;
41 import javax.portlet.PortletConfig;
42 import javax.portlet.RenderRequest;
43 import javax.portlet.RenderResponse;
44
45 import org.apache.struts.action.ActionForm;
46 import org.apache.struts.action.ActionForward;
47 import org.apache.struts.action.ActionMapping;
48
49
55 public class EditCategoryAction extends PortletAction {
56
57 public void processAction(
58 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
59 ActionRequest actionRequest, ActionResponse actionResponse)
60 throws Exception {
61
62 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
63
64 try {
65 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
66 updateCategory(actionRequest);
67 }
68 else if (cmd.equals(Constants.DELETE)) {
69 deleteCategory(actionRequest);
70 }
71 else if (cmd.equals(Constants.SUBSCRIBE)) {
72 subscribeCategory(actionRequest);
73 }
74 else if (cmd.equals(Constants.UNSUBSCRIBE)) {
75 unsubscribeCategory(actionRequest);
76 }
77
78 sendRedirect(actionRequest, actionResponse);
79 }
80 catch (Exception e) {
81 if (e instanceof NoSuchCategoryException ||
82 e instanceof PrincipalException) {
83
84 SessionErrors.add(actionRequest, e.getClass().getName());
85
86 setForward(actionRequest, "portlet.message_boards.error");
87 }
88 else if (e instanceof CaptchaTextException ||
89 e instanceof CategoryNameException) {
90
91 SessionErrors.add(actionRequest, e.getClass().getName());
92 }
93 else {
94 throw e;
95 }
96 }
97 }
98
99 public ActionForward render(
100 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
101 RenderRequest renderRequest, RenderResponse renderResponse)
102 throws Exception {
103
104 try {
105 ActionUtil.getCategory(renderRequest);
106 }
107 catch (Exception e) {
108 if (e instanceof NoSuchCategoryException ||
109 e instanceof PrincipalException) {
110
111 SessionErrors.add(renderRequest, e.getClass().getName());
112
113 return mapping.findForward("portlet.message_boards.error");
114 }
115 else {
116 throw e;
117 }
118 }
119
120 return mapping.findForward(
121 getForward(renderRequest, "portlet.message_boards.edit_category"));
122 }
123
124 protected void deleteCategory(ActionRequest actionRequest)
125 throws Exception {
126
127 long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
128
129 MBCategoryServiceUtil.deleteCategory(categoryId);
130 }
131
132 protected void subscribeCategory(ActionRequest actionRequest)
133 throws Exception {
134
135 long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
136
137 MBCategoryServiceUtil.subscribeCategory(categoryId);
138 }
139
140 protected void unsubscribeCategory(ActionRequest actionRequest)
141 throws Exception {
142
143 long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
144
145 MBCategoryServiceUtil.unsubscribeCategory(categoryId);
146 }
147
148 protected void updateCategory(ActionRequest actionRequest)
149 throws Exception {
150
151 Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
152
153 long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
154
155 long parentCategoryId = ParamUtil.getLong(
156 actionRequest, "parentCategoryId");
157 String name = ParamUtil.getString(actionRequest, "name");
158 String description = ParamUtil.getString(actionRequest, "description");
159
160 boolean mergeWithParentCategory = ParamUtil.getBoolean(
161 actionRequest, "mergeWithParentCategory");
162
163 String[] communityPermissions = actionRequest.getParameterValues(
164 "communityPermissions");
165 String[] guestPermissions = actionRequest.getParameterValues(
166 "guestPermissions");
167
168 if (categoryId <= 0) {
169 if (PropsValues.
170 CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_CATEGORY) {
171
172 CaptchaUtil.check(actionRequest);
173 }
174
175
177 MBCategoryServiceUtil.addCategory(
178 layout.getPlid(), parentCategoryId, name, description,
179 communityPermissions, guestPermissions);
180 }
181 else {
182
183
185 MBCategoryServiceUtil.updateCategory(
186 categoryId, parentCategoryId, name, description,
187 mergeWithParentCategory);
188 }
189 }
190
191 }