001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.messageboards.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portlet.messageboards.model.MBCategory;
023    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
024    import com.liferay.portlet.messageboards.service.base.MBCategoryServiceBaseImpl;
025    import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
026    
027    import java.util.ArrayList;
028    import java.util.Collections;
029    import java.util.List;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class MBCategoryServiceImpl extends MBCategoryServiceBaseImpl {
035    
036            public MBCategory addCategory(
037                            long parentCategoryId, String name, String description,
038                            String emailAddress, String inProtocol, String inServerName,
039                            int inServerPort, boolean inUseSSL, String inUserName,
040                            String inPassword, int inReadInterval, String outEmailAddress,
041                            boolean outCustom, String outServerName, int outServerPort,
042                            boolean outUseSSL, String outUserName, String outPassword,
043                            boolean mailingListActive, ServiceContext serviceContext)
044                    throws PortalException, SystemException {
045    
046                    MBCategoryPermission.check(
047                            getPermissionChecker(), serviceContext.getScopeGroupId(),
048                            parentCategoryId, ActionKeys.ADD_CATEGORY);
049    
050                    return mbCategoryLocalService.addCategory(
051                            getUserId(), parentCategoryId, name, description,
052                            emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
053                            inUserName, inPassword, inReadInterval, outEmailAddress, outCustom,
054                            outServerName, outServerPort, outUseSSL, outUserName, outPassword,
055                            mailingListActive, serviceContext);
056            }
057    
058            public void deleteCategory(long groupId, long categoryId)
059                    throws PortalException, SystemException {
060    
061                    MBCategoryPermission.check(
062                            getPermissionChecker(), groupId, categoryId, ActionKeys.DELETE);
063    
064                    mbCategoryLocalService.deleteCategory(categoryId);
065            }
066    
067            public List<MBCategory> getCategories(
068                            long groupId, long parentCategoryId, int start, int end)
069                    throws SystemException {
070    
071                    return mbCategoryPersistence.filterFindByG_P(
072                            groupId, parentCategoryId, start, end);
073            }
074    
075            public List<MBCategory> getCategories(
076                            long groupId, long[] parentCategoryIds, int start, int end)
077                    throws SystemException {
078    
079                    return mbCategoryPersistence.filterFindByG_P(
080                            groupId, parentCategoryIds, start, end);
081            }
082    
083            public int getCategoriesCount(long groupId, long parentCategoryId)
084                    throws SystemException {
085    
086                    return mbCategoryPersistence.filterCountByG_P(
087                            groupId, parentCategoryId);
088            }
089    
090            public int getCategoriesCount(long groupId, long[] parentCategoryIds)
091                    throws SystemException {
092    
093                    return mbCategoryPersistence.filterCountByG_P(
094                            groupId, parentCategoryIds);
095            }
096    
097            public MBCategory getCategory(long categoryId)
098                    throws PortalException, SystemException {
099    
100                    MBCategory category = mbCategoryLocalService.getCategory(categoryId);
101    
102                    MBCategoryPermission.check(
103                            getPermissionChecker(), category, ActionKeys.VIEW);
104    
105                    return category;
106            }
107    
108            public long[] getCategoryIds(long groupId, long categoryId)
109                    throws SystemException {
110    
111                    List<Long> categoryIds = new ArrayList<Long>();
112    
113                    categoryIds.add(categoryId);
114    
115                    getSubcategoryIds(categoryIds, groupId, categoryId);
116    
117                    return ArrayUtil.toArray(
118                            categoryIds.toArray(new Long[categoryIds.size()]));
119            }
120    
121            public List<Long> getSubcategoryIds(
122                            List<Long> categoryIds, long groupId, long categoryId)
123                    throws SystemException {
124    
125                    List<MBCategory> categories = mbCategoryPersistence.filterFindByG_P(
126                            groupId, categoryId);
127    
128                    for (MBCategory category : categories) {
129                            categoryIds.add(category.getCategoryId());
130    
131                            getSubcategoryIds(
132                                    categoryIds, category.getGroupId(), category.getCategoryId());
133                    }
134    
135                    return categoryIds;
136            }
137    
138            public List<MBCategory> getSubscribedCategories(
139                            long groupId, long userId, int start, int end)
140                    throws SystemException {
141    
142                    long[] categoryIds = getCategoryIds(
143                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
144    
145                    if (categoryIds.length == 0) {
146                            return Collections.EMPTY_LIST;
147                    }
148                    else {
149                            return mbCategoryFinder.filterFindByS_G_U_P(
150                                    groupId, userId, categoryIds, start, end);
151                    }
152            }
153    
154            public int getSubscribedCategoriesCount(long groupId, long userId)
155                    throws SystemException {
156    
157                    long[] categoryIds = getCategoryIds(
158                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
159    
160                    if (categoryIds.length == 0) {
161                            return 0;
162                    }
163                    else {
164                            return mbCategoryFinder.filterCountByS_G_U_P(
165                                    groupId, userId, categoryIds);
166                    }
167            }
168    
169            public void subscribeCategory(long groupId, long categoryId)
170                    throws PortalException, SystemException {
171    
172                    MBCategoryPermission.check(
173                            getPermissionChecker(), groupId, categoryId, ActionKeys.SUBSCRIBE);
174    
175                    mbCategoryLocalService.subscribeCategory(
176                            getUserId(), groupId, categoryId);
177            }
178    
179            public void unsubscribeCategory(long groupId, long categoryId)
180                    throws PortalException, SystemException {
181    
182                    MBCategoryPermission.check(
183                            getPermissionChecker(), groupId, categoryId, ActionKeys.SUBSCRIBE);
184    
185                    mbCategoryLocalService.unsubscribeCategory(
186                            getUserId(), groupId, categoryId);
187            }
188    
189            public MBCategory updateCategory(
190                            long categoryId, long parentCategoryId, String name,
191                            String description, String emailAddress, String inProtocol,
192                            String inServerName, int inServerPort, boolean inUseSSL,
193                            String inUserName, String inPassword, int inReadInterval,
194                            String outEmailAddress, boolean outCustom, String outServerName,
195                            int outServerPort, boolean outUseSSL, String outUserName,
196                            String outPassword, boolean mailingListActive,
197                            boolean mergeWithParentCategory, ServiceContext serviceContext)
198                    throws PortalException, SystemException {
199    
200                    MBCategory category = mbCategoryLocalService.getCategory(categoryId);
201    
202                    MBCategoryPermission.check(
203                            getPermissionChecker(), category, ActionKeys.UPDATE);
204    
205                    return mbCategoryLocalService.updateCategory(
206                            categoryId, parentCategoryId, name, description, emailAddress,
207                            inProtocol, inServerName, inServerPort, inUseSSL, inUserName,
208                            inPassword, inReadInterval, outEmailAddress, outCustom,
209                            outServerName, outServerPort, outUseSSL, outUserName, outPassword,
210                            mailingListActive, mergeWithParentCategory, serviceContext);
211            }
212    
213    }