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.core.group; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.component.Component; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027 028import org.ametys.core.datasource.DataSourceConsumer; 029import org.ametys.core.group.directory.GroupDirectory; 030import org.ametys.core.group.directory.GroupDirectoryFactory; 031import org.ametys.core.group.directory.GroupDirectoryModel; 032import org.ametys.runtime.model.ElementDefinition; 033import org.ametys.runtime.model.type.ModelItemTypeConstants; 034 035/** 036 * Implementation of {@link DataSourceConsumer} allowing to know whether a data source is used by the group directories or not. 037 * It also allows to retrieve the data source ids that are currently in use. 038 */ 039public class GroupDirectoriesDataSourceConsumer implements DataSourceConsumer, Component, Serviceable 040{ 041 private GroupDirectoryDAO _groupDirectoryDAO; 042 private GroupDirectoryFactory _groupDirectoryFactory; 043 044 @Override 045 public void service(ServiceManager manager) throws ServiceException 046 { 047 _groupDirectoryDAO = (GroupDirectoryDAO) manager.lookup(GroupDirectoryDAO.ROLE); 048 _groupDirectoryFactory = (GroupDirectoryFactory) manager.lookup(GroupDirectoryFactory.ROLE); 049 } 050 @Override 051 public TypeOfUse isInUse(String id) 052 { 053 for (String gdModelId : _groupDirectoryFactory.getExtensionsIds()) 054 { 055 GroupDirectoryModel gdModel = _groupDirectoryFactory.getExtension(gdModelId); 056 057 // for this model, which parameters are of type "datasource" 058 List<String> datasourceParameters = new ArrayList<>(); 059 Map<String, ? extends ElementDefinition> parameters = gdModel.getParameters(); 060 for (String parameterId : parameters.keySet()) 061 { 062 if (ModelItemTypeConstants.DATASOURCE_ELEMENT_TYPE_ID.equals(parameters.get(parameterId).getType().getId())) 063 { 064 datasourceParameters.add(parameterId); 065 } 066 } 067 068 // search the group directories of this model 069 for (GroupDirectory groupDirectory : _groupDirectoryDAO.getGroupDirectories()) 070 { 071 if (groupDirectory.getGroupDirectoryModelId().equals(gdModelId)) 072 { 073 for (String datasourceParameter : datasourceParameters) 074 { 075 // return true if it is the datasource id we're looking for, continue otherwise 076 if (id.equals(groupDirectory.getParameterValues().get(datasourceParameter))) 077 { 078 // FIXME RUNTIME-3211 the type of use should depends on if the group directory is used or not 079 return TypeOfUse.BLOCKING; 080 } 081 } 082 } 083 } 084 } 085 086 return TypeOfUse.NOT_USED; 087 } 088 089 @Override 090 public Map<String, TypeOfUse> getUsedDataSourceIds() 091 { 092 Map<String, TypeOfUse> result = new HashMap<>(); 093 094 for (String gdModelId : _groupDirectoryFactory.getExtensionsIds()) 095 { 096 GroupDirectoryModel gdModel = _groupDirectoryFactory.getExtension(gdModelId); 097 098 // for this model, which parameters are of type "datasource" 099 List<String> datasourceParameters = new ArrayList<>(); 100 Map<String, ? extends ElementDefinition> parameters = gdModel.getParameters(); 101 for (String parameterId : parameters.keySet()) 102 { 103 if (ModelItemTypeConstants.DATASOURCE_ELEMENT_TYPE_ID.equals(parameters.get(parameterId).getType().getId())) 104 { 105 datasourceParameters.add(parameterId); 106 } 107 } 108 109 // search the group directories of this model 110 for (GroupDirectory groupDirectory : _groupDirectoryDAO.getGroupDirectories()) 111 { 112 if (groupDirectory.getGroupDirectoryModelId().equals(gdModelId)) 113 { 114 for (String datasourceParameter : datasourceParameters) 115 { 116 // this datasource value is used 117 // FIXME RUNTIME-3211 the type of use should depends on if the group directory is used or not 118 result.put((String) groupDirectory.getParameterValues().get(datasourceParameter), TypeOfUse.BLOCKING); 119 } 120 } 121 } 122 } 123 124 return result; 125 } 126}