1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.shopping.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.shopping.NoSuchOrderException;
34  import com.liferay.portlet.shopping.service.ShoppingOrderServiceUtil;
35  import com.liferay.portlet.shopping.util.ShoppingUtil;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="EditOrderAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class EditOrderAction extends PortletAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              ActionRequest actionRequest, ActionResponse actionResponse)
58          throws Exception {
59  
60          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
61  
62          try {
63              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
64                  updateOrder(actionRequest);
65              }
66              else if (cmd.equals(Constants.DELETE)) {
67                  deleteOrders(actionRequest);
68              }
69              else if (cmd.equals("sendEmail")) {
70                  sendEmail(actionRequest);
71              }
72  
73              sendRedirect(actionRequest, actionResponse);
74          }
75          catch (Exception e) {
76              if (e instanceof NoSuchOrderException ||
77                  e instanceof PrincipalException) {
78  
79                  SessionErrors.add(actionRequest, e.getClass().getName());
80  
81                  setForward(actionRequest, "portlet.shopping.error");
82              }
83              else {
84                  throw e;
85              }
86          }
87      }
88  
89      public ActionForward render(
90              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
91              RenderRequest renderRequest, RenderResponse renderResponse)
92          throws Exception {
93  
94          try {
95              ActionUtil.getOrder(renderRequest);
96          }
97          catch (Exception e) {
98              if (e instanceof NoSuchOrderException ||
99                  e instanceof PrincipalException) {
100 
101                 SessionErrors.add(renderRequest, e.getClass().getName());
102 
103                 return mapping.findForward("portlet.shopping.error");
104             }
105             else {
106                 throw e;
107             }
108         }
109 
110         return mapping.findForward(
111             getForward(renderRequest, "portlet.shopping.edit_order"));
112     }
113 
114     protected void deleteOrders(ActionRequest actionRequest) throws Exception {
115         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
116             WebKeys.THEME_DISPLAY);
117 
118         long[] deleteOrderIds = StringUtil.split(
119             ParamUtil.getString(actionRequest, "deleteOrderIds"), 0L);
120 
121         for (int i = 0; i < deleteOrderIds.length; i++) {
122             ShoppingOrderServiceUtil.deleteOrder(
123                 themeDisplay.getScopeGroupId(), deleteOrderIds[i]);
124         }
125     }
126 
127     protected void sendEmail(ActionRequest actionRequest) throws Exception {
128         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
129             WebKeys.THEME_DISPLAY);
130 
131         long orderId = ParamUtil.getLong(actionRequest, "orderId");
132 
133         String emailType = ParamUtil.getString(actionRequest, "emailType");
134 
135         ShoppingOrderServiceUtil.sendEmail(
136             themeDisplay.getScopeGroupId(), orderId, emailType);
137     }
138 
139     protected void updateOrder(ActionRequest actionRequest) throws Exception {
140         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
141             WebKeys.THEME_DISPLAY);
142 
143         String number = ParamUtil.getString(actionRequest, "number");
144         String ppTxnId = ParamUtil.getString(actionRequest, "ppTxnId");
145         String ppPaymentStatus = ShoppingUtil.getPpPaymentStatus(
146             ParamUtil.getString(actionRequest, "ppPaymentStatus"));
147         double ppPaymentGross = ParamUtil.getDouble(
148             actionRequest, "ppPaymentGross");
149         String ppReceiverEmail = ParamUtil.getString(
150             actionRequest, "ppReceiverEmail");
151         String ppPayerEmail = ParamUtil.getString(
152             actionRequest, "ppPayerEmail");
153 
154         ShoppingOrderServiceUtil.completeOrder(
155             themeDisplay.getScopeGroupId(), number, ppTxnId, ppPaymentStatus,
156             ppPaymentGross, ppReceiverEmail, ppPayerEmail);
157     }
158 
159 }