001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.MBCategoryDisplay;
023    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
024    import com.liferay.portlet.messageboards.util.MBUtil;
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            public List<MBCategory> getAllCategories() {
046                    return _allCategories;
047            }
048    
049            public int getAllCategoriesCount() {
050                    return _allCategories.size();
051            }
052    
053            public List<MBCategory> getCategories() {
054                    return _categoryTree.getRootNode().getChildValues();
055            }
056    
057            public List<MBCategory> getCategories(MBCategory category) {
058                    TreeNode<MBCategory> node = _categoryNodesMap.get(
059                            category.getCategoryId());
060    
061                    return node.getChildValues();
062            }
063    
064            public MBCategory getRootCategory() {
065                    return _categoryTree.getRootNode().getValue();
066            }
067    
068            public int getSubcategoriesCount(MBCategory category) {
069                    TreeNode<MBCategory> node = _categoryNodesMap.get(
070                            category.getCategoryId());
071    
072                    return _categoryTree.getChildNodes(node).size();
073            }
074    
075            public int getSubcategoriesMessagesCount(MBCategory category) {
076                    int count = category.getMessageCount();
077    
078                    TreeNode<MBCategory> node = _categoryNodesMap.get(
079                            category.getCategoryId());
080    
081                    List<TreeNode<MBCategory>> childNodes = _categoryTree.getChildNodes(
082                            node);
083    
084                    for (TreeNode<MBCategory> curNode : childNodes) {
085                            MBCategory curCategory = curNode.getValue();
086    
087                            count += curCategory.getMessageCount();
088                    }
089    
090                    return count;
091            }
092    
093            public int getSubcategoriesThreadsCount(MBCategory category) {
094                    int count = category.getThreadCount();
095    
096                    TreeNode<MBCategory> node = _categoryNodesMap.get(
097                            category.getCategoryId());
098    
099                    List<TreeNode<MBCategory>> childNodes = _categoryTree.getChildNodes(
100                            node);
101    
102                    for (TreeNode<MBCategory> curNode : childNodes) {
103                            MBCategory curCategory = curNode.getValue();
104    
105                            count += curCategory.getThreadCount();
106                    }
107    
108                    return count;
109            }
110    
111            public void getSubcategoryIds(
112                    MBCategory category, List<Long> categoryIds) {
113    
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                    _allCategories = MBCategoryLocalServiceUtil.getCategories(scopeGroupId);
125    
126                    _rootCategory = new MBCategoryImpl();
127    
128                    _rootCategory.setCategoryId(categoryId);
129    
130                    _categoryTree = new ListTree<MBCategory>(_rootCategory);
131    
132                    _categoryNodesMap = new HashMap<Long, TreeNode<MBCategory>>();
133    
134                    Map<Long, List<MBCategory>> categoriesMap =
135                            new HashMap<Long, List<MBCategory>>();
136    
137                    for (MBCategory category : _allCategories) {
138                            Long parentCategoryId = category.getParentCategoryId();
139    
140                            List<MBCategory> curCategories = categoriesMap.get(
141                                    parentCategoryId);
142    
143                            if (curCategories == null) {
144                                    curCategories = new ArrayList<MBCategory>();
145    
146                                    categoriesMap.put(parentCategoryId, curCategories);
147                            }
148    
149                            curCategories.add(category);
150                    }
151    
152                    populateCategoryNodesMap(_categoryTree.getRootNode(), categoriesMap);
153            }
154    
155            protected void populateCategoryNodesMap(
156                    TreeNode<MBCategory> node, Map<Long, List<MBCategory>> categoriesMap) {
157    
158                    MBCategory category = node.getValue();
159    
160                    if (MBUtil.isDefaultParentCategoryId(category.getCategoryId())) {
161                            _categoryNodesMap.put(category.getCategoryId(), node);
162                    }
163    
164                    List<MBCategory> categories = categoriesMap.get(
165                            category.getCategoryId());
166    
167                    if (categories == null) {
168                            return;
169                    }
170    
171                    for (MBCategory curCategory : categories) {
172                            TreeNode<MBCategory> curNode = node.addChildNode(curCategory);
173    
174                            _categoryNodesMap.put(curCategory.getCategoryId(), curNode);
175    
176                            populateCategoryNodesMap(curNode, categoriesMap);
177                    }
178            }
179    
180            private static Log _log = LogFactoryUtil.getLog(
181                    MBCategoryDisplayImpl.class);
182    
183            private List<MBCategory> _allCategories;
184            private Map<Long, TreeNode<MBCategory>> _categoryNodesMap;
185            private ListTree<MBCategory> _categoryTree;
186            private MBCategory _rootCategory;
187    
188    }