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 @Override
045 public void processAction(
046 ActionMapping actionMapping, ActionForm actionForm,
047 PortletConfig portletConfig, ActionRequest actionRequest,
048 ActionResponse actionResponse)
049 throws Exception {
050
051 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
052
053 try {
054 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
055 updateCategory(actionRequest);
056 }
057 else if (cmd.equals(Constants.DELETE)) {
058 deleteCategory(actionRequest);
059 }
060
061 sendRedirect(actionRequest, actionResponse);
062 }
063 catch (Exception e) {
064 if (e instanceof NoSuchCategoryException ||
065 e instanceof PrincipalException) {
066
067 SessionErrors.add(actionRequest, e.getClass());
068
069 setForward(actionRequest, "portlet.shopping.error");
070 }
071 else if (e instanceof CategoryNameException) {
072 SessionErrors.add(actionRequest, e.getClass());
073 }
074 else {
075 throw e;
076 }
077 }
078 }
079
080 @Override
081 public ActionForward render(
082 ActionMapping actionMapping, ActionForm actionForm,
083 PortletConfig portletConfig, RenderRequest renderRequest,
084 RenderResponse renderResponse)
085 throws Exception {
086
087 try {
088 ActionUtil.getCategory(renderRequest);
089 }
090 catch (Exception e) {
091 if (e instanceof NoSuchCategoryException ||
092 e instanceof PrincipalException) {
093
094 SessionErrors.add(renderRequest, e.getClass());
095
096 return actionMapping.findForward("portlet.shopping.error");
097 }
098 else {
099 throw e;
100 }
101 }
102
103 return actionMapping.findForward(
104 getForward(renderRequest, "portlet.shopping.edit_category"));
105 }
106
107 protected void deleteCategory(ActionRequest actionRequest)
108 throws Exception {
109
110 long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
111
112 ShoppingCategoryServiceUtil.deleteCategory(categoryId);
113 }
114
115 protected void updateCategory(ActionRequest actionRequest)
116 throws Exception {
117
118 long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
119
120 long parentCategoryId = ParamUtil.getLong(
121 actionRequest, "parentCategoryId");
122 String name = ParamUtil.getString(actionRequest, "name");
123 String description = ParamUtil.getString(actionRequest, "description");
124
125 boolean mergeWithParentCategory = ParamUtil.getBoolean(
126 actionRequest, "mergeWithParentCategory");
127
128 ServiceContext serviceContext = ServiceContextFactory.getInstance(
129 ShoppingCategory.class.getName(), actionRequest);
130
131 if (categoryId <= 0) {
132
133
134
135 ShoppingCategoryServiceUtil.addCategory(
136 parentCategoryId, name, description, serviceContext);
137 }
138 else {
139
140
141
142 ShoppingCategoryServiceUtil.updateCategory(
143 categoryId, parentCategoryId, name, description,
144 mergeWithParentCategory, serviceContext);
145 }
146 }
147
148 }