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.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.search.Indexer;
021    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portal.model.ResourceConstants;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portlet.expando.model.ExpandoBridge;
028    import com.liferay.portlet.messageboards.CategoryNameException;
029    import com.liferay.portlet.messageboards.NoSuchMailingListException;
030    import com.liferay.portlet.messageboards.model.MBCategory;
031    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
032    import com.liferay.portlet.messageboards.model.MBMailingList;
033    import com.liferay.portlet.messageboards.model.MBMessage;
034    import com.liferay.portlet.messageboards.model.MBThread;
035    import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
036    import com.liferay.portlet.messageboards.service.base.MBCategoryLocalServiceBaseImpl;
037    import com.liferay.portlet.trash.model.TrashEntry;
038    
039    import java.util.ArrayList;
040    import java.util.Date;
041    import java.util.List;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     * @author Wesley Gong
046     */
047    public class MBCategoryLocalServiceImpl extends MBCategoryLocalServiceBaseImpl {
048    
049            public MBCategory addCategory(
050                            long userId, long parentCategoryId, String name, String description,
051                            ServiceContext serviceContext)
052                    throws PortalException, SystemException {
053    
054                    return addCategory(
055                            userId, parentCategoryId, name, description,
056                            MBCategoryConstants.DEFAULT_DISPLAY_STYLE, null, null, null, 0,
057                            false, null, null, 0, null, false, null, 0, false, null, null,
058                            false, false, serviceContext);
059            }
060    
061            public MBCategory addCategory(
062                            long userId, long parentCategoryId, String name, String description,
063                            String displayStyle, String emailAddress, String inProtocol,
064                            String inServerName, int inServerPort, boolean inUseSSL,
065                            String inUserName, String inPassword, int inReadInterval,
066                            String outEmailAddress, boolean outCustom, String outServerName,
067                            int outServerPort, boolean outUseSSL, String outUserName,
068                            String outPassword, boolean allowAnonymous,
069                            boolean mailingListActive, ServiceContext serviceContext)
070                    throws PortalException, SystemException {
071    
072                    // Category
073    
074                    User user = userPersistence.findByPrimaryKey(userId);
075                    long groupId = serviceContext.getScopeGroupId();
076                    parentCategoryId = getParentCategoryId(groupId, parentCategoryId);
077                    Date now = new Date();
078    
079                    validate(name);
080    
081                    long categoryId = counterLocalService.increment();
082    
083                    MBCategory category = mbCategoryPersistence.create(categoryId);
084    
085                    category.setUuid(serviceContext.getUuid());
086                    category.setGroupId(groupId);
087                    category.setCompanyId(user.getCompanyId());
088                    category.setUserId(user.getUserId());
089                    category.setUserName(user.getFullName());
090                    category.setCreateDate(serviceContext.getCreateDate(now));
091                    category.setModifiedDate(serviceContext.getModifiedDate(now));
092                    category.setParentCategoryId(parentCategoryId);
093                    category.setName(name);
094                    category.setDescription(description);
095                    category.setDisplayStyle(displayStyle);
096    
097                    mbCategoryPersistence.update(category);
098    
099                    // Resources
100    
101                    if (serviceContext.isAddGroupPermissions() ||
102                            serviceContext.isAddGuestPermissions()) {
103    
104                            addCategoryResources(
105                                    category, serviceContext.isAddGroupPermissions(),
106                                    serviceContext.isAddGuestPermissions());
107                    }
108                    else {
109                            addCategoryResources(
110                                    category, serviceContext.getGroupPermissions(),
111                                    serviceContext.getGuestPermissions());
112                    }
113    
114                    // Mailing list
115    
116                    mbMailingListLocalService.addMailingList(
117                            userId, groupId, category.getCategoryId(), emailAddress, inProtocol,
118                            inServerName, inServerPort, inUseSSL, inUserName, inPassword,
119                            inReadInterval, outEmailAddress, outCustom, outServerName,
120                            outServerPort, outUseSSL, outUserName, outPassword, allowAnonymous,
121                            mailingListActive, serviceContext);
122    
123                    // Expando
124    
125                    ExpandoBridge expandoBridge = category.getExpandoBridge();
126    
127                    expandoBridge.setAttributes(serviceContext);
128    
129                    return category;
130            }
131    
132            public void addCategoryResources(
133                            long categoryId, boolean addGroupPermissions,
134                            boolean addGuestPermissions)
135                    throws PortalException, SystemException {
136    
137                    if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
138                            (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
139    
140                            return;
141                    }
142    
143                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
144                            categoryId);
145    
146                    addCategoryResources(
147                            category, addGroupPermissions, addGuestPermissions);
148            }
149    
150            public void addCategoryResources(
151                            long categoryId, String[] groupPermissions,
152                            String[] guestPermissions)
153                    throws PortalException, SystemException {
154    
155                    if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
156                            (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
157    
158                            return;
159                    }
160    
161                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
162                            categoryId);
163    
164                    addCategoryResources(category, groupPermissions, guestPermissions);
165            }
166    
167            public void addCategoryResources(
168                            MBCategory category, boolean addGroupPermissions,
169                            boolean addGuestPermissions)
170                    throws PortalException, SystemException {
171    
172                    resourceLocalService.addResources(
173                            category.getCompanyId(), category.getGroupId(),
174                            category.getUserId(), MBCategory.class.getName(),
175                            category.getCategoryId(), false, addGroupPermissions,
176                            addGuestPermissions);
177            }
178    
179            public void addCategoryResources(
180                            MBCategory category, String[] groupPermissions,
181                            String[] guestPermissions)
182                    throws PortalException, SystemException {
183    
184                    resourceLocalService.addModelResources(
185                            category.getCompanyId(), category.getGroupId(),
186                            category.getUserId(), MBCategory.class.getName(),
187                            category.getCategoryId(), groupPermissions, guestPermissions);
188            }
189    
190            public void deleteCategories(long groupId)
191                    throws PortalException, SystemException {
192    
193                    List<MBCategory> categories = mbCategoryPersistence.findByG_P(
194                            groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
195    
196                    for (MBCategory category : categories) {
197                            deleteCategory(category);
198                    }
199            }
200    
201            public void deleteCategory(long categoryId)
202                    throws PortalException, SystemException {
203    
204                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
205                            categoryId);
206    
207                    deleteCategory(category);
208            }
209    
210            public void deleteCategory(MBCategory category)
211                    throws PortalException, SystemException {
212    
213                    deleteCategory(category, true);
214            }
215    
216            public void deleteCategory(
217                            MBCategory category, boolean includeTrashedEntries)
218                    throws PortalException, SystemException {
219    
220                    // Categories
221    
222                    List<MBCategory> categories = mbCategoryPersistence.findByG_P(
223                            category.getGroupId(), category.getCategoryId());
224    
225                    for (MBCategory curCategory : categories) {
226                            if (includeTrashedEntries || !curCategory.isInTrash()) {
227                                    deleteCategory(curCategory, includeTrashedEntries);
228                            }
229                    }
230    
231                    // Indexer
232    
233                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
234                            MBMessage.class);
235    
236                    indexer.delete(category);
237    
238                    // Threads
239    
240                    mbThreadLocalService.deleteThreads(
241                            category.getGroupId(), category.getCategoryId(),
242                            includeTrashedEntries);
243    
244                    // Mailing list
245    
246                    try {
247                            mbMailingListLocalService.deleteCategoryMailingList(
248                                    category.getGroupId(), category.getCategoryId());
249                    }
250                    catch (NoSuchMailingListException nsmle) {
251                    }
252    
253                    // Subscriptions
254    
255                    subscriptionLocalService.deleteSubscriptions(
256                            category.getCompanyId(), MBCategory.class.getName(),
257                            category.getCategoryId());
258    
259                    // Expando
260    
261                    expandoValueLocalService.deleteValues(
262                            MBCategory.class.getName(), category.getCategoryId());
263    
264                    // Resources
265    
266                    resourceLocalService.deleteResource(
267                            category.getCompanyId(), MBCategory.class.getName(),
268                            ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
269    
270                    // Trash
271    
272                    trashEntryLocalService.deleteEntry(
273                            MBCategory.class.getName(), category.getCategoryId());
274    
275                    // Category
276    
277                    mbCategoryPersistence.remove(category);
278            }
279    
280            public List<MBCategory> getCategories(long groupId) throws SystemException {
281                    return mbCategoryPersistence.findByGroupId(groupId);
282            }
283    
284            public List<MBCategory> getCategories(long groupId, int status)
285                    throws SystemException {
286    
287                    return mbCategoryPersistence.findByG_S(groupId, status);
288            }
289    
290            public List<MBCategory> getCategories(
291                            long groupId, long parentCategoryId, int start, int end)
292                    throws SystemException {
293    
294                    return mbCategoryPersistence.findByG_P(
295                            groupId, parentCategoryId, start, end);
296            }
297    
298            public List<MBCategory> getCategories(
299                            long groupId, long parentCategoryId, int status, int start, int end)
300                    throws SystemException {
301    
302                    if (status == WorkflowConstants.STATUS_ANY) {
303                            return mbCategoryPersistence.findByG_P(
304                                    groupId, parentCategoryId, start, end);
305                    }
306    
307                    return mbCategoryPersistence.findByG_P_S(
308                            groupId, parentCategoryId, status, start, end);
309            }
310    
311            public List<MBCategory> getCategories(
312                            long groupId, long[] parentCategoryIds, int start, int end)
313                    throws SystemException {
314    
315                    return mbCategoryPersistence.findByG_P(
316                            groupId, parentCategoryIds, start, end);
317            }
318    
319            public List<MBCategory> getCategories(
320                            long groupId, long[] parentCategoryIds, int status, int start,
321                            int end)
322                    throws SystemException {
323    
324                    if (status == WorkflowConstants.STATUS_ANY) {
325                            return mbCategoryPersistence.findByG_P(
326                                    groupId, parentCategoryIds, start, end);
327                    }
328    
329                    return mbCategoryPersistence.findByG_P_S(
330                            groupId, parentCategoryIds, status, start, end);
331            }
332    
333            public List<Object> getCategoriesAndThreads(long groupId, long categoryId)
334                    throws SystemException {
335    
336                    List<Object> categoriesAndThreads = new ArrayList<Object>();
337    
338                    List<MBCategory> categories = getCategories(
339                            groupId, categoryId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
340    
341                    categoriesAndThreads.addAll(categories);
342    
343                    List<MBThread> threads = mbThreadLocalService.getThreads(
344                            groupId, categoryId, WorkflowConstants.STATUS_ANY,
345                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
346    
347                    categoriesAndThreads.addAll(threads);
348    
349                    return categoriesAndThreads;
350            }
351    
352            public int getCategoriesCount(long groupId) throws SystemException {
353                    return mbCategoryPersistence.countByGroupId(groupId);
354            }
355    
356            public int getCategoriesCount(long groupId, int status)
357                    throws SystemException {
358    
359                    return mbCategoryPersistence.countByG_S(groupId, status);
360            }
361    
362            public int getCategoriesCount(long groupId, long parentCategoryId)
363                    throws SystemException {
364    
365                    return mbCategoryPersistence.countByG_P(groupId, parentCategoryId);
366            }
367    
368            public int getCategoriesCount(
369                            long groupId, long parentCategoryId, int status)
370                    throws SystemException {
371    
372                    if (status == WorkflowConstants.STATUS_ANY) {
373                            return mbCategoryPersistence.countByG_P(groupId, parentCategoryId);
374                    }
375    
376                    return mbCategoryPersistence.countByG_P_S(
377                            groupId, parentCategoryId, status);
378            }
379    
380            public int getCategoriesCount(long groupId, long[] parentCategoryIds)
381                    throws SystemException {
382    
383                    return mbCategoryPersistence.countByG_P(groupId, parentCategoryIds);
384            }
385    
386            public int getCategoriesCount(
387                            long groupId, long[] parentCategoryIds, int status)
388                    throws SystemException {
389    
390                    if (status == WorkflowConstants.STATUS_ANY) {
391                            return mbCategoryPersistence.countByG_P(groupId, parentCategoryIds);
392                    }
393    
394                    return mbCategoryPersistence.countByG_P_S(
395                            groupId, parentCategoryIds, status);
396            }
397    
398            public MBCategory getCategory(long categoryId)
399                    throws PortalException, SystemException {
400    
401                    MBCategory category = null;
402    
403                    if ((categoryId != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
404                            (categoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
405    
406                            category = mbCategoryPersistence.findByPrimaryKey(categoryId);
407                    }
408                    else {
409                            category = new MBCategoryImpl();
410    
411                            category.setCategoryId(categoryId);
412                            category.setParentCategoryId(categoryId);
413                    }
414    
415                    return category;
416            }
417    
418            public List<MBCategory> getCompanyCategories(
419                            long companyId, int start, int end)
420                    throws SystemException {
421    
422                    return mbCategoryPersistence.findByCompanyId(companyId, start, end);
423            }
424    
425            public int getCompanyCategoriesCount(long companyId)
426                    throws SystemException {
427    
428                    return mbCategoryPersistence.countByCompanyId(companyId);
429            }
430    
431            public List<Long> getSubcategoryIds(
432                            List<Long> categoryIds, long groupId, long categoryId)
433                    throws SystemException {
434    
435                    List<MBCategory> categories = mbCategoryPersistence.findByG_P(
436                            groupId, categoryId);
437    
438                    for (MBCategory category : categories) {
439                            categoryIds.add(category.getCategoryId());
440    
441                            getSubcategoryIds(
442                                    categoryIds, category.getGroupId(), category.getCategoryId());
443                    }
444    
445                    return categoryIds;
446            }
447    
448            public List<MBCategory> getSubscribedCategories(
449                            long groupId, long userId, int start, int end)
450                    throws SystemException {
451    
452                    return mbCategoryFinder.findByS_G_U_P(
453                            groupId, userId, null, start, end);
454            }
455    
456            public int getSubscribedCategoriesCount(long groupId, long userId)
457                    throws SystemException {
458    
459                    return mbCategoryFinder.countByS_G_U_P(groupId, userId, null);
460            }
461    
462            public void moveCategoriesToTrash(long groupId, long userId)
463                    throws PortalException, SystemException {
464    
465                    List<MBCategory> categories = mbCategoryPersistence.findByGroupId(
466                            groupId);
467    
468                    for (MBCategory category : categories) {
469                            moveCategoryToTrash(userId, category.getCategoryId());
470                    }
471            }
472    
473            public MBCategory moveCategory(
474                            long categoryId, long parentCategoryId,
475                            boolean mergeWithParentCategory)
476                    throws PortalException, SystemException {
477    
478                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
479                            categoryId);
480    
481                    parentCategoryId = getParentCategoryId(category, parentCategoryId);
482    
483                    if (mergeWithParentCategory &&
484                            (categoryId != parentCategoryId) &&
485                            (parentCategoryId !=
486                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
487                            (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
488    
489                            mergeCategories(category, parentCategoryId);
490    
491                            return category;
492                    }
493    
494                    category.setParentCategoryId(parentCategoryId);
495    
496                    return mbCategoryPersistence.update(category);
497            }
498    
499            public MBCategory moveCategoryFromTrash(
500                            long userId, long categoryId, long newCategoryId)
501                    throws PortalException, SystemException {
502    
503                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
504                            categoryId);
505    
506                    if (category.isInTrash()) {
507                            restoreCategoryFromTrash(userId, categoryId);
508                    }
509                    else {
510                            updateStatus(userId, categoryId, category.getStatus());
511                    }
512    
513                    return moveCategory(categoryId, newCategoryId, false);
514            }
515    
516            public MBCategory moveCategoryToTrash(long userId, long categoryId)
517                    throws PortalException, SystemException {
518    
519                    return updateStatus(
520                            userId, categoryId, WorkflowConstants.STATUS_IN_TRASH);
521            }
522    
523            public void restoreCategoryFromTrash(long userId, long categoryId)
524                    throws PortalException, SystemException {
525    
526                    // Category
527    
528                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
529                            MBCategory.class.getName(), categoryId);
530    
531                    updateStatus(userId, categoryId, WorkflowConstants.STATUS_APPROVED);
532    
533                    // Trash
534    
535                    trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
536            }
537    
538            public void subscribeCategory(long userId, long groupId, long categoryId)
539                    throws PortalException, SystemException {
540    
541                    if (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
542                            categoryId = groupId;
543                    }
544    
545                    subscriptionLocalService.addSubscription(
546                            userId, groupId, MBCategory.class.getName(), categoryId);
547            }
548    
549            public void unsubscribeCategory(long userId, long groupId, long categoryId)
550                    throws PortalException, SystemException {
551    
552                    if (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
553                            categoryId = groupId;
554                    }
555    
556                    subscriptionLocalService.deleteSubscription(
557                            userId, MBCategory.class.getName(), categoryId);
558            }
559    
560            public MBCategory updateCategory(
561                            long categoryId, long parentCategoryId, String name,
562                            String description, String displayStyle, String emailAddress,
563                            String inProtocol, String inServerName, int inServerPort,
564                            boolean inUseSSL, String inUserName, String inPassword,
565                            int inReadInterval, String outEmailAddress, boolean outCustom,
566                            String outServerName, int outServerPort, boolean outUseSSL,
567                            String outUserName, String outPassword, boolean allowAnonymous,
568                            boolean mailingListActive, boolean mergeWithParentCategory,
569                            ServiceContext serviceContext)
570                    throws PortalException, SystemException {
571    
572                    // Merge categories
573    
574                    if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
575                            (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
576    
577                            return null;
578                    }
579    
580                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
581                            categoryId);
582    
583                    parentCategoryId = getParentCategoryId(category, parentCategoryId);
584    
585                    if (mergeWithParentCategory &&
586                            (categoryId != parentCategoryId) &&
587                            (parentCategoryId !=
588                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
589                            (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
590    
591                            mergeCategories(category, parentCategoryId);
592    
593                            return category;
594                    }
595    
596                    // Category
597    
598                    validate(name);
599    
600                    category.setModifiedDate(serviceContext.getModifiedDate(null));
601                    category.setParentCategoryId(parentCategoryId);
602                    category.setName(name);
603                    category.setDescription(description);
604    
605                    if (!displayStyle.equals(category.getDisplayStyle())) {
606                            category.setDisplayStyle(displayStyle);
607    
608                            updateChildCategoriesDisplayStyle(category, displayStyle);
609                    }
610    
611                    mbCategoryPersistence.update(category);
612    
613                    // Mailing list
614    
615                    MBMailingList mailingList = mbMailingListPersistence.fetchByG_C(
616                            category.getGroupId(), category.getCategoryId());
617    
618                    if (mailingList != null) {
619                            mbMailingListLocalService.updateMailingList(
620                                    mailingList.getMailingListId(), emailAddress, inProtocol,
621                                    inServerName, inServerPort, inUseSSL, inUserName, inPassword,
622                                    inReadInterval, outEmailAddress, outCustom, outServerName,
623                                    outServerPort, outUseSSL, outUserName, outPassword,
624                                    allowAnonymous, mailingListActive, serviceContext);
625                    }
626                    else {
627                            mbMailingListLocalService.addMailingList(
628                                    category.getUserId(), category.getGroupId(),
629                                    category.getCategoryId(), emailAddress, inProtocol,
630                                    inServerName, inServerPort, inUseSSL, inUserName, inPassword,
631                                    inReadInterval, outEmailAddress, outCustom, outServerName,
632                                    outServerPort, outUseSSL, outUserName, outPassword,
633                                    allowAnonymous, mailingListActive, serviceContext);
634                    }
635    
636                    // Expando
637    
638                    ExpandoBridge expandoBridge = category.getExpandoBridge();
639    
640                    expandoBridge.setAttributes(serviceContext);
641    
642                    return category;
643            }
644    
645            public void updateDependentStatus(
646                            User user, List<Object> categoriesAndThreads, int status)
647                    throws PortalException, SystemException {
648    
649                    for (Object object : categoriesAndThreads) {
650                            if (object instanceof MBThread) {
651                                    MBThread thread = (MBThread)object;
652    
653                                    if ((status == WorkflowConstants.STATUS_APPROVED) &&
654                                            (thread.getStatus() == WorkflowConstants.STATUS_IN_TRASH)) {
655    
656                                            continue;
657                                    }
658    
659                                    mbThreadLocalService.updateStatus(
660                                            user.getUserId(), thread.getThreadId(), status, status);
661                            }
662                            else if (object instanceof MBCategory) {
663                                    MBCategory category = (MBCategory)object;
664    
665                                    if (category.isInTrash()) {
666                                            continue;
667                                    }
668    
669                                    updateDependentStatus(
670                                            user,
671                                            getCategoriesAndThreads(
672                                                    category.getGroupId(), category.getCategoryId()),
673                                            status);
674                            }
675                    }
676            }
677    
678            public MBCategory updateStatus(long userId, long categoryId, int status)
679                    throws PortalException, SystemException {
680    
681                    // Category
682    
683                    User user = userPersistence.findByPrimaryKey(userId);
684    
685                    MBCategory category = mbCategoryPersistence.findByPrimaryKey(
686                            categoryId);
687    
688                    category.setStatus(status);
689                    category.setStatusByUserId(user.getUserId());
690                    category.setStatusByUserName(user.getFullName());
691                    category.setStatusDate(new Date());
692    
693                    mbCategoryPersistence.update(category);
694    
695                    // Categories and threads
696    
697                    List<Object> categoriesAndThreads = getCategoriesAndThreads(
698                            category.getGroupId(), categoryId);
699    
700                    updateDependentStatus(user, categoriesAndThreads, status);
701    
702                    // Trash
703    
704                    if (status == WorkflowConstants.STATUS_IN_TRASH) {
705                            trashEntryLocalService.addTrashEntry(
706                                    userId, category.getGroupId(), MBCategory.class.getName(),
707                                    categoryId, WorkflowConstants.STATUS_APPROVED, null, null);
708                    }
709    
710                    return category;
711            }
712    
713            protected long getParentCategoryId(long groupId, long parentCategoryId)
714                    throws SystemException {
715    
716                    if ((parentCategoryId !=
717                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
718                            (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
719    
720                            MBCategory parentCategory = mbCategoryPersistence.fetchByPrimaryKey(
721                                    parentCategoryId);
722    
723                            if ((parentCategory == null) ||
724                                    (groupId != parentCategory.getGroupId())) {
725    
726                                    parentCategoryId =
727                                            MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
728                            }
729                    }
730    
731                    return parentCategoryId;
732            }
733    
734            protected long getParentCategoryId(
735                            MBCategory category, long parentCategoryId)
736                    throws SystemException {
737    
738                    if ((parentCategoryId ==
739                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
740                            (parentCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
741    
742                            return parentCategoryId;
743                    }
744    
745                    if (category.getCategoryId() == parentCategoryId) {
746                            return category.getParentCategoryId();
747                    }
748                    else {
749                            MBCategory parentCategory = mbCategoryPersistence.fetchByPrimaryKey(
750                                    parentCategoryId);
751    
752                            if ((parentCategory == null) ||
753                                    (category.getGroupId() != parentCategory.getGroupId())) {
754    
755                                    return category.getParentCategoryId();
756                            }
757    
758                            List<Long> subcategoryIds = new ArrayList<Long>();
759    
760                            getSubcategoryIds(
761                                    subcategoryIds, category.getGroupId(),
762                                    category.getCategoryId());
763    
764                            if (subcategoryIds.contains(parentCategoryId)) {
765                                    return category.getParentCategoryId();
766                            }
767    
768                            return parentCategoryId;
769                    }
770            }
771    
772            protected void mergeCategories(MBCategory fromCategory, long toCategoryId)
773                    throws PortalException, SystemException {
774    
775                    if ((toCategoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
776                            (toCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
777    
778                            return;
779                    }
780    
781                    List<MBCategory> categories = mbCategoryPersistence.findByG_P(
782                            fromCategory.getGroupId(), fromCategory.getCategoryId());
783    
784                    for (MBCategory category : categories) {
785                            mergeCategories(category, toCategoryId);
786                    }
787    
788                    List<MBThread> threads = mbThreadPersistence.findByG_C(
789                            fromCategory.getGroupId(), fromCategory.getCategoryId());
790    
791                    for (MBThread thread : threads) {
792    
793                            // Thread
794    
795                            thread.setCategoryId(toCategoryId);
796    
797                            mbThreadPersistence.update(thread);
798    
799                            List<MBMessage> messages = mbMessagePersistence.findByThreadId(
800                                    thread.getThreadId());
801    
802                            for (MBMessage message : messages) {
803    
804                                    // Message
805    
806                                    message.setCategoryId(toCategoryId);
807    
808                                    mbMessagePersistence.update(message);
809    
810                                    // Indexer
811    
812                                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
813                                            MBMessage.class);
814    
815                                    indexer.reindex(message);
816                            }
817                    }
818    
819                    MBCategory toCategory = mbCategoryPersistence.findByPrimaryKey(
820                            toCategoryId);
821    
822                    toCategory.setThreadCount(
823                            fromCategory.getThreadCount() + toCategory.getThreadCount());
824                    toCategory.setMessageCount(
825                            fromCategory.getMessageCount() + toCategory.getMessageCount());
826    
827                    mbCategoryPersistence.update(toCategory);
828    
829                    deleteCategory(fromCategory);
830            }
831    
832            protected void updateChildCategoriesDisplayStyle(
833                            MBCategory category, String displayStyle)
834                    throws PortalException, SystemException {
835    
836                    List<MBCategory> categories = getCategories(
837                            category.getGroupId(), category.getCategoryId(), QueryUtil.ALL_POS,
838                            QueryUtil.ALL_POS);
839    
840                    for (MBCategory curCategory : categories) {
841                            updateChildCategoriesDisplayStyle(curCategory, displayStyle);
842    
843                            curCategory.setDisplayStyle(displayStyle);
844    
845                            mbCategoryPersistence.update(curCategory);
846                    }
847            }
848    
849            protected void validate(String name) throws PortalException {
850                    if (Validator.isNull(name)) {
851                            throw new CategoryNameException();
852                    }
853            }
854    
855    }