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.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.core.datasource.DataSourceConsumer;
028import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
029import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO;
030import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionModel;
031import org.ametys.plugins.contentio.synchronize.SynchronizeContentsCollectionModelExtensionPoint;
032import org.ametys.runtime.model.ModelItem;
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 collections of synchronizable contents or not.
037 * It also allows to retrieve the data source ids that are currently in use.
038 */
039public class CollectionDataSourceConsumer implements DataSourceConsumer, Component, Serviceable
040{
041    /** Avalon Role */
042    public static final String ROLE = CollectionDataSourceConsumer.class.getName();
043    
044    /** The DAO for synchronizable contents collections */
045    protected SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO;
046
047    /** The extension point for synchronizable contents collection models */
048    protected SynchronizeContentsCollectionModelExtensionPoint _synchronizeContentsCollectionModelEP;
049    
050    @Override
051    public void service(ServiceManager manager) throws ServiceException
052    {
053        _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE);
054        _synchronizeContentsCollectionModelEP = (SynchronizeContentsCollectionModelExtensionPoint) manager.lookup(SynchronizeContentsCollectionModelExtensionPoint.ROLE);
055    }
056    
057    @Override
058    public TypeOfUse isInUse(String id)
059    {
060        for (String modelId : _synchronizeContentsCollectionModelEP.getExtensionsIds())
061        {
062            SynchronizableContentsCollectionModel model = _synchronizeContentsCollectionModelEP.getExtension(modelId);
063            
064            // for this model, which parameters are of type "datasource"
065            List<String> datasourceParameters = model.getModelItems()
066                                                     .stream()
067                                                     .filter(parameter -> ModelItemTypeConstants.DATASOURCE_ELEMENT_TYPE_ID.equals(parameter.getType().getId()))
068                                                     .map(ModelItem::getPath)
069                                                     .toList();
070            
071            // search the collections of this model
072            for (SynchronizableContentsCollection collection : _synchronizableContentsCollectionDAO.getSynchronizableContentsCollections())
073            {
074                if (modelId.equals(collection.getSynchronizeCollectionModelId()))
075                {
076                    for (String datasourceParameter : datasourceParameters)
077                    {
078                        if (id.equals(collection.getParameterValues().get(datasourceParameter)))
079                        {
080                            return TypeOfUse.NON_BLOCKING; // A data source use by a collection is always non-blocking
081                        }
082                    }
083                }
084            }
085        }
086        
087        return TypeOfUse.NOT_USED;
088    }
089
090    @Override
091    public Map<String, TypeOfUse> getUsedDataSourceIds()
092    {
093        Map<String, TypeOfUse> result = new HashMap<>();
094        
095        for (String modelId : _synchronizeContentsCollectionModelEP.getExtensionsIds())
096        {
097            SynchronizableContentsCollectionModel model = _synchronizeContentsCollectionModelEP.getExtension(modelId);
098            
099            // for this model, which parameters are of type "datasource"
100            List<String> datasourceParameters = model.getModelItems()
101                                                     .stream()
102                                                     .filter(parameter -> ModelItemTypeConstants.DATASOURCE_ELEMENT_TYPE_ID.equals(parameter.getType().getId()))
103                                                     .map(ModelItem::getPath)
104                                                     .toList();
105            
106            // search the collections of this model
107            for (SynchronizableContentsCollection collection : _synchronizableContentsCollectionDAO.getSynchronizableContentsCollections())
108            {
109                for (String datasourceParameter : datasourceParameters)
110                {
111                    // this datasource value is used
112                    result.put((String) collection.getParameterValues().get(datasourceParameter), TypeOfUse.NON_BLOCKING); // A data source use by a collection is always non-blocking
113                }
114            }
115        }
116        
117        return result;
118    }
119}