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.asset.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portlet.asset.model.AssetCategory;
022    import com.liferay.portlet.asset.model.AssetVocabulary;
023    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
024    import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    import java.util.Locale;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class AssetCategoryImpl extends AssetCategoryBaseImpl {
034    
035            public AssetCategoryImpl() {
036            }
037    
038            @Override
039            public List<AssetCategory> getAncestors() throws PortalException {
040                    List<AssetCategory> categories = new ArrayList<AssetCategory>();
041    
042                    AssetCategory category = this;
043    
044                    while (!category.isRootCategory()) {
045                            category = AssetCategoryLocalServiceUtil.getAssetCategory(
046                                    category.getParentCategoryId());
047    
048                            categories.add(category);
049                    }
050    
051                    return categories;
052            }
053    
054            @Override
055            public AssetCategory getParentCategory() {
056                    return AssetCategoryLocalServiceUtil.fetchCategory(
057                            getParentCategoryId());
058            }
059    
060            @Override
061            public String getPath(Locale locale) throws PortalException {
062                    List<AssetCategory> categories = getAncestors();
063    
064                    StringBundler sb = new StringBundler((categories.size() * 4) + 1);
065    
066                    AssetVocabulary vocabulary =
067                            AssetVocabularyLocalServiceUtil.getVocabulary(getVocabularyId());
068    
069                    sb.append(vocabulary.getTitle(locale));
070    
071                    for (AssetCategory category : categories) {
072                            sb.append(StringPool.SPACE);
073                            sb.append(StringPool.GREATER_THAN);
074                            sb.append(StringPool.SPACE);
075                            sb.append(category.getTitle(locale));
076                    }
077    
078                    return sb.toString();
079            }
080    
081            @Override
082            public String getTitle(String languageId) {
083                    String value = super.getTitle(languageId);
084    
085                    if (Validator.isNull(value)) {
086                            value = getName();
087                    }
088    
089                    return value;
090            }
091    
092            @Override
093            public String getTitle(String languageId, boolean useDefault) {
094                    String value = super.getTitle(languageId, useDefault);
095    
096                    if (Validator.isNull(value)) {
097                            value = getName();
098                    }
099    
100                    return value;
101            }
102    
103            @Override
104            public boolean isRootCategory() {
105                    if (getParentCategoryId() == 0) {
106                            return true;
107                    }
108    
109                    return false;
110            }
111    
112    }