001    /**
002     * Copyright (c) 2000-2013 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 actionMapping, ActionForm actionForm,
049                            PortletConfig portletConfig, ActionRequest actionRequest,
050                            ActionResponse actionResponse)
051                    throws Exception {
052    
053                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
054    
055                    try {
056                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
057                                    updateOrder(actionRequest);
058                            }
059                            else if (cmd.equals(Constants.DELETE)) {
060                                    deleteOrders(actionRequest);
061                            }
062                            else if (cmd.equals("sendEmail")) {
063                                    sendEmail(actionRequest);
064                            }
065    
066                            sendRedirect(actionRequest, actionResponse);
067                    }
068                    catch (Exception e) {
069                            if (e instanceof NoSuchOrderException ||
070                                    e instanceof PrincipalException) {
071    
072                                    SessionErrors.add(actionRequest, e.getClass());
073    
074                                    setForward(actionRequest, "portlet.shopping.error");
075                            }
076                            else {
077                                    throw e;
078                            }
079                    }
080            }
081    
082            @Override
083            public ActionForward render(
084                            ActionMapping actionMapping, ActionForm actionForm,
085                            PortletConfig portletConfig, RenderRequest renderRequest,
086                            RenderResponse renderResponse)
087                    throws Exception {
088    
089                    try {
090                            ActionUtil.getOrder(renderRequest);
091                    }
092                    catch (Exception e) {
093                            if (e instanceof NoSuchOrderException ||
094                                    e instanceof PrincipalException) {
095    
096                                    SessionErrors.add(renderRequest, e.getClass());
097    
098                                    return actionMapping.findForward("portlet.shopping.error");
099                            }
100                            else {
101                                    throw e;
102                            }
103                    }
104    
105                    return actionMapping.findForward(
106                            getForward(renderRequest, "portlet.shopping.edit_order"));
107            }
108    
109            protected void deleteOrders(ActionRequest actionRequest) throws Exception {
110                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
111                            WebKeys.THEME_DISPLAY);
112    
113                    long[] deleteOrderIds = StringUtil.split(
114                            ParamUtil.getString(actionRequest, "deleteOrderIds"), 0L);
115    
116                    for (int i = 0; i < deleteOrderIds.length; i++) {
117                            ShoppingOrderServiceUtil.deleteOrder(
118                                    themeDisplay.getScopeGroupId(), deleteOrderIds[i]);
119                    }
120            }
121    
122            protected void sendEmail(ActionRequest actionRequest) throws Exception {
123                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
124                            WebKeys.THEME_DISPLAY);
125    
126                    long orderId = ParamUtil.getLong(actionRequest, "orderId");
127    
128                    String emailType = ParamUtil.getString(actionRequest, "emailType");
129    
130                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
131                            actionRequest);
132    
133                    ShoppingOrderServiceUtil.sendEmail(
134                            themeDisplay.getScopeGroupId(), orderId, emailType, serviceContext);
135            }
136    
137            protected void updateOrder(ActionRequest actionRequest) throws Exception {
138                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
139                            WebKeys.THEME_DISPLAY);
140    
141                    String number = ParamUtil.getString(actionRequest, "number");
142                    String ppTxnId = ParamUtil.getString(actionRequest, "ppTxnId");
143                    String ppPaymentStatus = ShoppingUtil.getPpPaymentStatus(
144                            ParamUtil.getString(actionRequest, "ppPaymentStatus"));
145                    double ppPaymentGross = ParamUtil.getDouble(
146                            actionRequest, "ppPaymentGross");
147                    String ppReceiverEmail = ParamUtil.getString(
148                            actionRequest, "ppReceiverEmail");
149                    String ppPayerEmail = ParamUtil.getString(
150                            actionRequest, "ppPayerEmail");
151    
152                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
153                            actionRequest);
154    
155                    ShoppingOrderServiceUtil.completeOrder(
156                            themeDisplay.getScopeGroupId(), number, ppTxnId, ppPaymentStatus,
157                            ppPaymentGross, ppReceiverEmail, ppPayerEmail, serviceContext);
158            }
159    
160    }