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  public class MBCategoryDisplayImpl implements MBCategoryDisplay {
44  
45      public MBCategoryDisplayImpl(long scopeGroupId, long categoryId) {
46          try {
47              init(scopeGroupId, categoryId);
48          }
49          catch (Exception e) {
50              _log.error(e, e);
51          }
52      }
53  
54      public List<MBCategory> getAllCategories() {
55          return _allCategories;
56      }
57  
58      public int getAllCategoriesCount() {
59          return _allCategories.size();
60      }
61  
62      public List<MBCategory> getCategories() {
63          return _categoryTree.getRootNode().getChildValues();
64      }
65  
66      public List<MBCategory> getCategories(MBCategory category) {
67          TreeNode<MBCategory> node = _categoryNodesMap.get(
68              category.getCategoryId());
69  
70          return node.getChildValues();
71      }
72  
73      public MBCategory getRootCategory() {
74          return _categoryTree.getRootNode().getValue();
75      }
76  
77      public int getSubcategoriesCount(MBCategory category) {
78          TreeNode<MBCategory> node = _categoryNodesMap.get(
79              category.getCategoryId());
80  
81          return _categoryTree.getChildNodes(node).size();
82      }
83  
84      public int getSubcategoriesMessagesCount(MBCategory category) {
85          int count = category.getMessageCount();
86  
87          TreeNode<MBCategory> node = _categoryNodesMap.get(
88              category.getCategoryId());
89  
90          List<TreeNode<MBCategory>> childNodes = _categoryTree.getChildNodes(
91              node);
92  
93          for (TreeNode<MBCategory> curNode : childNodes) {
94              MBCategory curCategory = curNode.getValue();
95  
96              count += curCategory.getMessageCount();
97          }
98  
99          return count;
100     }
101 
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     public void getSubcategoryIds(
121         MBCategory category, List<Long> categoryIds) {
122 
123         List<MBCategory> categories = getCategories(category);
124 
125         for (MBCategory curCategory : categories) {
126             categoryIds.add(curCategory.getCategoryId());
127 
128             getSubcategoryIds(curCategory, categoryIds);
129         }
130     }
131 
132     protected void init(long scopeGroupId, long categoryId) throws Exception {
133         _allCategories = MBCategoryLocalServiceUtil.getCategories(scopeGroupId);
134 
135         _rootCategory = new MBCategoryImpl();
136 
137         _rootCategory.setCategoryId(categoryId);
138 
139         _categoryTree = new ListTree<MBCategory>(_rootCategory);
140 
141         _categoryNodesMap = new HashMap<Long, TreeNode<MBCategory>>();
142 
143         Map<Long, List<MBCategory>> categoriesMap =
144             new HashMap<Long, List<MBCategory>>();
145 
146         for (MBCategory category : _allCategories) {
147             Long parentCategoryId = category.getParentCategoryId();
148 
149             List<MBCategory> curCategories = categoriesMap.get(
150                 parentCategoryId);
151 
152             if (curCategories == null) {
153                 curCategories = new ArrayList<MBCategory>();
154 
155                 categoriesMap.put(parentCategoryId, curCategories);
156             }
157 
158             curCategories.add(category);
159         }
160 
161         populateCategoryNodesMap(_categoryTree.getRootNode(), categoriesMap);
162     }
163 
164     protected void populateCategoryNodesMap(
165         TreeNode<MBCategory> node, Map<Long, List<MBCategory>> categoriesMap) {
166 
167         MBCategory category = node.getValue();
168 
169         List<MBCategory> categories = categoriesMap.get(
170             category.getCategoryId());
171 
172         if (categories == null) {
173             return;
174         }
175 
176         for (MBCategory curCategory : categories) {
177             TreeNode<MBCategory> curNode = node.addChildNode(curCategory);
178 
179             _categoryNodesMap.put(curCategory.getCategoryId(), curNode);
180 
181             populateCategoryNodesMap(curNode, categoriesMap);
182         }
183     }
184 
185     private static Log _log =
186         LogFactoryUtil.getLog(MBCategoryDisplayImpl.class);
187 
188     private List<MBCategory> _allCategories;
189     private Map<Long, TreeNode<MBCategory>> _categoryNodesMap;
190     private ListTree<MBCategory> _categoryTree;
191     private MBCategory _rootCategory;
192 
193 }