001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portlet.messageboards.model.MBCategory;
020    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
021    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class MBCategoryImpl extends MBCategoryBaseImpl {
030    
031            public MBCategoryImpl() {
032            }
033    
034            public List<Long> getAncestorCategoryIds()
035                    throws PortalException, SystemException {
036    
037                    List<Long> ancestorCategoryIds = new ArrayList<Long>();
038    
039                    MBCategory category = this;
040    
041                    while (!category.isRoot()) {
042                            category = MBCategoryLocalServiceUtil.getCategory(
043                                    category.getParentCategoryId());
044    
045                            ancestorCategoryIds.add(category.getCategoryId());
046                    }
047    
048                    return ancestorCategoryIds;
049            }
050    
051            public List<MBCategory> getAncestors()
052                    throws PortalException, SystemException {
053    
054                    List<MBCategory> ancestors = new ArrayList<MBCategory>();
055    
056                    MBCategory category = this;
057    
058                    while (!category.isRoot()) {
059                            category = category.getParentCategory();
060    
061                            ancestors.add(category);
062                    }
063    
064                    return ancestors;
065            }
066    
067            public MBCategory getParentCategory()
068                    throws PortalException, SystemException {
069    
070                    long parentCategoryId = getParentCategoryId();
071    
072                    if ((parentCategoryId ==
073                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
074                            (parentCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
075    
076                            return null;
077                    }
078    
079                    return MBCategoryLocalServiceUtil.getCategory(getParentCategoryId());
080            }
081    
082            public MBCategory getTrashContainer() {
083                    MBCategory category = null;
084    
085                    try {
086                            category = getParentCategory();
087                    }
088                    catch (Exception e) {
089                            return null;
090                    }
091    
092                    while (category != null) {
093                            if (category.isInTrash()) {
094                                    return category;
095                            }
096    
097                            try {
098                                    category = category.getParentCategory();
099                            }
100                            catch (Exception e) {
101                                    return null;
102                            }
103                    }
104    
105                    return null;
106            }
107    
108            public boolean isInTrashContainer() {
109                    if (getTrashContainer() != null) {
110                            return true;
111                    }
112                    else {
113                            return false;
114                    }
115            }
116    
117            public boolean isRoot() {
118                    if (getParentCategoryId() ==
119                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
120    
121                            return true;
122                    }
123    
124                    return false;
125            }
126    
127    }