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