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