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.portal.kernel.workflow.WorkflowConstants;
022 import com.liferay.portlet.messageboards.model.MBCategory;
023 import com.liferay.portlet.messageboards.model.MBCategoryConstants;
024 import com.liferay.portlet.messageboards.model.MBCategoryDisplay;
025 import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
026
027 import java.util.ArrayList;
028 import java.util.HashMap;
029 import java.util.List;
030 import java.util.Map;
031
032
035 public class MBCategoryDisplayImpl implements MBCategoryDisplay {
036
037 public MBCategoryDisplayImpl(long scopeGroupId, long categoryId) {
038 try {
039 init(scopeGroupId, categoryId);
040 }
041 catch (Exception e) {
042 _log.error(e, e);
043 }
044 }
045
046 @Override
047 public List<MBCategory> getAllCategories() {
048 return _allCategories;
049 }
050
051 @Override
052 public int getAllCategoriesCount() {
053 return _allCategories.size();
054 }
055
056 @Override
057 public List<MBCategory> getCategories() {
058 return _categoryTree.getRootNode().getChildValues();
059 }
060
061 @Override
062 public List<MBCategory> getCategories(MBCategory category) {
063 TreeNode<MBCategory> node = _categoryNodesMap.get(
064 category.getCategoryId());
065
066 return node.getChildValues();
067 }
068
069 @Override
070 public MBCategory getRootCategory() {
071 return _categoryTree.getRootNode().getValue();
072 }
073
074 @Override
075 public int getSubcategoriesCount(MBCategory category) {
076 TreeNode<MBCategory> node = _categoryNodesMap.get(
077 category.getCategoryId());
078
079 return _categoryTree.getChildNodes(node).size();
080 }
081
082 @Override
083 public int getSubcategoriesMessagesCount(MBCategory category) {
084 int count = category.getMessageCount();
085
086 TreeNode<MBCategory> node = _categoryNodesMap.get(
087 category.getCategoryId());
088
089 List<TreeNode<MBCategory>> childNodes = _categoryTree.getChildNodes(
090 node);
091
092 for (TreeNode<MBCategory> curNode : childNodes) {
093 MBCategory curCategory = curNode.getValue();
094
095 count += curCategory.getMessageCount();
096 }
097
098 return count;
099 }
100
101 @Override
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 @Override
121 public void getSubcategoryIds(MBCategory category, List<Long> categoryIds) {
122 List<MBCategory> categories = getCategories(category);
123
124 for (MBCategory curCategory : categories) {
125 categoryIds.add(curCategory.getCategoryId());
126
127 getSubcategoryIds(curCategory, categoryIds);
128 }
129 }
130
131 protected void init(long scopeGroupId, long categoryId) throws Exception {
132 _allCategories = MBCategoryServiceUtil.getCategories(
133 scopeGroupId, WorkflowConstants.STATUS_APPROVED);
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 if (category.getCategoryId() ==
170 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
171
172 _categoryNodesMap.put(category.getCategoryId(), node);
173 }
174
175 List<MBCategory> categories = categoriesMap.get(
176 category.getCategoryId());
177
178 if (categories == null) {
179 return;
180 }
181
182 for (MBCategory curCategory : categories) {
183 TreeNode<MBCategory> curNode = node.addChildNode(curCategory);
184
185 _categoryNodesMap.put(curCategory.getCategoryId(), curNode);
186
187 populateCategoryNodesMap(curNode, categoriesMap);
188 }
189 }
190
191 private static Log _log = LogFactoryUtil.getLog(
192 MBCategoryDisplayImpl.class);
193
194 private List<MBCategory> _allCategories;
195 private Map<Long, TreeNode<MBCategory>> _categoryNodesMap;
196 private ListTree<MBCategory> _categoryTree;
197 private MBCategory _rootCategory;
198
199 }