001
014
015 package com.liferay.portal.kernel.servlet.taglib.ui;
016
017 import com.liferay.portal.kernel.util.ListUtil;
018 import com.liferay.portal.kernel.util.Validator;
019 import com.liferay.registry.Registry;
020 import com.liferay.registry.RegistryUtil;
021 import com.liferay.registry.ServiceReference;
022 import com.liferay.registry.collections.ServiceReferenceMapper;
023 import com.liferay.registry.collections.ServiceTrackerCollections;
024 import com.liferay.registry.collections.ServiceTrackerMap;
025
026 import java.io.Serializable;
027
028 import java.util.ArrayList;
029 import java.util.Collections;
030 import java.util.Comparator;
031 import java.util.List;
032 import java.util.Locale;
033
034
037 public class FormNavigatorCategoryUtil {
038
039 public static List<FormNavigatorCategory> getFormNavigatorCategories(
040 String formNavigatorId) {
041
042 List<FormNavigatorCategory> formNavigatorCategories =
043 _instance._formNavigatorCategories.getService(formNavigatorId);
044
045 if (formNavigatorCategories != null) {
046 return formNavigatorCategories;
047 }
048
049 return Collections.emptyList();
050 }
051
052 public static String[] getKeys(String formNavigatorId) {
053 List<String> keys = new ArrayList<>();
054
055 List<FormNavigatorCategory> formNavigatorCategories =
056 getFormNavigatorCategories(formNavigatorId);
057
058 if (ListUtil.isEmpty(formNavigatorCategories)) {
059 return new String[] {""};
060 }
061
062 for (FormNavigatorCategory formNavigatorCategory :
063 formNavigatorCategories) {
064
065 String key = formNavigatorCategory.getKey();
066
067 if (Validator.isNotNull(key)) {
068 keys.add(key);
069 }
070 }
071
072 return keys.toArray(new String[keys.size()]);
073 }
074
075 public static String[] getLabels(String formNavigatorId, Locale locale) {
076 List<String> labels = new ArrayList<>();
077
078 List<FormNavigatorCategory> formNavigatorCategories =
079 getFormNavigatorCategories(formNavigatorId);
080
081 if (ListUtil.isEmpty(formNavigatorCategories)) {
082 return new String[] {""};
083 }
084
085 for (FormNavigatorCategory formNavigatorCategory :
086 formNavigatorCategories) {
087
088 String label = formNavigatorCategory.getLabel(locale);
089
090 if (Validator.isNotNull(label)) {
091 labels.add(label);
092 }
093 }
094
095 return labels.toArray(new String[labels.size()]);
096 }
097
098 private FormNavigatorCategoryUtil() {
099 _formNavigatorCategories = ServiceTrackerCollections.openMultiValueMap(
100 FormNavigatorCategory.class, null,
101 new ServiceReferenceMapper<String, FormNavigatorCategory>() {
102
103 @Override
104 public void map(
105 ServiceReference<FormNavigatorCategory> serviceReference,
106 Emitter<String> emitter) {
107
108 Registry registry = RegistryUtil.getRegistry();
109
110 FormNavigatorCategory formNavigatorCategory =
111 registry.getService(serviceReference);
112
113 emitter.emit(formNavigatorCategory.getFormNavigatorId());
114
115 registry.ungetService(serviceReference);
116 }
117
118 },
119 new PropertyServiceReferenceComparator<FormNavigatorCategory>(
120 "form.navigator.category.order"));
121 }
122
123 private static final FormNavigatorCategoryUtil _instance =
124 new FormNavigatorCategoryUtil();
125
126 private final ServiceTrackerMap<String, List<FormNavigatorCategory>>
127 _formNavigatorCategories;
128
129
132 private class PropertyServiceReferenceComparator<T>
133 implements Comparator<ServiceReference<T>>, Serializable {
134
135 public PropertyServiceReferenceComparator(String propertyKey) {
136 _propertyKey = propertyKey;
137 }
138
139 @Override
140 public int compare(
141 ServiceReference<T> serviceReference1,
142 ServiceReference<T> serviceReference2) {
143
144 if (serviceReference1 == null) {
145 if (serviceReference2 == null) {
146 return 0;
147 }
148 else {
149 return 1;
150 }
151 }
152 else if (serviceReference2 == null) {
153 return -1;
154 }
155
156 Object propertyValue1 = serviceReference1.getProperty(_propertyKey);
157 Object propertyValue2 = serviceReference2.getProperty(_propertyKey);
158
159 if (propertyValue1 == null) {
160 if (propertyValue2 == null) {
161 return 0;
162 }
163 else {
164 return 1;
165 }
166 }
167 else if (propertyValue2 == null) {
168 return -1;
169 }
170
171 if (!(propertyValue2 instanceof Comparable)) {
172 return serviceReference2.compareTo(serviceReference1);
173 }
174
175 Comparable<Object> propertyValueComparable2 =
176 (Comparable<Object>)propertyValue2;
177
178 return propertyValueComparable2.compareTo(propertyValue1);
179 }
180
181 private final String _propertyKey;
182
183 }
184
185 }