001    /**
002     * Copyright (c) 2000-present 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.model.impl;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ListTree;
020    import com.liferay.portal.kernel.util.TreeNode;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portlet.messageboards.model.MBCategory;
023    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
024    import com.liferay.portlet.messageboards.model.MBCategoryDisplay;
025    import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
026    
027    import java.util.ArrayList;
028    import java.util.HashMap;
029    import java.util.List;
030    import java.util.Map;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class MBCategoryDisplayImpl implements MBCategoryDisplay {
036    
037            public MBCategoryDisplayImpl(long scopeGroupId, long categoryId) {
038                    try {
039                            init(scopeGroupId, categoryId);
040                    }
041                    catch (Exception e) {
042                            _log.error(e, e);
043                    }
044            }
045    
046            @Override
047            public List<MBCategory> getAllCategories() {
048                    return _allCategories;
049            }
050    
051            @Override
052            public int getAllCategoriesCount() {
053                    return _allCategories.size();
054            }
055    
056            @Override
057            public List<MBCategory> getCategories() {
058                    return _categoryTree.getRootNode().getChildValues();
059            }
060    
061            @Override
062            public List<MBCategory> getCategories(MBCategory category) {
063                    TreeNode<MBCategory> node = _categoryNodesMap.get(
064                            category.getCategoryId());
065    
066                    return node.getChildValues();
067            }
068    
069            @Override
070            public MBCategory getRootCategory() {
071                    return _categoryTree.getRootNode().getValue();
072            }
073    
074            @Override
075            public int getSubcategoriesCount(MBCategory category) {
076                    TreeNode<MBCategory> node = _categoryNodesMap.get(
077                            category.getCategoryId());
078    
079                    return _categoryTree.getChildNodes(node).size();
080            }
081    
082            @Override
083            public int getSubcategoriesMessagesCount(MBCategory category) {
084                    int count = category.getMessageCount();
085    
086                    TreeNode<MBCategory> node = _categoryNodesMap.get(
087                            category.getCategoryId());
088    
089                    List<TreeNode<MBCategory>> childNodes = _categoryTree.getChildNodes(
090                            node);
091    
092                    for (TreeNode<MBCategory> curNode : childNodes) {
093                            MBCategory curCategory = curNode.getValue();
094    
095                            count += curCategory.getMessageCount();
096                    }
097    
098                    return count;
099            }
100    
101            @Override
102            public int getSubcategoriesThreadsCount(MBCategory category) {
103                    int count = category.getThreadCount();
104    
105                    TreeNode<MBCategory> node = _categoryNodesMap.get(
106                            category.getCategoryId());
107    
108                    List<TreeNode<MBCategory>> childNodes = _categoryTree.getChildNodes(
109                            node);
110    
111                    for (TreeNode<MBCategory> curNode : childNodes) {
112                            MBCategory curCategory = curNode.getValue();
113    
114                            count += curCategory.getThreadCount();
115                    }
116    
117                    return count;
118            }
119    
120            @Override
121            public void getSubcategoryIds(MBCategory category, List<Long> categoryIds) {
122                    List<MBCategory> categories = getCategories(category);
123    
124                    for (MBCategory curCategory : categories) {
125                            categoryIds.add(curCategory.getCategoryId());
126    
127                            getSubcategoryIds(curCategory, categoryIds);
128                    }
129            }
130    
131            protected void init(long scopeGroupId, long categoryId) throws Exception {
132                    _allCategories = MBCategoryServiceUtil.getCategories(
133                            scopeGroupId, WorkflowConstants.STATUS_APPROVED);
134    
135                    _rootCategory = new MBCategoryImpl();
136    
137                    _rootCategory.setCategoryId(categoryId);
138    
139                    _categoryTree = new ListTree<>(_rootCategory);
140    
141                    _categoryNodesMap = new HashMap<>();
142    
143                    Map<Long, List<MBCategory>> categoriesMap = new HashMap<>();
144    
145                    for (MBCategory category : _allCategories) {
146                            Long parentCategoryId = category.getParentCategoryId();
147    
148                            List<MBCategory> curCategories = categoriesMap.get(
149                                    parentCategoryId);
150    
151                            if (curCategories == null) {
152                                    curCategories = new ArrayList<>();
153    
154                                    categoriesMap.put(parentCategoryId, curCategories);
155                            }
156    
157                            curCategories.add(category);
158                    }
159    
160                    populateCategoryNodesMap(_categoryTree.getRootNode(), categoriesMap);
161            }
162    
163            protected void populateCategoryNodesMap(
164                    TreeNode<MBCategory> node, Map<Long, List<MBCategory>> categoriesMap) {
165    
166                    MBCategory category = node.getValue();
167    
168                    if (category.getCategoryId() ==
169                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
170    
171                            _categoryNodesMap.put(category.getCategoryId(), node);
172                    }
173    
174                    List<MBCategory> categories = categoriesMap.get(
175                            category.getCategoryId());
176    
177                    if (categories == null) {
178                            return;
179                    }
180    
181                    for (MBCategory curCategory : categories) {
182                            TreeNode<MBCategory> curNode = node.addChildNode(curCategory);
183    
184                            _categoryNodesMap.put(curCategory.getCategoryId(), curNode);
185    
186                            populateCategoryNodesMap(curNode, categoriesMap);
187                    }
188            }
189    
190            private static final Log _log = LogFactoryUtil.getLog(
191                    MBCategoryDisplayImpl.class);
192    
193            private List<MBCategory> _allCategories;
194            private Map<Long, TreeNode<MBCategory>> _categoryNodesMap;
195            private ListTree<MBCategory> _categoryTree;
196            private MBCategory _rootCategory;
197    
198    }