001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class EditCategoryAction extends PortletAction {
043    
044            @Override
045            public void processAction(
046                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
047                            ActionRequest actionRequest, ActionResponse actionResponse)
048                    throws Exception {
049    
050                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
051    
052                    try {
053                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
054                                    updateCategory(actionRequest);
055                            }
056                            else if (cmd.equals(Constants.DELETE)) {
057                                    deleteCategory(actionRequest);
058                            }
059    
060                            sendRedirect(actionRequest, actionResponse);
061                    }
062                    catch (Exception e) {
063                            if (e instanceof NoSuchCategoryException ||
064                                    e instanceof PrincipalException) {
065    
066                                    SessionErrors.add(actionRequest, e.getClass().getName());
067    
068                                    setForward(actionRequest, "portlet.shopping.error");
069                            }
070                            else if (e instanceof CategoryNameException) {
071                                    SessionErrors.add(actionRequest, e.getClass().getName());
072                            }
073                            else {
074                                    throw e;
075                            }
076                    }
077            }
078    
079            @Override
080            public ActionForward render(
081                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
082                            RenderRequest renderRequest, RenderResponse renderResponse)
083                    throws Exception {
084    
085                    try {
086                            ActionUtil.getCategory(renderRequest);
087                    }
088                    catch (Exception e) {
089                            if (e instanceof NoSuchCategoryException ||
090                                    e instanceof PrincipalException) {
091    
092                                    SessionErrors.add(renderRequest, e.getClass().getName());
093    
094                                    return mapping.findForward("portlet.shopping.error");
095                            }
096                            else {
097                                    throw e;
098                            }
099                    }
100    
101                    return mapping.findForward(
102                            getForward(renderRequest, "portlet.shopping.edit_category"));
103            }
104    
105            protected void deleteCategory(ActionRequest actionRequest)
106                    throws Exception {
107    
108                    long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
109    
110                    ShoppingCategoryServiceUtil.deleteCategory(categoryId);
111            }
112    
113            protected void updateCategory(ActionRequest actionRequest)
114                    throws Exception {
115    
116                    long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
117    
118                    long parentCategoryId = ParamUtil.getLong(
119                            actionRequest, "parentCategoryId");
120                    String name = ParamUtil.getString(actionRequest, "name");
121                    String description = ParamUtil.getString(actionRequest, "description");
122    
123                    boolean mergeWithParentCategory = ParamUtil.getBoolean(
124                            actionRequest, "mergeWithParentCategory");
125    
126                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
127                            ShoppingCategory.class.getName(), actionRequest);
128    
129                    if (categoryId <= 0) {
130    
131                            // Add category
132    
133                            ShoppingCategoryServiceUtil.addCategory(
134                                    parentCategoryId, name, description, serviceContext);
135                    }
136                    else {
137    
138                            // Update category
139    
140                            ShoppingCategoryServiceUtil.updateCategory(
141                                    categoryId, parentCategoryId, name, description,
142                                    mergeWithParentCategory, serviceContext);
143                    }
144            }
145    
146    }