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