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