1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.shopping.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.Constants;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.model.Layout;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portal.util.WebKeys;
30  import com.liferay.portlet.shopping.NoSuchOrderException;
31  import com.liferay.portlet.shopping.service.ShoppingOrderServiceUtil;
32  import com.liferay.portlet.shopping.util.ShoppingUtil;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.PortletConfig;
37  import javax.portlet.RenderRequest;
38  import javax.portlet.RenderResponse;
39  
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionForward;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="EditOrderAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class EditOrderAction extends PortletAction {
51  
52      public void processAction(
53              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
54              ActionRequest actionRequest, ActionResponse actionResponse)
55          throws Exception {
56  
57          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
58  
59          try {
60              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
61                  updateOrder(actionRequest);
62              }
63              else if (cmd.equals(Constants.DELETE)) {
64                  deleteOrders(actionRequest);
65              }
66              else if (cmd.equals("sendEmail")) {
67                  sendEmail(actionRequest);
68              }
69  
70              sendRedirect(actionRequest, actionResponse);
71          }
72          catch (Exception e) {
73              if (e instanceof NoSuchOrderException ||
74                  e instanceof PrincipalException) {
75  
76                  SessionErrors.add(actionRequest, e.getClass().getName());
77  
78                  setForward(actionRequest, "portlet.shopping.error");
79              }
80              else {
81                  throw e;
82              }
83          }
84      }
85  
86      public ActionForward render(
87              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
88              RenderRequest renderRequest, RenderResponse renderResponse)
89          throws Exception {
90  
91          try {
92              ActionUtil.getOrder(renderRequest);
93          }
94          catch (Exception e) {
95              if (e instanceof NoSuchOrderException ||
96                  e instanceof PrincipalException) {
97  
98                  SessionErrors.add(renderRequest, e.getClass().getName());
99  
100                 return mapping.findForward("portlet.shopping.error");
101             }
102             else {
103                 throw e;
104             }
105         }
106 
107         return mapping.findForward(
108             getForward(renderRequest, "portlet.shopping.edit_order"));
109     }
110 
111     protected void deleteOrders(ActionRequest actionRequest) throws Exception {
112         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
113 
114         long[] deleteOrderIds = StringUtil.split(
115             ParamUtil.getString(actionRequest, "deleteOrderIds"), 0L);
116 
117         for (int i = 0; i < deleteOrderIds.length; i++) {
118             ShoppingOrderServiceUtil.deleteOrder(
119                 layout.getPlid(), deleteOrderIds[i]);
120         }
121     }
122 
123     protected void sendEmail(ActionRequest actionRequest) throws Exception {
124         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
125 
126         long orderId = ParamUtil.getLong(actionRequest, "orderId");
127 
128         String emailType = ParamUtil.getString(actionRequest, "emailType");
129 
130         ShoppingOrderServiceUtil.sendEmail(
131             layout.getPlid(), orderId, emailType);
132     }
133 
134     protected void updateOrder(ActionRequest actionRequest) throws Exception {
135         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
136 
137         String number = ParamUtil.getString(actionRequest, "number");
138         String ppTxnId = ParamUtil.getString(actionRequest, "ppTxnId");
139         String ppPaymentStatus = ShoppingUtil.getPpPaymentStatus(
140             ParamUtil.getString(actionRequest, "ppPaymentStatus"));
141         double ppPaymentGross = ParamUtil.getDouble(
142             actionRequest, "ppPaymentGross");
143         String ppReceiverEmail = ParamUtil.getString(
144             actionRequest, "ppReceiverEmail");
145         String ppPayerEmail = ParamUtil.getString(
146             actionRequest, "ppPayerEmail");
147 
148         ShoppingOrderServiceUtil.completeOrder(
149             layout.getPlid(), number, ppTxnId, ppPaymentStatus, ppPaymentGross,
150             ppReceiverEmail, ppPayerEmail);
151     }
152 
153 }