/*
 * $Id: PublishingJob.java,v 1.2 2005/08/30 14:34:09 jonyp Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * Created on 08.11.2004
 *
 * $Log: PublishingJob.java,v $
 * Revision 1.2  2005/08/30 14:34:09  jonyp
 * fixes due to changes of STATUS_.../FLAG_... from int to Integer
 *
 * Revision 1.1  2004/11/10 15:02:51  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 */
package vkc.server.jobs;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import vkc.db.entities.VKCDocument;
import vkc.db.serializer.VKCDocumentSerializer;
import vkc.exception.DBException;
import vkc.misc.VKCLogManager;

/**
 * This class represents
 * @author Andreas Koop
 */
public class PublishingJob implements Job {

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

        VKCLogManager.infoLog( this, "Start of the publishing process for documents where published date lies behind " + new Date( System.currentTimeMillis() ) );
		try {
            List documents = new ArrayList( VKCDocumentSerializer.getToBePublishedDocuments() );
            
            for ( int i = 0; i < documents.size(); i++ ) {
                VKCDocument document = (VKCDocument)documents.get( i );
                document.setPublished( new Date( System.currentTimeMillis() ) );
                document.setFlag( VKCDocument.FLAG_PUBLISHED );
                VKCDocumentSerializer.saveDocument( document, true, true );
            }
            
            VKCLogManager.infoLog( this, "End of the publishing process for documents where published date lies behind " + new Date( System.currentTimeMillis() ) );
        } catch (DBException e) {
            VKCLogManager.errorLog(this,"", "Exception while executing " 
                    + context.getJobDetail().getName(),e);
        }

    }

}
