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