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