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.model.ModelItem;
034import org.ametys.runtime.model.type.ModelItemTypeConstants;
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            for (ModelItem parameter : model.getModelItems())
069            {
070                if (ModelItemTypeConstants.DATASOURCE_ELEMENT_TYPE_ID.equals(parameter.getType().getId()))
071                {
072                    datasourceParameters.add(parameter.getPath());
073                }
074            }
075            
076            // search the collections of this model
077            for (SynchronizableContentsCollection collection : _synchronizableContentsCollectionDAO.getSynchronizableContentsCollections())
078            {
079                if (modelId.equals(collection.getSynchronizeCollectionModelId()))
080                {
081                    for (String datasourceParameter : datasourceParameters)
082                    {
083                        if (id.equals(collection.getParameterValues().get(datasourceParameter)))
084                        {
085                            return TypeOfUse.NON_BLOCKING; // A data source use by a collection is always non-blocking
086                        }
087                    }
088                }
089            }
090        }
091        
092        return TypeOfUse.NOT_USED;
093    }
094
095    @Override
096    public Map<String, TypeOfUse> getUsedDataSourceIds()
097    {
098        Map<String, TypeOfUse> result = new HashMap<>();
099        
100        for (String modelId : _synchronizeContentsCollectionModelEP.getExtensionsIds())
101        {
102            SynchronizableContentsCollectionModel model = _synchronizeContentsCollectionModelEP.getExtension(modelId);
103            
104            // for this model, which parameters are of type "datasource"
105            List<String> datasourceParameters = new ArrayList<>();
106            
107            for (ModelItem parameter : model.getModelItems())
108            {
109                if (ModelItemTypeConstants.DATASOURCE_ELEMENT_TYPE_ID.equals(parameter.getType().getId()))
110                {
111                    datasourceParameters.add(parameter.getPath());
112                }
113            }
114            
115            // search the collections of this model
116            for (SynchronizableContentsCollection collection : _synchronizableContentsCollectionDAO.getSynchronizableContentsCollections())
117            {
118                for (String datasourceParameter : datasourceParameters)
119                {
120                    // this datasource value is used
121                    result.put((String) collection.getParameterValues().get(datasourceParameter), TypeOfUse.NON_BLOCKING); // A data source use by a collection is always non-blocking
122                }
123            }
124        }
125        
126        return result;
127    }
128}