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