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    import com.liferay.portlet.trash.model.TrashEntry;
023    import com.liferay.portlet.trash.service.TrashEntryLocalServiceUtil;
024    
025    import java.util.ArrayList;
026    import java.util.List;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class MBCategoryImpl extends MBCategoryBaseImpl {
032    
033            public MBCategoryImpl() {
034            }
035    
036            @Override
037            public List<Long> getAncestorCategoryIds()
038                    throws PortalException, SystemException {
039    
040                    List<Long> ancestorCategoryIds = new ArrayList<Long>();
041    
042                    MBCategory category = this;
043    
044                    while (!category.isRoot()) {
045                            category = MBCategoryLocalServiceUtil.getCategory(
046                                    category.getParentCategoryId());
047    
048                            ancestorCategoryIds.add(category.getCategoryId());
049                    }
050    
051                    return ancestorCategoryIds;
052            }
053    
054            @Override
055            public List<MBCategory> getAncestors()
056                    throws PortalException, SystemException {
057    
058                    List<MBCategory> ancestors = new ArrayList<MBCategory>();
059    
060                    MBCategory category = this;
061    
062                    while (!category.isRoot()) {
063                            category = category.getParentCategory();
064    
065                            ancestors.add(category);
066                    }
067    
068                    return ancestors;
069            }
070    
071            @Override
072            public MBCategory getParentCategory()
073                    throws PortalException, SystemException {
074    
075                    long parentCategoryId = getParentCategoryId();
076    
077                    if ((parentCategoryId ==
078                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
079                            (parentCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
080    
081                            return null;
082                    }
083    
084                    return MBCategoryLocalServiceUtil.getCategory(getParentCategoryId());
085            }
086    
087            @Override
088            public boolean isInTrashExplicitly() throws SystemException {
089                    if (!isInTrash()) {
090                            return false;
091                    }
092    
093                    TrashEntry trashEntry = TrashEntryLocalServiceUtil.fetchEntry(
094                            getModelClassName(), getTrashEntryClassPK());
095    
096                    if (trashEntry != null) {
097                            return true;
098                    }
099    
100                    return false;
101            }
102    
103            @Override
104            public boolean isRoot() {
105                    if (getParentCategoryId() ==
106                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
107    
108                            return true;
109                    }
110    
111                    return false;
112            }
113    
114    }