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