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.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.messageboards.model.MBCategory;
024    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
025    import com.liferay.portlet.messageboards.service.base.MBCategoryServiceBaseImpl;
026    import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
027    
028    import java.util.ArrayList;
029    import java.util.Collections;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class MBCategoryServiceImpl extends MBCategoryServiceBaseImpl {
036    
037            public MBCategory addCategory(
038                            long userId, long parentCategoryId, String name, String description,
039                            ServiceContext serviceContext)
040                    throws PortalException, SystemException {
041    
042                    MBCategoryPermission.check(
043                            getPermissionChecker(), serviceContext.getScopeGroupId(),
044                            parentCategoryId, ActionKeys.ADD_CATEGORY);
045    
046                    return mbCategoryLocalService.addCategory(
047                            userId, parentCategoryId, name, description, serviceContext);
048            }
049    
050            public MBCategory addCategory(
051                            long parentCategoryId, String name, String description,
052                            String displayStyle, String emailAddress, String inProtocol,
053                            String inServerName, int inServerPort, boolean inUseSSL,
054                            String inUserName, String inPassword, int inReadInterval,
055                            String outEmailAddress, boolean outCustom, String outServerName,
056                            int outServerPort, boolean outUseSSL, String outUserName,
057                            String outPassword, boolean mailingListActive,
058                            boolean allowAnonymousEmail, ServiceContext serviceContext)
059                    throws PortalException, SystemException {
060    
061                    MBCategoryPermission.check(
062                            getPermissionChecker(), serviceContext.getScopeGroupId(),
063                            parentCategoryId, ActionKeys.ADD_CATEGORY);
064    
065                    return mbCategoryLocalService.addCategory(
066                            getUserId(), parentCategoryId, name, description, displayStyle,
067                            emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
068                            inUserName, inPassword, inReadInterval, outEmailAddress, outCustom,
069                            outServerName, outServerPort, outUseSSL, outUserName, outPassword,
070                            mailingListActive, allowAnonymousEmail, serviceContext);
071            }
072    
073            public void deleteCategory(long categoryId, boolean includeTrashedEntries)
074                    throws PortalException, SystemException {
075    
076                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
077                            categoryId);
078    
079                    MBCategoryPermission.check(
080                            getPermissionChecker(), category, ActionKeys.DELETE);
081    
082                    mbCategoryLocalService.deleteCategory(category, includeTrashedEntries);
083            }
084    
085            public void deleteCategory(long groupId, long categoryId)
086                    throws PortalException, SystemException {
087    
088                    MBCategoryPermission.check(
089                            getPermissionChecker(), groupId, categoryId, ActionKeys.DELETE);
090    
091                    mbCategoryLocalService.deleteCategory(categoryId);
092            }
093    
094            public List<MBCategory> getCategories(long groupId) throws SystemException {
095                    return mbCategoryPersistence.filterFindByGroupId(groupId);
096            }
097    
098            public List<MBCategory> getCategories(long groupId, int status)
099                    throws SystemException {
100    
101                    return mbCategoryPersistence.filterFindByG_S(groupId, status);
102            }
103    
104            public List<MBCategory> getCategories(
105                            long groupId, long parentCategoryId, int start, int end)
106                    throws SystemException {
107    
108                    return mbCategoryPersistence.filterFindByG_P(
109                            groupId, parentCategoryId, start, end);
110            }
111    
112            public List<MBCategory> getCategories(
113                            long groupId, long parentCategoryId, int status, int start, int end)
114                    throws SystemException {
115    
116                    if (status == WorkflowConstants.STATUS_ANY) {
117                            return mbCategoryPersistence.filterFindByG_P(
118                                    groupId, parentCategoryId, start, end);
119                    }
120    
121                    return mbCategoryPersistence.filterFindByG_P_S(
122                            groupId, parentCategoryId, status, start, end);
123            }
124    
125            public List<MBCategory> getCategories(
126                            long groupId, long[] parentCategoryIds, int start, int end)
127                    throws SystemException {
128    
129                    return mbCategoryPersistence.filterFindByG_P(
130                            groupId, parentCategoryIds, start, end);
131            }
132    
133            public List<MBCategory> getCategories(
134                            long groupId, long[] parentCategoryIds, int status, int start,
135                            int end)
136                    throws SystemException {
137    
138                    if (status == WorkflowConstants.STATUS_ANY) {
139                            return mbCategoryPersistence.filterFindByG_P(
140                                    groupId, parentCategoryIds, start, end);
141                    }
142    
143                    return mbCategoryPersistence.filterFindByG_P_S(
144                            groupId, parentCategoryIds, status, start, end);
145            }
146    
147            public int getCategoriesCount(long groupId, long parentCategoryId)
148                    throws SystemException {
149    
150                    return mbCategoryPersistence.filterCountByG_P(
151                            groupId, parentCategoryId);
152            }
153    
154            public int getCategoriesCount(
155                            long groupId, long parentCategoryId, int status)
156                    throws SystemException {
157    
158                    if (status == WorkflowConstants.STATUS_ANY) {
159                            return mbCategoryPersistence.filterCountByG_P(
160                                    groupId, parentCategoryId);
161                    }
162    
163                    return mbCategoryPersistence.filterCountByG_P_S(
164                            groupId, parentCategoryId, status);
165            }
166    
167            public int getCategoriesCount(long groupId, long[] parentCategoryIds)
168                    throws SystemException {
169    
170                    return mbCategoryPersistence.filterCountByG_P(
171                            groupId, parentCategoryIds);
172            }
173    
174            public int getCategoriesCount(
175                            long groupId, long[] parentCategoryIds, int status)
176                    throws SystemException {
177    
178                    if (status == WorkflowConstants.STATUS_ANY) {
179                            return mbCategoryPersistence.filterCountByG_P(
180                                    groupId, parentCategoryIds);
181                    }
182    
183                    return mbCategoryPersistence.filterCountByG_P_S(
184                            groupId, parentCategoryIds, status);
185            }
186    
187            public MBCategory getCategory(long categoryId)
188                    throws PortalException, SystemException {
189    
190                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
191                            categoryId);
192    
193                    MBCategoryPermission.check(
194                            getPermissionChecker(), category, ActionKeys.VIEW);
195    
196                    return category;
197            }
198    
199            public long[] getCategoryIds(long groupId, long categoryId)
200                    throws SystemException {
201    
202                    List<Long> categoryIds = new ArrayList<Long>();
203    
204                    categoryIds.add(categoryId);
205    
206                    getSubcategoryIds(categoryIds, groupId, categoryId);
207    
208                    return ArrayUtil.toArray(
209                            categoryIds.toArray(new Long[categoryIds.size()]));
210            }
211    
212            public List<Long> getSubcategoryIds(
213                            List<Long> categoryIds, long groupId, long categoryId)
214                    throws SystemException {
215    
216                    List<MBCategory> categories = mbCategoryPersistence.filterFindByG_P(
217                            groupId, categoryId);
218    
219                    for (MBCategory category : categories) {
220                            categoryIds.add(category.getCategoryId());
221    
222                            getSubcategoryIds(
223                                    categoryIds, category.getGroupId(), category.getCategoryId());
224                    }
225    
226                    return categoryIds;
227            }
228    
229            public List<MBCategory> getSubscribedCategories(
230                            long groupId, long userId, int start, int end)
231                    throws SystemException {
232    
233                    long[] categoryIds = getCategoryIds(
234                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
235    
236                    if (categoryIds.length == 0) {
237                            return Collections.emptyList();
238                    }
239                    else {
240                            return mbCategoryFinder.filterFindByS_G_U_P(
241                                    groupId, userId, categoryIds, start, end);
242                    }
243            }
244    
245            public int getSubscribedCategoriesCount(long groupId, long userId)
246                    throws SystemException {
247    
248                    long[] categoryIds = getCategoryIds(
249                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
250    
251                    if (categoryIds.length == 0) {
252                            return 0;
253                    }
254                    else {
255                            return mbCategoryFinder.filterCountByS_G_U_P(
256                                    groupId, userId, categoryIds);
257                    }
258            }
259    
260            public MBCategory moveCategory(
261                            long categoryId, long parentCategoryId,
262                            boolean mergeWithParentCategory)
263                    throws PortalException, SystemException {
264    
265                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
266                            categoryId);
267    
268                    MBCategoryPermission.check(
269                            getPermissionChecker(), category, ActionKeys.UPDATE);
270    
271                    return mbCategoryLocalService.moveCategory(
272                            categoryId, parentCategoryId, mergeWithParentCategory);
273            }
274    
275            public MBCategory moveCategoryFromTrash(long categoryId, long newCategoryId)
276                    throws PortalException, SystemException {
277    
278                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
279                            categoryId);
280    
281                    MBCategoryPermission.check(
282                            getPermissionChecker(), category, ActionKeys.UPDATE);
283    
284                    return mbCategoryLocalService.moveCategoryFromTrash(
285                            getUserId(), categoryId, newCategoryId);
286            }
287    
288            public MBCategory moveCategoryToTrash(long categoryId)
289                    throws PortalException, SystemException {
290    
291                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
292                            categoryId);
293    
294                    MBCategoryPermission.check(
295                            getPermissionChecker(), category, ActionKeys.DELETE);
296    
297                    return mbCategoryLocalService.moveCategoryToTrash(
298                            getUserId(), categoryId);
299            }
300    
301            public void restoreCategoryFromTrash(long categoryId)
302                    throws PortalException, SystemException {
303    
304                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
305                            categoryId);
306    
307                    MBCategoryPermission.check(
308                            getPermissionChecker(), category, ActionKeys.DELETE);
309    
310                    mbCategoryLocalService.restoreCategoryFromTrash(
311                            getUserId(), categoryId);
312            }
313    
314            public void subscribeCategory(long groupId, long categoryId)
315                    throws PortalException, SystemException {
316    
317                    MBCategoryPermission.check(
318                            getPermissionChecker(), groupId, categoryId, ActionKeys.SUBSCRIBE);
319    
320                    mbCategoryLocalService.subscribeCategory(
321                            getUserId(), groupId, categoryId);
322            }
323    
324            public void unsubscribeCategory(long groupId, long categoryId)
325                    throws PortalException, SystemException {
326    
327                    MBCategoryPermission.check(
328                            getPermissionChecker(), groupId, categoryId, ActionKeys.SUBSCRIBE);
329    
330                    mbCategoryLocalService.unsubscribeCategory(
331                            getUserId(), groupId, categoryId);
332            }
333    
334            public MBCategory updateCategory(
335                            long categoryId, long parentCategoryId, String name,
336                            String description, String displayStyle, String emailAddress,
337                            String inProtocol, String inServerName, int inServerPort,
338                            boolean inUseSSL, String inUserName, String inPassword,
339                            int inReadInterval, String outEmailAddress, boolean outCustom,
340                            String outServerName, int outServerPort, boolean outUseSSL,
341                            String outUserName, String outPassword, boolean mailingListActive,
342                            boolean allowAnonymousEmail, boolean mergeWithParentCategory,
343                            ServiceContext serviceContext)
344                    throws PortalException, SystemException {
345    
346                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
347                            categoryId);
348    
349                    MBCategoryPermission.check(
350                            getPermissionChecker(), category, ActionKeys.UPDATE);
351    
352                    return mbCategoryLocalService.updateCategory(
353                            categoryId, parentCategoryId, name, description, displayStyle,
354                            emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
355                            inUserName, inPassword, inReadInterval, outEmailAddress, outCustom,
356                            outServerName, outServerPort, outUseSSL, outUserName, outPassword,
357                            mailingListActive, allowAnonymousEmail, mergeWithParentCategory,
358                            serviceContext);
359            }
360    
361    }