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.core.schedule.Schedulable;
027import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
028import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO;
029import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
030import org.ametys.plugins.odfsync.apogee.scc.AbstractApogeeSynchronizableContentsCollection;
031import org.ametys.plugins.odfsync.apogee.scc.ApogeeSynchronizableContentsCollection;
032
033/**
034 * A {@link Schedulable} job which synchronizes Apogee collections.
035 */
036public class ApogeeSchedulable extends AbstractStaticSchedulable
037{
038    private SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO;
039    
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        super.service(manager);
044        _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE); 
045    }
046    
047    @Override
048    public void execute(JobExecutionContext context) throws Exception
049    {
050        long begin = System.currentTimeMillis();
051        Request request = ContextHelper.getRequest(_context);
052        try
053        {
054            // Save handle contents
055            request.setAttribute(AbstractApogeeSynchronizableContentsCollection.HANDLE_CONTENTS, new HashSet<String>());
056            
057            synchronizeCollections(context);
058
059            getLogger().info("Global synchronization ended in {} ms", System.currentTimeMillis() - begin);
060        }
061        catch (Exception ex)
062        {
063            getLogger().error("The global synchronization have failed.", ex);
064        }
065        finally
066        {
067            request.removeAttribute(AbstractApogeeSynchronizableContentsCollection.HANDLE_CONTENTS);
068        }
069    }
070    
071    /**
072     * Synchronize all the Apogee collections.
073     * @param context The execution context
074     */
075    protected void synchronizeCollections(JobExecutionContext context)
076    {
077        for (SynchronizableContentsCollection scc : _synchronizableContentsCollectionDAO.getSynchronizableContentsCollections())
078        {
079            if (scc instanceof ApogeeSynchronizableContentsCollection)
080            {
081                scc.populate(getLogger());
082            }
083        }
084    }
085}