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;
017
018import java.util.Arrays;
019import java.util.Collection;
020import java.util.List;
021import java.util.Map;
022import java.util.Objects;
023import java.util.Set;
024import java.util.stream.Collectors;
025
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.avalon.framework.service.Serviceable;
029
030import org.ametys.cms.content.external.ExternalizableMetadataProvider;
031import org.ametys.cms.repository.Content;
032import org.ametys.runtime.plugin.component.AbstractLogEnabled;
033
034/**
035 * {@link ExternalizableMetadataProvider} based on a {@link SynchronizableContentsCollection}
036 */
037public class SynchronizableContentsCollectionMetadataProvider extends AbstractLogEnabled implements ExternalizableMetadataProvider, Serviceable
038{
039    /** The DAO for synchronizable contents collections */
040    protected SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO;
041    /** SCC helper */
042    protected SynchronizableContentsCollectionHelper _sccHelper;
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE);
048        _sccHelper = (SynchronizableContentsCollectionHelper) manager.lookup(SynchronizableContentsCollectionHelper.ROLE);
049    }
050    
051    @Override
052    public Set<String> getExternalOnlyMetadata(Content content)
053    {
054        List<SynchronizableContentsCollection> collections = _sccHelper.getSynchronizableCollectionIds(content).stream()
055                .map(_synchronizableContentsCollectionDAO::getSynchronizableContentsCollection)
056                .filter(Objects::nonNull)
057                .collect(Collectors.toList());
058        
059        Map<String, Object> params = buildParametersMap(content);
060        
061        // Return the intersection of the no-synchronized metadata of each collection
062        Set<String> externalMetadata = collections.get(0).getExternalOnlyFields(params);
063        for (int i = 1; i < collections.size(); i++)
064        {
065            externalMetadata.retainAll(collections.get(i).getExternalOnlyFields(params));
066        }
067        
068        return externalMetadata;
069    }
070    
071    @Override
072    public Set<String> getExternalAndLocalMetadata(Content content)
073    {
074        return _sccHelper.getSynchronizableCollectionIds(content).stream()
075                .map(_synchronizableContentsCollectionDAO::getSynchronizableContentsCollection)
076                .filter(Objects::nonNull)
077                .map(col -> col.getLocalAndExternalFields(buildParametersMap(content)))
078                .flatMap(Collection::stream)
079                .collect(Collectors.toSet());
080    }
081    
082    private Map<String, Object> buildParametersMap(Content content)
083    {
084        return Map.of("contentTypes", Arrays.asList(content.getTypes()));
085    }
086}