001
014
015 package com.liferay.portlet.messageboards.model.impl;
016
017 import com.liferay.portal.kernel.dao.orm.QueryUtil;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.ListTree;
021 import com.liferay.portal.kernel.util.TreeNode;
022 import com.liferay.portal.kernel.workflow.WorkflowConstants;
023 import com.liferay.portlet.messageboards.model.MBCategory;
024 import com.liferay.portlet.messageboards.model.MBCategoryConstants;
025 import com.liferay.portlet.messageboards.model.MBCategoryDisplay;
026 import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
027
028 import java.util.ArrayList;
029 import java.util.HashMap;
030 import java.util.List;
031 import java.util.Map;
032
033
036 public class MBCategoryDisplayImpl implements MBCategoryDisplay {
037
038 public MBCategoryDisplayImpl(long scopeGroupId, long categoryId) {
039 try {
040 init(scopeGroupId, categoryId);
041 }
042 catch (Exception e) {
043 _log.error(e, e);
044 }
045 }
046
047 @Override
048 public List<MBCategory> getAllCategories() {
049 return _allCategories;
050 }
051
052 @Override
053 public int getAllCategoriesCount() {
054 return _allCategories.size();
055 }
056
057 @Override
058 public List<MBCategory> getCategories() {
059 return _categoryTree.getRootNode().getChildValues();
060 }
061
062 @Override
063 public List<MBCategory> getCategories(MBCategory category) {
064 TreeNode<MBCategory> node = _categoryNodesMap.get(
065 category.getCategoryId());
066
067 return node.getChildValues();
068 }
069
070 @Override
071 public MBCategory getRootCategory() {
072 return _categoryTree.getRootNode().getValue();
073 }
074
075 @Override
076 public int getSubcategoriesCount(MBCategory category) {
077 TreeNode<MBCategory> node = _categoryNodesMap.get(
078 category.getCategoryId());
079
080 return _categoryTree.getChildNodes(node).size();
081 }
082
083 @Override
084 public int getSubcategoriesMessagesCount(MBCategory category) {
085 int count = category.getMessageCount();
086
087 TreeNode<MBCategory> node = _categoryNodesMap.get(
088 category.getCategoryId());
089
090 List<TreeNode<MBCategory>> childNodes = _categoryTree.getChildNodes(
091 node);
092
093 for (TreeNode<MBCategory> curNode : childNodes) {
094 MBCategory curCategory = curNode.getValue();
095
096 count += curCategory.getMessageCount();
097 }
098
099 return count;
100 }
101
102 @Override
103 public int getSubcategoriesThreadsCount(MBCategory category) {
104 int count = category.getThreadCount();
105
106 TreeNode<MBCategory> node = _categoryNodesMap.get(
107 category.getCategoryId());
108
109 List<TreeNode<MBCategory>> childNodes = _categoryTree.getChildNodes(
110 node);
111
112 for (TreeNode<MBCategory> curNode : childNodes) {
113 MBCategory curCategory = curNode.getValue();
114
115 count += curCategory.getThreadCount();
116 }
117
118 return count;
119 }
120
121 @Override
122 public void getSubcategoryIds(MBCategory category, List<Long> categoryIds) {
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 long[] categoryIds = MBCategoryServiceUtil.getCategoryIds(
134 scopeGroupId, categoryId);
135
136 _allCategories = MBCategoryServiceUtil.getCategories(
137 scopeGroupId, categoryIds, WorkflowConstants.STATUS_APPROVED,
138 QueryUtil.ALL_POS, QueryUtil.ALL_POS);
139
140 _rootCategory = new MBCategoryImpl();
141
142 _rootCategory.setCategoryId(categoryId);
143
144 _categoryTree = new ListTree<MBCategory>(_rootCategory);
145
146 _categoryNodesMap = new HashMap<Long, TreeNode<MBCategory>>();
147
148 Map<Long, List<MBCategory>> categoriesMap =
149 new HashMap<Long, List<MBCategory>>();
150
151 for (MBCategory category : _allCategories) {
152 Long parentCategoryId = category.getParentCategoryId();
153
154 List<MBCategory> curCategories = categoriesMap.get(
155 parentCategoryId);
156
157 if (curCategories == null) {
158 curCategories = new ArrayList<MBCategory>();
159
160 categoriesMap.put(parentCategoryId, curCategories);
161 }
162
163 curCategories.add(category);
164 }
165
166 populateCategoryNodesMap(_categoryTree.getRootNode(), categoriesMap);
167 }
168
169 protected void populateCategoryNodesMap(
170 TreeNode<MBCategory> node, Map<Long, List<MBCategory>> categoriesMap) {
171
172 MBCategory category = node.getValue();
173
174 if (category.getCategoryId() ==
175 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
176
177 _categoryNodesMap.put(category.getCategoryId(), node);
178 }
179
180 List<MBCategory> categories = categoriesMap.get(
181 category.getCategoryId());
182
183 if (categories == null) {
184 return;
185 }
186
187 for (MBCategory curCategory : categories) {
188 TreeNode<MBCategory> curNode = node.addChildNode(curCategory);
189
190 _categoryNodesMap.put(curCategory.getCategoryId(), curNode);
191
192 populateCategoryNodesMap(curNode, categoriesMap);
193 }
194 }
195
196 private static Log _log = LogFactoryUtil.getLog(
197 MBCategoryDisplayImpl.class);
198
199 private List<MBCategory> _allCategories;
200 private Map<Long, TreeNode<MBCategory>> _categoryNodesMap;
201 private ListTree<MBCategory> _categoryTree;
202 private MBCategory _rootCategory;
203
204 }