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