001    /**
002     * Copyright (c) 2000-2012 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.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.security.permission.ActionKeys;
020    import com.liferay.portal.service.ServiceContext;
021    import com.liferay.portlet.shopping.model.ShoppingCategory;
022    import com.liferay.portlet.shopping.service.base.ShoppingCategoryServiceBaseImpl;
023    import com.liferay.portlet.shopping.service.permission.ShoppingCategoryPermission;
024    
025    import java.util.List;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class ShoppingCategoryServiceImpl
031            extends ShoppingCategoryServiceBaseImpl {
032    
033            public ShoppingCategory addCategory(
034                            long parentCategoryId, String name, String description,
035                            ServiceContext serviceContext)
036                    throws PortalException, SystemException {
037    
038                    ShoppingCategoryPermission.check(
039                            getPermissionChecker(), serviceContext.getScopeGroupId(),
040                            parentCategoryId, ActionKeys.ADD_CATEGORY);
041    
042                    return shoppingCategoryLocalService.addCategory(
043                            getUserId(), parentCategoryId, name, description, serviceContext);
044            }
045    
046            public void deleteCategory(long categoryId)
047                    throws PortalException, SystemException {
048    
049                    ShoppingCategory category = shoppingCategoryLocalService.getCategory(
050                            categoryId);
051    
052                    ShoppingCategoryPermission.check(
053                            getPermissionChecker(), category, ActionKeys.DELETE);
054    
055                    shoppingCategoryLocalService.deleteCategory(categoryId);
056            }
057    
058            public List<ShoppingCategory> getCategories(long groupId)
059                    throws SystemException {
060    
061                    return shoppingCategoryPersistence.filterFindByGroupId(groupId);
062            }
063    
064            public List<ShoppingCategory> getCategories(
065                            long groupId, long parentCategoryId, int start, int end)
066                    throws SystemException {
067    
068                    return shoppingCategoryPersistence.filterFindByG_P(
069                            groupId, parentCategoryId, start, end);
070            }
071    
072            public int getCategoriesCount(long groupId, long parentCategoryId)
073                    throws SystemException {
074    
075                    return shoppingCategoryPersistence.filterCountByG_P(
076                            groupId, parentCategoryId);
077            }
078    
079            public ShoppingCategory getCategory(long categoryId)
080                    throws PortalException, SystemException {
081    
082                    ShoppingCategory category = shoppingCategoryLocalService.getCategory(
083                            categoryId);
084    
085                    ShoppingCategoryPermission.check(
086                            getPermissionChecker(), category, ActionKeys.VIEW);
087    
088                    return category;
089            }
090    
091            public void getSubcategoryIds(
092                            List<Long> categoryIds, long groupId, long categoryId)
093                    throws SystemException {
094    
095                    List<ShoppingCategory> categories =
096                            shoppingCategoryPersistence.filterFindByG_P(groupId, categoryId);
097    
098                    for (ShoppingCategory category : categories) {
099                            categoryIds.add(category.getCategoryId());
100    
101                            getSubcategoryIds(
102                                    categoryIds, category.getGroupId(), category.getCategoryId());
103                    }
104            }
105    
106            public ShoppingCategory updateCategory(
107                            long categoryId, long parentCategoryId, String name,
108                            String description, boolean mergeWithParentCategory,
109                            ServiceContext serviceContext)
110                    throws PortalException, SystemException {
111    
112                    ShoppingCategory category = shoppingCategoryLocalService.getCategory(
113                            categoryId);
114    
115                    ShoppingCategoryPermission.check(
116                            getPermissionChecker(), category, ActionKeys.UPDATE);
117    
118                    return shoppingCategoryLocalService.updateCategory(
119                            categoryId, parentCategoryId, name, description,
120                            mergeWithParentCategory, serviceContext);
121            }
122    
123    }