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

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

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

import org.apache.log4j.Logger;

import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;
import de.mpg.mpiwg.berlin.mpdl.general.MpdlConstants;
import de.mpg.mpiwg.berlin.mpdl.schedule.MpdlDocOperation;
import de.mpg.mpiwg.berlin.mpdl.xmlrpc.MpdlXmlRpcInterface;
import de.mpg.mpiwg.berlin.mpdl.xmlrpc.MpdlXmlRpcInterfaceImpl;

/**
 * Handler for eXist collections and documents (singleton). 
 * It could not be used in a multi threading environment.
 * Locally saved documents could be stored (over XML-RPC) into eXist
 * collections. Collections could be configured language specific (see
 * instance variable "languages" below).
 * Your local directory structure must look like this:
 * documents
 *   archimedes
 *     ar
 *       yourDoc1.xml
 *       ...
 *     ...
 *     zh
 *       yourDoc1.xml
 *       ...
 *   echo
 *     ar
 *       yourDoc1.xml
 *       ...
 *     ...
 *     zh
 *       yourDoc1.xml
 *       ...
 *       
 */
public class MpdlXmlRpcDocHandler {
  private static MpdlXmlRpcDocHandler instance;
  private static Logger LOGGER = Logger.getLogger(MpdlXmlRpcDocHandler.class); // Logs to EXIST_HOME/webapp/WEB-INF/logs/exist.log
  private static String DOC_ROOT_COLLECTION_MORPH = "/db/mpdl/documents/morph";
  private static String DOC_ROOT_COLLECTION_STANDARD = "/db/mpdl/documents/standard";
  private static String LOCAL_DOC_DIR = MpdlConstants.MPDL_EXIST_DATA_DIR + "/" + "documents";
  private static String SERVER_NAME = MpdlConstants.MPDL_EXIST_HOST_NAME;
  private static int SERVER_PORT =  MpdlConstants.MPDL_EXIST_PORT;
  private static String ADMIN_USER_NAME = MpdlConstants.MPDL_EXIST_ADMIN_USER_NAME;
  private static String ADMIN_USER_PW = MpdlConstants.MPDL_EXIST_ADMIN_USER_PW;
  private MpdlXmlRpcInterface eXistXmlRpcInterface = null;

  public static MpdlXmlRpcDocHandler getInstance() throws ApplicationException {
    if (instance == null) {
      instance = new MpdlXmlRpcDocHandler();
      instance.init();
    }
    return instance;
  }

  private void init() throws ApplicationException {
    eXistXmlRpcInterface = MpdlXmlRpcInterfaceImpl.getInstance(SERVER_NAME, SERVER_PORT, ADMIN_USER_NAME, ADMIN_USER_PW);
  }
  
  public boolean documentExists(MpdlDocOperation docOperation) throws ApplicationException {
    String docFileName = docOperation.getFileName();
    String docBase = docOperation.getDocBase();
    String language = docOperation.getLanguage();
    boolean docExists = documentExists(docBase, language, docFileName);
    return docExists;
  }
  
  public void saveDocumentFile(MpdlDocOperation docOperation) throws ApplicationException {
    String docFileName = docOperation.getFileName();
    String docBase = docOperation.getDocBase();
    String language = docOperation.getLanguage();
    saveDocumentFile(docBase, language, docFileName);
  }
  
  public void saveDocumentFile(String localFile, String existIdentifier) throws ApplicationException {
    int index = existIdentifier.lastIndexOf("/");
    String collection = existIdentifier.substring(0, index);
    String docFileName = existIdentifier.substring(index); 
    String documentCollectionMorph = DOC_ROOT_COLLECTION_MORPH + collection;
    String documentCollectionStandard = DOC_ROOT_COLLECTION_STANDARD + collection;
    eXistXmlRpcInterface.saveDocument(documentCollectionMorph, docFileName, localFile);
    LOGGER.info("MPDL: XML-RPC: Document: \"" + localFile + "\" saved to eXist collection: \"" + documentCollectionMorph + "\"");
    eXistXmlRpcInterface.saveDocument(documentCollectionStandard, docFileName, localFile);
    LOGGER.info("MPDL: XML-RPC: Document: \"" + localFile + "\" saved to eXist collection: \"" + documentCollectionStandard + "\"");
  }
  
