view software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/schedule/MpdlDocJob.java @ 0:408254cf2f1d

Erstellung
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Wed, 24 Nov 2010 17:24:23 +0100
parents
children 5df60f24e997
line wrap: on
line source

package de.mpg.mpiwg.berlin.mpdl.schedule;

import java.util.Date;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import de.mpg.mpiwg.berlin.mpdl.client.DocumentHandler;
import de.mpg.mpiwg.berlin.mpdl.escidoc.ESciDocIngestor;
import de.mpg.mpiwg.berlin.mpdl.escidoc.ESciDocRestSession;
import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;
import de.mpg.mpiwg.berlin.mpdl.xmlrpc.MpdlXmlRpcDocHandler;

public class MpdlDocJob implements Job {
  public static String STATUS_BEGIN = "started";
  private static Logger LOGGER = Logger.getLogger(MpdlDocJob.class); // Logs to EXIST_HOME/webapp/WEB-INF/logs/exist.log
  private JobExecutionContext currentExecutedContext;
  
  public void execute(JobExecutionContext context) throws JobExecutionException {
    this.currentExecutedContext = context;
    MpdlDocOperation docOperation = getDocOperation();
    docOperation.setIncludePdf(true); // default is true: handle also Pdf/Html version of the document
    try {
      docOperation.setStatus(STATUS_BEGIN);
      String operationName = docOperation.getName();   
      String cookieId = docOperation.getESciDocCookieId();
      MpdlXmlRpcDocHandler mpdlXmlRpcDocHandler = MpdlXmlRpcDocHandler.getInstance();
      ESciDocRestSession eSciDocSession = ESciDocRestSession.getInstance(cookieId);
      ESciDocIngestor eSciDocIngestor = new ESciDocIngestor(eSciDocSession);
      if (operationName.equals("create") || operationName.equals("update")) {
        DocumentHandler docHandler = new DocumentHandler(mpdlXmlRpcDocHandler, eSciDocIngestor);
        docHandler.doOperation(docOperation);
      } else if (operationName.equals("delete")) {
        DocumentHandler docHandler = new DocumentHandler(mpdlXmlRpcDocHandler, eSciDocIngestor);
        docHandler.doOperation(docOperation);
      } else if (operationName.equals("updateExist")) {
        DocumentHandler docHandler = new DocumentHandler(mpdlXmlRpcDocHandler);
        docHandler.doOperation(docOperation);
      } else if (operationName.equals("deleteExist")) {
        DocumentHandler docHandler = new DocumentHandler(mpdlXmlRpcDocHandler);
        docHandler.doOperation(docOperation);
      } else if (operationName.equals("importAllDocumentsLocallyExist")) {
        DocumentHandler docHandler = new DocumentHandler(mpdlXmlRpcDocHandler);
        docOperation.setIncludePdf(false);  // for performance reasons while importing documents: do not generate Pdf/Html-Versions of the document
        docHandler.doOperation(docOperation);
      } else if (operationName.equals("generatePdfHtmlDocumentFiles")) {
        DocumentHandler docHandler = new DocumentHandler(mpdlXmlRpcDocHandler);
        docOperation.setIncludePdf(true);  
        docHandler.doOperation(docOperation);
      }
      Date startingTime = docOperation.getStart();
      String jobInfo = "MPDL: Document operation " + docOperation.toString() + ": started at: " + startingTime;
      LOGGER.info(jobInfo);
      this.currentExecutedContext = null;
    } catch (Exception e) {
      try {
        // Quartz will automatically unschedule all triggers associated with this job so that it does not run again
        MpdlChainScheduler mpdlChainScheduler = MpdlChainScheduler.getInstance();
        mpdlChainScheduler.finishOperation(docOperation);
        String errorMessage = e.getMessage();
        if (errorMessage == null) {
          Throwable t = e.getCause();
          if (t == null) {
            errorMessage = e.toString();
          } else {
            errorMessage = t.getMessage();
          }
        }
        docOperation.setErrorMessage(errorMessage);
        LOGGER.error(errorMessage, e);
        JobExecutionException jobExecutionException = new JobExecutionException(e);
        jobExecutionException.setUnscheduleAllTriggers(true);
        throw jobExecutionException;
      } catch (ApplicationException ex) {
        // nothing
      }
    }
  } 

  private MpdlDocOperation getDocOperation() {
    MpdlDocOperation docOperation = null;
    if (currentExecutedContext != null) {
      JobDetail job = currentExecutedContext.getJobDetail();
      JobDataMap parameters = job.getJobDataMap();
      docOperation = (MpdlDocOperation) parameters.get("operation");
    }
    return docOperation;
  }

}