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<Long>();
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 =
302                            new QueryDefinition<MBCategory>(
303                                    WorkflowConstants.STATUS_ANY, start, end, null);
304    
305                    return mbCategoryFinder.filterFindByS_G_U_P(
306                            groupId, userId, categoryIds, queryDefinition);
307            }
308    
309            @Override
310            public int getSubscribedCategoriesCount(long groupId, long userId) {
311                    long[] categoryIds = getCategoryIds(
312                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
313    
314                    if (categoryIds.length == 0) {
315                            return 0;
316                    }
317    
318                    QueryDefinition<MBCategory> queryDefinition =
319                            new QueryDefinition<MBCategory>(WorkflowConstants.STATUS_ANY);
320    
321                    return mbCategoryFinder.filterCountByS_G_U_P(
322                            groupId, userId, categoryIds, queryDefinition);
323            }
324    
325            @Override
326            public MBCategory moveCategory(
327                            long categoryId, long parentCategoryId,
328                            boolean mergeWithParentCategory)
329                    throws PortalException {
330    
331                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
332                            categoryId);
333    
334                    MBCategoryPermission.check(
335                            getPermissionChecker(), category, ActionKeys.UPDATE);
336    
337                    return mbCategoryLocalService.moveCategory(
338                            categoryId, parentCategoryId, mergeWithParentCategory);
339            }
340    
341            @Override
342            public MBCategory moveCategoryFromTrash(long categoryId, long newCategoryId)
343                    throws PortalException {
344    
345                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
346                            categoryId);
347    
348                    MBCategoryPermission.check(
349                            getPermissionChecker(), category, ActionKeys.UPDATE);
350    
351                    return mbCategoryLocalService.moveCategoryFromTrash(
352                            getUserId(), categoryId, newCategoryId);
353            }
354    
355            @Override
356            public MBCategory moveCategoryToTrash(long categoryId)
357                    throws PortalException {
358    
359                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
360                            categoryId);
361    
362                    MBCategoryPermission.check(
363                            getPermissionChecker(), category, ActionKeys.DELETE);
364    
365                    return mbCategoryLocalService.moveCategoryToTrash(
366                            getUserId(), categoryId);
367            }
368    
369            @Override
370            public void restoreCategoryFromTrash(long categoryId)
371                    throws PortalException {
372    
373                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
374                            categoryId);
375    
376                    MBCategoryPermission.check(
377                            getPermissionChecker(), category, ActionKeys.DELETE);
378    
379                    mbCategoryLocalService.restoreCategoryFromTrash(
380                            getUserId(), categoryId);
381            }
382    
383            @Override
384            public void subscribeCategory(long groupId, long categoryId)
385                    throws PortalException {
386    
387                    MBCategoryPermission.check(
388                            getPermissionChecker(), groupId, categoryId, ActionKeys.SUBSCRIBE);
389    
390                    mbCategoryLocalService.subscribeCategory(
391                            getUserId(), groupId, categoryId);
392            }
393    
394            @Override
395            public void unsubscribeCategory(long groupId, long categoryId)
396                    throws PortalException {
397    
398                    MBCategoryPermission.check(
399                            getPermissionChecker(), groupId, categoryId, ActionKeys.SUBSCRIBE);
400    
401                    mbCategoryLocalService.unsubscribeCategory(
402                            getUserId(), groupId, categoryId);
403            }
404    
405            @Override
406            public MBCategory updateCategory(
407                            long categoryId, long parentCategoryId, String name,
408                            String description, String displayStyle, String emailAddress,
409                            String inProtocol, String inServerName, int inServerPort,
410                            boolean inUseSSL, String inUserName, String inPassword,
411                            int inReadInterval, String outEmailAddress, boolean outCustom,
412                            String outServerName, int outServerPort, boolean outUseSSL,
413                            String outUserName, String outPassword, boolean mailingListActive,
414                            boolean allowAnonymousEmail, boolean mergeWithParentCategory,
415                            ServiceContext serviceContext)
416                    throws PortalException {
417    
418                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
419                            categoryId);
420    
421                    MBCategoryPermission.check(
422                            getPermissionChecker(), category, ActionKeys.UPDATE);
423    
424                    return mbCategoryLocalService.updateCategory(
425                            categoryId, parentCategoryId, name, description, displayStyle,
426                            emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
427                            inUserName, inPassword, inReadInterval, outEmailAddress, outCustom,
428                            outServerName, outServerPort, outUseSSL, outUserName, outPassword,
429                            mailingListActive, allowAnonymousEmail, mergeWithParentCategory,
430                            serviceContext);
431            }
432    
433    }