  public void deleteDocumentFile(MpdlDocOperation docOperation) throws ApplicationException {
    String docFileName = docOperation.getFileName();
    String docBase = docOperation.getDocBase();
    String language = docOperation.getLanguage();
    deleteDocumentFile(docBase, language, docFileName);
  }
  
  public void deleteDocumentFile(String existIdentifier) throws ApplicationException {
    int index = existIdentifier.lastIndexOf("/");
    String collection = existIdentifier.substring(0, index);
    String docFileName = existIdentifier.substring(index); 
    String documentCollectionMorph = DOC_ROOT_COLLECTION_MORPH + collection;
    String documentCollectionStandard = DOC_ROOT_COLLECTION_STANDARD + collection;
    eXistXmlRpcInterface.deleteDocument(documentCollectionMorph, docFileName);
    LOGGER.info("MPDL: XML-RPC: Document deleted: \"" + documentCollectionMorph + "/" + docFileName + "\"");
    eXistXmlRpcInterface.deleteDocument(documentCollectionStandard, docFileName);
    LOGGER.info("MPDL: XML-RPC: Document deleted: \"" + documentCollectionStandard + "/" + docFileName + "\"");
  }
  
  public void createCollection(String fullCollectionName) throws ApplicationException {
    eXistXmlRpcInterface.createCollection(fullCollectionName);
  }
  
  public void deleteCollection(String fullCollectionName) throws ApplicationException {
    eXistXmlRpcInterface.deleteCollection(fullCollectionName);
  }
  
  private boolean documentExists(String docBase, String language, String docFileName) throws ApplicationException {
    String documentCollection = DOC_ROOT_COLLECTION_STANDARD + "/" + docBase + "/" + language;
    String fullDocName = documentCollection + "/" + docFileName;
    String[] fullCollectionDocNames = eXistXmlRpcInterface.getDocumentNames(documentCollection);
    boolean isAvailable = false;
    if (fullCollectionDocNames != null) {
      for (int i=0; i<fullCollectionDocNames.length; i++) {
        String docName = fullCollectionDocNames[i];
        if (docName.equals(fullDocName))
          isAvailable = true;
      }
    }
    return isAvailable;
  }
  
  private void saveDocumentFile(String docBase, String language, String docFileName) throws ApplicationException {
    String documentCollectionMorph = DOC_ROOT_COLLECTION_MORPH + "/" + docBase + "/" + language;
    String documentCollectionStandard = DOC_ROOT_COLLECTION_STANDARD + "/" + docBase + "/" + language;
    String localFile = LOCAL_DOC_DIR + "/" + docBase + "/" + language + "/" + docFileName;
    eXistXmlRpcInterface.saveDocument(documentCollectionMorph, docFileName, localFile);
    LOGGER.info("MPDL: XML-RPC: Document: \"" + localFile + "\" saved to eXist collection: \"" + documentCollectionMorph + "\"");
    eXistXmlRpcInterface.saveDocument(documentCollectionStandard, docFileName, localFile);
    LOGGER.info("MPDL: XML-RPC: Document: \"" + localFile + "\" saved to eXist collection: \"" + documentCollectionStandard + "\"");
  }
  
  private void deleteDocumentFile(String docBase, String language, String docFileName) throws ApplicationException {
    String documentCollectionMorph = DOC_ROOT_COLLECTION_MORPH + "/" + docBase + "/" + language;
    String documentCollectionStandard = DOC_ROOT_COLLECTION_STANDARD + "/" + docBase + "/" + language;
    eXistXmlRpcInterface.deleteDocument(documentCollectionMorph, docFileName);
    LOGGER.info("MPDL: XML-RPC: Document deleted: \"" + documentCollectionMorph + "/" + docFileName + "\"");
    eXistXmlRpcInterface.deleteDocument(documentCollectionStandard, docFileName);
    LOGGER.info("MPDL: XML-RPC: Document deleted: \"" + documentCollectionStandard + "/" + docFileName + "\"");
  }
}