1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util.ParamUtil;
26  import com.liferay.portal.theme.ThemeDisplay;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.shopping.model.ShoppingCategory;
30  import com.liferay.portlet.shopping.model.ShoppingCoupon;
31  import com.liferay.portlet.shopping.model.ShoppingItem;
32  import com.liferay.portlet.shopping.model.ShoppingOrder;
33  import com.liferay.portlet.shopping.model.impl.ShoppingCategoryImpl;
34  import com.liferay.portlet.shopping.service.ShoppingCategoryServiceUtil;
35  import com.liferay.portlet.shopping.service.ShoppingCouponServiceUtil;
36  import com.liferay.portlet.shopping.service.ShoppingItemServiceUtil;
37  import com.liferay.portlet.shopping.service.ShoppingOrderServiceUtil;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.RenderRequest;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  /**
45   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class ActionUtil {
51  
52      public static void getCategory(ActionRequest actionRequest)
53          throws Exception {
54  
55          HttpServletRequest request = PortalUtil.getHttpServletRequest(
56              actionRequest);
57  
58          getCategory(request);
59      }
60  
61      public static void getCategory(RenderRequest renderRequest)
62          throws Exception {
63  
64          HttpServletRequest request = PortalUtil.getHttpServletRequest(
65              renderRequest);
66  
67          getCategory(request);
68      }
69  
70      public static void getCategory(HttpServletRequest request)
71          throws Exception {
72  
73          long categoryId = ParamUtil.getLong(request, "categoryId");
74  
75          ShoppingCategory category = null;
76  
77          if ((categoryId > 0) &&
78              (categoryId != ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID)) {
79  
80              category = ShoppingCategoryServiceUtil.getCategory(categoryId);
81          }
82  
83          request.setAttribute(WebKeys.SHOPPING_CATEGORY, category);
84      }
85  
86      public static void getCoupon(ActionRequest actionRequest) throws Exception {
87          HttpServletRequest request = PortalUtil.getHttpServletRequest(
88              actionRequest);
89  
90          getCoupon(request);
91      }
92  
93      public static void getCoupon(RenderRequest renderRequest) throws Exception {
94          HttpServletRequest request = PortalUtil.getHttpServletRequest(
95              renderRequest);
96  
97          getCoupon(request);
98      }
99  
100     public static void getCoupon(HttpServletRequest request) throws Exception {
101         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
102             WebKeys.THEME_DISPLAY);
103 
104         long couponId = ParamUtil.getLong(request, "couponId");
105 
106         ShoppingCoupon coupon = null;
107 
108         if (couponId > 0) {
109             coupon = ShoppingCouponServiceUtil.getCoupon(
110                 themeDisplay.getScopeGroupId(), couponId);
111         }
112 
113         request.setAttribute(WebKeys.SHOPPING_COUPON, coupon);
114     }
115 
116     public static void getItem(ActionRequest actionRequest) throws Exception {
117         HttpServletRequest request = PortalUtil.getHttpServletRequest(
118             actionRequest);
119 
120         getItem(request);
121     }
122 
123     public static void getItem(RenderRequest renderRequest) throws Exception {
124         HttpServletRequest request = PortalUtil.getHttpServletRequest(
125             renderRequest);
126 
127         getItem(request);
128     }
129 
130     public static void getItem(HttpServletRequest request) throws Exception {
131         long itemId = ParamUtil.getLong(request, "itemId");
132 
133         ShoppingItem item = null;
134 
135         if (itemId > 0) {
136             item = ShoppingItemServiceUtil.getItem(itemId);
137         }
138 
139         request.setAttribute(WebKeys.SHOPPING_ITEM, item);
140     }
141 
142     public static void getOrder(ActionRequest actionRequest) throws Exception {
143         HttpServletRequest request = PortalUtil.getHttpServletRequest(
144             actionRequest);
145 
146         getOrder(request);
147     }
148 
149     public static void getOrder(RenderRequest renderRequest) throws Exception {
150         HttpServletRequest request = PortalUtil.getHttpServletRequest(
151             renderRequest);
152 
153         getOrder(request);
154     }
155 
156     public static void getOrder(HttpServletRequest request) throws Exception {
157         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
158             WebKeys.THEME_DISPLAY);
159 
160         long orderId = ParamUtil.getLong(request, "orderId");
161 
162         ShoppingOrder order = null;
163 
164         if (orderId > 0) {
165             order = ShoppingOrderServiceUtil.getOrder(
166                 themeDisplay.getScopeGroupId(), orderId);
167         }
168 
169         request.setAttribute(WebKeys.SHOPPING_ORDER, order);
170     }
171 
172 }