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.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.util.ArrayList;
027    import java.util.Collections;
028    import java.util.List;
029    import java.util.Locale;
030    
031    /**
032     * @author Sergio Gonz??lez
033     */
034    public class FormNavigatorCategoryUtil {
035    
036            public static List<FormNavigatorCategory> getFormNavigatorCategories(
037                    String formNavigatorId) {
038    
039                    List<FormNavigatorCategory> formNavigatorCategories =
040                            _instance._formNavigatorCategories.getService(formNavigatorId);
041    
042                    if (formNavigatorCategories != null) {
043                            return formNavigatorCategories;
044                    }
045    
046                    return Collections.emptyList();
047            }
048    
049            public static String[] getKeys(String formNavigatorId) {
050                    List<String> keys = new ArrayList<>();
051    
052                    List<FormNavigatorCategory> formNavigatorCategories =
053                            getFormNavigatorCategories(formNavigatorId);
054    
055                    if (ListUtil.isEmpty(formNavigatorCategories)) {
056                            return new String[] {""};
057                    }
058    
059                    for (FormNavigatorCategory formNavigatorCategory :
060                                    formNavigatorCategories) {
061    
062                            String key = formNavigatorCategory.getKey();
063    
064                            if (Validator.isNotNull(key)) {
065                                    keys.add(key);
066                            }
067                    }
068    
069                    return keys.toArray(new String[keys.size()]);
070            }
071    
072            public static String[] getLabels(String formNavigatorId, Locale locale) {
073                    List<String> labels = new ArrayList<>();
074    
075                    List<FormNavigatorCategory> formNavigatorCategories =
076                            getFormNavigatorCategories(formNavigatorId);
077    
078                    if (ListUtil.isEmpty(formNavigatorCategories)) {
079                            return new String[] {""};
080                    }
081    
082                    for (FormNavigatorCategory formNavigatorCategory :
083                                    formNavigatorCategories) {
084    
085                            String label = formNavigatorCategory.getLabel(locale);
086    
087                            if (Validator.isNotNull(label)) {
088                                    labels.add(label);
089                            }
090                    }
091    
092                    return labels.toArray(new String[labels.size()]);
093            }
094    
095            private FormNavigatorCategoryUtil() {
096                    _formNavigatorCategories = ServiceTrackerCollections.openMultiValueMap(
097                            FormNavigatorCategory.class, null,
098                            new ServiceReferenceMapper<String, FormNavigatorCategory>() {
099    
100                                    @Override
101                                    public void map(
102                                            ServiceReference<FormNavigatorCategory> serviceReference,
103                                            Emitter<String> emitter) {
104    
105                                            Registry registry = RegistryUtil.getRegistry();
106    
107                                            FormNavigatorCategory formNavigatorCategory =
108                                                    registry.getService(serviceReference);
109    
110                                            emitter.emit(formNavigatorCategory.getFormNavigatorId());
111    
112                                            registry.ungetService(serviceReference);
113                                    }
114    
115                            });
116            }
117    
118            private static final FormNavigatorCategoryUtil _instance =
119                    new FormNavigatorCategoryUtil();
120    
121            private final ServiceTrackerMap<String, List<FormNavigatorCategory>>
122                    _formNavigatorCategories;
123    
124    }