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.portlet.configuration.icon;
016    
017    import com.liferay.portal.kernel.portlet.configuration.icon.locator.PortletConfigurationIconLocator;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.kernel.util.SetUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.registry.collections.ServiceTrackerCollections;
024    import com.liferay.registry.collections.ServiceTrackerList;
025    import com.liferay.registry.collections.ServiceTrackerMap;
026    
027    import java.util.ArrayList;
028    import java.util.Comparator;
029    import java.util.HashSet;
030    import java.util.List;
031    import java.util.Set;
032    
033    import javax.portlet.PortletRequest;
034    import javax.portlet.PortletResponse;
035    
036    /**
037     * @author Eudaldo Alonso
038     */
039    public class PortletConfigurationIconTracker {
040    
041            public static List<PortletConfigurationIconFactory>
042                    getPortletConfigurationFactories(
043                            String portletId, PortletRequest portletRequest) {
044    
045                    List<PortletConfigurationIconFactory>
046                            portletConfigurationIconFactories = new ArrayList<>();
047    
048                    for (String path : getPaths(portletId, portletRequest)) {
049                            List<PortletConfigurationIconFactory>
050                                    portletPortletConfigurationIconFactories =
051                                            _serviceTrackerMap.getService(
052                                                    getKey(StringPool.STAR, path));
053    
054                            if (portletPortletConfigurationIconFactories != null) {
055                                    portletConfigurationIconFactories.addAll(
056                                            portletPortletConfigurationIconFactories);
057                            }
058    
059                            portletPortletConfigurationIconFactories =
060                                    _serviceTrackerMap.getService(getKey(portletId, path));
061    
062                            if (portletPortletConfigurationIconFactories != null) {
063                                    portletConfigurationIconFactories.addAll(
064                                            portletPortletConfigurationIconFactories);
065                            }
066                    }
067    
068                    return portletConfigurationIconFactories;
069            }
070    
071            public static List<PortletConfigurationIcon> getPortletConfigurationIcons(
072                    String portletId, PortletRequest portletRequest,
073                    PortletResponse portletResponse, Comparator<?> comparator) {
074    
075                    List<PortletConfigurationIcon> portletConfigurationIcons =
076                            new ArrayList<>();
077    
078                    List<PortletConfigurationIconFactory>
079                            portletConfigurationIconFactories = ListUtil.sort(
080                                    getPortletConfigurationFactories(portletId, portletRequest),
081                                    (Comparator<PortletConfigurationIconFactory>)comparator);
082    
083                    for (PortletConfigurationIconFactory portletConfigurationIconFactory :
084                                    portletConfigurationIconFactories) {
085    
086                            PortletConfigurationIcon portletConfigurationIcon =
087                                    portletConfigurationIconFactory.create(
088                                            portletRequest, portletResponse);
089    
090                            if ((portletConfigurationIcon != null) &&
091                                    portletConfigurationIcon.isShow()) {
092    
093                                    portletConfigurationIcons.add(portletConfigurationIcon);
094                            }
095                    }
096    
097                    return portletConfigurationIcons;
098            }
099    
100            protected static String getKey(String portletId, String path) {
101                    return portletId + StringPool.COLON + path;
102            }
103    
104            protected static Set<String> getPaths(
105                    String portletId, PortletRequest portletRequest) {
106    
107                    Set<String> paths = new HashSet<>();
108    
109                    for (PortletConfigurationIconLocator portletConfigurationIconLocator :
110                                    _serviceTrackerList) {
111    
112                            String path = portletConfigurationIconLocator.getPath(
113                                    portletRequest);
114    
115                            List<String> defaultViews =
116                                    portletConfigurationIconLocator.getDefaultViews(portletId);
117    
118                            String[] defaultViewsArray = ArrayUtil.toStringArray(defaultViews);
119    
120                            if (Validator.isNotNull(path)) {
121                                    paths.add(path);
122    
123                                    if (ArrayUtil.isNotEmpty(defaultViewsArray) &&
124                                            ArrayUtil.contains(defaultViewsArray, path)) {
125    
126                                            paths.add(StringPool.DASH);
127                                    }
128                            }
129                    }
130    
131                    if (SetUtil.isEmpty(paths)) {
132                            paths.add(StringPool.DASH);
133                    }
134    
135                    return paths;
136            }
137    
138            private static final ServiceTrackerList<PortletConfigurationIconLocator>
139                    _serviceTrackerList = ServiceTrackerCollections.openList(
140                            PortletConfigurationIconLocator.class);
141            private static final ServiceTrackerMap
142                    <String, List<PortletConfigurationIconFactory>>
143                            _serviceTrackerMap = ServiceTrackerCollections.openMultiValueMap(
144                                    PortletConfigurationIconFactory.class, null,
145                                    new PortletConfigurationIconServiceReferenceMapper());
146    
147    }