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.kernel.util.StringUtil;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.ServiceContextFactory;
024    import com.liferay.portal.struts.PortletAction;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.WebKeys;
027    import com.liferay.portlet.shopping.NoSuchOrderException;
028    import com.liferay.portlet.shopping.service.ShoppingOrderServiceUtil;
029    import com.liferay.portlet.shopping.util.ShoppingUtil;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class EditOrderAction extends PortletAction {
045    
046            @Override
047            public void processAction(
048                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
049                            ActionRequest actionRequest, ActionResponse actionResponse)
050                    throws Exception {
051    
052                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
053    
054                    try {
055                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
056                                    updateOrder(actionRequest);
057                            }
058                            else if (cmd.equals(Constants.DELETE)) {
059                                    deleteOrders(actionRequest);
060                            }
061                            else if (cmd.equals("sendEmail")) {
062                                    sendEmail(actionRequest);
063                            }
064    
065                            sendRedirect(actionRequest, actionResponse);
066                    }
067                    catch (Exception e) {
068                            if (e instanceof NoSuchOrderException ||
069                                    e instanceof PrincipalException) {
070    
071                                    SessionErrors.add(actionRequest, e.getClass().getName());
072    
073                                    setForward(actionRequest, "portlet.shopping.error");
074                            }
075                            else {
076                                    throw e;
077                            }
078                    }
079            }
080    
081            @Override
082            public ActionForward render(
083                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
084                            RenderRequest renderRequest, RenderResponse renderResponse)
085                    throws Exception {
086    
087                    try {
088                            ActionUtil.getOrder(renderRequest);
089                    }
090                    catch (Exception e) {
091                            if (e instanceof NoSuchOrderException ||
092                                    e instanceof PrincipalException) {
093    
094                                    SessionErrors.add(renderRequest, e.getClass().getName());
095    
096                                    return mapping.findForward("portlet.shopping.error");
097                            }
098                            else {
099                                    throw e;
100                            }
101                    }
102    
103                    return mapping.findForward(
104                            getForward(renderRequest, "portlet.shopping.edit_order"));
105            }
106    
107            protected void deleteOrders(ActionRequest actionRequest) throws Exception {
108                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
109                            WebKeys.THEME_DISPLAY);
110    
111                    long[] deleteOrderIds = StringUtil.split(
112                            ParamUtil.getString(actionRequest, "deleteOrderIds"), 0L);
113    
114                    for (int i = 0; i < deleteOrderIds.length; i++) {
115                            ShoppingOrderServiceUtil.deleteOrder(
116                                    themeDisplay.getScopeGroupId(), deleteOrderIds[i]);
117                    }
118            }
119    
120            protected void sendEmail(ActionRequest actionRequest) throws Exception {
121                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
122                            WebKeys.THEME_DISPLAY);
123    
124                    long orderId = ParamUtil.getLong(actionRequest, "orderId");
125    
126                    String emailType = ParamUtil.getString(actionRequest, "emailType");
127    
128                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
129                            actionRequest);
130    
131                    ShoppingOrderServiceUtil.sendEmail(
132                            themeDisplay.getScopeGroupId(), orderId, emailType, serviceContext);
133            }
134    
135            protected void updateOrder(ActionRequest actionRequest) throws Exception {
136                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
137                            WebKeys.THEME_DISPLAY);
138    
139                    String number = ParamUtil.getString(actionRequest, "number");
140                    String ppTxnId = ParamUtil.getString(actionRequest, "ppTxnId");
141                    String ppPaymentStatus = ShoppingUtil.getPpPaymentStatus(
142                            ParamUtil.getString(actionRequest, "ppPaymentStatus"));
143                    double ppPaymentGross = ParamUtil.getDouble(
144                            actionRequest, "ppPaymentGross");
145                    String ppReceiverEmail = ParamUtil.getString(
146                            actionRequest, "ppReceiverEmail");
147                    String ppPayerEmail = ParamUtil.getString(
148                            actionRequest, "ppPayerEmail");
149    
150                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
151                            actionRequest);
152    
153                    ShoppingOrderServiceUtil.completeOrder(
154                            themeDisplay.getScopeGroupId(), number, ppTxnId, ppPaymentStatus,
155                            ppPaymentGross, ppReceiverEmail, ppPayerEmail, serviceContext);
156            }
157    
158    }