001    /**
002     * Copyright (c) 2000-2012 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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.search.Indexer;
020    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.ResourceConstants;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portlet.expando.model.ExpandoBridge;
026    import com.liferay.portlet.messageboards.CategoryNameException;
027    import com.liferay.portlet.messageboards.NoSuchMailingListException;
028    import com.liferay.portlet.messageboards.model.MBCategory;
029    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
030    import com.liferay.portlet.messageboards.model.MBMailingList;
031    import com.liferay.portlet.messageboards.model.MBMessage;
032    import com.liferay.portlet.messageboards.model.MBThread;
033    import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
034    import com.liferay.portlet.messageboards.service.base.MBCategoryLocalServiceBaseImpl;
035    
036    import java.util.ArrayList;
037    import java.util.Date;
038    import java.util.List;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Wesley Gong
043     */
044    public class MBCategoryLocalServiceImpl extends MBCategoryLocalServiceBaseImpl {
045    
046            public MBCategory addCategory(
047                            long userId, long parentCategoryId, String name, String description,
048                            String displayStyle, String emailAddress, String inProtocol,
049                            String inServerName, int inServerPort, boolean inUseSSL,
050                            String inUserName, String inPassword, int inReadInterval,
051                            String outEmailAddress, boolean outCustom, String outServerName,
052                            int outServerPort, boolean outUseSSL, String outUserName,
053                            String outPassword, boolean allowAnonymous,
054                            boolean mailingListActive, ServiceContext serviceContext)
055                    throws PortalException, SystemException {
056    
057                    // Category
058    
059                    User user = userPersistence.findByPrimaryKey(userId);
060                    long groupId = serviceContext.getScopeGroupId();
061                    parentCategoryId = getParentCategoryId(groupId, parentCategoryId);
062                    Date now = new Date();
063    
064                    validate(name);
065    
066                    long categoryId = counterLocalService.increment();
067    
068                    MBCategory category = mbCategoryPersistence.create(categoryId);
069    
070                    category.setUuid(serviceContext.getUuid());
071                    category.setGroupId(groupId);
072                    category.setCompanyId(user.getCompanyId());
073                    category.setUserId(user.getUserId());
074                    category.setUserName(user.getFullName());
075                    category.setCreateDate(serviceContext.getCreateDate(now));
076                    category.setModifiedDate(serviceContext.getModifiedDate(now));
077                    category.setParentCategoryId(parentCategoryId);
078                    category.setName(name);
079                    category.setDescription(description);
080                    category.setDisplayStyle(displayStyle);
081    
082                    mbCategoryPersistence.update(category, false);
083    
084                    // Resources
085    
086                    if (serviceContext.isAddGroupPermissions() ||
087                            serviceContext.isAddGuestPermissions()) {
088    
089                            addCategoryResources(
090                                    category, serviceContext.isAddGroupPermissions(),
091                                    serviceContext.isAddGuestPermissions());
092                    }
093                    else {
094                            addCategoryResources(
095                                    category, serviceContext.getGroupPermissions(),
096                                    serviceContext.getGuestPermissions());
097                    }
098    
099                    // Mailing list
100    
101                    mbMailingListLocalService.addMailingList(
102                            userId, groupId, category.getCategoryId(), emailAddress, inProtocol,
103                            inServerName, inServerPort, inUseSSL, inUserName, inPassword,
104                            inReadInterval, outEmailAddress, outCustom, outServerName,
105                            outServerPort, outUseSSL, outUserName, outPassword, allowAnonymous,
106                            mailingListActive, serviceContext);
107    
108                    // Expando
109    
110                    ExpandoBridge expandoBridge = category.getExpandoBridge();
111    
112                    expandoBridge.setAttributes(serviceContext);
113    
114                    return category;
115            }
116    
117            public void addCategoryResources(
118                            long categoryId, boolean addGroupPermissions,
119                            boolean addGuestPermissions)
120                    throws PortalException, SystemException {
121    
122                    if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
123                            (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
124    
125                            return;
126                    }
127    
128                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
129                            categoryId);
130    
131                    addCategoryResources(
132                            category, addGroupPermissions, addGuestPermissions);
133            }
134    
135            public void addCategoryResources(
136                            long categoryId, String[] groupPermissions,
137                            String[] guestPermissions)
138                    throws PortalException, SystemException {
139    
140                    if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
141                            (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
142    
143                            return;
144                    }
145    
146                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
147                            categoryId);
148    
149                    addCategoryResources(category, groupPermissions, guestPermissions);
150            }
151    
152            public void addCategoryResources(
153                            MBCategory category, boolean addGroupPermissions,
154                            boolean addGuestPermissions)
155                    throws PortalException, SystemException {
156    
157                    resourceLocalService.addResources(
158                            category.getCompanyId(), category.getGroupId(),
159                            category.getUserId(), MBCategory.class.getName(),
160                            category.getCategoryId(), false, addGroupPermissions,
161                            addGuestPermissions);
162            }
163    
164            public void addCategoryResources(
165                            MBCategory category, String[] groupPermissions,
166                            String[] guestPermissions)
167                    throws PortalException, SystemException {
168    
169                    resourceLocalService.addModelResources(
170                            category.getCompanyId(), category.getGroupId(),
171                            category.getUserId(), MBCategory.class.getName(),
172                            category.getCategoryId(), groupPermissions, guestPermissions);
173            }
174    
175            public void deleteCategories(long groupId)
176                    throws PortalException, SystemException {
177    
178                    List<MBCategory> categories = mbCategoryPersistence.findByG_P(
179                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
180    
181                    for (MBCategory category : categories) {
182                            deleteCategory(category);
183                    }
184            }
185    
186            public void deleteCategory(long categoryId)
187                    throws PortalException, SystemException {
188    
189                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
190                            categoryId);
191    
192                    deleteCategory(category);
193            }
194    
195            public void deleteCategory(MBCategory category)
196                    throws PortalException, SystemException {
197    
198                    // Categories
199    
200                    List<MBCategory> categories = mbCategoryPersistence.findByG_P(
201                            category.getGroupId(), category.getCategoryId());
202    
203                    for (MBCategory curCategory : categories) {
204                            deleteCategory(curCategory);
205                    }
206    
207                    // Indexer
208    
209                    Indexer indexer = IndexerRegistryUtil.getIndexer(MBMessage.class);
210    
211                    indexer.delete(category);
212    
213                    // Threads
214    
215                    mbThreadLocalService.deleteThreads(
216                            category.getGroupId(), category.getCategoryId());
217    
218                    // Mailing list
219    
220                    try {
221                            mbMailingListLocalService.deleteCategoryMailingList(
222                                    category.getGroupId(), category.getCategoryId());
223                    }
224                    catch (NoSuchMailingListException nsmle) {
225                    }
226    
227                    // Subscriptions
228    
229                    subscriptionLocalService.deleteSubscriptions(
230                            category.getCompanyId(), MBCategory.class.getName(),
231                            category.getCategoryId());
232    
233                    // Expando
234    
235                    expandoValueLocalService.deleteValues(
236                            MBCategory.class.getName(), category.getCategoryId());
237    
238                    // Resources
239    
240                    resourceLocalService.deleteResource(
241                            category.getCompanyId(), MBCategory.class.getName(),
242                            ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
243    
244                    // Category
245    
246                    mbCategoryPersistence.remove(category);
247            }
248    
249            public List<MBCategory> getCategories(long groupId) throws SystemException {
250                    return mbCategoryPersistence.findByGroupId(groupId);
251            }
252    
253            public List<MBCategory> getCategories(
254                            long groupId, long parentCategoryId, int start, int end)
255                    throws SystemException {
256    
257                    return mbCategoryPersistence.findByG_P(
258                            groupId, parentCategoryId, start, end);
259            }
260    
261            public List<MBCategory> getCategories(
262                            long groupId, long[] parentCategoryIds, int start, int end)
263                    throws SystemException {
264    
265                    return mbCategoryPersistence.findByG_P(
266                            groupId, parentCategoryIds, start, end);
267            }
268    
269            public int getCategoriesCount(long groupId) throws SystemException {
270                    return mbCategoryPersistence.countByGroupId(groupId);
271            }
272    
273            public int getCategoriesCount(long groupId, long parentCategoryId)
274                    throws SystemException {
275    
276                    return mbCategoryPersistence.countByG_P(groupId, parentCategoryId);
277            }
278    
279            public int getCategoriesCount(long groupId, long[] parentCategoryIds)
280                    throws SystemException {
281    
282                    return mbCategoryPersistence.countByG_P(groupId, parentCategoryIds);
283            }
284    
285            public MBCategory getCategory(long categoryId)
286                    throws PortalException, SystemException {
287    
288                    MBCategory category = null;
289    
290                    if ((categoryId != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
291                            (categoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
292    
293                            category = mbCategoryPersistence.findByPrimaryKey(categoryId);
294                    }
295                    else {
296                            category = new MBCategoryImpl();
297    
298                            category.setCategoryId(categoryId);
299                            category.setParentCategoryId(categoryId);
300                    }
301    
302                    return category;
303            }
304    
305            public List<MBCategory> getCompanyCategories(
306                            long companyId, int start, int end)
307                    throws SystemException {
308    
309                    return mbCategoryPersistence.findByCompanyId(companyId, start, end);
310            }
311    
312            public int getCompanyCategoriesCount(long companyId)
313                    throws SystemException {
314    
315                    return mbCategoryPersistence.countByCompanyId(companyId);
316            }
317    
318            public List<Long> getSubcategoryIds(
319                            List<Long> categoryIds, long groupId, long categoryId)
320                    throws SystemException {
321    
322                    List<MBCategory> categories = mbCategoryPersistence.findByG_P(
323                            groupId, categoryId);
324    
325                    for (MBCategory category : categories) {
326                            categoryIds.add(category.getCategoryId());
327    
328                            getSubcategoryIds(
329                                    categoryIds, category.getGroupId(), category.getCategoryId());
330                    }
331    
332                    return categoryIds;
333            }
334    
335            public List<MBCategory> getSubscribedCategories(
336                            long groupId, long userId, int start, int end)
337                    throws SystemException {
338    
339                    return mbCategoryFinder.findByS_G_U_P(
340                            groupId, userId, null, start, end);
341            }
342    
343            public int getSubscribedCategoriesCount(long groupId, long userId)
344                    throws SystemException {
345    
346                    return mbCategoryFinder.countByS_G_U_P(groupId, userId, null);
347            }
348    
349            public void subscribeCategory(long userId, long groupId, long categoryId)
350                    throws PortalException, SystemException {
351    
352                    if (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
353                            categoryId = groupId;
354                    }
355    
356                    subscriptionLocalService.addSubscription(
357                            userId, groupId, MBCategory.class.getName(), categoryId);
358            }
359    
360            public void unsubscribeCategory(long userId, long groupId, long categoryId)
361                    throws PortalException, SystemException {
362    
363                    if (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
364                            categoryId = groupId;
365                    }
366    
367                    subscriptionLocalService.deleteSubscription(
368                            userId, MBCategory.class.getName(), categoryId);
369            }
370    
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 allowAnonymous,
379                            boolean mailingListActive, boolean mergeWithParentCategory,
380                            ServiceContext serviceContext)
381                    throws PortalException, SystemException {
382    
383                    // Merge categories
384    
385                    if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
386                            (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
387    
388                            return null;
389                    }
390    
391                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
392                            categoryId);
393    
394                    parentCategoryId = getParentCategoryId(category, parentCategoryId);
395    
396                    if (mergeWithParentCategory &&
397                            (categoryId != parentCategoryId) &&
398                            (parentCategoryId !=
399                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
400                            (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
401    
402                            mergeCategories(category, parentCategoryId);
403    
404                            return category;
405                    }
406    
407                    // Category
408    
409                    validate(name);
410    
411                    category.setModifiedDate(serviceContext.getModifiedDate(null));
412                    category.setParentCategoryId(parentCategoryId);
413                    category.setName(name);
414                    category.setDescription(description);
415                    category.setDisplayStyle(displayStyle);
416    
417                    mbCategoryPersistence.update(category, false);
418    
419                    // Mailing list
420    
421                    MBMailingList mailingList = mbMailingListPersistence.fetchByG_C(
422                            category.getGroupId(), category.getCategoryId());
423    
424                    if (mailingList != null) {
425                            mbMailingListLocalService.updateMailingList(
426                                    mailingList.getMailingListId(), emailAddress, inProtocol,
427                                    inServerName, inServerPort, inUseSSL, inUserName, inPassword,
428                                    inReadInterval, outEmailAddress, outCustom, outServerName,
429                                    outServerPort, outUseSSL, outUserName, outPassword,
430                                    allowAnonymous, mailingListActive, serviceContext);
431                    }
432                    else {
433                            mbMailingListLocalService.addMailingList(
434                                    category.getUserId(), category.getGroupId(),
435                                    category.getCategoryId(), emailAddress, inProtocol,
436                                    inServerName, inServerPort, inUseSSL, inUserName, inPassword,
437                                    inReadInterval, outEmailAddress, outCustom, outServerName,
438                                    outServerPort, outUseSSL, outUserName, outPassword,
439                                    allowAnonymous, mailingListActive, serviceContext);
440                    }
441    
442                    // Expando
443    
444                    ExpandoBridge expandoBridge = category.getExpandoBridge();
445    
446                    expandoBridge.setAttributes(serviceContext);
447    
448                    return category;
449            }
450    
451            protected long getParentCategoryId(long groupId, long parentCategoryId)
452                    throws SystemException {
453    
454                    if ((parentCategoryId !=
455                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
456                            (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
457    
458                            MBCategory parentCategory = mbCategoryPersistence.fetchByPrimaryKey(
459                                    parentCategoryId);
460    
461                            if ((parentCategory == null) ||
462                                    (groupId != parentCategory.getGroupId())) {
463    
464                                    parentCategoryId =
465                                            MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
466                            }
467                    }
468    
469                    return parentCategoryId;
470            }
471    
472            protected long getParentCategoryId(
473                            MBCategory category, long parentCategoryId)
474                    throws SystemException {
475    
476                    if ((parentCategoryId ==
477                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
478                            (parentCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
479    
480                            return parentCategoryId;
481                    }
482    
483                    if (category.getCategoryId() == parentCategoryId) {
484                            return category.getParentCategoryId();
485                    }
486                    else {
487                            MBCategory parentCategory = mbCategoryPersistence.fetchByPrimaryKey(
488                                    parentCategoryId);
489    
490                            if ((parentCategory == null) ||
491                                    (category.getGroupId() != parentCategory.getGroupId())) {
492    
493                                    return category.getParentCategoryId();
494                            }
495    
496                            List<Long> subcategoryIds = new ArrayList<Long>();
497    
498                            getSubcategoryIds(
499                                    subcategoryIds, category.getGroupId(),
500                                    category.getCategoryId());
501    
502                            if (subcategoryIds.contains(parentCategoryId)) {
503                                    return category.getParentCategoryId();
504                            }
505    
506                            return parentCategoryId;
507                    }
508            }
509    
510            protected void mergeCategories(MBCategory fromCategory, long toCategoryId)
511                    throws PortalException, SystemException {
512    
513                    if ((toCategoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
514                            (toCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
515    
516                            return;
517                    }
518    
519                    List<MBCategory> categories = mbCategoryPersistence.findByG_P(
520                            fromCategory.getGroupId(), fromCategory.getCategoryId());
521    
522                    for (MBCategory category : categories) {
523                            mergeCategories(category, toCategoryId);
524                    }
525    
526                    List<MBThread> threads = mbThreadPersistence.findByG_C(
527                            fromCategory.getGroupId(), fromCategory.getCategoryId());
528    
529                    for (MBThread thread : threads) {
530    
531                            // Thread
532    
533                            thread.setCategoryId(toCategoryId);
534    
535                            mbThreadPersistence.update(thread, false);
536    
537                            List<MBMessage> messages = mbMessagePersistence.findByThreadId(
538                                    thread.getThreadId());
539    
540                            for (MBMessage message : messages) {
541    
542                                    // Message
543    
544                                    message.setCategoryId(toCategoryId);
545    
546                                    mbMessagePersistence.update(message, false);
547    
548                                    // Indexer
549    
550                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
551                                            MBMessage.class);
552    
553                                    indexer.reindex(message);
554                            }
555                    }
556    
557                    MBCategory toCategory = mbCategoryPersistence.findByPrimaryKey(
558                            toCategoryId);
559    
560                    toCategory.setThreadCount(
561                            fromCategory.getThreadCount() + toCategory.getThreadCount());
562                    toCategory.setMessageCount(
563                            fromCategory.getMessageCount() + toCategory.getMessageCount());
564    
565                    mbCategoryPersistence.update(toCategory, false);
566    
567                    deleteCategory(fromCategory);
568            }
569    
570            protected void validate(String name) throws PortalException {
571                    if (Validator.isNull(name)) {
572                            throw new CategoryNameException();
573                    }
574            }
575    
576    }