001/*
002 *  Copyright 2017 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.odfsync.apogee;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.commons.lang3.StringUtils;
024
025import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
026import org.ametys.plugins.odfsync.GlobalSynchronizationClientSideElement;
027
028/**
029 * This implementation test if at least one SCC is associated to the model defined by the sccModelId.
030 * If yes, the first collection is used to create the button, it there are no collections, the button is not displayed.
031 * Also, it set some elements to be used by the import tool (SCCSearchTool for most cases).
032 */
033public class ApogeeGlobalSynchronizationClientSideElement extends GlobalSynchronizationClientSideElement
034{
035    @Override
036    protected List<Script> getClonedScripts(Script script, String[] modelsToSync)
037    {
038        List<Script> clonedScripts = new ArrayList<>();
039        List<String> collections = new ArrayList<>();
040        
041        for (String sccModelId : modelsToSync)
042        {
043            SynchronizableContentsCollection collection = _sccHelper.getSCCFromModelId(sccModelId);
044            if (collection != null)
045            {
046                collections.add(collection.getId());
047            }
048        }
049
050        if (!collections.isEmpty())
051        {
052            Script clonedScript = new Script(script);
053            
054            Map<String, Object> openToolParams = new HashMap<>();
055            openToolParams.put("collectionIds", StringUtils.join(collections, ","));
056            clonedScript.getParameters().put("schedulable-parameters", openToolParams);
057            
058            clonedScripts.add(clonedScript);
059        }
060        
061        return clonedScripts;
062    }
063}