001/*
002 *  Copyright 2016 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.runtime.plugins.admin.datasource;
017
018import java.io.IOException;
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.xml.sax.SAXException;
028
029import org.ametys.core.datasource.AbstractDataSourceManager.DataSourceDefinition;
030import org.ametys.core.datasource.DataSourceClientInteraction.DataSourceType;
031import org.ametys.core.datasource.DataSourceConsumer.TypeOfUse;
032import org.ametys.core.datasource.DataSourceConsumerExtensionPoint;
033import org.ametys.core.datasource.LDAPDataSourceManager;
034import org.ametys.core.datasource.SQLDataSourceManager;
035import org.ametys.core.ui.Callable;
036import org.ametys.core.ui.StaticClientSideElement;
037
038/**
039 * This element creates a ribbon button to delete a data source if it is not currently used.
040 */
041public class DeleteDataSourceClientSideElement extends StaticClientSideElement 
042{
043    private SQLDataSourceManager _sqlDataSourceManager;
044    private LDAPDataSourceManager _ldapSourceManager;
045    private DataSourceConsumerExtensionPoint _dataSourceConsumerEP;
046    
047    @Override
048    public void service(ServiceManager smanager) throws ServiceException
049    {
050        super.service(smanager);
051        _sqlDataSourceManager = (SQLDataSourceManager) smanager.lookup(SQLDataSourceManager.ROLE);
052        _ldapSourceManager = (LDAPDataSourceManager) smanager.lookup(LDAPDataSourceManager.ROLE);
053        _dataSourceConsumerEP = (DataSourceConsumerExtensionPoint) smanager.lookup(DataSourceConsumerExtensionPoint.ROLE);
054    }
055    
056    /**
057     * Get state of data sources
058     * @param datasourceIds the ids of data sources with their type.
059     * @return informations on datasource's state
060     * @throws IOException if an error occurred while reading configuration file
061     * @throws SAXException if an error occurred while parsing configuration file
062     * @throws ConfigurationException if an error occurred while parsing configuration reading file
063     */
064    @SuppressWarnings("unchecked")
065    @Callable
066    public Map<String, Object> getStatus(Map<String, String> datasourceIds) throws ConfigurationException, SAXException, IOException
067    {
068        Map<String, Object> results = new HashMap<>();
069        
070        results.put("allright-datasources", new ArrayList<Map<String, Object>>());
071        results.put("internal-datasources", new ArrayList<Map<String, Object>>());
072        results.put("inuse-datasources", new ArrayList<Map<String, Object>>());
073        results.put("unknown-datasources", new ArrayList<String>());
074        
075        for (String id : datasourceIds.keySet())
076        {
077            DataSourceDefinition dsDef = null;
078            boolean isInUse = false;
079            
080            DataSourceType type = DataSourceType.valueOf(datasourceIds.get(id));
081            switch (type)
082            {
083                case SQL:
084                    dsDef = _sqlDataSourceManager.getDataSourceDefinition(id);
085                    isInUse = dsDef != null ? TypeOfUse.merge(_dataSourceConsumerEP.isInUse(id), dsDef.isDefault() ? _dataSourceConsumerEP.isInUse(_sqlDataSourceManager.getDefaultDataSourceId()) : TypeOfUse.NOT_USED) != TypeOfUse.NOT_USED : false;
086                    break;
087                case LDAP:
088                    dsDef = _ldapSourceManager.getDataSourceDefinition(id);
089                    isInUse = dsDef != null ? TypeOfUse.merge(_dataSourceConsumerEP.isInUse(id), dsDef.isDefault() ? _dataSourceConsumerEP.isInUse(_ldapSourceManager.getDefaultDataSourceId()) : TypeOfUse.NOT_USED) != TypeOfUse.NOT_USED : false;
090                    break;
091                default:
092                    break;
093            }
094            
095            if (dsDef != null)
096            {
097                if (dsDef.getId().equals(org.ametys.core.datasource.SQLDataSourceManager.AMETYS_INTERNAL_DATASOURCE_ID))
098                {
099                    List<Map<String, Object>> internalDataSource = (List<Map<String, Object>>) results.get("internal-datasources");
100                    internalDataSource.add(_getDataSourceParameters(dsDef));
101                }
102                else if (isInUse)
103                {
104                    List<Map<String, Object>> inUseDataSources = (List<Map<String, Object>>) results.get("inuse-datasources");
105                    inUseDataSources.add(_getDataSourceParameters(dsDef));
106                }
107                else      
108                {
109                    List<Map<String, Object>> allRightDataSources = (List<Map<String, Object>>) results.get("allright-datasources");
110                    allRightDataSources.add(_getDataSourceParameters(dsDef));
111                }
112            }
113            else
114            {
115                List<String> unknownDataSources = (List<String>) results.get("unknown-datasources");
116                unknownDataSources.add(id);
117            }
118            
119        }
120        
121        return results;
122    }
123    
124    private Map<String, Object> _getDataSourceParameters (DataSourceDefinition datasource)
125    {
126        Map<String, Object> params = new HashMap<>();
127        params.put("id", datasource.getId());
128        params.put("name", datasource.getName());
129        
130        return params;
131    }
132}