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.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.kernel.util.PredicateFilter;
022    import com.liferay.portal.kernel.util.UnicodeProperties;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.service.GroupLocalServiceUtil;
026    import com.liferay.portlet.asset.model.AssetCategory;
027    import com.liferay.portlet.asset.model.AssetCategoryConstants;
028    import com.liferay.portlet.asset.model.AssetVocabulary;
029    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
030    import com.liferay.portlet.asset.util.AssetVocabularySettingsHelper;
031    
032    import java.util.List;
033    import java.util.Locale;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Juan Fern??ndez
038     */
039    public class AssetVocabularyImpl extends AssetVocabularyBaseImpl {
040    
041            @Override
042            public List<AssetCategory> getCategories() {
043                    return AssetCategoryLocalServiceUtil.getVocabularyCategories(
044                            getVocabularyId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
045            }
046    
047            @Override
048            public int getCategoriesCount() {
049                    return AssetCategoryLocalServiceUtil.getVocabularyCategoriesCount(
050                            getVocabularyId());
051            }
052    
053            @Override
054            public long[] getRequiredClassNameIds() {
055                    AssetVocabularySettingsHelper vocabularySettingsHelper =
056                            getVocabularySettingsHelper();
057    
058                    return vocabularySettingsHelper.getRequiredClassNameIds();
059            }
060    
061            @Override
062            public long[] getSelectedClassNameIds() {
063                    AssetVocabularySettingsHelper vocabularySettingsHelper =
064                            getVocabularySettingsHelper();
065    
066                    return vocabularySettingsHelper.getClassNameIds();
067            }
068    
069            @Override
070            public long[] getSelectedClassTypePKs() {
071                    AssetVocabularySettingsHelper vocabularySettingsHelper =
072                            getVocabularySettingsHelper();
073    
074                    return vocabularySettingsHelper.getClassTypePKs();
075            }
076    
077            @Override
078            public String getSettings() {
079                    if (_vocabularySettingsHelper == null) {
080                            return super.getSettings();
081                    }
082                    else {
083                            return _vocabularySettingsHelper.toString();
084                    }
085            }
086    
087            /**
088             * @deprecated As of 7.0.0, with no direct replacement
089             */
090            @Deprecated
091            @Override
092            public UnicodeProperties getSettingsProperties() {
093                    AssetVocabularySettingsHelper vocabularySettingsHelper =
094                            getVocabularySettingsHelper();
095    
096                    UnicodeProperties settingsProperties = new UnicodeProperties(true);
097    
098                    settingsProperties.fastLoad(vocabularySettingsHelper.toString());
099    
100                    return settingsProperties;
101            }
102    
103            @Override
104            public String getTitle(String languageId) {
105                    String value = super.getTitle(languageId);
106    
107                    if (Validator.isNull(value)) {
108                            value = getName();
109                    }
110    
111                    return value;
112            }
113    
114            @Override
115            public String getTitle(String languageId, boolean useDefault) {
116                    String value = super.getTitle(languageId, useDefault);
117    
118                    if (Validator.isNull(value)) {
119                            value = getName();
120                    }
121    
122                    return value;
123            }
124    
125            @Override
126            public String getUnambiguousTitle(
127                            List<AssetVocabulary> vocabularies, long groupId,
128                            final Locale locale)
129                    throws PortalException {
130    
131                    if (getGroupId() == groupId ) {
132                            return getTitle(locale);
133                    }
134    
135                    boolean hasAmbiguousTitle = ListUtil.exists(
136                            vocabularies,
137                            new PredicateFilter<AssetVocabulary>() {
138    
139                                    @Override
140                                    public boolean filter(AssetVocabulary vocabulary) {
141                                            String title = vocabulary.getTitle(locale);
142    
143                                            if (title.equals(getTitle(locale)) &&
144                                                    (vocabulary.getVocabularyId() != getVocabularyId())) {
145    
146                                                    return true;
147                                            }
148    
149                                            return false;
150                                    }
151    
152                            });
153    
154                    if (hasAmbiguousTitle) {
155                            Group group = GroupLocalServiceUtil.getGroup(getGroupId());
156    
157                            return group.getUnambiguousName(getTitle(locale), locale);
158                    }
159    
160                    return getTitle(locale);
161            }
162    
163            @Override
164            public boolean hasMoreThanOneCategorySelected(final long[] categoryIds) {
165                    PredicateFilter<AssetCategory> predicateFilter =
166                            new PredicateFilter<AssetCategory>() {
167    
168                                    @Override
169                                    public boolean filter(AssetCategory assetCategory) {
170                                            return ArrayUtil.contains(
171                                                    categoryIds, assetCategory.getCategoryId());
172                                    }
173    
174                            };
175    
176                    if (ListUtil.count(getCategories(), predicateFilter) > 1) {
177                            return true;
178                    }
179    
180                    return false;
181            }
182    
183            @Override
184            public boolean isAssociatedToClassNameId(long classNameId) {
185                    return isAssociatedToClassNameIdAndClassTypePK(
186                            classNameId, AssetCategoryConstants.ALL_CLASS_TYPE_PK);
187            }
188    
189            @Override
190            public boolean isAssociatedToClassNameIdAndClassTypePK(
191                    long classNameId, long classTypePK) {
192    
193                    AssetVocabularySettingsHelper vocabularySettingsHelper =
194                            getVocabularySettingsHelper();
195    
196                    return vocabularySettingsHelper.hasClassNameIdAndClassTypePK(
197                            classNameId, classTypePK);
198            }
199    
200            @Override
201            public boolean isMissingRequiredCategory(
202                    long classNameId, long classTypePK, final long[] categoryIds) {
203    
204                    if (!isRequired(classNameId, classTypePK)) {
205                            return false;
206                    }
207    
208                    PredicateFilter<AssetCategory> predicateFilter =
209                            new PredicateFilter<AssetCategory>() {
210    
211                                    @Override
212                                    public boolean filter(AssetCategory assetCategory) {
213                                            return ArrayUtil.contains(
214                                                    categoryIds, assetCategory.getCategoryId());
215                                    }
216    
217                            };
218    
219                    return !ListUtil.exists(getCategories(), predicateFilter);
220            }
221    
222            @Override
223            public boolean isMultiValued() {
224                    AssetVocabularySettingsHelper vocabularySettingsHelper =
225                            getVocabularySettingsHelper();
226    
227                    return vocabularySettingsHelper.isMultiValued();
228            }
229    
230            /**
231             * @deprecated As of 7.0.0, replaced by {@link #isRequired(long, long)}
232             */
233            @Deprecated
234            @Override
235            public boolean isRequired(long classNameId) {
236                    return isRequired(
237                            classNameId, AssetCategoryConstants.ALL_CLASS_TYPE_PK);
238            }
239    
240            @Override
241            public boolean isRequired(long classNameId, long classTypePK) {
242                    AssetVocabularySettingsHelper vocabularySettingsHelper =
243                            getVocabularySettingsHelper();
244    
245                    return vocabularySettingsHelper.isClassNameIdAndClassTypePKRequired(
246                            classNameId, classTypePK);
247            }
248    
249            @Override
250            public void setSettings(String settings) {
251                    _vocabularySettingsHelper = null;
252    
253                    super.setSettings(settings);
254            }
255    
256            /**
257             * @deprecated As of 7.0.0, with no direct replacement
258             */
259            @Deprecated
260            @Override
261            public void setSettingsProperties(UnicodeProperties settingsProperties) {
262                    super.setSettings(settingsProperties.toString());
263    
264                    _vocabularySettingsHelper = getVocabularySettingsHelper();
265            }
266    
267            protected AssetVocabularySettingsHelper getVocabularySettingsHelper() {
268                    if (_vocabularySettingsHelper == null) {
269                            _vocabularySettingsHelper = new AssetVocabularySettingsHelper(
270                                    super.getSettings());
271                    }
272    
273                    return _vocabularySettingsHelper;
274            }
275    
276            private AssetVocabularySettingsHelper _vocabularySettingsHelper;
277    
278    }