1
22
23 package com.liferay.portlet.blogs.action;
24
25 import com.liferay.portal.kernel.util.Constants;
26 import com.liferay.portal.kernel.util.ParamUtil;
27 import com.liferay.portal.security.auth.PrincipalException;
28 import com.liferay.portal.struts.PortletAction;
29 import com.liferay.portlet.blogs.CategoryNameException;
30 import com.liferay.portlet.blogs.NoSuchCategoryException;
31 import com.liferay.portlet.blogs.service.BlogsCategoryServiceUtil;
32 import com.liferay.util.servlet.SessionErrors;
33
34 import javax.portlet.ActionRequest;
35 import javax.portlet.ActionResponse;
36 import javax.portlet.PortletConfig;
37 import javax.portlet.RenderRequest;
38 import javax.portlet.RenderResponse;
39
40 import org.apache.struts.action.ActionForm;
41 import org.apache.struts.action.ActionForward;
42 import org.apache.struts.action.ActionMapping;
43
44
50 public class EditCategoryAction extends PortletAction {
51
52 public void processAction(
53 ActionMapping mapping, ActionForm form, PortletConfig config,
54 ActionRequest req, ActionResponse res)
55 throws Exception {
56
57 String cmd = ParamUtil.getString(req, Constants.CMD);
58
59 try {
60 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
61 updateCategory(req);
62 }
63 else if (cmd.equals(Constants.DELETE)) {
64 deleteCategory(req);
65 }
66
67 sendRedirect(req, res);
68 }
69 catch (Exception e) {
70 if (e instanceof NoSuchCategoryException ||
71 e instanceof PrincipalException) {
72
73 SessionErrors.add(req, e.getClass().getName());
74
75 setForward(req, "portlet.blogs.error");
76 }
77 else if (e instanceof CategoryNameException) {
78 SessionErrors.add(req, e.getClass().getName());
79 }
80 else {
81 throw e;
82 }
83 }
84 }
85
86 public ActionForward render(
87 ActionMapping mapping, ActionForm form, PortletConfig config,
88 RenderRequest req, RenderResponse res)
89 throws Exception {
90
91 try {
92 ActionUtil.getCategory(req);
93 }
94 catch (Exception e) {
95 if (e instanceof NoSuchCategoryException ||
96 e instanceof PrincipalException) {
97
98 SessionErrors.add(req, e.getClass().getName());
99
100 return mapping.findForward("portlet.blogs.error");
101 }
102 else {
103 throw e;
104 }
105 }
106
107 return mapping.findForward(
108 getForward(req, "portlet.blogs.edit_category"));
109 }
110
111 protected void deleteCategory(ActionRequest req) throws Exception {
112 long categoryId = ParamUtil.getLong(req, "categoryId");
113
114 BlogsCategoryServiceUtil.deleteCategory(categoryId);
115 }
116
117 protected void updateCategory(ActionRequest req) throws Exception {
118 long categoryId = ParamUtil.getLong(req, "categoryId");
119
120 long parentCategoryId = ParamUtil.getLong(req, "parentCategoryId");
121 String name = ParamUtil.getString(req, "name");
122 String description = ParamUtil.getString(req, "description");
123
124 String[] communityPermissions = req.getParameterValues(
125 "communityPermissions");
126 String[] guestPermissions = req.getParameterValues(
127 "guestPermissions");
128
129 if (categoryId <= 0) {
130
131
133 BlogsCategoryServiceUtil.addCategory(
134 parentCategoryId, name, description, communityPermissions,
135 guestPermissions);
136 }
137 else {
138
139
141 BlogsCategoryServiceUtil.updateCategory(
142 categoryId, parentCategoryId, name, description);
143 }
144 }
145
146 }