001/*
002 *  Copyright 2015 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.externaldata.ui;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.acting.ServiceableAction;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Redirector;
030import org.apache.cocoon.environment.Request;
031import org.apache.cocoon.environment.SourceResolver;
032import org.apache.commons.lang.StringUtils;
033
034import org.ametys.core.cocoon.JSonReader;
035import org.ametys.core.datasource.DataSourceClientInteraction;
036import org.ametys.core.datasource.DataSourceClientInteraction.DataSourceType;
037import org.ametys.plugins.externaldata.data.DataSourceFactoryExtensionPoint;
038import org.ametys.plugins.externaldata.data.Query;
039import org.ametys.plugins.externaldata.data.QueryDao;
040import org.ametys.plugins.externaldata.data.Query.ResultType;
041
042/**
043 * SAX events for datasources types, datasources and queries
044 *
045 */
046public class GetDataSourceQueriesAction extends ServiceableAction
047{
048    /** The manager handling the data sources */
049    protected DataSourceClientInteraction _dataSourceClientInteraction;
050    /** The manager for data source factories */
051    protected DataSourceFactoryExtensionPoint _dataSourceFactoryEP;
052    /** The Query DAO. */
053    protected QueryDao _queryDao;
054    
055    @Override
056    public void service(ServiceManager smanager) throws ServiceException
057    {
058        super.service(smanager);
059        
060        _dataSourceClientInteraction = (DataSourceClientInteraction) smanager.lookup(DataSourceClientInteraction.ROLE);
061        _dataSourceFactoryEP = (DataSourceFactoryExtensionPoint) smanager.lookup(DataSourceFactoryExtensionPoint.ROLE);
062        _queryDao = (QueryDao) smanager.lookup(QueryDao.ROLE);
063    }
064
065    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
066    {
067        @SuppressWarnings("unchecked")
068        Map jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
069        
070        String siteName = (String) jsParameters.get("siteName");
071        String nodeId = (String) jsParameters.get("node");
072        String nodeType = (String) jsParameters.get("nodeType");
073        String resultTypeFilter = (String) jsParameters.get("resultType");
074        
075        ResultType resultType = null;
076        if (StringUtils.isNotEmpty(resultTypeFilter))
077        {
078            resultType = ResultType.valueOf(resultTypeFilter);
079        }
080        
081        @SuppressWarnings("unchecked")
082        List<String> dataSourceTypeFilter = (List<String>) jsParameters.get("dataSourceType");
083        
084        List<Map<String, Object>> nodes = new ArrayList<>();
085        
086        if ("root".equals(nodeType) || StringUtils.isBlank(nodeType))
087        {
088            if (dataSourceTypeFilter != null && !dataSourceTypeFilter.isEmpty())
089            {
090                for (String type : dataSourceTypeFilter)
091                {
092                    nodes.add(dataSourceTypeToJSON(DataSourceType.valueOf(type)));
093                }
094            }
095            else
096            {
097                Collection<DataSourceType> allTypes = _dataSourceFactoryEP.getAllTypes();
098                for (DataSourceType type : allTypes)
099                {
100                    nodes.add(dataSourceTypeToJSON(type));
101                }
102            }
103        }
104        else if ("dataSourceType".equals(nodeType))
105        {
106            DataSourceType dataSourceType = DataSourceType.valueOf(nodeId);
107            
108            List<Map<String, Object>> dataSources = _dataSourceClientInteraction.getDataSources(dataSourceType, false, false, false, null);
109            for (Map<String, Object> dataSource : dataSources)
110            {
111                Collection<Query> queries = _queryDao.getDataSourceQueries(siteName, (String) dataSource.get("id"), resultType).values();
112                dataSource.put("leaf", queries.isEmpty());
113                dataSource.put("type", "datasource");
114                dataSource.put("dataSourceType", dataSourceType.name());
115                nodes.add(dataSource);
116            }
117        }
118        else if ("datasource".equals(nodeType))
119        {
120            for (Query query : _queryDao.getDataSourceQueries(siteName, nodeId, null, resultType).values())
121            {
122                nodes.add(queryToJSON(query));
123            }
124        }
125        
126        Map<String, Object> result = new HashMap<>();
127        result.put("children", nodes);
128        
129        Request request = ObjectModelHelper.getRequest(objectModel);
130        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
131        
132        return EMPTY_MAP;
133    }
134    
135    /**
136     * Get the JSON representation of a type of data source
137     * @param dataSourceType The data source type ID
138     * @return The datasource type as JSON
139     * @throws Exception If an error occurred
140     */
141    protected Map<String, Object> dataSourceTypeToJSON(DataSourceType dataSourceType) throws Exception
142    {
143        Map<String, Object> infos = new HashMap<>();
144        
145        infos.put("id", dataSourceType.name());
146        infos.put("type", "dataSourceType");
147        infos.put("dataSourceType", dataSourceType.name());
148        infos.put("name", dataSourceType.name());
149        
150        List<Map<String, Object>> dataSources = _dataSourceClientInteraction.getDataSources(dataSourceType, false, false, false, null);
151        infos.put("leaf", dataSources.isEmpty());
152        
153        return infos;
154    }
155    
156    /**
157     * Gets query's properties to JSON format
158     * @param query The query
159     * @return The query's properties
160     */
161    protected Map<String, Object> queryToJSON(Query query)
162    {
163        Map<String, Object> infos = new HashMap<>();
164        
165        infos.put("id", query.getId());
166        infos.put("type", "query");
167        infos.put("dataSourceType", query.getType());
168        infos.put("name", query.getName());
169        infos.put("description", query.getDescription());
170        infos.put("resultType", query.getResultType().name());
171        infos.put("leaf", true);
172        
173        Map<String, String> additionalConf = query.getAdditionalConfiguration();
174        for (String confName : additionalConf.keySet())
175        {
176            String value = additionalConf.get(confName);
177            infos.put(confName, value);
178        }
179        
180        infos.put("parameters", query.getParameters());
181        
182        return infos;
183    }
184
185}