001
014
015 package com.liferay.portal.kernel.servlet.taglib.ui;
016
017 import com.liferay.portal.kernel.model.User;
018 import com.liferay.portal.kernel.util.ListUtil;
019 import com.liferay.portal.kernel.util.StringPool;
020 import com.liferay.portal.kernel.util.Validator;
021 import com.liferay.registry.Registry;
022 import com.liferay.registry.RegistryUtil;
023 import com.liferay.registry.ServiceReference;
024 import com.liferay.registry.collections.ServiceReferenceMapper;
025 import com.liferay.registry.collections.ServiceTrackerCollections;
026 import com.liferay.registry.collections.ServiceTrackerMap;
027
028 import java.io.Serializable;
029
030 import java.util.ArrayList;
031 import java.util.Comparator;
032 import java.util.List;
033 import java.util.Locale;
034
035
038 public class FormNavigatorEntryUtil {
039
040 public static <T> List<FormNavigatorEntry<T>> getFormNavigatorEntries(
041 String formNavigatorId, String categoryKey, User user,
042 T formModelBean) {
043
044 @SuppressWarnings("rawtypes")
045 List<FormNavigatorEntry<T>> formNavigatorEntries =
046 (List)_instance._formNavigatorEntries.getService(
047 _getKey(formNavigatorId, categoryKey));
048
049 return filterVisibleFormNavigatorEntries(
050 formNavigatorEntries, user, formModelBean);
051 }
052
053 public static <T> List<FormNavigatorEntry<T>> getFormNavigatorEntries(
054 String formNavigatorId, User user, T formModelBean) {
055
056 List<FormNavigatorEntry<T>> formNavigatorEntries = new ArrayList<>();
057
058 String[] categoryKeys = FormNavigatorCategoryUtil.getKeys(
059 formNavigatorId);
060
061 for (String categoryKey : categoryKeys) {
062
063 @SuppressWarnings("rawtypes")
064 List<FormNavigatorEntry<T>> curFormNavigatorEntries =
065 (List)_instance._formNavigatorEntries.getService(
066 _getKey(formNavigatorId, categoryKey));
067
068 if (curFormNavigatorEntries != null) {
069 formNavigatorEntries.addAll(curFormNavigatorEntries);
070 }
071 }
072
073 return filterVisibleFormNavigatorEntries(
074 formNavigatorEntries, user, formModelBean);
075 }
076
077 public static <T> String[] getKeys(
078 String formNavigatorId, String categoryKey, User user,
079 T formModelBean) {
080
081 List<String> keys = new ArrayList<>();
082
083 List<FormNavigatorEntry<T>> formNavigatorEntries =
084 getFormNavigatorEntries(
085 formNavigatorId, categoryKey, user, formModelBean);
086
087 for (FormNavigatorEntry<T> formNavigatorEntry : formNavigatorEntries) {
088 String key = formNavigatorEntry.getKey();
089
090 if (Validator.isNotNull(key)) {
091 keys.add(key);
092 }
093 }
094
095 return keys.toArray(new String[keys.size()]);
096 }
097
098 public static <T> String[] getLabels(
099 String formNavigatorId, String categoryKey, User user, T formModelBean,
100 Locale locale) {
101
102 List<String> labels = new ArrayList<>();
103
104 List<FormNavigatorEntry<T>> formNavigatorEntries =
105 getFormNavigatorEntries(
106 formNavigatorId, categoryKey, user, formModelBean);
107
108 for (FormNavigatorEntry<T> formNavigatorEntry : formNavigatorEntries) {
109 String label = formNavigatorEntry.getLabel(locale);
110
111 if (Validator.isNotNull(label)) {
112 labels.add(label);
113 }
114 }
115
116 return labels.toArray(new String[labels.size()]);
117 }
118
119 protected static <T> List<FormNavigatorEntry<T>>
120 filterVisibleFormNavigatorEntries(
121 List<FormNavigatorEntry<T>> formNavigatorEntries, User user,
122 T formModelBean) {
123
124 List<FormNavigatorEntry<T>> filterFormNavigatorEntries =
125 new ArrayList<>();
126
127 if (ListUtil.isEmpty(formNavigatorEntries)) {
128 return filterFormNavigatorEntries;
129 }
130
131 for (FormNavigatorEntry<T> formNavigatorEntry : formNavigatorEntries) {
132 if (formNavigatorEntry.isVisible(user, formModelBean)) {
133 filterFormNavigatorEntries.add(formNavigatorEntry);
134 }
135 }
136
137 return filterFormNavigatorEntries;
138 }
139
140 private static String _getKey(String formNavigatorId, String categoryKey) {
141 return formNavigatorId + StringPool.PERIOD + categoryKey;
142 }
143
144 @SuppressWarnings("rawtypes")
145 private FormNavigatorEntryUtil() {
146 _formNavigatorEntries = ServiceTrackerCollections.openMultiValueMap(
147 FormNavigatorEntry.class, null,
148 new ServiceReferenceMapper<String, FormNavigatorEntry>() {
149
150 @Override
151 public void map(
152 ServiceReference<FormNavigatorEntry> serviceReference,
153 Emitter<String> emitter) {
154
155 Registry registry = RegistryUtil.getRegistry();
156
157 FormNavigatorEntry<?> formNavigatorEntry =
158 registry.getService(serviceReference);
159
160 emitter.emit(
161 _getKey(
162 formNavigatorEntry.getFormNavigatorId(),
163 formNavigatorEntry.getCategoryKey()));
164
165 registry.ungetService(serviceReference);
166 }
167
168 },
169 new PropertyServiceReferenceComparator<FormNavigatorEntry>(
170 "form.navigator.entry.order"));
171 }
172
173 private static final FormNavigatorEntryUtil _instance =
174 new FormNavigatorEntryUtil();
175
176 @SuppressWarnings("rawtypes")
177 private final ServiceTrackerMap<String, List<FormNavigatorEntry>>
178 _formNavigatorEntries;
179
180
183 private class PropertyServiceReferenceComparator<T>
184 implements Comparator<ServiceReference<T>>, Serializable {
185
186 public PropertyServiceReferenceComparator(String propertyKey) {
187 _propertyKey = propertyKey;
188 }
189
190 @Override
191 public int compare(
192 ServiceReference<T> serviceReference1,
193 ServiceReference<T> serviceReference2) {
194
195 if (serviceReference1 == null) {
196 if (serviceReference2 == null) {
197 return 0;
198 }
199 else {
200 return 1;
201 }
202 }
203 else if (serviceReference2 == null) {
204 return -1;
205 }
206
207 Object propertyValue1 = serviceReference1.getProperty(_propertyKey);
208 Object propertyValue2 = serviceReference2.getProperty(_propertyKey);
209
210 if (propertyValue1 == null) {
211 if (propertyValue2 == null) {
212 return 0;
213 }
214 else {
215 return 1;
216 }
217 }
218 else if (propertyValue2 == null) {
219 return -1;
220 }
221
222 if (!(propertyValue2 instanceof Comparable)) {
223 return serviceReference2.compareTo(serviceReference1);
224 }
225
226 Comparable<Object> propertyValueComparable2 =
227 (Comparable<Object>)propertyValue2;
228
229 return propertyValueComparable2.compareTo(propertyValue1);
230 }
231
232 private final String _propertyKey;
233
234 }
235
236 }