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