001    /**
002     * Copyright (c) 2000-2013 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            @Override
035            public List<Long> getAncestorCategoryIds()
036                    throws PortalException, SystemException {
037    
038                    List<Long> ancestorCategoryIds = new ArrayList<Long>();
039    
040                    MBCategory category = this;
041    
042                    while (true) {
043                            if (!category.isRoot()) {
044                                    category = MBCategoryLocalServiceUtil.getCategory(
045                                            category.getParentCategoryId());
046    
047                                    ancestorCategoryIds.add(category.getCategoryId());
048                            }
049                            else {
050                                    break;
051                            }
052                    }
053    
054                    return ancestorCategoryIds;
055            }
056    
057            @Override
058            public List<MBCategory> getAncestors()
059                    throws PortalException, SystemException {
060    
061                    List<MBCategory> ancestors = new ArrayList<MBCategory>();
062    
063                    MBCategory category = this;
064    
065                    while (true) {
066                            if (!category.isRoot()) {
067                                    category = MBCategoryLocalServiceUtil.getCategory(
068                                            category.getParentCategoryId());
069    
070                                    ancestors.add(category);
071                            }
072                            else {
073                                    break;
074                            }
075                    }
076    
077                    return ancestors;
078            }
079    
080            @Override
081            public boolean isRoot() {
082                    if (getParentCategoryId() ==
083                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
084    
085                            return true;
086                    }
087                    else {
088                            return false;
089                    }
090            }
091    
092    }