1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.messageboards.model.impl;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.ListTree;
20  import com.liferay.portal.kernel.util.TreeNode;
21  import com.liferay.portlet.messageboards.model.MBCategory;
22  import com.liferay.portlet.messageboards.model.MBCategoryDisplay;
23  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
24  
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  /**
31   * <a href="MBCategoryDisplayImpl.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Shuyang Zhou
34   */
35  public class MBCategoryDisplayImpl implements MBCategoryDisplay {
36  
37      public MBCategoryDisplayImpl(long scopeGroupId, long categoryId) {
38          try {
39              init(scopeGroupId, categoryId);
40          }
41          catch (Exception e) {
42              _log.error(e, e);
43          }
44      }
45  
46      public List<MBCategory> getAllCategories() {
47          return _allCategories;
48      }
49  
50      public int getAllCategoriesCount() {
51          return _allCategories.size();
52      }
53  
54      public List<MBCategory> getCategories() {
55          return _categoryTree.getRootNode().getChildValues();
56      }
57  
58      public List<MBCategory> getCategories(MBCategory category) {
59          TreeNode<MBCategory> node = _categoryNodesMap.get(
60              category.getCategoryId());
61  
62          return node.getChildValues();
63      }
64  
65      public MBCategory getRootCategory() {
66          return _categoryTree.getRootNode().getValue();
67      }
68  
69      public int getSubcategoriesCount(MBCategory category) {
70          TreeNode<MBCategory> node = _categoryNodesMap.get(
71              category.getCategoryId());
72  
73          return _categoryTree.getChildNodes(node).size();
74      }
75  
76      public int getSubcategoriesMessagesCount(MBCategory category) {
77          int count = category.getMessageCount();
78  
79          TreeNode<MBCategory> node = _categoryNodesMap.get(
80              category.getCategoryId());
81  
82          List<TreeNode<MBCategory>> childNodes = _categoryTree.getChildNodes(
83              node);
84  
85          for (TreeNode<MBCategory> curNode : childNodes) {
86              MBCategory curCategory = curNode.getValue();
87  
88              count += curCategory.getMessageCount();
89          }
90  
91          return count;
92      }
93  
94      public int getSubcategoriesThreadsCount(MBCategory category) {
95          int count = category.getThreadCount();
96  
97          TreeNode<MBCategory> node = _categoryNodesMap.get(
98              category.getCategoryId());
99  
100         List<TreeNode<MBCategory>> childNodes = _categoryTree.getChildNodes(
101             node);
102 
103         for (TreeNode<MBCategory> curNode : childNodes) {
104             MBCategory curCategory = curNode.getValue();
105 
106             count += curCategory.getThreadCount();
107         }
108 
109         return count;
110     }
111 
112     public void getSubcategoryIds(
113         MBCategory category, List<Long> categoryIds) {
114 
115         List<MBCategory> categories = getCategories(category);
116 
117         for (MBCategory curCategory : categories) {
118             categoryIds.add(curCategory.getCategoryId());
119 
120             getSubcategoryIds(curCategory, categoryIds);
121         }
122     }
123 
124     protected void init(long scopeGroupId, long categoryId) throws Exception {
125         _allCategories = MBCategoryLocalServiceUtil.getCategories(scopeGroupId);
126 
127         _rootCategory = new MBCategoryImpl();
128 
129         _rootCategory.setCategoryId(categoryId);
130 
131         _categoryTree = new ListTree<MBCategory>(_rootCategory);
132 
133         _categoryNodesMap = new HashMap<Long, TreeNode<MBCategory>>();
134 
135         Map<Long, List<MBCategory>> categoriesMap =
136             new HashMap<Long, List<MBCategory>>();
137 
138         for (MBCategory category : _allCategories) {
139             Long parentCategoryId = category.getParentCategoryId();
140 
141             List<MBCategory> curCategories = categoriesMap.get(
142                 parentCategoryId);
143 
144             if (curCategories == null) {
145                 curCategories = new ArrayList<MBCategory>();
146 
147                 categoriesMap.put(parentCategoryId, curCategories);
148             }
149 
150             curCategories.add(category);
151         }
152 
153         populateCategoryNodesMap(_categoryTree.getRootNode(), categoriesMap);
154     }
155 
156     protected void populateCategoryNodesMap(
157         TreeNode<MBCategory> node, Map<Long, List<MBCategory>> categoriesMap) {
158 
159         MBCategory category = node.getValue();
160 
161         List<MBCategory> categories = categoriesMap.get(
162             category.getCategoryId());
163 
164         if (categories == null) {
165             return;
166         }
167 
168         for (MBCategory curCategory : categories) {
169             TreeNode<MBCategory> curNode = node.addChildNode(curCategory);
170 
171             _categoryNodesMap.put(curCategory.getCategoryId(), curNode);
172 
173             populateCategoryNodesMap(curNode, categoriesMap);
174         }
175     }
176 
177     private static Log _log = LogFactoryUtil.getLog(
178         MBCategoryDisplayImpl.class);
179 
180     private List<MBCategory> _allCategories;
181     private Map<Long, TreeNode<MBCategory>> _categoryNodesMap;
182     private ListTree<MBCategory> _categoryTree;
183     private MBCategory _rootCategory;
184 
185 }