1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
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.model.Layout;
22  import com.liferay.portal.security.auth.PrincipalException;
23  import com.liferay.portal.struts.PortletAction;
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         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
107 
108         long[] deleteOrderIds = StringUtil.split(
109             ParamUtil.getString(actionRequest, "deleteOrderIds"), 0L);
110 
111         for (int i = 0; i < deleteOrderIds.length; i++) {
112             ShoppingOrderServiceUtil.deleteOrder(
113                 layout.getPlid(), deleteOrderIds[i]);
114         }
115     }
116 
117     protected void sendEmail(ActionRequest actionRequest) throws Exception {
118         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
119 
120         long orderId = ParamUtil.getLong(actionRequest, "orderId");
121 
122         String emailType = ParamUtil.getString(actionRequest, "emailType");
123 
124         ShoppingOrderServiceUtil.sendEmail(
125             layout.getPlid(), orderId, emailType);
126     }
127 
128     protected void updateOrder(ActionRequest actionRequest) throws Exception {
129         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
130 
131         String number = ParamUtil.getString(actionRequest, "number");
132         String ppTxnId = ParamUtil.getString(actionRequest, "ppTxnId");
133         String ppPaymentStatus = ShoppingUtil.getPpPaymentStatus(
134             ParamUtil.getString(actionRequest, "ppPaymentStatus"));
135         double ppPaymentGross = ParamUtil.getDouble(
136             actionRequest, "ppPaymentGross");
137         String ppReceiverEmail = ParamUtil.getString(
138             actionRequest, "ppReceiverEmail");
139         String ppPayerEmail = ParamUtil.getString(
140             actionRequest, "ppPayerEmail");
141 
142         ShoppingOrderServiceUtil.completeOrder(
143             layout.getPlid(), number, ppTxnId, ppPaymentStatus, ppPaymentGross,
144             ppReceiverEmail, ppPayerEmail);
145     }
146 
147 }