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