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