Package letweb.semanticum

Source Code of letweb.semanticum.LinkCollectionReader

package letweb.semanticum;

import java.io.IOException;
import java.sql.SQLException;

import org.apache.uima.cas.CAS;
import org.apache.uima.cas.CASException;
import org.apache.uima.collection.CollectionException;
import org.apache.uima.collection.CollectionReader_ImplBase;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.cas.StringArray;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Level;
import org.apache.uima.util.Progress;
import org.apache.uima.util.ProgressImpl;

import letweb.semanticum.DatabaseCalls;
import letweb.semanticum.tsd.LinkType;

public class LinkCollectionReader extends CollectionReader_ImplBase {
 
  public static final String nlink = "numLinks";
  public static final String filenameConf = "dbFileConf";
 
  private DatabaseCalls connDb;
 
  private String fileConf;
 
  private Integer numLinks;
  private int idx;//indice dei link pagina http, link principali non singoli link video
 
  @Override
  public void initialize() throws ResourceInitializationException{
   
    fileConf  = (String) getConfigParameterValue(filenameConf);
    numLinks  = (Integer) getConfigParameterValue(nlink);
    idx = 0;
   
    try {
      connDb = new DatabaseCalls(fileConf,numLinks);
      connDb.startConnection();
      connDb.parse();
    } catch (IOException e) {
      getUimaContext().getLogger().log(Level.WARNING, "IOError: " + e.getMessage());
    } catch (InstantiationException e) {
      getUimaContext().getLogger().log(Level.WARNING, "InstError: " + e.getMessage());
    } catch (IllegalAccessException e) {
      getUimaContext().getLogger().log(Level.WARNING, "IllegalAccesError: " + e.getMessage());
    } catch (ClassNotFoundException e) {
      getUimaContext().getLogger().log(Level.WARNING, "ClassError: " + e.getMessage());
    } catch (SQLException e) {
//      StringBuffer s = new StringBuffer();
//      s.append(e.getMessage() + " SQLError: ");
//      for(StackTraceElement x: e.getStackTrace())
//        s.append(x.toString() + "\n");
//      getUimaContext().getLogger().log(Level.WARNING, s.toString() + "\n" + "FETCH: " + connDb.getTempFetch());
      getUimaContext().getLogger().log(Level.WARNING, "SQLError: " + e.getMessage());
    }
    //Chiamata alla classe gestione DB, e realizza Apertura connessione con DB
    //Chiamata alla classe gestione DB, ed Estrae n link contenuti nel DB, non processed (Verificare Inizio e Fine)
    //Chiamata alla classe gestione DB, e realizza Chiusura connessione con DB

  }

  @Override
  public void getNext(CAS aCAS) throws CollectionException {
    // Restituisce il link da analizzare successivo (link del file video/audio)
   
    JCas jcas;
      try {
        jcas = aCAS.getJCas();
      } catch (CASException e) {
        throw new CollectionException(e);
      }
     
      LinkType lt = new LinkType(jcas);
     
      //lt.setVideoUrl(links.get(0));
      lt.setVideoUrl(connDb.getVideoUrl(idx));
     
      String [] temp = connDb.getLinks(idx);
      //Inizializzazione dell'array di stringhe
      StringArray ArrayVideoUriStringArray = new StringArray(jcas, temp.length);
      lt.setArrayVideoUri(ArrayVideoUriStringArray);
      //Riempimento dell'array di stringhe
      int i;
      for(i = 0; i<temp.length; i++)
        lt.setArrayVideoUri(i,temp[i]);
      lt.setSourceType(connDb.getSourceType(idx));
     
      temp = connDb.getAuthors(idx);
      StringArray authorsStringArray = new StringArray(jcas, temp.length);
      lt.setAuthors(authorsStringArray);
      for(i = 0; i<temp.length; i++)
        lt.setAuthors(i,temp[i]);
     
      lt.setDate(connDb.getDate(idx));
      lt.setMainTopic(connDb.getMainTopic(idx));
      lt.setDescription(connDb.getDescription(idx));
     
      temp = connDb.getKeywords(idx);
      StringArray keywordsStringArray = new StringArray(jcas,temp.length);
      lt.setKeywords(keywordsStringArray);
      for(i = 0; i<temp.length; i++)
        lt.setKeywords(i,temp[i]);
      lt.setId(connDb.getId().get(idx++));
      lt.addToIndexes();
     
  }

  @Override
  public boolean hasNext() throws CollectionException {
    // Se la lista di link generata dal collection reader ha
    // link da analizzare, conferma
    return connDb.hasNext(idx);
  }

  @Override
  public Progress[] getProgress() {
    // Quanti link mancano alla chiusura di UIMA
    //return della progressione dell'analisi in termini di link corrente rispetto al numero di link
    return new Progress[]{new ProgressImpl(idx, numLinks, Progress.ENTITIES)};
  }

  @Override
  public void close() {
    //chiudere connessione al db
   
    try {
      for(Integer i: connDb.getId())
        connDb.insertTe(i);
      connDb.closeConnection();
    } catch (SQLException e) {
      getUimaContext().getLogger().log(Level.WARNING, "Error: " + e.getMessage());
    }
  }
}
TOP

Related Classes of letweb.semanticum.LinkCollectionReader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.