/*
 * $Id: DatastoreCleaningJob.java,v 1.1 2004/11/10 15:02:51 koppy Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * Created on 09.11.2004
 *
 * $Log: DatastoreCleaningJob.java,v $
 * Revision 1.1  2004/11/10 15:02:51  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 */
package vkc.server.jobs;

import java.util.Calendar;

import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;

import vkc.misc.VKCLogManager;
import vkc.server.archive.Cleaner;
import vkc.view.WebShare;

/**
 * Dieser Job dient dazu, den Datastore anhand der Dokumente
 * in der VKC-Datenbank zu synchronisieren. Alles, was im Datastore,
 * aber nicht in der Datenbank ist, wird also gelöscht.
 * @author Andreas Koop
 */
public class DatastoreCleaningJob implements Job {

    public static final String NAME = "DATASTORE_CLEANING";
    
    /** 
     * (non-Javadoc)
     * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
     */
    public void execute(JobExecutionContext context)
            throws JobExecutionException {

        JobDetail job = context.getJobDetail();
        
        VKCLogManager.infoLog( this, "Executing Job " + job.getFullName());
        try {
            
            // Den Cleaner könnte man auch noch bequem über einen Parameter setzen
            Cleaner cleaner = (Cleaner)Class.forName("vkc.server.archive.VKCDBCleaner").newInstance();
            
            SimpleTrigger repTrigger = (SimpleTrigger)WebShare.scheduler.getTrigger( DatastoreCleaningJob.NAME+"_TRIGGER",Scheduler.DEFAULT_GROUP);
            repTrigger.setRepeatInterval( cleaner.getCleaningPeriod() );
            
            if( !WebShare.archive.isBackupTime() ) {
                cleaner.cleanDataStore( WebShare.archive );
                return;
            }
                
            // Da Backup-Time ist, wird die Aufräumaktion
            // nach Backup-Ende gescheduled!
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis( System.currentTimeMillis() );
            int currentHour = calendar.get( Calendar.HOUR_OF_DAY );
            int currentMinute = calendar.get( Calendar.MINUTE );
            int delay = ( 60 * WebShare.archive.getBackupEnd() - ( 60 * currentHour + currentMinute ) ) * 60 * 1000;
            calendar.add( Calendar.MILLISECOND, delay );
            
            SimpleTrigger delayedTrigger = new SimpleTrigger( DatastoreCleaningJob.NAME+"_DELAYED_TRIGGER",
            	      Scheduler.DEFAULT_GROUP,
            	      calendar.getTime(),
            	      null,
            	      0,
            	      0);
            
            delayedTrigger.setJobGroup(Scheduler.DEFAULT_GROUP);
            delayedTrigger.setJobName(DatastoreCleaningJob.NAME);
            
            Trigger trigger = WebShare.scheduler.getTrigger( DatastoreCleaningJob.NAME+"_DELAYED_TRIGGER",Scheduler.DEFAULT_GROUP );
            if(trigger != null)
                WebShare.scheduler.rescheduleJob(DatastoreCleaningJob.NAME+"_DELAYED_TRIGGER",Scheduler.DEFAULT_GROUP, delayedTrigger);
            else
            	WebShare.scheduler.scheduleJob(delayedTrigger);

            VKCLogManager.infoLog( this, "Cleaning delayed until " + delayedTrigger.getNextFireTime() + ", it's backup time!" );
            
        } catch (Exception e) {
            VKCLogManager.errorLog(this,"", "Error occured while executing " + job.getFullName(), e);
        }
		
			
	}
}
