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