1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.shopping.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.security.auth.PrincipalException;
22  import com.liferay.portal.struts.PortletAction;
23  import com.liferay.portal.theme.ThemeDisplay;
24  import com.liferay.portal.util.WebKeys;
25  import com.liferay.portlet.shopping.NoSuchOrderException;
26  import com.liferay.portlet.shopping.service.ShoppingOrderServiceUtil;
27  import com.liferay.portlet.shopping.util.ShoppingUtil;
28  
29  import javax.portlet.ActionRequest;
30  import javax.portlet.ActionResponse;
31  import javax.portlet.PortletConfig;
32  import javax.portlet.RenderRequest;
33  import javax.portlet.RenderResponse;
34  
35  import org.apache.struts.action.ActionForm;
36  import org.apache.struts.action.ActionForward;
37  import org.apache.struts.action.ActionMapping;
38  
39  /**
40   * <a href="EditOrderAction.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class EditOrderAction extends PortletAction {
45  
46      public void processAction(
47              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
48              ActionRequest actionRequest, ActionResponse actionResponse)
49          throws Exception {
50  
51          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
52  
53          try {
54              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
55                  updateOrder(actionRequest);
56              }
57              else if (cmd.equals(Constants.DELETE)) {
58                  deleteOrders(actionRequest);
59              }
60              else if (cmd.equals("sendEmail")) {
61                  sendEmail(actionRequest);
62              }
63  
64              sendRedirect(actionRequest, actionResponse);
65          }
66          catch (Exception e) {
67              if (e instanceof NoSuchOrderException ||
68                  e instanceof PrincipalException) {
69  
70                  SessionErrors.add(actionRequest, e.getClass().getName());
71  
72                  setForward(actionRequest, "portlet.shopping.error");
73              }
74              else {
75                  throw e;
76              }
77          }
78      }
79  
80      public ActionForward render(
81              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
82              RenderRequest renderRequest, RenderResponse renderResponse)
83          throws Exception {
84  
85          try {
86              ActionUtil.getOrder(renderRequest);
87          }
88          catch (Exception e) {
89              if (e instanceof NoSuchOrderException ||
90                  e instanceof PrincipalException) {
91  
92                  SessionErrors.add(renderRequest, e.getClass().getName());
93  
94                  return mapping.findForward("portlet.shopping.error");
95              }
96              else {
97                  throw e;
98              }
99          }
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 }