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.editor.configuration;
016    
017    import com.liferay.portal.kernel.util.ListUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.registry.ServiceReference;
022    import com.liferay.registry.collections.ServiceReferenceMapper;
023    import com.liferay.registry.collections.ServiceTrackerMap;
024    import com.liferay.registry.util.StringPlus;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    
029    /**
030     * @author Sergio Gonz??lez
031     */
032    public abstract class BaseEditorConfigurationProvider<T> {
033    
034            protected static String getKey(
035                    String portletName, String editorConfigKey, String editorName) {
036    
037                    if (Validator.isNull(portletName)) {
038                            portletName = "null";
039                    }
040    
041                    if (Validator.isNull(editorConfigKey)) {
042                            editorConfigKey = "null";
043                    }
044    
045                    if (Validator.isNull(editorName)) {
046                            editorName = "null";
047                    }
048    
049                    StringBundler sb = new StringBundler(5);
050    
051                    sb.append(portletName);
052                    sb.append(StringPool.PERIOD);
053                    sb.append(editorConfigKey);
054                    sb.append(StringPool.PERIOD);
055                    sb.append(editorName);
056    
057                    return sb.toString();
058            }
059    
060            protected List<T> getContributors(
061                    String portletName, String editorConfigKey, String editorName) {
062    
063                    List<T> contributors = new ArrayList<>();
064    
065                    populateContributor(
066                            contributors, getKey(portletName, editorConfigKey, editorName));
067    
068                    populateContributor(
069                            contributors, getKey(portletName, editorConfigKey, null));
070    
071                    populateContributor(
072                            contributors, getKey(null, editorConfigKey, editorName));
073    
074                    populateContributor(
075                            contributors, getKey(portletName, null, editorName));
076    
077                    populateContributor(contributors, getKey(null, editorConfigKey, null));
078    
079                    populateContributor(contributors, getKey(portletName, null, null));
080    
081                    populateContributor(contributors, getKey(null, null, editorName));
082    
083                    populateContributor(contributors, getKey(null, null, null));
084    
085                    return contributors;
086            }
087    
088            protected abstract ServiceTrackerMap<String, List<T>>
089                    getServiceTrackerMap();
090    
091            protected void populateContributor(List<T> contributors, String key) {
092                    ServiceTrackerMap<String, List<T>> serviceTrackerMap =
093                            getServiceTrackerMap();
094    
095                    List<T> curContributors = serviceTrackerMap.getService(key);
096    
097                    if (ListUtil.isNotEmpty(curContributors)) {
098                            contributors.addAll(curContributors);
099                    }
100            }
101    
102            protected static class EditorServiceReferenceMapper<T>
103                    implements ServiceReferenceMapper<String, T> {
104    
105                    @Override
106                    public void map(
107                            ServiceReference<T> serviceReference, Emitter<String> emitter) {
108    
109                            List<String> portletNames = StringPlus.asList(
110                                    serviceReference.getProperty("javax.portlet.name"));
111    
112                            if (portletNames.isEmpty()) {
113                                    portletNames.add(StringPool.BLANK);
114                            }
115    
116                            List<String> editorConfigKeys = StringPlus.asList(
117                                    serviceReference.getProperty("editor.config.key"));
118    
119                            if (editorConfigKeys.isEmpty()) {
120                                    editorConfigKeys.add(StringPool.BLANK);
121                            }
122    
123                            List<String> editorNames = StringPlus.asList(
124                                    serviceReference.getProperty("editor.name"));
125    
126                            if (editorNames.isEmpty()) {
127                                    editorNames.add(StringPool.BLANK);
128                            }
129    
130                            for (String portletName : portletNames) {
131                                    for (String editorConfigKey : editorConfigKeys) {
132                                            for (String editorName : editorNames) {
133                                                    emitter.emit(
134                                                            getKey(portletName, editorConfigKey, editorName));
135                                            }
136                                    }
137                            }
138                    }
139    
140            }
141    
142    }