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;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.core.ui.AddTaskClientSideElement;
027import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
028import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionHelper;
029
030/**
031 * This implementation test if at least one SCC is associated to the model defined by the sccModelId.
032 * If yes, the first collection is used to create the button, it there are no collections, the button is not displayed.
033 * Also, it set some elements to be used by the import tool (SCCSearchTool for most cases).
034 */
035public class GlobalSynchronizationClientSideElement extends AddTaskClientSideElement
036{
037    /** SCCClientSideElement helper */
038    protected SynchronizableContentsCollectionHelper _sccHelper;
039    
040    @Override
041    public void service(ServiceManager smanager) throws ServiceException
042    {
043        super.service(smanager);
044        _sccHelper = (SynchronizableContentsCollectionHelper) smanager.lookup(SynchronizableContentsCollectionHelper.ROLE);
045    }
046    
047    @Override
048    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
049    {
050        List<Script> clonedScripts = new ArrayList<>();
051        
052        List<Script> scripts = super.getScripts(ignoreRights, contextParameters);
053        
054        for (Script script : scripts)
055        {
056            if (script.getParameters().containsKey("modelsToSync"))
057            {
058                String[] modelsToSync = ((String) script.getParameters().get("modelsToSync")).split(",");
059                clonedScripts.addAll(getClonedScripts(script, modelsToSync));
060            }
061        }
062        
063        return clonedScripts;
064    }
065    
066    /**
067     * Get cloned scripts from the original script.
068     * @param script Original script
069     * @param modelsToSync SCC models to synchronize
070     * @return {@link List} of modified scripts
071     */
072    protected List<Script> getClonedScripts(Script script, String[] modelsToSync)
073    {
074        List<Script> clonedScripts = new ArrayList<>();
075        
076        for (String sccModelId : modelsToSync)
077        {
078            SynchronizableContentsCollection collection = _sccHelper.getSCCFromModelId(sccModelId);
079            if (collection != null)
080            {
081                Script clonedScript = new Script(script);
082                
083                Map<String, Object> openToolParams = new HashMap<>();
084                openToolParams.put("collectionId", collection.getId());
085                clonedScript.getParameters().put("schedulable-parameters", openToolParams);
086                
087                clonedScripts.add(clonedScript);
088            }
089        }
090        
091        return clonedScripts;
092    }
093}