001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            @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                                    updateOrder(actionRequest);
055                            }
056                            else if (cmd.equals(Constants.DELETE)) {
057                                    deleteOrders(actionRequest);
058                            }
059                            else if (cmd.equals("sendEmail")) {
060                                    sendEmail(actionRequest);
061                            }
062    
063                            sendRedirect(actionRequest, actionResponse);
064                    }
065                    catch (Exception e) {
066                            if (e instanceof NoSuchOrderException ||
067                                    e instanceof PrincipalException) {
068    
069                                    SessionErrors.add(actionRequest, e.getClass().getName());
070    
071                                    setForward(actionRequest, "portlet.shopping.error");
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.getOrder(renderRequest);
087                    }
088                    catch (Exception e) {
089                            if (e instanceof NoSuchOrderException ||
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_order"));
103            }
104    
105            protected void deleteOrders(ActionRequest actionRequest) throws Exception {
106                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
107                            WebKeys.THEME_DISPLAY);
108    
109                    long[] deleteOrderIds = StringUtil.split(
110                            ParamUtil.getString(actionRequest, "deleteOrderIds"), 0L);
111    
112                    for (int i = 0; i < deleteOrderIds.length; i++) {
113                            ShoppingOrderServiceUtil.deleteOrder(
114                                    themeDisplay.getScopeGroupId(), deleteOrderIds[i]);
115                    }
116            }
117    
118            protected void sendEmail(ActionRequest actionRequest) throws Exception {
119                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
120                            WebKeys.THEME_DISPLAY);
121    
122                    long orderId = ParamUtil.getLong(actionRequest, "orderId");
123    
124                    String emailType = ParamUtil.getString(actionRequest, "emailType");
125    
126                    ShoppingOrderServiceUtil.sendEmail(
127                            themeDisplay.getScopeGroupId(), orderId, emailType);
128            }
129    
130            protected void updateOrder(ActionRequest actionRequest) throws Exception {
131                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
132                            WebKeys.THEME_DISPLAY);
133    
134                    String number = ParamUtil.getString(actionRequest, "number");
135                    String ppTxnId = ParamUtil.getString(actionRequest, "ppTxnId");
136                    String ppPaymentStatus = ShoppingUtil.getPpPaymentStatus(
137                            ParamUtil.getString(actionRequest, "ppPaymentStatus"));
138                    double ppPaymentGross = ParamUtil.getDouble(
139                            actionRequest, "ppPaymentGross");
140                    String ppReceiverEmail = ParamUtil.getString(
141                            actionRequest, "ppReceiverEmail");
142                    String ppPayerEmail = ParamUtil.getString(
143                            actionRequest, "ppPayerEmail");
144    
145                    ShoppingOrderServiceUtil.completeOrder(
146                            themeDisplay.getScopeGroupId(), number, ppTxnId, ppPaymentStatus,
147                            ppPaymentGross, ppReceiverEmail, ppPayerEmail);
148            }
149    
150    }