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.Map;
021import java.util.Objects;
022import java.util.Set;
023import java.util.stream.Collectors;
024
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.cms.content.external.ExternalizableMetadataProvider;
030import org.ametys.cms.repository.Content;
031import org.ametys.runtime.plugin.component.AbstractLogEnabled;
032
033/**
034 * {@link ExternalizableMetadataProvider} based on a {@link SynchronizableContentsCollection}
035 */
036public class SynchronizableContentsCollectionMetadataProvider extends AbstractLogEnabled implements ExternalizableMetadataProvider, Serviceable
037{
038    /** The DAO for synchronizable contents collections */
039    protected SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO;
040    /** SCC helper */
041    protected SynchronizableContentsCollectionHelper _sccHelper;
042    
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE);
047        _sccHelper = (SynchronizableContentsCollectionHelper) manager.lookup(SynchronizableContentsCollectionHelper.ROLE);
048    }
049    
050    @Override
051    public Set<String> getExternalAndLocalMetadata(Content content)
052    {
053        return _sccHelper.getSynchronizableCollectionIds(content).stream()
054                .map(_synchronizableContentsCollectionDAO::getSynchronizableContentsCollection)
055                .filter(Objects::nonNull)
056                .map(col -> col.getLocalAndExternalFields(buildParametersMap(content)))
057                .flatMap(Collection::stream)
058                .collect(Collectors.toSet());
059    }
060    
061    private Map<String, Object> buildParametersMap(Content content)
062    {
063        return Map.of("contentTypes", Arrays.asList(content.getTypes()));
064    }
065}