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.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.core.ui.AddTaskClientSideElement;
026import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
027import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO;
028import org.ametys.plugins.odfsync.apogee.scc.ApogeeSynchronizableContentsCollection;
029
030/**
031 * This implementation test if at least one SCC is instance of {@link ApogeeSynchronizableContentsCollection}
032 * If yes, the first collection is used to create the button, it there are no collections, the button is not displayed.
033 */
034public class ApogeeGlobalSynchronizationClientSideElement extends AddTaskClientSideElement
035{
036    /** The SCC DAO */
037    protected SynchronizableContentsCollectionDAO _sccDAO;
038    
039    @Override
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        super.service(manager);
043        _sccDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE); 
044    }
045    
046    @Override
047    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
048    {
049        // Only activate if they are an Apogee SCC
050        for (SynchronizableContentsCollection scc : _sccDAO.getSynchronizableContentsCollections())
051        {
052            if (scc instanceof ApogeeSynchronizableContentsCollection)
053            {
054                return super.getScripts(ignoreRights, contextParameters);
055            }
056        }
057        
058        return new ArrayList<>();
059    }
060}