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