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