001    /**
002     * Copyright (c) 2000-present 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.portlet.messageboards.model.MBCategory;
019    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
020    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class MBCategoryImpl extends MBCategoryBaseImpl {
029    
030            public MBCategoryImpl() {
031            }
032    
033            @Override
034            public List<Long> getAncestorCategoryIds() throws PortalException {
035                    List<Long> ancestorCategoryIds = new ArrayList<Long>();
036    
037                    MBCategory category = this;
038    
039                    while (!category.isRoot()) {
040                            category = MBCategoryLocalServiceUtil.getCategory(
041                                    category.getParentCategoryId());
042    
043                            ancestorCategoryIds.add(category.getCategoryId());
044                    }
045    
046                    return ancestorCategoryIds;
047            }
048    
049            @Override
050            public List<MBCategory> getAncestors() throws PortalException {
051                    List<MBCategory> ancestors = new ArrayList<MBCategory>();
052    
053                    MBCategory category = this;
054    
055                    while (!category.isRoot()) {
056                            category = category.getParentCategory();
057    
058                            ancestors.add(category);
059                    }
060    
061                    return ancestors;
062            }
063    
064            @Override
065            public MBCategory getParentCategory() throws PortalException {
066                    long parentCategoryId = getParentCategoryId();
067    
068                    if ((parentCategoryId ==
069                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
070                            (parentCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
071    
072                            return null;
073                    }
074    
075                    return MBCategoryLocalServiceUtil.getCategory(getParentCategoryId());
076            }
077    
078            @Override
079            public boolean isRoot() {
080                    if (getParentCategoryId() ==
081                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
082    
083                            return true;
084                    }
085    
086                    return false;
087            }
088    
089    }