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 int getCategoriesCount(long groupId, long parentCategoryId) {
182                    return mbCategoryPersistence.filterCountByG_P(
183                            groupId, parentCategoryId);
184            }
185    
186            @Override
187            public int getCategoriesCount(
188                    long groupId, long parentCategoryId, int status) {
189    
190                    if (status == WorkflowConstants.STATUS_ANY) {
191                            return mbCategoryPersistence.filterCountByG_P(
192                                    groupId, parentCategoryId);
193                    }
194    
195                    return mbCategoryPersistence.filterCountByG_P_S(
196                            groupId, parentCategoryId, status);
197            }
198    
199            @Override
200            public int getCategoriesCount(
201                    long groupId, long excludedCategoryId, long parentCategoryId,
202                    int status) {
203    
204                    if (status == WorkflowConstants.STATUS_ANY) {
205                            return mbCategoryPersistence.filterCountByNotC_G_P(
206                                    excludedCategoryId, groupId, parentCategoryId);
207                    }
208    
209                    return mbCategoryPersistence.filterCountByNotC_G_P_S(
210                            excludedCategoryId, groupId, parentCategoryId, status);
211            }
212    
213            @Override
214            public int getCategoriesCount(long groupId, long[] parentCategoryIds) {
215                    return mbCategoryPersistence.filterCountByG_P(
216                            groupId, parentCategoryIds);
217            }
218    
219            @Override
220            public int getCategoriesCount(
221                    long groupId, long[] parentCategoryIds, int status) {
222    
223                    if (status == WorkflowConstants.STATUS_ANY) {
224                            return mbCategoryPersistence.filterCountByG_P(
225                                    groupId, parentCategoryIds);
226                    }
227    
228                    return mbCategoryPersistence.filterCountByG_P_S(
229                            groupId, parentCategoryIds, status);
230            }
231    
232            @Override
233            public int getCategoriesCount(
234                    long groupId, long[] excludedCategoryIds, long[] parentCategoryIds,
235                    int status) {
236    
237                    if (status == WorkflowConstants.STATUS_ANY) {
238                            return mbCategoryPersistence.filterCountByNotC_G_P(
239                                    excludedCategoryIds, groupId, parentCategoryIds);
240                    }
241    
242                    return mbCategoryPersistence.filterCountByNotC_G_P_S(
243                            excludedCategoryIds, groupId, parentCategoryIds, status);
244            }
245    
246            @Override
247            public MBCategory getCategory(long categoryId) throws PortalException {
248                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
249                            categoryId);
250    
251                    MBCategoryPermission.check(
252                            getPermissionChecker(), category, ActionKeys.VIEW);
253    
254                    return category;
255            }
256    
257            @Override
258            public long[] getCategoryIds(long groupId, long categoryId) {
259                    List<Long> categoryIds = new ArrayList<>();
260    
261                    categoryIds.add(categoryId);
262    
263                    getSubcategoryIds(categoryIds, groupId, categoryId);
264    
265                    return ArrayUtil.toArray(
266                            categoryIds.toArray(new Long[categoryIds.size()]));
267            }
268    
269            @Override
270            public List<Long> getSubcategoryIds(
271                    List<Long> categoryIds, long groupId, long categoryId) {
272    
273                    List<MBCategory> categories = mbCategoryPersistence.filterFindByG_P(
274                            groupId, categoryId);
275    
276                    for (MBCategory category : categories) {
277                            if (category.isInTrash()) {
278                                    continue;
279                            }
280    
281                            categoryIds.add(category.getCategoryId());
282    
283                            getSubcategoryIds(
284                                    categoryIds, category.getGroupId(), category.getCategoryId());
285                    }
286    
287                    return categoryIds;
288            }
289    
290            @Override
291            public List<MBCategory> getSubscribedCategories(
292                    long groupId, long userId, int start, int end) {
293    
294                    long[] categoryIds = getCategoryIds(
295                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
296    
297                    if (categoryIds.length == 0) {
298                            return Collections.emptyList();
299                    }
300    
301                    QueryDefinition<MBCategory> queryDefinition = new QueryDefinition<>(
302                            WorkflowConstants.STATUS_ANY, start, end, null);
303    
304                    return mbCategoryFinder.filterFindByS_G_U_P(
305                            groupId, userId, categoryIds, queryDefinition);
306            }
307    
308            @Override
309            public int getSubscribedCategoriesCount(long groupId, long userId) {
310                    long[] categoryIds = getCategoryIds(
311                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
312    
313                    if (categoryIds.length == 0) {
314                            return 0;
315                    }
316    
317                    QueryDefinition<MBCategory> queryDefinition = new QueryDefinition<>(
318                            WorkflowConstants.STATUS_ANY);
319    
320                    return mbCategoryFinder.filterCountByS_G_U_P(
321                            groupId, userId, categoryIds, queryDefinition);
322            }
323    
324            @Override
325            public MBCategory moveCategory(
326                            long categoryId, long parentCategoryId,
327                            boolean mergeWithParentCategory)
328                    throws PortalException {
329    
330                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
331                            categoryId);
332    
333                    MBCategoryPermission.check(
334                            getPermissionChecker(), category, ActionKeys.UPDATE);
335    
336                    return mbCategoryLocalService.moveCategory(
337                            categoryId, parentCategoryId, mergeWithParentCategory);
338            }
339    
340            @Override
341            public MBCategory moveCategoryFromTrash(long categoryId, long newCategoryId)
342                    throws PortalException {
343    
344                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
345                            categoryId);
346    
347                    MBCategoryPermission.check(
348                            getPermissionChecker(), category, ActionKeys.UPDATE);
349    
350                    return mbCategoryLocalService.moveCategoryFromTrash(
351                            getUserId(), categoryId, newCategoryId);
352            }
353    
354            @Override
355            public MBCategory moveCategoryToTrash(long categoryId)
356                    throws PortalException {
357    
358                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
359                            categoryId);
360    
361                    MBCategoryPermission.check(
362                            getPermissionChecker(), category, ActionKeys.DELETE);
363    
364                    return mbCategoryLocalService.moveCategoryToTrash(
365                            getUserId(), categoryId);
366            }
367    
368            @Override
369            public void restoreCategoryFromTrash(long categoryId)
370                    throws PortalException {
371    
372                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
373                            categoryId);
374    
375                    MBCategoryPermission.check(
376                            getPermissionChecker(), category, ActionKeys.DELETE);
377    
378                    mbCategoryLocalService.restoreCategoryFromTrash(
379                            getUserId(), categoryId);
380            }
381    
382            @Override
383            public void subscribeCategory(long groupId, long categoryId)
384                    throws PortalException {
385    
386                    MBCategoryPermission.check(
387                            getPermissionChecker(), groupId, categoryId, ActionKeys.SUBSCRIBE);
388    
389                    mbCategoryLocalService.subscribeCategory(
390                            getUserId(), groupId, categoryId);
391            }
392    
393            @Override
394            public void unsubscribeCategory(long groupId, long categoryId)
395                    throws PortalException {
396    
397                    MBCategoryPermission.check(
398                            getPermissionChecker(), groupId, categoryId, ActionKeys.SUBSCRIBE);
399    
400                    mbCategoryLocalService.unsubscribeCategory(
401                            getUserId(), groupId, categoryId);
402            }
403    
404            @Override
405            public MBCategory updateCategory(
406                            long categoryId, long parentCategoryId, String name,
407                            String description, String displayStyle, String emailAddress,
408                            String inProtocol, String inServerName, int inServerPort,
409                            boolean inUseSSL, String inUserName, String inPassword,
410                            int inReadInterval, String outEmailAddress, boolean outCustom,
411                            String outServerName, int outServerPort, boolean outUseSSL,
412                            String outUserName, String outPassword, boolean mailingListActive,
413                            boolean allowAnonymousEmail, boolean mergeWithParentCategory,
414                            ServiceContext serviceContext)
415                    throws PortalException {
416    
417                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
418                            categoryId);
419    
420                    MBCategoryPermission.check(
421                            getPermissionChecker(), category, ActionKeys.UPDATE);
422    
423                    return mbCategoryLocalService.updateCategory(
424                            categoryId, parentCategoryId, name, description, displayStyle,
425                            emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
426                            inUserName, inPassword, inReadInterval, outEmailAddress, outCustom,
427                            outServerName, outServerPort, outUseSSL, outUserName, outPassword,
428                            mailingListActive, allowAnonymousEmail, mergeWithParentCategory,
429                            serviceContext);
430            }
431    
432    }