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.model.Layout;
29 import com.liferay.portal.security.auth.PrincipalException;
30 import com.liferay.portal.struts.PortletAction;
31 import com.liferay.portal.util.PortalUtil;
32 import com.liferay.portal.util.WebKeys;
33 import com.liferay.portlet.shopping.CategoryNameException;
34 import com.liferay.portlet.shopping.NoSuchCategoryException;
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
52 public class EditCategoryAction extends PortletAction {
53
54 public void processAction(
55 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
56 ActionRequest actionRequest, ActionResponse actionResponse)
57 throws Exception {
58
59 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
60
61 try {
62 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
63 updateCategory(actionRequest);
64 }
65 else if (cmd.equals(Constants.DELETE)) {
66 deleteCategory(actionRequest);
67 }
68
69 sendRedirect(actionRequest, actionResponse);
70 }
71 catch (Exception e) {
72 if (e instanceof NoSuchCategoryException ||
73 e instanceof PrincipalException) {
74
75 SessionErrors.add(actionRequest, e.getClass().getName());
76
77 setForward(actionRequest, "portlet.shopping.error");
78 }
79 else if (e instanceof CategoryNameException) {
80 SessionErrors.add(actionRequest, e.getClass().getName());
81 }
82 else {
83 throw e;
84 }
85 }
86 }
87
88 public ActionForward render(
89 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
90 RenderRequest renderRequest, RenderResponse renderResponse)
91 throws Exception {
92
93 try {
94 ActionUtil.getCategory(renderRequest);
95 }
96 catch (Exception e) {
97 if (e instanceof NoSuchCategoryException ||
98 e instanceof PrincipalException) {
99
100 SessionErrors.add(renderRequest, e.getClass().getName());
101
102 return mapping.findForward("portlet.shopping.error");
103 }
104 else {
105 throw e;
106 }
107 }
108
109 return mapping.findForward(
110 getForward(renderRequest, "portlet.shopping.edit_category"));
111 }
112
113 protected void deleteCategory(ActionRequest actionRequest)
114 throws Exception {
115
116 long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
117
118 ShoppingCategoryServiceUtil.deleteCategory(categoryId);
119 }
120
121 protected void updateCategory(ActionRequest actionRequest)
122 throws Exception {
123
124 Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
125
126 long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
127
128 long parentCategoryId = ParamUtil.getLong(
129 actionRequest, "parentCategoryId");
130 String name = ParamUtil.getString(actionRequest, "name");
131 String description = ParamUtil.getString(actionRequest, "description");
132
133 boolean mergeWithParentCategory = ParamUtil.getBoolean(
134 actionRequest, "mergeWithParentCategory");
135
136 String[] communityPermissions = PortalUtil.getCommunityPermissions(
137 actionRequest);
138 String[] guestPermissions = PortalUtil.getGuestPermissions(
139 actionRequest);
140
141 if (categoryId <= 0) {
142
143
145 ShoppingCategoryServiceUtil.addCategory(
146 layout.getPlid(), parentCategoryId, name, description,
147 communityPermissions, guestPermissions);
148 }
149 else {
150
151
153 ShoppingCategoryServiceUtil.updateCategory(
154 categoryId, parentCategoryId, name, description,
155 mergeWithParentCategory);
156 }
157 }
158
159 }