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.ParamUtil;
19  import com.liferay.portal.security.auth.PrincipalException;
20  import com.liferay.portal.struts.PortletAction;
21  import com.liferay.portal.util.WebKeys;
22  import com.liferay.portlet.shopping.NoSuchCouponException;
23  import com.liferay.portlet.shopping.model.ShoppingCoupon;
24  import com.liferay.portlet.shopping.service.ShoppingCouponLocalServiceUtil;
25  
26  import javax.portlet.PortletConfig;
27  import javax.portlet.RenderRequest;
28  import javax.portlet.RenderResponse;
29  
30  import org.apache.struts.action.ActionForm;
31  import org.apache.struts.action.ActionForward;
32  import org.apache.struts.action.ActionMapping;
33  
34  /**
35   * <a href="ViewCouponAction.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class ViewCouponAction extends PortletAction {
40  
41      public ActionForward render(
42              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
43              RenderRequest renderRequest, RenderResponse renderResponse)
44          throws Exception {
45  
46          try {
47              long couponId = ParamUtil.getLong(renderRequest, "couponId");
48  
49              String code = ParamUtil.getString(renderRequest, "code");
50  
51              ShoppingCoupon coupon = null;
52  
53              if (couponId > 0) {
54                  coupon = ShoppingCouponLocalServiceUtil.getCoupon(couponId);
55              }
56              else {
57                  coupon = ShoppingCouponLocalServiceUtil.getCoupon(code);
58              }
59  
60              renderRequest.setAttribute(WebKeys.SHOPPING_COUPON, coupon);
61          }
62          catch (Exception e) {
63              if (e instanceof NoSuchCouponException ||
64                  e instanceof PrincipalException) {
65  
66                  SessionErrors.add(renderRequest, e.getClass().getName());
67  
68                  return mapping.findForward("portlet.shopping.error");
69              }
70              else {
71                  throw e;
72              }
73          }
74  
75          return mapping.findForward("portlet.shopping.view_coupon");
76      }
77  
78  }