view software/mpdl-services/mpiwg-mpdl-lt/src/de/mpg/mpiwg/berlin/mpdl/lt/morph/db/DBMorphHandler.java @ 19:4a3641ae14d2

Erstellung
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Wed, 09 Nov 2011 15:32:05 +0100
parents
children e845310098ba
line wrap: on
line source

package de.mpg.mpiwg.berlin.mpdl.lt.morph.db;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Hashtable;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
import com.sun.org.apache.xerces.internal.parsers.SAXParser;

import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;
import de.mpg.mpiwg.berlin.mpdl.lt.general.Language;
import de.mpg.mpiwg.berlin.mpdl.lt.morph.app.Form;
import de.mpg.mpiwg.berlin.mpdl.lt.morph.app.Lemma;
import de.mpg.mpiwg.berlin.mpdl.lt.morph.app.SimpleMorphContentHandler;

public class DBMorphHandler {
  private String dbDirectory;
  private DbEnvMorph morphDbEnv;
  
  public DBMorphHandler(String dbDir) {
    this.dbDirectory = dbDir;
  }
  
  public void start() throws ApplicationException {
    morphDbEnv = new DbEnvMorph();
    morphDbEnv.setDataDir(dbDirectory);
    morphDbEnv.init(); // open databases in read/write mode
  }
  
  public void openDatabases() throws ApplicationException {
    morphDbEnv.openDatabases();
  }

  public void closeDatabases() throws ApplicationException {
    morphDbEnv.close();
  }

  public void deleteMorphData() throws ApplicationException {
    morphDbEnv.removeDatabases();
  }
  
  public long getSize() throws ApplicationException {
    long size = 0;
    try {
      Database formDB = morphDbEnv.getFormDB();
      size = formDB.count();
    } catch (DatabaseException e) {
      throw new ApplicationException(e);
    }
    return size;
  }
  

  public void writeFormLemma(Form form, Lemma lemma) throws ApplicationException {
    try {
      String lang = Language.getInstance().getLanguageId(form.getLanguage());
      String keyStr = lang + "###" + form.getFormName();
      String valueStr = lemma.getXmlString();
      DatabaseEntry dbEntryKey = new DatabaseEntry(keyStr.getBytes("utf-8"));
      DatabaseEntry dbEntryValue = new DatabaseEntry(valueStr.getBytes("utf-8"));
      Database formDB = morphDbEnv.getFormDB();
      formDB.put(null, dbEntryKey, dbEntryValue);
    } catch (DatabaseException e) {
      throw new ApplicationException(e);
    } catch (UnsupportedEncodingException e) {
      throw new ApplicationException(e);
    }
  }
    
  public void writeLemmaForm(Lemma lemma, Form form) throws ApplicationException {
    try {
      String lang = Language.getInstance().getLanguageId(lemma.getLanguage());
      String keyStr = lang + "###" + lemma.getLemmaName();
      String valueStr = form.getXmlString();
      DatabaseEntry dbEntryKey = new DatabaseEntry(keyStr.getBytes("utf-8"));
      DatabaseEntry dbEntryValue = new DatabaseEntry(valueStr.getBytes("utf-8"));
      Database lemmaDB = morphDbEnv.getLemmaDB();
      lemmaDB.put(null, dbEntryKey, dbEntryValue);
    } catch (DatabaseException e) {
      throw new ApplicationException(e);
    } catch (UnsupportedEncodingException e) {
      throw new ApplicationException(e);
    }
  }
    
  public void deleteLemma(Lemma lemma) throws ApplicationException {
    try {
      String lang = Language.getInstance().getLanguageId(lemma.getLanguage());
      String keyStr = lang + "###" + lemma.getLemmaName();
      DatabaseEntry dbEntryKey = new DatabaseEntry(keyStr.getBytes("utf-8"));
      Database lemmaDB = morphDbEnv.getLemmaDB();
      lemmaDB.delete(null, dbEntryKey);
    } catch (DatabaseException e) {
      throw new ApplicationException(e);
    } catch (UnsupportedEncodingException e) {
      throw new ApplicationException(e);
    }
  }      
      
  public void deleteForm(Form form) throws ApplicationException {
    try {
      String lang = Language.getInstance().getLanguageId(form.getLanguage());
      String keyStr = lang + "###" + form.getFormName();
      DatabaseEntry dbEntryKey = new DatabaseEntry(keyStr.getBytes("utf-8"));
      Database formDB = morphDbEnv.getFormDB();
      formDB.delete(null, dbEntryKey);
    } catch (DatabaseException e) {
      throw new ApplicationException(e);
    } catch (UnsupportedEncodingException e) {
      throw new ApplicationException(e);
    }
  }      
      
