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.contentio.synchronize.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.clientsideelement.SmartContentClientSideElement;
026import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
027import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionHelper;
028
029/**
030 * Smart content client side element for SCC, the SCC model ID configured in plugin.xml is used.
031 */
032public class SCCSmartContentClientSideElement extends SmartContentClientSideElement
033{
034    /** SCC helper */
035    protected SynchronizableContentsCollectionHelper _sccHelper;
036    
037    @Override
038    public void service(ServiceManager smanager) throws ServiceException
039    {
040        super.service(smanager);
041        _sccHelper = (SynchronizableContentsCollectionHelper) smanager.lookup(SynchronizableContentsCollectionHelper.ROLE);
042    }
043    
044    @Override
045    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
046    {
047        List<Script> clonedScripts = new ArrayList<>();
048        
049        List<Script> scripts = super.getScripts(ignoreRights, contextParameters);
050        
051        for (Script script : scripts)
052        {
053            if (script.getParameters().containsKey("sccModelId"))
054            {
055                String sccModelId = (String) script.getParameters().get("sccModelId");
056                SynchronizableContentsCollection collection = _sccHelper.getSCCFromModelId(sccModelId);
057                if (collection != null)
058                {
059                    clonedScripts.add(_getScriptFromCollection(script, collection));
060                }
061            }
062        }
063        
064        return clonedScripts;
065    }
066
067    /**
068     * Get a cloned script by updating configuration with collection values.
069     * @param script The script to clone
070     * @param collection The collection
071     * @return A cloned script
072     */
073    protected Script _getScriptFromCollection(Script script, SynchronizableContentsCollection collection)
074    {
075        Script clonedScript = new Script(script);
076        clonedScript.getParameters().put("collectionId", collection.getId());
077        return clonedScript;
078    }
079}