1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.model.Layout;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.struts.PortletAction;
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  public class EditOrderAction extends PortletAction {
53  
54      public void processAction(
55              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
56              ActionRequest actionRequest, ActionResponse actionResponse)
57          throws Exception {
58  
59          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
60  
61          try {
62              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
63                  updateOrder(actionRequest);
64              }
65              else if (cmd.equals(Constants.DELETE)) {
66                  deleteOrders(actionRequest);
67              }
68              else if (cmd.equals("sendEmail")) {
69                  sendEmail(actionRequest);
70              }
71  
72              sendRedirect(actionRequest, actionResponse);
73          }
74          catch (Exception e) {
75              if (e instanceof NoSuchOrderException ||
76                  e instanceof PrincipalException) {
77  
78                  SessionErrors.add(actionRequest, e.getClass().getName());
79  
80                  setForward(actionRequest, "portlet.shopping.error");
81              }
82              else {
83                  throw e;
84              }
85          }
86      }
87  
88      public ActionForward render(
89              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
90              RenderRequest renderRequest, RenderResponse renderResponse)
91          throws Exception {
92  
93          try {
94              ActionUtil.getOrder(renderRequest);
95          }
96          catch (Exception e) {
97              if (e instanceof NoSuchOrderException ||
98                  e instanceof PrincipalException) {
99  
100                 SessionErrors.add(renderRequest, e.getClass().getName());
101 
102                 return mapping.findForward("portlet.shopping.error");
103             }
104             else {
105                 throw e;
106             }
107         }
108 
109         return mapping.findForward(
110             getForward(renderRequest, "portlet.shopping.edit_order"));
111     }
112 
113     protected void deleteOrders(ActionRequest actionRequest) throws Exception {
114         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
115 
116         long[] deleteOrderIds = StringUtil.split(
117             ParamUtil.getString(actionRequest, "deleteOrderIds"), 0L);
118 
119         for (int i = 0; i < deleteOrderIds.length; i++) {
120             ShoppingOrderServiceUtil.deleteOrder(
121                 layout.getPlid(), deleteOrderIds[i]);
122         }
123     }
124 
125     protected void sendEmail(ActionRequest actionRequest) throws Exception {
126         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
127 
128         long orderId = ParamUtil.getLong(actionRequest, "orderId");
129 
130         String emailType = ParamUtil.getString(actionRequest, "emailType");
131 
132         ShoppingOrderServiceUtil.sendEmail(
133             layout.getPlid(), orderId, emailType);
134     }
135 
136     protected void updateOrder(ActionRequest actionRequest) throws Exception {
137         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
138 
139         String number = ParamUtil.getString(actionRequest, "number");
140         String ppTxnId = ParamUtil.getString(actionRequest, "ppTxnId");
141         String ppPaymentStatus = ShoppingUtil.getPpPaymentStatus(
142             ParamUtil.getString(actionRequest, "ppPaymentStatus"));
143         double ppPaymentGross = ParamUtil.getDouble(
144             actionRequest, "ppPaymentGross");
145         String ppReceiverEmail = ParamUtil.getString(
146             actionRequest, "ppReceiverEmail");
147         String ppPayerEmail = ParamUtil.getString(
148             actionRequest, "ppPayerEmail");
149 
150         ShoppingOrderServiceUtil.completeOrder(
151             layout.getPlid(), number, ppTxnId, ppPaymentStatus, ppPaymentGross,
152             ppReceiverEmail, ppPayerEmail);
153     }
154 
155 }