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.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  }