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