es.ucm.fdi.gaia.jcolibri.connector.DataBaseConnector Maven / Gradle / Ivy
Show all versions of jCOLIBRI Show documentation
package es.ucm.fdi.gaia.jcolibri.connector;
import org.apache.logging.log4j.LogManager;
import java.net.URL;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.w3c.dom.Document;
import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase;
import es.ucm.fdi.gaia.jcolibri.cbrcore.CaseBaseFilter;
import es.ucm.fdi.gaia.jcolibri.cbrcore.CaseComponent;
import es.ucm.fdi.gaia.jcolibri.cbrcore.Connector;
import es.ucm.fdi.gaia.jcolibri.exception.AttributeAccessException;
import es.ucm.fdi.gaia.jcolibri.exception.InitializingException;
import es.ucm.fdi.gaia.jcolibri.util.FileIO;
/**
* Implements a data base connector using the Hibernate package.
*
* The configuration file follows the schema defined in
* /doc/configfilesSchemas/DataBaseConnector.xsd:
*
*
*
* There are several examples that incrementally show how to use this connector: Test1, Test2, Test3, Test4 and Test5.
*
* @author Juan Antonio Recio García
* @version 3.0
*
*/
public class DataBaseConnector implements Connector {
private SessionFactory sessionFactory;
private String descriptionClassName;
private String solutionClassName;
private String justOfSolutionClassName;
private String resultClassName;
/* (non-Javadoc)
* @see jcolibri.cbrcore.Connector#close()
*/
public void close() {}
/* (non-Javadoc)
* @see jcolibri.cbrcore.Connector#deleteCases(java.util.Collection)
*/
public void deleteCases(Collection cases) {}
/* (non-Javadoc)
* @see jcolibri.cbrcore.Connector#initFromXMLfile(java.io.File)
*/
public void initFromXMLfile(URL file) throws InitializingException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//factory.setValidating(true);
//factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse( file.openStream() );
String hcf = document.getElementsByTagName("HibernateConfigFile").item(0).getTextContent();
String descriptionMapFile = document.getElementsByTagName("DescriptionMappingFile").item(0).getTextContent();
descriptionClassName = document.getElementsByTagName("DescriptionClassName").item(0).getTextContent();
Configuration hbconfig = new Configuration();
hbconfig.configure(FileIO.findFile(hcf));
hbconfig.addURL(FileIO.findFile(descriptionMapFile));
try{
String solutionMapFile = document.getElementsByTagName("SolutionMappingFile").item(0).getTextContent();
solutionClassName = document.getElementsByTagName("SolutionClassName").item(0).getTextContent();
hbconfig.addResource(solutionMapFile);
}catch(Exception e)
{
LogManager.getLogger(this.getClass()).info("Case does not have solution");
}
try{
String justOfSolutionMapFile = document.getElementsByTagName("JustificationOfSolutionMappingFile").item(0).getTextContent();
justOfSolutionClassName = document.getElementsByTagName("JustificationOfSolutionClassName").item(0).getTextContent();
hbconfig.addResource(justOfSolutionMapFile);
}catch(Exception e)
{
LogManager.getLogger(this.getClass()).info("Case does not have justification of the solution");
}
try{
String resultMapFile = document.getElementsByTagName("ResultMappingFile").item(0).getTextContent();
resultClassName = document.getElementsByTagName("ResultClassName").item(0).getTextContent();
hbconfig.addResource(resultMapFile);
}catch(Exception e)
{
LogManager.getLogger(this.getClass()).info("Case does not have result");
}
sessionFactory = hbconfig.buildSessionFactory();
} catch (Throwable ex) {
throw new InitializingException(ex);
}
}
/* (non-Javadoc)
* @see jcolibri.cbrcore.Connector#retrieveAllCases()
*/
public Collection retrieveAllCases(){
List res = new ArrayList<>();
try {
Session session;
Transaction transaction;
List> descList;
HashMap