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.plugins.contentio.synchronize.impl; 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.plugins.contentio.synchronize.SynchronizableContentsCollection; 030import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO; 031import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionModel; 032import org.ametys.plugins.contentio.synchronize.SynchronizeContentsCollectionModelExtensionPoint; 033import org.ametys.runtime.parameter.Parameter; 034import org.ametys.runtime.parameter.ParameterHelper.ParameterType; 035 036/** 037 * Implementation of {@link DataSourceConsumer} allowing to know whether a data source is used by the collections of synchronizable contents or not. 038 * It also allows to retrieve the data source ids that are currently in use. 039 */ 040public class CollectionDataSourceConsumer implements DataSourceConsumer, Component, Serviceable 041{ 042 /** Avalon Role */ 043 public static final String ROLE = CollectionDataSourceConsumer.class.getName(); 044 045 /** The DAO for synchronizable contents collections */ 046 protected SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO; 047 048 /** The extension point for synchronizable contents collection models */ 049 protected SynchronizeContentsCollectionModelExtensionPoint _synchronizeContentsCollectionModelEP; 050 051 @Override 052 public void service(ServiceManager manager) throws ServiceException 053 { 054 _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE); 055 _synchronizeContentsCollectionModelEP = (SynchronizeContentsCollectionModelExtensionPoint) manager.lookup(SynchronizeContentsCollectionModelExtensionPoint.ROLE); 056 } 057 058 @Override 059 public TypeOfUse isInUse(String id) 060 { 061 for (String modelId : _synchronizeContentsCollectionModelEP.getExtensionsIds()) 062 { 063 SynchronizableContentsCollectionModel model = _synchronizeContentsCollectionModelEP.getExtension(modelId); 064 065 // for this model, which parameters are of type "datasource" 066 List<String> datasourceParameters = new ArrayList<>(); 067 068 Map<String, ? extends Parameter<ParameterType>> parameters = model.getParameters(); 069 for (String parameterId : parameters.keySet()) 070 { 071 if (ParameterType.DATASOURCE.equals(parameters.get(parameterId).getType())) 072 { 073 datasourceParameters.add(parameterId); 074 } 075 } 076 077 // search the collections of this model 078 for (SynchronizableContentsCollection collection : _synchronizableContentsCollectionDAO.getSynchronizableContentsCollections()) 079 { 080 if (modelId.equals(collection.getSynchronizeCollectionModelId())) 081 { 082 for (String datasourceParameter : datasourceParameters) 083 { 084 if (id.equals(collection.getParameterValues().get(datasourceParameter))) 085 { 086 return TypeOfUse.NON_BLOCKING; // A data source use by a collection is always non-blocking 087 } 088 } 089 } 090 } 091 } 092 093 return TypeOfUse.NOT_USED; 094 } 095 096 @Override 097 public Map<String, TypeOfUse> getUsedDataSourceIds() 098 { 099 Map<String, TypeOfUse> result = new HashMap<>(); 100 101 for (String modelId : _synchronizeContentsCollectionModelEP.getExtensionsIds()) 102 { 103 SynchronizableContentsCollectionModel model = _synchronizeContentsCollectionModelEP.getExtension(modelId); 104 105 // for this model, which parameters are of type "datasource" 106 List<String> datasourceParameters = new ArrayList<>(); 107 108 Map<String, ? extends Parameter<ParameterType>> parameters = model.getParameters(); 109 for (String parameterId : parameters.keySet()) 110 { 111 if (ParameterType.DATASOURCE.equals(parameters.get(parameterId).getType())) 112 { 113 datasourceParameters.add(parameterId); 114 } 115 } 116 117 // search the collections of this model 118 for (SynchronizableContentsCollection collection : _synchronizableContentsCollectionDAO.getSynchronizableContentsCollections()) 119 { 120 for (String datasourceParameter : datasourceParameters) 121 { 122 // this datasource value is used 123 result.put((String) collection.getParameterValues().get(datasourceParameter), TypeOfUse.NON_BLOCKING); // A data source use by a collection is always non-blocking 124 } 125 } 126 } 127 128 return result; 129 } 130}