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.editor.configuration.EditorConfigContributor;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.theme.ThemeDisplay;
022    import com.liferay.portlet.RequestBackedPortletURLFactory;
023    import com.liferay.registry.collections.ServiceReferenceMapper;
024    import com.liferay.registry.collections.ServiceTrackerCollections;
025    import com.liferay.registry.collections.ServiceTrackerMap;
026    
027    import java.util.Iterator;
028    import java.util.List;
029    import java.util.Map;
030    
031    /**
032     * @author Sergio Gonz??lez
033     */
034    public class EditorConfigProvider
035            extends BaseEditorConfigurationProvider<EditorConfigContributor> {
036    
037            public JSONObject getConfigJSONObject(
038                    String portletName, String editorConfigKey, String editorName,
039                    Map<String, Object> inputEditorTaglibAttributes,
040                    ThemeDisplay themeDisplay,
041                    RequestBackedPortletURLFactory requestBackedPortletURLFactory) {
042    
043                    JSONObject configJSONObject = JSONFactoryUtil.createJSONObject();
044    
045                    List<EditorConfigContributor> editorConfigContributors =
046                            getContributors(portletName, editorConfigKey, editorName);
047    
048                    Iterator<EditorConfigContributor> iterator = ListUtil.reverseIterator(
049                            editorConfigContributors);
050    
051                    while (iterator.hasNext()) {
052                            EditorConfigContributor editorConfigContributor = iterator.next();
053    
054                            editorConfigContributor.populateConfigJSONObject(
055                                    configJSONObject, inputEditorTaglibAttributes, themeDisplay,
056                                    requestBackedPortletURLFactory);
057                    }
058    
059                    return configJSONObject;
060            }
061    
062            @Override
063            protected ServiceTrackerMap<String, List<EditorConfigContributor>>
064                    getServiceTrackerMap() {
065    
066                    return _serviceTrackerMap;
067            }
068    
069            private static final ServiceReferenceMapper<String, EditorConfigContributor>
070                    _serviceReferenceMapper = new EditorServiceReferenceMapper<>();
071            private static final ServiceTrackerMap
072                    <String, List<EditorConfigContributor>>
073                            _serviceTrackerMap = ServiceTrackerCollections.openMultiValueMap(
074                                    EditorConfigContributor.class,
075                                    "(|(editor.config.key=*)(editor.name=*)" +
076                                            "(javax.portlet.name=*)(objectClass=" +
077                                                    EditorConfigContributor.class.getName() + "))",
078                                    _serviceReferenceMapper);
079    
080    }