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.JobDataMap; 025import org.quartz.JobExecutionContext; 026 027import org.ametys.core.schedule.Schedulable; 028import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection; 029import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO; 030import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable; 031import org.ametys.plugins.core.schedule.Scheduler; 032import org.ametys.plugins.odfsync.apogee.scc.AbstractApogeeSynchronizableContentsCollection; 033 034/** 035 * A {@link Schedulable} job which synchronizes Apogee collections. 036 */ 037public class ApogeeSchedulable extends AbstractStaticSchedulable 038{ 039 /** The key for the collection to synchronize */ 040 public static final String JOBDATAMAP_COLLECTIONS_KEY = "collectionIds"; 041 042 private SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO; 043 044 @Override 045 public void service(ServiceManager manager) throws ServiceException 046 { 047 super.service(manager); 048 _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.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(AbstractApogeeSynchronizableContentsCollection.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(AbstractApogeeSynchronizableContentsCollection.HANDLE_CONTENTS); 072 } 073 } 074 075 /** 076 * Synchronize all the collections in the collectionIds parameter. 077 * @param context The execution context 078 */ 079 protected void synchronizeCollections(JobExecutionContext context) 080 { 081 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 082 // FIXME RUNTIME-2631 After resolution get a List<String> 083 String[] collectionIds = jobDataMap.getString(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_COLLECTIONS_KEY).split(","); 084 085 for (String collectionId : collectionIds) 086 { 087 SynchronizableContentsCollection scc = _synchronizableContentsCollectionDAO.getSynchronizableContentsCollection(collectionId); 088 scc.populate(getLogger()); 089 } 090 } 091}