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.util;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.PrefixPredicateFilter;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.UnicodeProperties;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portlet.asset.model.AssetCategoryConstants;
026    
027    import java.util.LinkedHashSet;
028    import java.util.Set;
029    
030    /**
031     * @author Jos?? Manuel Navarro
032     */
033    public class AssetVocabularySettingsHelper {
034    
035            public static final long[] DEFAULT_SELECTED_CLASSNAME_IDS =
036                    {AssetCategoryConstants.ALL_CLASS_NAME_ID};
037    
038            public static final long[] DEFAULT_SELECTED_CLASSTYPE_PKS =
039                    {AssetCategoryConstants.ALL_CLASS_TYPE_PK};
040    
041            public AssetVocabularySettingsHelper() {
042                    super();
043    
044                    _properties = new UnicodeProperties(true);
045            }
046    
047            public AssetVocabularySettingsHelper(String propertiesString) {
048                    this();
049    
050                    _properties.fastLoad(propertiesString);
051            }
052    
053            public long[] getClassNameIds() {
054                    String[] classNameIdsAndClassTypePKs = getClassNameIdsAndClassTypePKs();
055    
056                    return getClassNameIds(classNameIdsAndClassTypePKs);
057            }
058    
059            public long[] getClassTypePKs() {
060                    String[] classNameIdsAndClassTypePKs = getClassNameIdsAndClassTypePKs();
061    
062                    return getClassTypePKs(classNameIdsAndClassTypePKs);
063            }
064    
065            public long[] getRequiredClassNameIds() {
066                    String[] classNameIdsAndClassTypePKs =
067                            getRequiredClassNameIdsAndClassTypePKs();
068    
069                    return getClassNameIds(classNameIdsAndClassTypePKs);
070            }
071    
072            public long[] getRequiredClassTypePKs() {
073                    String[] classNameIdsAndClassTypePKs =
074                            getRequiredClassNameIdsAndClassTypePKs();
075    
076                    return getClassTypePKs(classNameIdsAndClassTypePKs);
077            }
078    
079            public boolean hasClassNameIdAndClassTypePK(
080                    long classNameId, long classTypePK) {
081    
082                    return isClassNameIdAndClassTypePKSpecified(
083                            classNameId, classTypePK, getClassNameIdsAndClassTypePKs());
084            }
085    
086            public boolean isClassNameIdAndClassTypePKRequired(
087                    long classNameId, long classTypePK) {
088    
089                    return isClassNameIdAndClassTypePKSpecified(
090                            classNameId, classTypePK, getRequiredClassNameIdsAndClassTypePKs());
091            }
092    
093            public boolean isMultiValued() {
094                    String value = _properties.getProperty(_KEY_MULTI_VALUED);
095    
096                    return GetterUtil.getBoolean(value, true);
097            }
098    
099            public void setClassNameIdsAndClassTypePKs(
100                    long[] classNameIds, long[] classTypePKs, boolean[] requireds) {
101    
102                    Set<String> requiredClassNameIds = new LinkedHashSet<String>();
103                    Set<String> selectedClassNameIds = new LinkedHashSet<String>();
104    
105                    for (int i = 0; i < classNameIds.length; ++i) {
106                            long classNameId = classNameIds[i];
107                            long classTypePK = classTypePKs[i];
108                            boolean required = requireds[i];
109    
110                            String classNameIdAndClassTypePK = getClassNameIdAndClassTypePK(
111                                    classNameId, classTypePK);
112    
113                            if (classNameIdAndClassTypePK.equals(
114                                            AssetCategoryConstants.
115                                                    ALL_CLASS_NAME_IDS_AND_CLASS_TYPE_PKS)) {
116    
117                                    if (required) {
118                                            requiredClassNameIds.clear();
119    
120                                            requiredClassNameIds.add(classNameIdAndClassTypePK);
121                                    }
122    
123                                    selectedClassNameIds.clear();
124    
125                                    selectedClassNameIds.add(classNameIdAndClassTypePK);
126    
127                                    break;
128                            }
129                            else {
130                                    if (required) {
131                                            requiredClassNameIds.add(classNameIdAndClassTypePK);
132                                    }
133    
134                                    selectedClassNameIds.add(classNameIdAndClassTypePK);
135                            }
136                    }
137    
138                    _properties.setProperty(
139                            _KEY_REQUIRED_CLASS_NAME_IDS_AND_CLASS_TYPE_PKS,
140                            StringUtil.merge(requiredClassNameIds));
141                    _properties.setProperty(
142                            _KEY_SELECTED_CLASS_NAME_IDS_AND_CLASS_TYPE_PKS,
143                            StringUtil.merge(selectedClassNameIds));
144            }
145    
146            public void setMultiValued(boolean multiValued) {
147                    _properties.setProperty(_KEY_MULTI_VALUED, String.valueOf(multiValued));
148            }
149    
150            @Override
151            public String toString() {
152                    return _properties.toString();
153            }
154    
155            protected long getClassNameId(String classNameIdAndClassTypePK) {
156                    String[] parts = StringUtil.split(
157                            classNameIdAndClassTypePK, CharPool.COLON);
158    
159                    return GetterUtil.getLong(parts[0]);
160            }
161    
162            protected String getClassNameIdAndClassTypePK(
163                    long classNameId, long classTypePK) {
164    
165                    return String.valueOf(classNameId).concat(StringPool.COLON).concat(
166                            String.valueOf(classTypePK));
167            }
168    
169            protected long[] getClassNameIds(String[] classNameIdsAndClassTypePKs) {
170                    long[] classNameIds = new long[classNameIdsAndClassTypePKs.length];
171    
172                    for (int i = 0; i < classNameIdsAndClassTypePKs.length; i++) {
173                            long classNameId = getClassNameId(classNameIdsAndClassTypePKs[i]);
174    
175                            classNameIds[i] = classNameId;
176                    }
177    
178                    return classNameIds;
179            }
180    
181            protected String[] getClassNameIdsAndClassTypePKs() {
182                    String value = _properties.getProperty(
183                            _KEY_SELECTED_CLASS_NAME_IDS_AND_CLASS_TYPE_PKS);
184    
185                    if (Validator.isNull(value)) {
186                            return new String[] {
187                                    getClassNameIdAndClassTypePK(
188                                            AssetCategoryConstants.ALL_CLASS_NAME_ID,
189                                            AssetCategoryConstants.ALL_CLASS_TYPE_PK)
190                            };
191                    }
192    
193                    return StringUtil.split(value);
194            }
195    
196            protected long getClassTypePK(String classNameIdAndClassTypePK) {
197                    String[] parts = StringUtil.split(
198                            classNameIdAndClassTypePK, CharPool.COLON);
199    
200                    if (parts.length == 1) {
201                            return AssetCategoryConstants.ALL_CLASS_TYPE_PK;
202                    }
203                    else {
204                            return GetterUtil.getLong(parts[1]);
205                    }
206            }
207    
208            protected long[] getClassTypePKs(String[] classNameIdsAndClassTypePKs) {
209                    long[] classTypePKs = new long[classNameIdsAndClassTypePKs.length];
210    
211                    for (int i = 0; i < classNameIdsAndClassTypePKs.length; i++) {
212                            long classTypePK = getClassTypePK(classNameIdsAndClassTypePKs[i]);
213    
214                            classTypePKs[i] = classTypePK;
215                    }
216    
217                    return classTypePKs;
218            }
219    
220            protected String[] getRequiredClassNameIdsAndClassTypePKs() {
221                    String value = _properties.getProperty(
222                            _KEY_REQUIRED_CLASS_NAME_IDS_AND_CLASS_TYPE_PKS);
223    
224                    if (Validator.isNull(value)) {
225                            return new String[0];
226                    }
227    
228                    return StringUtil.split(value);
229            }
230    
231            protected boolean isClassNameIdAndClassTypePKSpecified(
232                    long classNameId, long classTypePK,
233                    String[] classNameIdsAndClassTypePKs) {
234    
235                    if (classNameIdsAndClassTypePKs.length == 0) {
236                            return false;
237                    }
238    
239                    if (classNameIdsAndClassTypePKs[0].equals(
240                                    AssetCategoryConstants.ALL_CLASS_NAME_IDS_AND_CLASS_TYPE_PKS)) {
241    
242                            return true;
243                    }
244    
245                    if (classTypePK == AssetCategoryConstants.ALL_CLASS_TYPE_PK) {
246                            PrefixPredicateFilter prefixPredicateFilter =
247                                    new PrefixPredicateFilter(classNameId + StringPool.COLON, true);
248    
249                            return ArrayUtil.exists(
250                                    classNameIdsAndClassTypePKs, prefixPredicateFilter);
251                    }
252                    else {
253                            String classNameIdAndClassTypePK = getClassNameIdAndClassTypePK(
254                                    classNameId, classTypePK);
255    
256                            if (ArrayUtil.contains(
257                                            classNameIdsAndClassTypePKs, classNameIdAndClassTypePK)) {
258    
259                                    return true;
260                            }
261    
262                            String classNameIdAndAllClassTypePK = getClassNameIdAndClassTypePK(
263                                    classNameId, AssetCategoryConstants.ALL_CLASS_TYPE_PK);
264    
265                            return ArrayUtil.contains(
266                                    classNameIdsAndClassTypePKs, classNameIdAndAllClassTypePK);
267                    }
268            }
269    
270            private static final String _KEY_MULTI_VALUED = "multiValued";
271    
272            private static final String
273                    _KEY_REQUIRED_CLASS_NAME_IDS_AND_CLASS_TYPE_PKS =
274                            "requiredClassNameIds";
275    
276            private static final String
277                    _KEY_SELECTED_CLASS_NAME_IDS_AND_CLASS_TYPE_PKS =
278                            "selectedClassNameIds";
279    
280            private UnicodeProperties _properties;
281    
282    }