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.HashSet;
019import java.util.List;
020import java.util.Optional;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.components.ContextHelper;
025import org.apache.cocoon.environment.Request;
026import org.quartz.JobDetail;
027import org.quartz.JobExecutionContext;
028
029import org.ametys.cms.repository.Content;
030import org.ametys.cms.repository.ModifiableContent;
031import org.ametys.core.schedule.Schedulable;
032import org.ametys.core.schedule.progression.ContainerProgressionTracker;
033import org.ametys.core.schedule.progression.SimpleProgressionTracker;
034import org.ametys.odf.ODFHelper;
035import org.ametys.odf.catalog.Catalog;
036import org.ametys.odf.catalog.CatalogsManager;
037import org.ametys.odf.program.ProgramFactory;
038import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
039import org.ametys.plugins.core.schedule.Scheduler;
040import org.ametys.plugins.odfsync.apogee.scc.ApogeeSynchronizableContentsCollectionHelper;
041import org.ametys.plugins.repository.AmetysObjectIterable;
042import org.ametys.runtime.i18n.I18nizableText;
043
044/**
045 * A {@link Schedulable} job which synchronizes Apogee collections.
046 */
047public class ApogeeSchedulable extends AbstractStaticSchedulable
048{
049    private ODFHelper _odfHelper;
050    private ApogeeSynchronizableContentsCollectionHelper _apogeeSCCHelper;
051    private CatalogsManager _catalogsManager;
052    
053    @Override
054    public void service(ServiceManager manager) throws ServiceException
055    {
056        super.service(manager);
057        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
058        _apogeeSCCHelper = (ApogeeSynchronizableContentsCollectionHelper) manager.lookup(ApogeeSynchronizableContentsCollectionHelper.ROLE);
059        _catalogsManager = (CatalogsManager) manager.lookup(CatalogsManager.ROLE);
060    }
061    
062    @Override
063    public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception
064    {
065        long begin = System.currentTimeMillis();
066        Request request = ContextHelper.getRequest(_context);
067        try
068        {
069            // Save handle contents
070            request.setAttribute(ApogeeSynchronizableContentsCollectionHelper.HANDLED_CONTENTS, new HashSet<>());
071            
072            synchronizeCollections(context, progressionTracker);
073
074            getLogger().info("Global synchronization ended in {} ms", System.currentTimeMillis() - begin);
075        }
076        catch (Exception ex)
077        {
078            getLogger().error("The global synchronization have failed.", ex);
079        }
080        finally
081        {
082            request.removeAttribute(ApogeeSynchronizableContentsCollectionHelper.HANDLED_CONTENTS);
083        }
084    }
085    
086    /**
087     * Synchronize all contents with Apogee SCC starting from programs.
088     * @param context The execution context
089     * @param progressionTracker The progression tracker
090     */
091    protected void synchronizeCollections(JobExecutionContext context, ContainerProgressionTracker progressionTracker)
092    {
093        String catalog = Optional.of(context)
094            .map(JobExecutionContext::getJobDetail)
095            .map(JobDetail::getJobDataMap)
096            .map(map -> map.getString(Scheduler.PARAM_VALUES_PREFIX + "catalog"))
097            .map(_catalogsManager::getCatalog)
098            .map(Catalog::getName)
099            .orElseGet(_catalogsManager::getDefaultCatalogName);
100        
101        AmetysObjectIterable<Content> programs = _odfHelper.getProgramItems(ProgramFactory.PROGRAM_CONTENT_TYPE, null, catalog, null);
102        
103        for (Content program : programs)
104        {
105            progressionTracker.addSimpleStep(program.getId(), new I18nizableText("plugin.odf-sync", "PLUGINS_ODF_SYNC_GLOBAL_SYNCHRONIZATION_SCHEDULER_PROGRAM_SYNC_STEP_LABEL", List.of(program.getTitle())));
106        }
107        
108        for (Content program : programs)
109        {
110            _apogeeSCCHelper.synchronizeContent((ModifiableContent) program, getLogger());
111            ((SimpleProgressionTracker) progressionTracker.getStep(program.getId())).increment();
112        }
113    }
114}