001
014
015 package com.liferay.portlet.shopping.action;
016
017 import com.liferay.portal.kernel.servlet.SessionErrors;
018 import com.liferay.portal.kernel.util.Constants;
019 import com.liferay.portal.kernel.util.ParamUtil;
020 import com.liferay.portal.security.auth.PrincipalException;
021 import com.liferay.portal.service.ServiceContext;
022 import com.liferay.portal.service.ServiceContextFactory;
023 import com.liferay.portal.struts.PortletAction;
024 import com.liferay.portlet.shopping.CategoryNameException;
025 import com.liferay.portlet.shopping.NoSuchCategoryException;
026 import com.liferay.portlet.shopping.model.ShoppingCategory;
027 import com.liferay.portlet.shopping.service.ShoppingCategoryServiceUtil;
028
029 import javax.portlet.ActionRequest;
030 import javax.portlet.ActionResponse;
031 import javax.portlet.PortletConfig;
032 import javax.portlet.RenderRequest;
033 import javax.portlet.RenderResponse;
034
035 import org.apache.struts.action.ActionForm;
036 import org.apache.struts.action.ActionForward;
037 import org.apache.struts.action.ActionMapping;
038
039
042 public class EditCategoryAction extends PortletAction {
043
044 public void processAction(
045 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
046 ActionRequest actionRequest, ActionResponse actionResponse)
047 throws Exception {
048
049 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
050
051 try {
052 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
053 updateCategory(actionRequest);
054 }
055 else if (cmd.equals(Constants.DELETE)) {
056 deleteCategory(actionRequest);
057 }
058
059 sendRedirect(actionRequest, actionResponse);
060 }
061 catch (Exception e) {
062 if (e instanceof NoSuchCategoryException ||
063 e instanceof PrincipalException) {
064
065 SessionErrors.add(actionRequest, e.getClass().getName());
066
067 setForward(actionRequest, "portlet.shopping.error");
068 }
069 else if (e instanceof CategoryNameException) {
070 SessionErrors.add(actionRequest, e.getClass().getName());
071 }
072 else {
073 throw e;
074 }
075 }
076 }
077
078 public ActionForward render(
079 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
080 RenderRequest renderRequest, RenderResponse renderResponse)
081 throws Exception {
082
083 try {
084 ActionUtil.getCategory(renderRequest);
085 }
086 catch (Exception e) {
087 if (e instanceof NoSuchCategoryException ||
088 e instanceof PrincipalException) {
089
090 SessionErrors.add(renderRequest, e.getClass().getName());
091
092 return mapping.findForward("portlet.shopping.error");
093 }
094 else {
095 throw e;
096 }
097 }
098
099 return mapping.findForward(
100 getForward(renderRequest, "portlet.shopping.edit_category"));
101 }
102
103 protected void deleteCategory(ActionRequest actionRequest)
104 throws Exception {
105
106 long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
107
108 ShoppingCategoryServiceUtil.deleteCategory(categoryId);
109 }
110
111 protected void updateCategory(ActionRequest actionRequest)
112 throws Exception {
113
114 long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
115
116 long parentCategoryId = ParamUtil.getLong(
117 actionRequest, "parentCategoryId");
118 String name = ParamUtil.getString(actionRequest, "name");
119 String description = ParamUtil.getString(actionRequest, "description");
120
121 boolean mergeWithParentCategory = ParamUtil.getBoolean(
122 actionRequest, "mergeWithParentCategory");
123
124 ServiceContext serviceContext = ServiceContextFactory.getInstance(
125 ShoppingCategory.class.getName(), actionRequest);
126
127 if (categoryId <= 0) {
128
129
130
131 ShoppingCategoryServiceUtil.addCategory(
132 parentCategoryId, name, description, serviceContext);
133 }
134 else {
135
136
137
138 ShoppingCategoryServiceUtil.updateCategory(
139 categoryId, parentCategoryId, name, description,
140 mergeWithParentCategory, serviceContext);
141 }
142 }
143
144 }