001
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.MBCategoryConstants;
023 import com.liferay.portlet.messageboards.model.MBCategoryDisplay;
024 import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
025
026 import java.util.ArrayList;
027 import java.util.HashMap;
028 import java.util.List;
029 import java.util.Map;
030
031
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(MBCategory category, List<Long> categoryIds) {
112 List<MBCategory> categories = getCategories(category);
113
114 for (MBCategory curCategory : categories) {
115 categoryIds.add(curCategory.getCategoryId());
116
117 getSubcategoryIds(curCategory, categoryIds);
118 }
119 }
120
121 protected void init(long scopeGroupId, long categoryId) throws Exception {
122 _allCategories = MBCategoryServiceUtil.getCategories(scopeGroupId);
123
124 _rootCategory = new MBCategoryImpl();
125
126 _rootCategory.setCategoryId(categoryId);
127
128 _categoryTree = new ListTree<MBCategory>(_rootCategory);
129
130 _categoryNodesMap = new HashMap<Long, TreeNode<MBCategory>>();
131
132 Map<Long, List<MBCategory>> categoriesMap =
133 new HashMap<Long, List<MBCategory>>();
134
135 for (MBCategory category : _allCategories) {
136 Long parentCategoryId = category.getParentCategoryId();
137
138 List<MBCategory> curCategories = categoriesMap.get(
139 parentCategoryId);
140
141 if (curCategories == null) {
142 curCategories = new ArrayList<MBCategory>();
143
144 categoriesMap.put(parentCategoryId, curCategories);
145 }
146
147 curCategories.add(category);
148 }
149
150 populateCategoryNodesMap(_categoryTree.getRootNode(), categoriesMap);
151 }
152
153 protected void populateCategoryNodesMap(
154 TreeNode<MBCategory> node, Map<Long, List<MBCategory>> categoriesMap) {
155
156 MBCategory category = node.getValue();
157
158 if (category.getCategoryId() ==
159 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
160
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 }