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            @Override
031            public List<Long> getAncestorCategoryIds() throws PortalException {
032                    List<Long> ancestorCategoryIds = new ArrayList<>();
033    
034                    MBCategory category = this;
035    
036                    while (!category.isRoot()) {
037                            category = MBCategoryLocalServiceUtil.getCategory(
038                                    category.getParentCategoryId());
039    
040                            ancestorCategoryIds.add(category.getCategoryId());
041                    }
042    
043                    return ancestorCategoryIds;
044            }
045    
046            @Override
047            public List<MBCategory> getAncestors() throws PortalException {
048                    List<MBCategory> ancestors = new ArrayList<>();
049    
050                    MBCategory category = this;
051    
052                    while (!category.isRoot()) {
053                            category = category.getParentCategory();
054    
055                            ancestors.add(category);
056                    }
057    
058                    return ancestors;
059            }
060    
061            @Override
062            public MBCategory getParentCategory() throws PortalException {
063                    long parentCategoryId = getParentCategoryId();
064    
065                    if ((parentCategoryId ==
066                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
067                            (parentCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
068    
069                            return null;
070                    }
071    
072                    return MBCategoryLocalServiceUtil.getCategory(getParentCategoryId());
073            }
074    
075            @Override
076            public boolean isRoot() {
077                    if (getParentCategoryId() ==
078                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
079    
080                            return true;
081                    }
082    
083                    return false;
084            }
085    
086    }