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