001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.action;
016    
017    import com.liferay.portal.kernel.util.ParamUtil;
018    import com.liferay.portal.security.permission.ActionKeys;
019    import com.liferay.portal.theme.ThemeDisplay;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portal.util.WebKeys;
022    import com.liferay.portlet.shopping.model.ShoppingCategory;
023    import com.liferay.portlet.shopping.model.ShoppingCategoryConstants;
024    import com.liferay.portlet.shopping.model.ShoppingCoupon;
025    import com.liferay.portlet.shopping.model.ShoppingItem;
026    import com.liferay.portlet.shopping.model.ShoppingOrder;
027    import com.liferay.portlet.shopping.service.ShoppingCategoryServiceUtil;
028    import com.liferay.portlet.shopping.service.ShoppingCouponServiceUtil;
029    import com.liferay.portlet.shopping.service.ShoppingItemServiceUtil;
030    import com.liferay.portlet.shopping.service.ShoppingOrderServiceUtil;
031    import com.liferay.portlet.shopping.service.permission.ShoppingPermission;
032    
033    import javax.portlet.PortletRequest;
034    
035    import javax.servlet.http.HttpServletRequest;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class ActionUtil {
041    
042            public static void getCategory(HttpServletRequest request)
043                    throws Exception {
044    
045                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
046                            WebKeys.THEME_DISPLAY);
047    
048                    long categoryId = ParamUtil.getLong(request, "categoryId");
049    
050                    ShoppingCategory category = null;
051    
052                    if ((categoryId > 0) &&
053                            (categoryId !=
054                                    ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)) {
055    
056                            category = ShoppingCategoryServiceUtil.getCategory(categoryId);
057                    }
058                    else {
059                            ShoppingPermission.check(
060                                    themeDisplay.getPermissionChecker(),
061                                    themeDisplay.getScopeGroupId(), ActionKeys.VIEW);
062                    }
063    
064                    request.setAttribute(WebKeys.SHOPPING_CATEGORY, category);
065            }
066    
067            public static void getCategory(PortletRequest portletRequest)
068                    throws Exception {
069    
070                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
071                            portletRequest);
072    
073                    getCategory(request);
074            }
075    
076            public static void getCoupon(HttpServletRequest request) throws Exception {
077                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
078                            WebKeys.THEME_DISPLAY);
079    
080                    long couponId = ParamUtil.getLong(request, "couponId");
081    
082                    ShoppingCoupon coupon = null;
083    
084                    if (couponId > 0) {
085                            coupon = ShoppingCouponServiceUtil.getCoupon(
086                                    themeDisplay.getScopeGroupId(), couponId);
087                    }
088    
089                    request.setAttribute(WebKeys.SHOPPING_COUPON, coupon);
090            }
091    
092            public static void getCoupon(PortletRequest portletRequest)
093                    throws Exception {
094    
095                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
096                            portletRequest);
097    
098                    getCoupon(request);
099            }
100    
101            public static void getItem(HttpServletRequest request) throws Exception {
102                    long itemId = ParamUtil.getLong(request, "itemId");
103    
104                    ShoppingItem item = null;
105    
106                    if (itemId > 0) {
107                            item = ShoppingItemServiceUtil.getItem(itemId);
108                    }
109    
110                    request.setAttribute(WebKeys.SHOPPING_ITEM, item);
111            }
112    
113            public static void getItem(PortletRequest portletRequest) throws Exception {
114                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
115                            portletRequest);
116    
117                    getItem(request);
118            }
119    
120            public static void getOrder(HttpServletRequest request) throws Exception {
121                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
122                            WebKeys.THEME_DISPLAY);
123    
124                    long orderId = ParamUtil.getLong(request, "orderId");
125    
126                    ShoppingOrder order = null;
127    
128                    if (orderId > 0) {
129                            order = ShoppingOrderServiceUtil.getOrder(
130                                    themeDisplay.getScopeGroupId(), orderId);
131                    }
132    
133                    request.setAttribute(WebKeys.SHOPPING_ORDER, order);
134            }
135    
136            public static void getOrder(PortletRequest portletRequest)
137                    throws Exception {
138    
139                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
140                            portletRequest);
141    
142                    getOrder(request);
143            }
144    
145    }