1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.model.impl;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.ListTree;
28  import com.liferay.portal.kernel.util.TreeNode;
29  import com.liferay.portlet.messageboards.model.MBCategory;
30  import com.liferay.portlet.messageboards.model.MBCategoryDisplay;
31  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
32  
33  import java.util.ArrayList;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Map;
37  
38  /**
39   * <a href="MBCategoryDisplayImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Shuyang Zhou
42   *
43   */
44  public class MBCategoryDisplayImpl implements MBCategoryDisplay {
45  
46      public MBCategoryDisplayImpl(long scopeGroupId, long categoryId) {
47          try {
48              init(scopeGroupId, categoryId);
49          }
50          catch (Exception e) {
51              _log.error(e, e);
52          }
53      }
54  
55      public List<MBCategory> getAllCategories() {
56          return _allCategories;
57      }
58  
59      public int getAllCategoriesCount() {
60          return _allCategories.size();
61      }
62  
63      public List<MBCategory> getCategories() {
64          return _categoryTree.getRootNode().getChildValues();
65      }
66  
67      public List<MBCategory> getCategories(MBCategory category) {
68          TreeNode<MBCategory> node = _categoryNodesMap.get(
69              category.getCategoryId());
70  
71          return node.getChildValues();
72      }
73  
74      public MBCategory getRootCategory() {
75          return _categoryTree.getRootNode().getValue();
76      }
77  
78      public int getSubcategoriesCount(MBCategory category) {
79          TreeNode<MBCategory> node = _categoryNodesMap.get(
80              category.getCategoryId());
81  
82          return _categoryTree.getChildNodes(node).size();
83      }
84  
85      public int getSubcategoriesMessagesCount(MBCategory category) {
86          int count = category.getMessageCount();
87  
88          TreeNode<MBCategory> node = _categoryNodesMap.get(
89              category.getCategoryId());
90  
91          List<TreeNode<MBCategory>> childNodes = _categoryTree.getChildNodes(
92              node);
93  
94          for (TreeNode<MBCategory> curNode : childNodes) {
95              MBCategory curCategory = curNode.getValue();
96  
97              count += curCategory.getMessageCount();
98          }
99  
100         return count;
101     }
102 
103     public int getSubcategoriesThreadsCount(MBCategory category) {
104         int count = category.getThreadCount();
105 
106         TreeNode<MBCategory> node = _categoryNodesMap.get(
107             category.getCategoryId());
108 
109         List<TreeNode<MBCategory>> childNodes = _categoryTree.getChildNodes(
110             node);
111 
112         for (TreeNode<MBCategory> curNode : childNodes) {
113             MBCategory curCategory = curNode.getValue();
114 
115             count += curCategory.getThreadCount();
116         }
117 
118         return count;
119     }
120 
121     public void getSubcategoryIds(
122         MBCategory category, List<Long> categoryIds) {
123 
124         List<MBCategory> categories = getCategories(category);
125 
126         for (MBCategory curCategory : categories) {
127             categoryIds.add(curCategory.getCategoryId());
128 
129             getSubcategoryIds(curCategory, categoryIds);
130         }
131     }
132 
133     protected void init(long scopeGroupId, long categoryId) throws Exception {
134         _allCategories = MBCategoryLocalServiceUtil.getCategories(scopeGroupId);
135 
136         _rootCategory = new MBCategoryImpl();
137 
138         _rootCategory.setCategoryId(categoryId);
139 
140         _categoryTree = new ListTree<MBCategory>(_rootCategory);
141 
142         _categoryNodesMap = new HashMap<Long, TreeNode<MBCategory>>();
143 
144         Map<Long, List<MBCategory>> categoriesMap =
145             new HashMap<Long, List<MBCategory>>();
146 
147         for (MBCategory category : _allCategories) {
148             Long parentCategoryId = category.getParentCategoryId();
149 
150             List<MBCategory> curCategories = categoriesMap.get(
151                 parentCategoryId);
152 
153             if (curCategories == null) {
154                 curCategories = new ArrayList<MBCategory>();
155 
156                 categoriesMap.put(parentCategoryId, curCategories);
157             }
158 
159             curCategories.add(category);
160         }
161 
162         populateCategoryNodesMap(_categoryTree.getRootNode(), categoriesMap);
163     }
164 
165     protected void populateCategoryNodesMap(
166         TreeNode<MBCategory> node, Map<Long, List<MBCategory>> categoriesMap) {
167 
168         MBCategory category = node.getValue();
169 
170         List<MBCategory> categories = categoriesMap.get(
171             category.getCategoryId());
172 
173         if (categories == null) {
174             return;
175         }
176 
177         for (MBCategory curCategory : categories) {
178             TreeNode<MBCategory> curNode = node.addChildNode(curCategory);
179 
180             _categoryNodesMap.put(curCategory.getCategoryId(), curNode);
181 
182             populateCategoryNodesMap(curNode, categoriesMap);
183         }
184     }
185 
186     private static Log _log =
187         LogFactoryUtil.getLog(MBCategoryDisplayImpl.class);
188 
189     private List<MBCategory> _allCategories;
190     private Map<Long, TreeNode<MBCategory>> _categoryNodesMap;
191     private ListTree<MBCategory> _categoryTree;
192     private MBCategory _rootCategory;
193 
194 }