  public ArrayList<Form> readForms(String language, String lemmaName) throws ApplicationException {
    ArrayList<Form> retForms = new ArrayList<Form>();
    String lang = Language.getInstance().getLanguageId(language);
    String hashKey = lang + "###" + lemmaName;
    try {
      Database lemmaDB = morphDbEnv.getLemmaDB();
      Cursor cursor = lemmaDB.openCursor(null, null);
      byte[] bHashKey = hashKey.getBytes("utf-8");
      DatabaseEntry dbEntryKey = new DatabaseEntry(bHashKey);
      DatabaseEntry foundFormValue = new DatabaseEntry();
      OperationStatus operationStatus = cursor.getSearchKey(dbEntryKey, foundFormValue, LockMode.DEFAULT);
      while (operationStatus == OperationStatus.SUCCESS) {
        byte[] foundFormValueBytes = foundFormValue.getData();
        String foundFormValueStr = new String(foundFormValueBytes, "utf-8");
        Form f = parseXmlFormString(foundFormValueStr);
        retForms.add(f);
        operationStatus = cursor.getNextDup(dbEntryKey, foundFormValue, LockMode.DEFAULT);
      }
      cursor.close();
    } catch (DatabaseException e) {
      throw new ApplicationException(e);
    } catch (UnsupportedEncodingException e) {
      throw new ApplicationException(e);
    }
    return retForms;
  }
  
  // TODO diese Methode wird nicht verwendet bis jetzt
  public Hashtable<String, Form> readForms() throws ApplicationException {
    Hashtable<String, Form> retForms = new Hashtable<String, Form>();
    try {
      Database lemmaDB = morphDbEnv.getLemmaDB();
      Cursor cursor = lemmaDB.openCursor(null, null);
      DatabaseEntry dbEntryKey = new DatabaseEntry();
      DatabaseEntry foundFormValue = new DatabaseEntry();
      OperationStatus operationStatus = cursor.getFirst(dbEntryKey, foundFormValue, LockMode.DEFAULT);
      while (operationStatus == OperationStatus.SUCCESS) {
        byte[] foundFormValueBytes = foundFormValue.getData();
        String foundFormValueStr = new String(foundFormValueBytes, "utf-8");
        Form f = parseXmlFormString(foundFormValueStr);
        String formHashKey = f.getLanguage() + "###" + f.getFormName();
        retForms.put(formHashKey, f);
        operationStatus = cursor.getNext(dbEntryKey, foundFormValue, LockMode.DEFAULT);
      }
      cursor.close();
    } catch (DatabaseException e) {
      throw new ApplicationException(e);
    } catch (UnsupportedEncodingException e) {
      throw new ApplicationException(e);
    }
    return retForms;
  }
  
  public ArrayList<Lemma> readLemmas(String language, String formName) throws ApplicationException {
    ArrayList<Lemma> retForms = new ArrayList<Lemma>();
    String lang = Language.getInstance().getLanguageId(language);
    String hashKey = lang + "###" + formName;
    try {
      Database formDB = morphDbEnv.getFormDB();
      Cursor cursor = formDB.openCursor(null, null);
      byte[] bHashKey = hashKey.getBytes("utf-8");
      DatabaseEntry dbEntryKey = new DatabaseEntry(bHashKey);
      DatabaseEntry foundLemmaValue = new DatabaseEntry();
      OperationStatus operationStatus = cursor.getSearchKey(dbEntryKey, foundLemmaValue, LockMode.DEFAULT);
      while (operationStatus == OperationStatus.SUCCESS) {
        byte[] foundLemmaValueBytes = foundLemmaValue.getData();
        String foundLemmaValueStr = new String(foundLemmaValueBytes, "utf-8");
        Lemma l = parseXmlLemmaString(foundLemmaValueStr);
        retForms.add(l);
        operationStatus = cursor.getNextDup(dbEntryKey, foundLemmaValue, LockMode.DEFAULT);
      }
      cursor.close();
    } catch (DatabaseException e) {
      throw new ApplicationException(e);
    } catch (UnsupportedEncodingException e) {
      throw new ApplicationException(e);
    }
    return retForms;
  }
  
  private Form parseXmlFormString(String xmlString) throws ApplicationException {
    Form form = null;
    try {
      XMLReader xmlParser = new SAXParser();
      SimpleMorphContentHandler morphContentHandler = new SimpleMorphContentHandler();
      xmlParser.setContentHandler(morphContentHandler);
      Reader reader = new StringReader(xmlString);
      InputSource input = new InputSource(reader);
      xmlParser.parse(input);
      form = morphContentHandler.getForm();
    } catch (SAXException e) {
      throw new ApplicationException(e);
    } catch (IOException e) {
      throw new ApplicationException(e);
    }
    return form;
  }

  private Lemma parseXmlLemmaString(String xmlString) throws ApplicationException {
    Lemma lemma = null;
    try {
      XMLReader xmlParser = new SAXParser();
      SimpleMorphContentHandler morphContentHandler = new SimpleMorphContentHandler();
      xmlParser.setContentHandler(morphContentHandler);
      Reader reader = new StringReader(xmlString);
      InputSource input = new InputSource(reader);
      xmlParser.parse(input);
      lemma = morphContentHandler.getLemma();
    } catch (SAXException e) {
      throw new ApplicationException(e);
    } catch (IOException e) {
      throw new ApplicationException(e);
    }
    return lemma;
  }

}