1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.model.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portlet.messageboards.model.MBCategory;
20  import com.liferay.portlet.messageboards.model.MBCategoryConstants;
21  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /**
27   * <a href="MBCategoryImpl.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class MBCategoryImpl extends MBCategoryModelImpl implements MBCategory {
32  
33      public MBCategoryImpl() {
34      }
35  
36      public List<Long> getAncestorCategoryIds()
37          throws PortalException, SystemException {
38  
39          List<Long> ancestorCategoryIds = new ArrayList<Long>();
40  
41          MBCategory category = this;
42  
43          while (true) {
44              if (!category.isRoot()) {
45                  category = MBCategoryLocalServiceUtil.getCategory(
46                      category.getParentCategoryId());
47  
48                  ancestorCategoryIds.add(category.getCategoryId());
49              }
50              else {
51                  break;
52              }
53          }
54  
55          return ancestorCategoryIds;
56      }
57  
58      public List<MBCategory> getAncestors()
59          throws PortalException, SystemException {
60  
61          List<MBCategory> ancestors = new ArrayList<MBCategory>();
62  
63          MBCategory category = this;
64  
65          while (true) {
66              if (!category.isRoot()) {
67                  category = MBCategoryLocalServiceUtil.getCategory(
68                      category.getParentCategoryId());
69  
70                  ancestors.add(category);
71              }
72              else {
73                  break;
74              }
75          }
76  
77          return ancestors;
78      }
79  
80      public boolean isRoot() {
81          if (getParentCategoryId() ==
82                  MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
83  
84              return true;
85          }
86          else {
87              return false;
88          }
89      }
90  
91  }