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