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.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            @Override
037            public MBCategory addCategory(
038                            long parentCategoryId, String name, String description,
039                            String displayStyle, String emailAddress, String inProtocol,
040                            String inServerName, int inServerPort, boolean inUseSSL,
041                            String inUserName, String inPassword, int inReadInterval,
042                            String outEmailAddress, boolean outCustom, String outServerName,
043                            int outServerPort, boolean outUseSSL, String outUserName,
044                            String outPassword, boolean mailingListActive,
045                            boolean allowAnonymousEmail, ServiceContext serviceContext)
046                    throws PortalException, SystemException {
047    
048                    MBCategoryPermission.check(
049                            getPermissionChecker(), serviceContext.getScopeGroupId(),
050                            parentCategoryId, ActionKeys.ADD_CATEGORY);
051    
052                    return mbCategoryLocalService.addCategory(
053                            getUserId(), parentCategoryId, name, description, displayStyle,
054                            emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
055                            inUserName, inPassword, inReadInterval, outEmailAddress, outCustom,
056                            outServerName, outServerPort, outUseSSL, outUserName, outPassword,
057                            mailingListActive, allowAnonymousEmail, serviceContext);
058            }
059    
060            @Override
061            public void deleteCategory(long groupId, long categoryId)
062                    throws PortalException, SystemException {
063    
064                    MBCategoryPermission.check(
065                            getPermissionChecker(), groupId, categoryId, ActionKeys.DELETE);
066    
067                    mbCategoryLocalService.deleteCategory(categoryId);
068            }
069    
070            @Override
071            public List<MBCategory> getCategories(long groupId) throws SystemException {
072                    return mbCategoryPersistence.filterFindByGroupId(groupId);
073            }
074    
075            @Override
076            public List<MBCategory> getCategories(
077                            long groupId, long parentCategoryId, int start, int end)
078                    throws SystemException {
079    
080                    return mbCategoryPersistence.filterFindByG_P(
081                            groupId, parentCategoryId, start, end);
082            }
083    
084            @Override
085            public List<MBCategory> getCategories(
086                            long groupId, long[] parentCategoryIds, int start, int end)
087                    throws SystemException {
088    
089                    return mbCategoryPersistence.filterFindByG_P(
090                            groupId, parentCategoryIds, start, end);
091            }
092    
093            @Override
094            public int getCategoriesCount(long groupId, long parentCategoryId)
095                    throws SystemException {
096    
097                    return mbCategoryPersistence.filterCountByG_P(
098                            groupId, parentCategoryId);
099            }
100    
101            @Override
102            public int getCategoriesCount(long groupId, long[] parentCategoryIds)
103                    throws SystemException {
104    
105                    return mbCategoryPersistence.filterCountByG_P(
106                            groupId, parentCategoryIds);
107            }
108    
109            @Override
110            public MBCategory getCategory(long categoryId)
111                    throws PortalException, SystemException {
112    
113                    MBCategory category = mbCategoryLocalService.getCategory(categoryId);
114    
115                    MBCategoryPermission.check(
116                            getPermissionChecker(), category, ActionKeys.VIEW);
117    
118                    return category;
119            }
120    
121            @Override
122            public long[] getCategoryIds(long groupId, long categoryId)
123                    throws SystemException {
124    
125                    List<Long> categoryIds = new ArrayList<Long>();
126    
127                    categoryIds.add(categoryId);
128    
129                    getSubcategoryIds(categoryIds, groupId, categoryId);
130    
131                    return ArrayUtil.toArray(
132                            categoryIds.toArray(new Long[categoryIds.size()]));
133            }
134    
135            @Override
136            public List<Long> getSubcategoryIds(
137                            List<Long> categoryIds, long groupId, long categoryId)
138                    throws SystemException {
139    
140                    List<MBCategory> categories = mbCategoryPersistence.filterFindByG_P(
141                            groupId, categoryId);
142    
143                    for (MBCategory category : categories) {
144                            categoryIds.add(category.getCategoryId());
145    
146                            getSubcategoryIds(
147                                    categoryIds, category.getGroupId(), category.getCategoryId());
148                    }
149    
150                    return categoryIds;
151            }
152    
153            @Override
154            public List<MBCategory> getSubscribedCategories(
155                            long groupId, long userId, int start, int end)
156                    throws SystemException {
157    
158                    long[] categoryIds = getCategoryIds(
159                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
160    
161                    if (categoryIds.length == 0) {
162                            return Collections.emptyList();
163                    }
164                    else {
165                            return mbCategoryFinder.filterFindByS_G_U_P(
166                                    groupId, userId, categoryIds, start, end);
167                    }
168            }
169    
170            @Override
171            public int getSubscribedCategoriesCount(long groupId, long userId)
172                    throws SystemException {
173    
174                    long[] categoryIds = getCategoryIds(
175                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
176    
177                    if (categoryIds.length == 0) {
178                            return 0;
179                    }
180                    else {
181                            return mbCategoryFinder.filterCountByS_G_U_P(
182                                    groupId, userId, categoryIds);
183                    }
184            }
185    
186            @Override
187            public void subscribeCategory(long groupId, long categoryId)
188                    throws PortalException, SystemException {
189    
190                    MBCategoryPermission.check(
191                            getPermissionChecker(), groupId, categoryId, ActionKeys.SUBSCRIBE);
192    
193                    mbCategoryLocalService.subscribeCategory(
194                            getUserId(), groupId, categoryId);
195            }
196    
197            @Override
198            public void unsubscribeCategory(long groupId, long categoryId)
199                    throws PortalException, SystemException {
200    
201                    MBCategoryPermission.check(
202                            getPermissionChecker(), groupId, categoryId, ActionKeys.SUBSCRIBE);
203    
204                    mbCategoryLocalService.unsubscribeCategory(
205                            getUserId(), groupId, categoryId);
206            }
207    
208            @Override
209            public MBCategory updateCategory(
210                            long categoryId, long parentCategoryId, String name,
211                            String description, String displayStyle, String emailAddress,
212                            String inProtocol, String inServerName, int inServerPort,
213                            boolean inUseSSL, String inUserName, String inPassword,
214                            int inReadInterval, String outEmailAddress, boolean outCustom,
215                            String outServerName, int outServerPort, boolean outUseSSL,
216                            String outUserName, String outPassword, boolean mailingListActive,
217                            boolean allowAnonymousEmail, boolean mergeWithParentCategory,
218                            ServiceContext serviceContext)
219                    throws PortalException, SystemException {
220    
221                    MBCategory category = mbCategoryLocalService.getCategory(categoryId);
222    
223                    MBCategoryPermission.check(
224                            getPermissionChecker(), category, ActionKeys.UPDATE);
225    
226                    return mbCategoryLocalService.updateCategory(
227                            categoryId, parentCategoryId, name, description, displayStyle,
228                            emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
229                            inUserName, inPassword, inReadInterval, outEmailAddress, outCustom,
230                            outServerName, outServerPort, outUseSSL, outUserName, outPassword,
231                            mailingListActive, allowAnonymousEmail, mergeWithParentCategory,
232                            serviceContext);
233            }
234    
235    }