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()) {
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    
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            @Override
271            public int getSubscribedCategoriesCount(long groupId, long userId)
272                    throws SystemException {
273    
274                    long[] categoryIds = getCategoryIds(
275                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
276    
277                    if (categoryIds.length == 0) {
278                            return 0;
279                    }
280    
281                    QueryDefinition queryDefinition = new QueryDefinition(
282                            WorkflowConstants.STATUS_ANY);
283    
284                    return mbCategoryFinder.filterCountByS_G_U_P(
285                            groupId, userId, categoryIds, queryDefinition);
286            }
287    
288            @Override
289            public MBCategory moveCategory(
290                            long categoryId, long parentCategoryId,
291                            boolean mergeWithParentCategory)
292                    throws PortalException, SystemException {
293    
294                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
295                            categoryId);
296    
297                    MBCategoryPermission.check(
298                            getPermissionChecker(), category, ActionKeys.UPDATE);
299    
300                    return mbCategoryLocalService.moveCategory(
301                            categoryId, parentCategoryId, mergeWithParentCategory);
302            }
303    
304            @Override
305            public MBCategory moveCategoryFromTrash(long categoryId, long newCategoryId)
306                    throws PortalException, SystemException {
307    
308                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
309                            categoryId);
310    
311                    MBCategoryPermission.check(
312                            getPermissionChecker(), category, ActionKeys.UPDATE);
313    
314                    return mbCategoryLocalService.moveCategoryFromTrash(
315                            getUserId(), categoryId, newCategoryId);
316            }
317    
318            @Override
319            public MBCategory moveCategoryToTrash(long categoryId)
320                    throws PortalException, SystemException {
321    
322                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
323                            categoryId);
324    
325                    MBCategoryPermission.check(
326                            getPermissionChecker(), category, ActionKeys.DELETE);
327    
328                    return mbCategoryLocalService.moveCategoryToTrash(
329                            getUserId(), categoryId);
330            }
331    
332            @Override
333            public void restoreCategoryFromTrash(long categoryId)
334                    throws PortalException, SystemException {
335    
336                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
337                            categoryId);
338    
339                    MBCategoryPermission.check(
340                            getPermissionChecker(), category, ActionKeys.DELETE);
341    
342                    mbCategoryLocalService.restoreCategoryFromTrash(
343                            getUserId(), categoryId);
344            }
345    
346            @Override
347            public void subscribeCategory(long groupId, long categoryId)
348                    throws PortalException, SystemException {
349    
350                    MBCategoryPermission.check(
351                            getPermissionChecker(), groupId, categoryId, ActionKeys.SUBSCRIBE);
352    
353                    mbCategoryLocalService.subscribeCategory(
354                            getUserId(), groupId, categoryId);
355            }
356    
357            @Override
358            public void unsubscribeCategory(long groupId, long categoryId)
359                    throws PortalException, SystemException {
360    
361                    MBCategoryPermission.check(
362                            getPermissionChecker(), groupId, categoryId, ActionKeys.SUBSCRIBE);
363    
364                    mbCategoryLocalService.unsubscribeCategory(
365                            getUserId(), groupId, categoryId);
366            }
367    
368            @Override
369            public MBCategory updateCategory(
370                            long categoryId, long parentCategoryId, String name,
371                            String description, String displayStyle, String emailAddress,
372                            String inProtocol, String inServerName, int inServerPort,
373                            boolean inUseSSL, String inUserName, String inPassword,
374                            int inReadInterval, String outEmailAddress, boolean outCustom,
375                            String outServerName, int outServerPort, boolean outUseSSL,
376                            String outUserName, String outPassword, boolean mailingListActive,
377                            boolean allowAnonymousEmail, boolean mergeWithParentCategory,
378                            ServiceContext serviceContext)
379                    throws PortalException, SystemException {
380    
381                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
382                            categoryId);
383    
384                    MBCategoryPermission.check(
385                            getPermissionChecker(), category, ActionKeys.UPDATE);
386    
387                    return mbCategoryLocalService.updateCategory(
388                            categoryId, parentCategoryId, name, description, displayStyle,
389                            emailAddress, inProtocol, inServerName, inServerPort, inUseSSL,
390                            inUserName, inPassword, inReadInterval, outEmailAddress, outCustom,
391                            outServerName, outServerPort, outUseSSL, outUserName, outPassword,
392                            mailingListActive, allowAnonymousEmail, mergeWithParentCategory,
393                            serviceContext);
394            }
395    
396    }