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.messageboards.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
018    import com.liferay.portal.kernel.exception.PortalException;
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            @Override
038            public MBCategory addCategory(
039                            long userId, long parentCategoryId, String name, String description,
040                            ServiceContext serviceContext)
041                    throws PortalException {
042    
043                    MBCategoryPermission.check(
044                            getPermissionChecker(), serviceContext.getScopeGroupId(),
045                            parentCategoryId, ActionKeys.ADD_CATEGORY);
046    
047                    return mbCategoryLocalService.addCategory(
048                            userId, parentCategoryId, name, description, serviceContext);
049            }
050    
051            @Override
052            public MBCategory addCategory(
053                            long parentCategoryId, String name, String description,
054                            String displayStyle, String emailAddress, String inProtocol,
055                            String inServerName, int inServerPort, boolean inUseSSL,
056                            String inUserName, String inPassword, int inReadInterval,
057                            String outEmailAddress, boolean outCustom, String outServerName,
058                            int outServerPort, boolean outUseSSL, String outUserName,
059                            String outPassword, boolean mailingListActive,
060                            boolean allowAnonymousEmail, ServiceContext serviceContext)
061                    throws PortalException {
062    
063                    MBCategoryPermission.check(
064                            getPermissionChecker(), serviceContext.getScopeGroupId(),
065                            parentCategoryId, ActionKeys.ADD_CATEGORY);
066    
067                    return mbCategoryLocalService.addCategory(
068                            getUserId(), parentCategoryId, name, description, displayStyle,
069                            emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
070                            inUserName, inPassword, inReadInterval, outEmailAddress, outCustom,
071                            outServerName, outServerPort, outUseSSL, outUserName, outPassword,
072                            mailingListActive, allowAnonymousEmail, serviceContext);
073            }
074    
075            @Override
076            public void deleteCategory(long categoryId, boolean includeTrashedEntries)
077                    throws PortalException {
078    
079                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
080                            categoryId);
081    
082                    MBCategoryPermission.check(
083                            getPermissionChecker(), category, ActionKeys.DELETE);
084    
085                    mbCategoryLocalService.deleteCategory(category, includeTrashedEntries);
086            }
087    
088            @Override
089            public void deleteCategory(long groupId, long categoryId)
090                    throws PortalException {
091    
092                    MBCategoryPermission.check(
093                            getPermissionChecker(), groupId, categoryId, ActionKeys.DELETE);
094    
095                    mbCategoryLocalService.deleteCategory(categoryId);
096            }
097    
098            @Override
099            public List<MBCategory> getCategories(long groupId) {
100                    return mbCategoryPersistence.filterFindByGroupId(groupId);
101            }
102    
103            @Override
104            public List<MBCategory> getCategories(long groupId, int status) {
105                    return mbCategoryPersistence.filterFindByG_S(groupId, status);
106            }
107    
108            @Override
109            public List<MBCategory> getCategories(
110                    long groupId, long parentCategoryId, int start, int end) {
111    
112                    return mbCategoryPersistence.filterFindByG_P(
113                            groupId, parentCategoryId, start, end);
114            }
115    
116            @Override
117            public List<MBCategory> getCategories(
118                    long groupId, long parentCategoryId, int status, int start, int end) {
119    
120                    if (status == WorkflowConstants.STATUS_ANY) {
121                            return mbCategoryPersistence.filterFindByG_P(
122                                    groupId, parentCategoryId, start, end);
123                    }
124    
125                    return mbCategoryPersistence.filterFindByG_P_S(
126                            groupId, parentCategoryId, status, start, end);
127            }
128    
129            @Override
130            public List<MBCategory> getCategories(
131                    long groupId, long excludedCategoryId, long parentCategoryId,
132                    int status, int start, int end) {
133    
134                    if (status == WorkflowConstants.STATUS_ANY) {
135                            return mbCategoryPersistence.filterFindByNotC_G_P(
136                                    excludedCategoryId, groupId, parentCategoryId, start, end);
137                    }
138    
139                    return mbCategoryPersistence.filterFindByNotC_G_P_S(
140                            excludedCategoryId, groupId, parentCategoryId, status, start, end);
141            }
142    
143            @Override
144            public List<MBCategory> getCategories(
145                    long groupId, long[] parentCategoryIds, int start, int end) {
146    
147                    return mbCategoryPersistence.filterFindByG_P(
148                            groupId, parentCategoryIds, start, end);
149            }
150    
151            @Override
152            public List<MBCategory> getCategories(
153                    long groupId, long[] parentCategoryIds, int status, int start,
154                    int end) {
155    
156                    if (status == WorkflowConstants.STATUS_ANY) {
157                            return mbCategoryPersistence.filterFindByG_P(
158                                    groupId, parentCategoryIds, start, end);
159                    }
160    
161                    return mbCategoryPersistence.filterFindByG_P_S(
162                            groupId, parentCategoryIds, status, start, end);
163            }
164    
165            @Override
166            public List<MBCategory> getCategories(
167                    long groupId, long[] excludedCategoryIds, long[] parentCategoryIds,
168                    int status, int start, int end) {
169    
170                    if (status == WorkflowConstants.STATUS_ANY) {
171                            return mbCategoryPersistence.filterFindByNotC_G_P(
172                                    excludedCategoryIds, groupId, parentCategoryIds, start, end);
173                    }
174    
175                    return mbCategoryPersistence.filterFindByNotC_G_P_S(
176                            excludedCategoryIds, groupId, parentCategoryIds, status, start,
177                            end);
178            }
179    
180            @Override
181            public List<Object> getCategoriesAndThreads(long groupId, long categoryId) {
182                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(
183                            WorkflowConstants.STATUS_ANY);
184    
185                    return mbCategoryFinder.filterFindC_T_ByG_C(
186                            groupId, categoryId, queryDefinition);
187            }
188    
189            @Override
190            public List<Object> getCategoriesAndThreads(
191                    long groupId, long categoryId, int status) {
192    
193                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(status);
194    
195                    return mbCategoryFinder.filterFindC_T_ByG_C(
196                            groupId, categoryId, queryDefinition);
197            }
198    
199            @Override
200            public List<Object> getCategoriesAndThreads(
201                    long groupId, long categoryId, int status, int start, int end) {
202    
203                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(
204                            status, start, end, null);
205    
206                    return mbCategoryFinder.filterFindC_T_ByG_C(
207                            groupId, categoryId, queryDefinition);
208            }
209    
210            @Override
211            public int getCategoriesAndThreadsCount(long groupId, long categoryId) {
212                    QueryDefinition<MBCategory> queryDefinition = new QueryDefinition<>(
213                            WorkflowConstants.STATUS_ANY);
214    
215                    return mbCategoryFinder.filterCountC_T_ByG_C(
216                            groupId, categoryId, queryDefinition);
217            }
218    
219            @Override
220            public int getCategoriesAndThreadsCount(
221                    long groupId, long categoryId, int status) {
222    
223                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(status);
224    
225                    return mbCategoryFinder.filterCountC_T_ByG_C(
226                            groupId, categoryId, queryDefinition);
227            }
228    
229            @Override
230            public int getCategoriesCount(long groupId, long parentCategoryId) {
231                    return mbCategoryPersistence.filterCountByG_P(
232                            groupId, parentCategoryId);
233            }
234    
235            @Override
236            public int getCategoriesCount(
237                    long groupId, long parentCategoryId, int status) {
238    
239                    if (status == WorkflowConstants.STATUS_ANY) {
240                            return mbCategoryPersistence.filterCountByG_P(
241                                    groupId, parentCategoryId);
242                    }
243    
244                    return mbCategoryPersistence.filterCountByG_P_S(
245                            groupId, parentCategoryId, status);
246            }
247    
248            @Override
249            public int getCategoriesCount(
250                    long groupId, long excludedCategoryId, long parentCategoryId,
251                    int status) {
252    
253                    if (status == WorkflowConstants.STATUS_ANY) {
254                            return mbCategoryPersistence.filterCountByNotC_G_P(
255                                    excludedCategoryId, groupId, parentCategoryId);
256                    }
257    
258                    return mbCategoryPersistence.filterCountByNotC_G_P_S(
259                            excludedCategoryId, groupId, parentCategoryId, status);
260            }
261    
262            @Override
263            public int getCategoriesCount(long groupId, long[] parentCategoryIds) {
264                    return mbCategoryPersistence.filterCountByG_P(
265                            groupId, parentCategoryIds);
266            }
267    
268            @Override
269            public int getCategoriesCount(
270                    long groupId, long[] parentCategoryIds, int status) {
271    
272                    if (status == WorkflowConstants.STATUS_ANY) {
273                            return mbCategoryPersistence.filterCountByG_P(
274                                    groupId, parentCategoryIds);
275                    }
276    
277                    return mbCategoryPersistence.filterCountByG_P_S(
278                            groupId, parentCategoryIds, status);
279            }
280    
281            @Override
282            public int getCategoriesCount(
283                    long groupId, long[] excludedCategoryIds, long[] parentCategoryIds,
284                    int status) {
285    
286                    if (status == WorkflowConstants.STATUS_ANY) {
287                            return mbCategoryPersistence.filterCountByNotC_G_P(
288                                    excludedCategoryIds, groupId, parentCategoryIds);
289                    }
290    
291                    return mbCategoryPersistence.filterCountByNotC_G_P_S(
292                            excludedCategoryIds, groupId, parentCategoryIds, status);
293            }
294    
295            @Override
296            public MBCategory getCategory(long categoryId) throws PortalException {
297                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
298                            categoryId);
299    
300                    MBCategoryPermission.check(
301                            getPermissionChecker(), category, ActionKeys.VIEW);
302    
303                    return category;
304            }
305    
306            @Override
307            public long[] getCategoryIds(long groupId, long categoryId) {
308                    List<Long> categoryIds = new ArrayList<>();
309    
310                    categoryIds.add(categoryId);
311    
312                    getSubcategoryIds(categoryIds, groupId, categoryId);
313    
314                    return ArrayUtil.toArray(
315                            categoryIds.toArray(new Long[categoryIds.size()]));
316            }
317    
318            @Override
319            public List<Long> getSubcategoryIds(
320                    List<Long> categoryIds, long groupId, long categoryId) {
321    
322                    List<MBCategory> categories = mbCategoryPersistence.filterFindByG_P(
323                            groupId, categoryId);
324    
325                    for (MBCategory category : categories) {
326                            if (category.isInTrash()) {
327                                    continue;
328                            }
329    
330                            categoryIds.add(category.getCategoryId());
331    
332                            getSubcategoryIds(
333                                    categoryIds, category.getGroupId(), category.getCategoryId());
334                    }
335    
336                    return categoryIds;
337            }
338    
339            @Override
340            public List<MBCategory> getSubscribedCategories(
341                    long groupId, long userId, int start, int end) {
342    
343                    long[] categoryIds = getCategoryIds(
344                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
345    
346                    if (categoryIds.length == 0) {
347                            return Collections.emptyList();
348                    }
349    
350                    QueryDefinition<MBCategory> queryDefinition = new QueryDefinition<>(
351                            WorkflowConstants.STATUS_ANY, start, end, null);
352    
353                    return mbCategoryFinder.filterFindC_ByS_G_U_P(
354                            groupId, userId, categoryIds, queryDefinition);
355            }
356    
357            @Override
358            public int getSubscribedCategoriesCount(long groupId, long userId) {
359                    long[] categoryIds = getCategoryIds(
360                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
361    
362                    if (categoryIds.length == 0) {
363                            return 0;
364                    }
365    
366                    QueryDefinition<MBCategory> queryDefinition = new QueryDefinition<>(
367                            WorkflowConstants.STATUS_ANY);
368    
369                    return mbCategoryFinder.filterCountC_ByS_G_U_P(
370                            groupId, userId, categoryIds, queryDefinition);
371            }
372    
373            @Override
374            public MBCategory moveCategory(
375                            long categoryId, long parentCategoryId,
376                            boolean mergeWithParentCategory)
377                    throws PortalException {
378    
379                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
380                            categoryId);
381    
382                    MBCategoryPermission.check(
383                            getPermissionChecker(), category, ActionKeys.UPDATE);
384    
385                    return mbCategoryLocalService.moveCategory(
386                            categoryId, parentCategoryId, mergeWithParentCategory);
387            }
388    
389            @Override
390            public MBCategory moveCategoryFromTrash(long categoryId, long newCategoryId)
391                    throws PortalException {
392    
393                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
394                            categoryId);
395    
396                    MBCategoryPermission.check(
397                            getPermissionChecker(), category, ActionKeys.UPDATE);
398    
399                    return mbCategoryLocalService.moveCategoryFromTrash(
400                            getUserId(), categoryId, newCategoryId);
401            }
402    
403            @Override
404            public MBCategory moveCategoryToTrash(long categoryId)
405                    throws PortalException {
406    
407                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
408                            categoryId);
409    
410                    MBCategoryPermission.check(
411                            getPermissionChecker(), category, ActionKeys.DELETE);
412    
413                    return mbCategoryLocalService.moveCategoryToTrash(
414                            getUserId(), categoryId);
415            }
416    
417            @Override
418            public void restoreCategoryFromTrash(long categoryId)
419                    throws PortalException {
420    
421                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
422                            categoryId);
423    
424                    MBCategoryPermission.check(
425                            getPermissionChecker(), category, ActionKeys.DELETE);
426    
427                    mbCategoryLocalService.restoreCategoryFromTrash(
428                            getUserId(), categoryId);
429            }
430    
431            @Override
432            public void subscribeCategory(long groupId, long categoryId)
433                    throws PortalException {
434    
435                    MBCategoryPermission.check(
436                            getPermissionChecker(), groupId, categoryId, ActionKeys.SUBSCRIBE);
437    
438                    mbCategoryLocalService.subscribeCategory(
439                            getUserId(), groupId, categoryId);
440            }
441    
442            @Override
443            public void unsubscribeCategory(long groupId, long categoryId)
444                    throws PortalException {
445    
446                    MBCategoryPermission.check(
447                            getPermissionChecker(), groupId, categoryId, ActionKeys.SUBSCRIBE);
448    
449                    mbCategoryLocalService.unsubscribeCategory(
450                            getUserId(), groupId, categoryId);
451            }
452    
453            @Override
454            public MBCategory updateCategory(
455                            long categoryId, long parentCategoryId, String name,
456                            String description, String displayStyle, String emailAddress,
457                            String inProtocol, String inServerName, int inServerPort,
458                            boolean inUseSSL, String inUserName, String inPassword,
459                            int inReadInterval, String outEmailAddress, boolean outCustom,
460                            String outServerName, int outServerPort, boolean outUseSSL,
461                            String outUserName, String outPassword, boolean mailingListActive,
462                            boolean allowAnonymousEmail, boolean mergeWithParentCategory,
463                            ServiceContext serviceContext)
464                    throws PortalException {
465    
466                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
467                            categoryId);
468    
469                    MBCategoryPermission.check(
470                            getPermissionChecker(), category, ActionKeys.UPDATE);
471    
472                    return mbCategoryLocalService.updateCategory(
473                            categoryId, parentCategoryId, name, description, displayStyle,
474                            emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
475                            inUserName, inPassword, inReadInterval, outEmailAddress, outCustom,
476                            outServerName, outServerPort, outUseSSL, outUserName, outPassword,
477                            mailingListActive, allowAnonymousEmail, mergeWithParentCategory,
478                            serviceContext);
479            }
480    
481    }