001    /**
002     * Copyright (c) 2000-2010 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.kernel.util.StringUtil;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.struts.PortletAction;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.WebKeys;
025    import com.liferay.portlet.shopping.NoSuchOrderException;
026    import com.liferay.portlet.shopping.service.ShoppingOrderServiceUtil;
027    import com.liferay.portlet.shopping.util.ShoppingUtil;
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 EditOrderAction 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                                    updateOrder(actionRequest);
054                            }
055                            else if (cmd.equals(Constants.DELETE)) {
056                                    deleteOrders(actionRequest);
057                            }
058                            else if (cmd.equals("sendEmail")) {
059                                    sendEmail(actionRequest);
060                            }
061    
062                            sendRedirect(actionRequest, actionResponse);
063                    }
064                    catch (Exception e) {
065                            if (e instanceof NoSuchOrderException ||
066                                    e instanceof PrincipalException) {
067    
068                                    SessionErrors.add(actionRequest, e.getClass().getName());
069    
070                                    setForward(actionRequest, "portlet.shopping.error");
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.getOrder(renderRequest);
085                    }
086                    catch (Exception e) {
087                            if (e instanceof NoSuchOrderException ||
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_order"));
101            }
102    
103            protected void deleteOrders(ActionRequest actionRequest) throws Exception {
104                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
105                            WebKeys.THEME_DISPLAY);
106    
107                    long[] deleteOrderIds = StringUtil.split(
108                            ParamUtil.getString(actionRequest, "deleteOrderIds"), 0L);
109    
110                    for (int i = 0; i < deleteOrderIds.length; i++) {
111                            ShoppingOrderServiceUtil.deleteOrder(
112                                    themeDisplay.getScopeGroupId(), deleteOrderIds[i]);
113                    }
114            }
115    
116            protected void sendEmail(ActionRequest actionRequest) throws Exception {
117                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
118                            WebKeys.THEME_DISPLAY);
119    
120                    long orderId = ParamUtil.getLong(actionRequest, "orderId");
121    
122                    String emailType = ParamUtil.getString(actionRequest, "emailType");
123    
124                    ShoppingOrderServiceUtil.sendEmail(
125                            themeDisplay.getScopeGroupId(), orderId, emailType);
126            }
127    
128            protected void updateOrder(ActionRequest actionRequest) throws Exception {
129                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
130                            WebKeys.THEME_DISPLAY);
131    
132                    String number = ParamUtil.getString(actionRequest, "number");
133                    String ppTxnId = ParamUtil.getString(actionRequest, "ppTxnId");
134                    String ppPaymentStatus = ShoppingUtil.getPpPaymentStatus(
135                            ParamUtil.getString(actionRequest, "ppPaymentStatus"));
136                    double ppPaymentGross = ParamUtil.getDouble(
137                            actionRequest, "ppPaymentGross");
138                    String ppReceiverEmail = ParamUtil.getString(
139                            actionRequest, "ppReceiverEmail");
140                    String ppPayerEmail = ParamUtil.getString(
141                            actionRequest, "ppPayerEmail");
142    
143                    ShoppingOrderServiceUtil.completeOrder(
144                            themeDisplay.getScopeGroupId(), number, ppTxnId, ppPaymentStatus,
145                            ppPaymentGross, ppReceiverEmail, ppPayerEmail);
146            }
147    
148    }