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