1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.shopping.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.Constants;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.model.Layout;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.struts.PortletAction;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.shopping.CategoryNameException;
30  import com.liferay.portlet.shopping.NoSuchCategoryException;
31  import com.liferay.portlet.shopping.service.ShoppingCategoryServiceUtil;
32  
33  import javax.portlet.ActionRequest;
34  import javax.portlet.ActionResponse;
35  import javax.portlet.PortletConfig;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="EditCategoryAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class EditCategoryAction extends PortletAction {
50  
51      public void processAction(
52              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
53              ActionRequest actionRequest, ActionResponse actionResponse)
54          throws Exception {
55  
56          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
57  
58          try {
59              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
60                  updateCategory(actionRequest);
61              }
62              else if (cmd.equals(Constants.DELETE)) {
63                  deleteCategory(actionRequest);
64              }
65  
66              sendRedirect(actionRequest, actionResponse);
67          }
68          catch (Exception e) {
69              if (e instanceof NoSuchCategoryException ||
70                  e instanceof PrincipalException) {
71  
72                  SessionErrors.add(actionRequest, e.getClass().getName());
73  
74                  setForward(actionRequest, "portlet.shopping.error");
75              }
76              else if (e instanceof CategoryNameException) {
77                  SessionErrors.add(actionRequest, e.getClass().getName());
78              }
79              else {
80                  throw e;
81              }
82          }
83      }
84  
85      public ActionForward render(
86              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
87              RenderRequest renderRequest, RenderResponse renderResponse)
88          throws Exception {
89  
90          try {
91              ActionUtil.getCategory(renderRequest);
92          }
93          catch (Exception e) {
94              if (e instanceof NoSuchCategoryException ||
95                  e instanceof PrincipalException) {
96  
97                  SessionErrors.add(renderRequest, e.getClass().getName());
98  
99                  return mapping.findForward("portlet.shopping.error");
100             }
101             else {
102                 throw e;
103             }
104         }
105 
106         return mapping.findForward(
107             getForward(renderRequest, "portlet.shopping.edit_category"));
108     }
109 
110     protected void deleteCategory(ActionRequest actionRequest)
111         throws Exception {
112 
113         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
114 
115         ShoppingCategoryServiceUtil.deleteCategory(categoryId);
116     }
117 
118     protected void updateCategory(ActionRequest actionRequest)
119         throws Exception {
120 
121         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
122 
123         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
124 
125         long parentCategoryId = ParamUtil.getLong(
126             actionRequest, "parentCategoryId");
127         String name = ParamUtil.getString(actionRequest, "name");
128         String description = ParamUtil.getString(actionRequest, "description");
129 
130         boolean mergeWithParentCategory = ParamUtil.getBoolean(
131             actionRequest, "mergeWithParentCategory");
132 
133         String[] communityPermissions = actionRequest.getParameterValues(
134             "communityPermissions");
135         String[] guestPermissions = actionRequest.getParameterValues(
136             "guestPermissions");
137 
138         if (categoryId <= 0) {
139 
140             // Add category
141 
142             ShoppingCategoryServiceUtil.addCategory(
143                 layout.getPlid(), parentCategoryId, name, description,
144                 communityPermissions, guestPermissions);
145         }
146         else {
147 
148             // Update category
149 
150             ShoppingCategoryServiceUtil.updateCategory(
151                 categoryId, parentCategoryId, name, description,
152                 mergeWithParentCategory);
153         }
154     }
155 
156 }