001/*
002 *  Copyright 2020 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.component.Component;
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.repository.Content;
031import org.ametys.plugins.repository.data.external.ExternalizableDataProvider;
032import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
033import org.ametys.runtime.model.ModelItem;
034import org.ametys.runtime.plugin.component.AbstractLogEnabled;
035
036/**
037 * {@link ExternalizableDataProvider} based on {@link SynchronizableContentsCollection}
038 */
039public class SynchronizableContentsCollectionDataProvider extends AbstractLogEnabled implements ExternalizableDataProvider, Component, Serviceable
040{
041    /** The DAO for synchronizable contents collections */
042    protected SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO;
043    /** SCC helper */
044    protected SynchronizableContentsCollectionHelper _sccHelper;
045    
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE);
050        _sccHelper = (SynchronizableContentsCollectionHelper) manager.lookup(SynchronizableContentsCollectionHelper.ROLE);
051    }
052
053    public Set<String> getExternalizableDataPaths(ModelAwareDataHolder dataHolder)
054    {
055        if (dataHolder instanceof Content)
056        {
057            Content content = (Content) dataHolder;
058            return _sccHelper.getSynchronizableCollectionIds(content).stream()
059                    .map(_synchronizableContentsCollectionDAO::getSynchronizableContentsCollection)
060                    .filter(Objects::nonNull)
061                    .map(col -> col.getLocalAndExternalFields(buildParametersMap(content)))
062                    .flatMap(Collection::stream)
063                    .collect(Collectors.toSet());
064        }
065        else
066        {
067            return Set.of();
068        }
069    }
070
071    public boolean isDataExternalizable(ModelAwareDataHolder dataHolder, ModelItem modelItem)
072    {
073        return getExternalizableDataPaths(dataHolder).contains(modelItem.getPath());
074    }
075    
076    private Map<String, Object> buildParametersMap(Content content)
077    {
078        return Map.of("contentTypes", Arrays.asList(content.getTypes()));
079    }
080
081}