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            @Override
036            public List<AssetCategory> getAncestors() throws PortalException {
037                    List<AssetCategory> categories = new ArrayList<>();
038    
039                    AssetCategory category = this;
040    
041                    while (!category.isRootCategory()) {
042                            category = AssetCategoryLocalServiceUtil.getAssetCategory(
043                                    category.getParentCategoryId());
044    
045                            categories.add(category);
046                    }
047    
048                    return categories;
049            }
050    
051            @Override
052            public AssetCategory getParentCategory() {
053                    return AssetCategoryLocalServiceUtil.fetchCategory(
054                            getParentCategoryId());
055            }
056    
057            @Override
058            public String getPath(Locale locale) throws PortalException {
059                    List<AssetCategory> categories = getAncestors();
060    
061                    StringBundler sb = new StringBundler((categories.size() * 4) + 1);
062    
063                    AssetVocabulary vocabulary =
064                            AssetVocabularyLocalServiceUtil.getVocabulary(getVocabularyId());
065    
066                    sb.append(vocabulary.getTitle(locale));
067    
068                    for (AssetCategory category : categories) {
069                            sb.append(StringPool.SPACE);
070                            sb.append(StringPool.GREATER_THAN);
071                            sb.append(StringPool.SPACE);
072                            sb.append(category.getTitle(locale));
073                    }
074    
075                    return sb.toString();
076            }
077    
078            @Override
079            public String getTitle(String languageId) {
080                    String value = super.getTitle(languageId);
081    
082                    if (Validator.isNull(value)) {
083                            value = getName();
084                    }
085    
086                    return value;
087            }
088    
089            @Override
090            public String getTitle(String languageId, boolean useDefault) {
091                    String value = super.getTitle(languageId, useDefault);
092    
093                    if (Validator.isNull(value)) {
094                            value = getName();
095                    }
096    
097                    return value;
098            }
099    
100            @Override
101            public boolean isRootCategory() {
102                    if (getParentCategoryId() == 0) {
103                            return true;
104                    }
105    
106                    return false;
107            }
108    
109    }