git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/EcologicalEngine@150568 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
38ce4eb4b5
commit
dbaedc1ce7
|
@ -22,7 +22,7 @@
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
</attributes>
|
</attributes>
|
||||||
</classpathentry>
|
</classpathentry>
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||||
<attributes>
|
<attributes>
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
</attributes>
|
</attributes>
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
eclipse.preferences.version=1
|
eclipse.preferences.version=1
|
||||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||||
org.eclipse.jdt.core.compiler.compliance=1.7
|
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||||
org.eclipse.jdt.core.compiler.source=1.7
|
org.eclipse.jdt.core.compiler.source=1.8
|
||||||
|
|
9
pom.xml
9
pom.xml
|
@ -67,11 +67,6 @@
|
||||||
<artifactId>commons-logging</artifactId>
|
<artifactId>commons-logging</artifactId>
|
||||||
<version>1.0.4</version>
|
<version>1.0.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>dom4j</groupId>
|
|
||||||
<artifactId>dom4j</artifactId>
|
|
||||||
<version>1.6</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>graphlayout</groupId>
|
<groupId>graphlayout</groupId>
|
||||||
<artifactId>graphlayout</artifactId>
|
<artifactId>graphlayout</artifactId>
|
||||||
|
@ -233,8 +228,8 @@
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.1</version>
|
<version>3.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.7</source>
|
<source>1.8</source>
|
||||||
<target>1.7</target>
|
<target>1.8</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|
|
@ -1,18 +1,13 @@
|
||||||
package org.gcube.contentmanagement.lexicalmatcher.utils;
|
package org.gcube.contentmanagement.lexicalmatcher.utils;
|
||||||
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
|
||||||
import org.dom4j.Document;
|
|
||||||
import org.dom4j.Node;
|
|
||||||
import org.dom4j.io.SAXReader;
|
|
||||||
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
|
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
|
||||||
import org.hibernate.Query;
|
import org.hibernate.Query;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
|
@ -30,78 +25,41 @@ public class DatabaseFactory {
|
||||||
return DBSessionFactory;
|
return DBSessionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"unchecked"})
|
|
||||||
public static SessionFactory initDBConnection(String configurationFile, LexicalEngineConfiguration config) throws Exception {
|
public static SessionFactory initDBConnection(String configurationFile, LexicalEngineConfiguration config) throws Exception {
|
||||||
|
|
||||||
|
InputStream stream= null;
|
||||||
if (config==null)
|
try{
|
||||||
return initDBConnection(configurationFile);
|
if (config==null)
|
||||||
|
return initDBConnection(configurationFile);
|
||||||
InputStream stream;
|
|
||||||
try {
|
|
||||||
File fl = new File(configurationFile);
|
|
||||||
stream = new FileInputStream(fl);
|
|
||||||
} catch (Exception e) {
|
|
||||||
stream = ClassLoader.getSystemResourceAsStream(configurationFile);
|
|
||||||
}
|
|
||||||
SAXReader saxReader = new SAXReader();
|
|
||||||
Document document = saxReader.read(stream);
|
|
||||||
|
|
||||||
List<Node> nodes = document.selectNodes("//hibernate-configuration/session-factory/property");
|
|
||||||
Iterator<Node> nodesIterator = nodes.iterator();
|
|
||||||
|
|
||||||
// System.out.println("--- DATABASE Configuration --- ");
|
|
||||||
|
|
||||||
while (nodesIterator.hasNext()) {
|
|
||||||
Node currentnode = nodesIterator.next();
|
|
||||||
String element = currentnode.valueOf("@name");
|
|
||||||
if (element.equals("connection.driver_class"))
|
|
||||||
if (config.getDatabaseDriver() != null){
|
|
||||||
currentnode.setText(config.getDatabaseDriver());
|
|
||||||
}
|
|
||||||
if (element.equals("connection.url")) {
|
|
||||||
if (config.getDatabaseURL() != null)
|
|
||||||
currentnode.setText(config.getDatabaseURL());
|
|
||||||
}
|
|
||||||
if (element.equals("connection.username")) {
|
|
||||||
if (config.getDatabaseUserName() != null)
|
|
||||||
currentnode.setText(config.getDatabaseUserName());
|
|
||||||
}
|
|
||||||
if (element.equals("connection.password")) {
|
|
||||||
if (config.getDatabasePassword() != null)
|
|
||||||
currentnode.setText(config.getDatabasePassword());
|
|
||||||
}
|
|
||||||
if (element.equals("dialect")) {
|
|
||||||
AnalysisLogger.getLogger().trace("Dialect -> "+config.getDatabaseDialect());
|
|
||||||
if (config.getDatabaseDialect() != null)
|
|
||||||
currentnode.setText(config.getDatabaseDialect());
|
|
||||||
}
|
|
||||||
if (element.equals("c3p0.idleConnectionTestPeriod")) {
|
|
||||||
if (config.getDatabaseIdleConnectionTestPeriod() != null)
|
|
||||||
currentnode.setText(config.getDatabaseIdleConnectionTestPeriod());
|
|
||||||
}
|
|
||||||
if (element.equals("c3p0.automaticTestTable")) {
|
|
||||||
if (config.getDatabaseAutomaticTestTable() != null)
|
|
||||||
currentnode.setText(config.getDatabaseAutomaticTestTable());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Configuration cfg = new Configuration();
|
|
||||||
cfg = cfg.configure(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(document.asXML().getBytes())));
|
|
||||||
cfg.setProperty("hibernate.hbm2ddl.auto", "create");
|
|
||||||
|
|
||||||
SessionFactory DBSessionFactory = null;
|
|
||||||
DBSessionFactory = cfg.buildSessionFactory();
|
|
||||||
|
|
||||||
// close stream
|
try {
|
||||||
stream.close();
|
File fl = new File(configurationFile);
|
||||||
|
stream = new FileInputStream(fl);
|
||||||
return DBSessionFactory;
|
} catch (Exception e) {
|
||||||
|
stream = ClassLoader.getSystemResourceAsStream(configurationFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
Configuration cfg = new Configuration();
|
||||||
|
cfg = cfg.configure(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stream));
|
||||||
|
cfg.setProperty("hibernate.hbm2ddl.auto", "create");
|
||||||
|
|
||||||
|
SessionFactory DBSessionFactory = null;
|
||||||
|
DBSessionFactory = cfg.buildSessionFactory();
|
||||||
|
|
||||||
|
// close stream
|
||||||
|
stream.close();
|
||||||
|
|
||||||
|
return DBSessionFactory;
|
||||||
|
}finally{
|
||||||
|
if (stream!=null)
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"unchecked"})
|
@SuppressWarnings({"unchecked"})
|
||||||
public static List<Object> executeHQLQuery(String query, SessionFactory DBSessionFactory, boolean useSQL) {
|
public static List<Object> executeHQLQuery(String query, SessionFactory DBSessionFactory, boolean useSQL) {
|
||||||
|
|
||||||
List<Object> obj = null;
|
List<Object> obj = null;
|
||||||
Session ss = null;
|
Session ss = null;
|
||||||
try {
|
try {
|
||||||
|
@ -126,15 +84,15 @@ public class DatabaseFactory {
|
||||||
|
|
||||||
if (result != null && result.size() == 0)
|
if (result != null && result.size() == 0)
|
||||||
System.out.println(String.format("found nothing in database"));
|
System.out.println(String.format("found nothing in database"));
|
||||||
*/
|
*/
|
||||||
if (result != null && result.size() != 0) {
|
if (result != null && result.size() != 0) {
|
||||||
obj = result;
|
obj = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
||||||
// System.out.println(String.format("Error while executing query: %1$s %2$s", query, e.getMessage()));
|
// System.out.println(String.format("Error while executing query: %1$s %2$s", query, e.getMessage()));
|
||||||
// e.printStackTrace();
|
// e.printStackTrace();
|
||||||
System.out.println(String.format("Error while executing query: %1$s %2$s", query, e.getMessage()));
|
System.out.println(String.format("Error while executing query: %1$s %2$s", query, e.getMessage()));
|
||||||
rollback(ss);
|
rollback(ss);
|
||||||
}
|
}
|
||||||
|
@ -144,13 +102,13 @@ public class DatabaseFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void executeHQLUpdate(String query, SessionFactory DBSessionFactory, boolean useSQL) throws Exception{
|
public static void executeHQLUpdate(String query, SessionFactory DBSessionFactory, boolean useSQL) throws Exception{
|
||||||
// System.out.println("executing query: " + query);
|
// System.out.println("executing query: " + query);
|
||||||
Session ss = null;
|
Session ss = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
ss = DBSessionFactory.getCurrentSession();
|
ss = DBSessionFactory.getCurrentSession();
|
||||||
// System.out.println("executing query");
|
// System.out.println("executing query");
|
||||||
ss.beginTransaction();
|
ss.beginTransaction();
|
||||||
Query qr = null;
|
Query qr = null;
|
||||||
|
|
||||||
|
|
|
@ -9,29 +9,59 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
|
||||||
import org.dom4j.Document;
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
import org.dom4j.io.SAXReader;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import javax.xml.transform.OutputKeys;
|
||||||
|
import javax.xml.transform.Transformer;
|
||||||
|
import javax.xml.transform.TransformerFactory;
|
||||||
|
import javax.xml.transform.dom.DOMSource;
|
||||||
|
import javax.xml.transform.stream.StreamResult;
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
|
||||||
public class FileTools {
|
public class FileTools {
|
||||||
|
|
||||||
public static String readXMLDoc(String xmlFilePath) throws Exception {
|
public static String readXMLDoc(String xmlFilePath) throws Exception {
|
||||||
String xml = null;
|
String xml = null;
|
||||||
InputStream stream;
|
InputStream stream = null;
|
||||||
try {
|
try{
|
||||||
File fl = new File(xmlFilePath);
|
try {
|
||||||
stream = new FileInputStream(fl);
|
File fl = new File(xmlFilePath);
|
||||||
} catch (Exception e) {
|
stream = new FileInputStream(fl);
|
||||||
stream = ClassLoader.getSystemResourceAsStream(xmlFilePath);
|
} catch (Exception e) {
|
||||||
}
|
stream = ClassLoader.getSystemResourceAsStream(xmlFilePath);
|
||||||
SAXReader saxReader = new SAXReader();
|
}
|
||||||
Document document = saxReader.read(stream);
|
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
|
||||||
xml = document.asXML();
|
.newDocumentBuilder();
|
||||||
stream.close();
|
|
||||||
|
|
||||||
return xml;
|
Document doc = dBuilder.parse(stream);
|
||||||
|
xml = toString(doc);
|
||||||
|
return xml;
|
||||||
|
}finally{
|
||||||
|
if (stream!=null)
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String toString(Document doc) {
|
||||||
|
try {
|
||||||
|
StringWriter sw = new StringWriter();
|
||||||
|
TransformerFactory tf = TransformerFactory.newInstance();
|
||||||
|
Transformer transformer = tf.newTransformer();
|
||||||
|
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
|
||||||
|
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
|
||||||
|
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||||
|
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
||||||
|
|
||||||
|
transformer.transform(new DOMSource(doc), new StreamResult(sw));
|
||||||
|
return sw.toString();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
throw new RuntimeException("Error converting to String", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void saveString2File(String filename, String string2save) throws Exception {
|
public static void saveString2File(String filename, String string2save) throws Exception {
|
||||||
|
|
|
@ -8,14 +8,12 @@ import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import javax.xml.parsers.SAXParser;
|
||||||
|
import javax.xml.parsers.SAXParserFactory;
|
||||||
|
|
||||||
import org.dom4j.Document;
|
|
||||||
import org.dom4j.Node;
|
|
||||||
import org.dom4j.io.SAXReader;
|
|
||||||
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
|
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
|
||||||
import org.gcube.contentmanagement.lexicalmatcher.utils.FileTools;
|
import org.gcube.contentmanagement.lexicalmatcher.utils.FileTools;
|
||||||
import org.gcube.dataanalysis.ecoengine.datatypes.DatabaseType;
|
import org.gcube.dataanalysis.ecoengine.datatypes.DatabaseType;
|
||||||
|
@ -25,6 +23,9 @@ import org.hibernate.Query;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
|
import org.xml.sax.Attributes;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
import org.xml.sax.helpers.DefaultHandler;
|
||||||
|
|
||||||
public class DatabaseFactory{
|
public class DatabaseFactory{
|
||||||
|
|
||||||
|
@ -37,110 +38,86 @@ public class DatabaseFactory{
|
||||||
return DBSessionFactory;
|
return DBSessionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static List<StatisticalType> getDefaultDatabaseConfiguration(String configurationFile) throws Exception {
|
public static List<StatisticalType> getDefaultDatabaseConfiguration(String configurationFile) throws Exception {
|
||||||
|
|
||||||
List<StatisticalType> defaultconfig = new ArrayList<StatisticalType>();
|
final List<StatisticalType> defaultconfig = new ArrayList<StatisticalType>();
|
||||||
|
|
||||||
// take the configuration file
|
// take the configuration file
|
||||||
File fl = new File(configurationFile);
|
File fl = new File(configurationFile);
|
||||||
FileInputStream stream = new FileInputStream(fl);
|
FileInputStream stream = new FileInputStream(fl);
|
||||||
|
|
||||||
SAXReader saxReader = new SAXReader();
|
|
||||||
Document document = saxReader.read(stream);
|
|
||||||
List<Node> nodes = document.selectNodes("//hibernate-configuration/session-factory/property");
|
|
||||||
Iterator<Node> nodesIterator = nodes.iterator();
|
|
||||||
|
|
||||||
while (nodesIterator.hasNext()) {
|
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
|
||||||
|
|
||||||
Node currentnode = nodesIterator.next();
|
parser.parse(stream, new DefaultHandler(){
|
||||||
String element = currentnode.valueOf("@name");
|
|
||||||
if (element.equals("connection.driver_class"))
|
private DatabaseParameters actualparameter = null;
|
||||||
defaultconfig.add(new DatabaseType(DatabaseParameters.DATABASEDRIVER, "DatabaseDriver", "db driver",currentnode.getText()));
|
private String actualParameterDescription= null;
|
||||||
if (element.equals("connection.url"))
|
|
||||||
defaultconfig.add(new DatabaseType(DatabaseParameters.DATABASEURL, "DatabaseURL", "db url",currentnode.getText()));
|
@Override
|
||||||
if (element.equals("connection.username"))
|
public void startElement(String uri, String localName,
|
||||||
defaultconfig.add(new DatabaseType(DatabaseParameters.DATABASEUSERNAME, "DatabaseUserName", "db user name",currentnode.getText()));
|
String qName, Attributes attributes) throws SAXException {
|
||||||
if (element.equals("connection.password"))
|
|
||||||
defaultconfig.add(new DatabaseType(DatabaseParameters.DATABASEPASSWORD, "DatabasePassword", "db password",currentnode.getText()));
|
if (localName.equals("property")){
|
||||||
if (element.equals("dialect"))
|
String element = attributes.getValue("name");
|
||||||
defaultconfig.add(new DatabaseType(DatabaseParameters.DATABASEDIALECT, "DatabaseDialect", "db dialect",currentnode.getText()));
|
|
||||||
}
|
if (element.equals("connection.driver_class")){
|
||||||
|
actualparameter = DatabaseParameters.DATABASEDRIVER;
|
||||||
|
actualParameterDescription = "DatabaseDriver";
|
||||||
|
}else if (element.equals("connection.url")) {
|
||||||
|
actualparameter = DatabaseParameters.DATABASEURL;
|
||||||
|
actualParameterDescription = "DatabaseURL";
|
||||||
|
}else if (element.equals("connection.username")){
|
||||||
|
actualparameter = DatabaseParameters.DATABASEUSERNAME;
|
||||||
|
actualParameterDescription = "DatabaseUserName";
|
||||||
|
}else if (element.equals("connection.password")){
|
||||||
|
actualparameter = DatabaseParameters.DATABASEPASSWORD;
|
||||||
|
actualParameterDescription = "DATABASEPASSWORD";
|
||||||
|
}else if (element.equals("dialect")){
|
||||||
|
actualparameter = DatabaseParameters.DATABASEDIALECT;
|
||||||
|
actualParameterDescription = "DATABASEDIALECT";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void characters(char ch[], int start, int length) throws SAXException {
|
||||||
|
|
||||||
|
if (actualParameterDescription!=null){
|
||||||
|
defaultconfig.add(new DatabaseType(actualparameter, actualParameterDescription, actualParameterDescription, new String(ch, start, length)));
|
||||||
|
actualparameter = null;
|
||||||
|
actualParameterDescription= null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return defaultconfig;
|
return defaultconfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"unchecked"})
|
@SuppressWarnings({"unchecked"})
|
||||||
public static SessionFactory initDBConnection(String configurationFile, LexicalEngineConfiguration config) throws Exception {
|
public static SessionFactory initDBConnection(String configurationFile, LexicalEngineConfiguration config) throws Exception {
|
||||||
|
|
||||||
|
|
||||||
if (config==null)
|
if (config==null)
|
||||||
return initDBConnection(configurationFile);
|
return initDBConnection(configurationFile);
|
||||||
|
|
||||||
|
|
||||||
// take the configuration file
|
// take the configuration file
|
||||||
File fl = new File(configurationFile);
|
File fl = new File(configurationFile);
|
||||||
FileInputStream stream = new FileInputStream(fl);
|
FileInputStream stream = new FileInputStream(fl);
|
||||||
|
|
||||||
SAXReader saxReader = new SAXReader();
|
|
||||||
Document document = saxReader.read(stream);
|
|
||||||
List<Node> nodes = document.selectNodes("//hibernate-configuration/session-factory/property");
|
|
||||||
Iterator<Node> nodesIterator = nodes.iterator();
|
|
||||||
|
|
||||||
while (nodesIterator.hasNext()) {
|
|
||||||
Node currentnode = nodesIterator.next();
|
|
||||||
String element = currentnode.valueOf("@name");
|
|
||||||
if (element.equals("connection.driver_class")){
|
|
||||||
if ((config.getDatabaseDriver() != null)&&(config.getDatabaseDriver().length()>0))
|
|
||||||
currentnode.setText(config.getDatabaseDriver());
|
|
||||||
else
|
|
||||||
config.setDatabaseDriver(currentnode.getText());
|
|
||||||
}
|
|
||||||
if (element.equals("connection.url")) {
|
|
||||||
if ((config.getDatabaseURL() != null)&&(config.getDatabaseURL().length()>0))
|
|
||||||
currentnode.setText(config.getDatabaseURL());
|
|
||||||
else
|
|
||||||
config.setDatabaseURL(currentnode.getText());
|
|
||||||
}
|
|
||||||
if (element.equals("connection.username")) {
|
|
||||||
if ((config.getDatabaseUserName() != null)&&(config.getDatabaseUserName().length()>0))
|
|
||||||
currentnode.setText(config.getDatabaseUserName());
|
|
||||||
else
|
|
||||||
config.setDatabaseUserName(currentnode.getText());
|
|
||||||
}
|
|
||||||
if (element.equals("connection.password")) {
|
|
||||||
if ((config.getDatabasePassword() != null)&&(config.getDatabasePassword().length()>0))
|
|
||||||
currentnode.setText(config.getDatabasePassword());
|
|
||||||
else
|
|
||||||
config.setDatabasePassword(currentnode.getText());
|
|
||||||
}
|
|
||||||
if (element.equals("dialect")) {
|
|
||||||
if ((config.getDatabaseDialect() != null)&&(config.getDatabaseDialect().length()>0))
|
|
||||||
currentnode.setText(config.getDatabaseDialect());
|
|
||||||
else
|
|
||||||
config.setDatabaseDialect(currentnode.getText());
|
|
||||||
}
|
|
||||||
if (element.equals("c3p0.idleConnectionTestPeriod")) {
|
|
||||||
if (config.getDatabaseIdleConnectionTestPeriod() != null)
|
|
||||||
currentnode.setText(config.getDatabaseIdleConnectionTestPeriod());
|
|
||||||
else
|
|
||||||
config.setDatabaseIdleConnectionTestPeriod(currentnode.getText());
|
|
||||||
}
|
|
||||||
if (element.equals("c3p0.automaticTestTable")) {
|
|
||||||
if (config.getDatabaseAutomaticTestTable() != null)
|
|
||||||
currentnode.setText(config.getDatabaseAutomaticTestTable());
|
|
||||||
else
|
|
||||||
config.setDatabaseAutomaticTestTable(currentnode.getText());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Configuration cfg = new Configuration();
|
Configuration cfg = new Configuration();
|
||||||
cfg = cfg.configure(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(document.asXML().getBytes())));
|
cfg = cfg.configure(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stream));
|
||||||
cfg.setProperty("hibernate.hbm2ddl.auto", "create");
|
cfg.setProperty("hibernate.hbm2ddl.auto", "create");
|
||||||
|
|
||||||
SessionFactory DBSessionFactory = null;
|
SessionFactory DBSessionFactory = null;
|
||||||
DBSessionFactory = cfg.buildSessionFactory();
|
DBSessionFactory = cfg.buildSessionFactory();
|
||||||
|
|
||||||
// close stream
|
// close stream
|
||||||
stream.close();
|
stream.close();
|
||||||
return DBSessionFactory;
|
return DBSessionFactory;
|
||||||
|
@ -150,43 +127,43 @@ public class DatabaseFactory{
|
||||||
public static List<Object> executeHQLQuery(String query, SessionFactory DBSessionFactory, boolean useSQL) {
|
public static List<Object> executeHQLQuery(String query, SessionFactory DBSessionFactory, boolean useSQL) {
|
||||||
List<Object> obj = null;
|
List<Object> obj = null;
|
||||||
Session ss = null;
|
Session ss = null;
|
||||||
ss = DBSessionFactory.getCurrentSession();
|
ss = DBSessionFactory.getCurrentSession();
|
||||||
|
|
||||||
ss.beginTransaction();
|
ss.beginTransaction();
|
||||||
|
|
||||||
Query qr = null;
|
Query qr = null;
|
||||||
|
|
||||||
if (useSQL)
|
if (useSQL)
|
||||||
qr = ss.createSQLQuery(query);
|
qr = ss.createSQLQuery(query);
|
||||||
else
|
else
|
||||||
qr = ss.createQuery(query);
|
qr = ss.createQuery(query);
|
||||||
List<Object> result = null;
|
List<Object> result = null;
|
||||||
try{
|
try{
|
||||||
result = qr.list();
|
result = qr.list();
|
||||||
ss.getTransaction().commit();
|
ss.getTransaction().commit();
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
System.out.println("Could not execute query "+e.getMessage());
|
System.out.println("Could not execute query "+e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
rollback(ss);
|
rollback(ss);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == null)
|
if (result == null)
|
||||||
System.out.println("Hibernate doesn't return a valid object when org.gcube.contentmanagement.lexicalmatcher retrieve UserState Object");
|
System.out.println("Hibernate doesn't return a valid object when org.gcube.contentmanagement.lexicalmatcher retrieve UserState Object");
|
||||||
|
|
||||||
// if (result != null && result.size() == 0)
|
// if (result != null && result.size() == 0)
|
||||||
// System.out.println(String.format("found nothing in database for query: "+query));
|
// System.out.println(String.format("found nothing in database for query: "+query));
|
||||||
|
|
||||||
if (result != null && result.size() != 0) {
|
if (result != null && result.size() != 0) {
|
||||||
obj = result;
|
obj = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// rollback(ss);
|
// rollback(ss);
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Connection getDBConnection(String drivername,String username, String password, String databaseurl) throws Exception{
|
public static Connection getDBConnection(String drivername,String username, String password, String databaseurl) throws Exception{
|
||||||
// Load the database driver
|
// Load the database driver
|
||||||
Class.forName(drivername) ;
|
Class.forName(drivername) ;
|
||||||
|
@ -196,63 +173,63 @@ public class DatabaseFactory{
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void executeUpdateNoTransaction(final String query, String drivername,String username, String password, String databaseurl, boolean useSQL) throws Exception{
|
public static void executeUpdateNoTransaction(final String query, String drivername,String username, String password, String databaseurl, boolean useSQL) throws Exception{
|
||||||
|
|
||||||
// Load the database driver
|
// Load the database driver
|
||||||
Class.forName(drivername) ;
|
Class.forName(drivername) ;
|
||||||
|
|
||||||
// Get a connection to the database
|
// Get a connection to the database
|
||||||
Connection conn = DriverManager.getConnection(databaseurl,username,password) ;
|
Connection conn = DriverManager.getConnection(databaseurl,username,password) ;
|
||||||
|
|
||||||
// Get a statement from the connection
|
// Get a statement from the connection
|
||||||
Statement stmt = conn.createStatement() ;
|
Statement stmt = conn.createStatement() ;
|
||||||
|
|
||||||
|
// Execute the query
|
||||||
|
stmt.executeUpdate( query) ;
|
||||||
|
|
||||||
|
stmt.close() ;
|
||||||
|
conn.close() ;
|
||||||
|
|
||||||
// Execute the query
|
|
||||||
stmt.executeUpdate( query) ;
|
|
||||||
|
|
||||||
stmt.close() ;
|
|
||||||
conn.close() ;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void executeUpdateNoTransaction(final String query, SessionFactory DBSessionFactory, boolean useSQL) throws Exception{
|
public static void executeUpdateNoTransaction(final String query, SessionFactory DBSessionFactory, boolean useSQL) throws Exception{
|
||||||
// System.out.println("executing query: " + query);
|
// System.out.println("executing query: " + query);
|
||||||
Session ss = null;
|
Session ss = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
ss = DBSessionFactory.getCurrentSession();
|
ss = DBSessionFactory.getCurrentSession();
|
||||||
|
|
||||||
// System.out.println("executing query");
|
// System.out.println("executing query");
|
||||||
ss.doWork(new Work() {
|
ss.doWork(new Work() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(Connection conn) throws SQLException {
|
public void execute(Connection conn) throws SQLException {
|
||||||
Statement stmt = conn.createStatement() ;
|
Statement stmt = conn.createStatement() ;
|
||||||
// Execute the query
|
// Execute the query
|
||||||
ResultSet rs = stmt.executeQuery(query) ;
|
ResultSet rs = stmt.executeQuery(query) ;
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void executeHQLUpdate(String query, SessionFactory DBSessionFactory, boolean useSQL) throws Exception{
|
public static void executeHQLUpdate(String query, SessionFactory DBSessionFactory, boolean useSQL) throws Exception{
|
||||||
// System.out.println("executing query: " + query);
|
// System.out.println("executing query: " + query);
|
||||||
Session ss = null;
|
Session ss = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
ss = DBSessionFactory.getCurrentSession();
|
ss = DBSessionFactory.getCurrentSession();
|
||||||
// System.out.println("executing query");
|
// System.out.println("executing query");
|
||||||
ss.beginTransaction();
|
ss.beginTransaction();
|
||||||
Query qr = null;
|
Query qr = null;
|
||||||
|
|
||||||
|
@ -266,25 +243,25 @@ public class DatabaseFactory{
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
rollback(ss);
|
rollback(ss);
|
||||||
// e.printStackTrace();
|
// e.printStackTrace();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void executeNativeUpdate(String query, SessionFactory DBSessionFactory) {
|
public static void executeNativeUpdate(String query, SessionFactory DBSessionFactory) {
|
||||||
// System.out.println("executing query: " + query);
|
// System.out.println("executing query: " + query);
|
||||||
Session ss = null;
|
Session ss = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
ss = DBSessionFactory.getCurrentSession();
|
ss = DBSessionFactory.getCurrentSession();
|
||||||
|
|
||||||
System.out.println("executing query");
|
System.out.println("executing query");
|
||||||
ss.beginTransaction();
|
ss.beginTransaction();
|
||||||
Query qr = null;
|
Query qr = null;
|
||||||
|
|
||||||
|
|
||||||
qr = DBSessionFactory.getCurrentSession().getNamedQuery("mySp").setParameter("param", query);
|
qr = DBSessionFactory.getCurrentSession().getNamedQuery("mySp").setParameter("param", query);
|
||||||
|
|
||||||
qr.executeUpdate();
|
qr.executeUpdate();
|
||||||
|
@ -295,14 +272,14 @@ public class DatabaseFactory{
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void executeSQLUpdate(String query, SessionFactory DBSessionFactory) throws Exception {
|
public static void executeSQLUpdate(String query, SessionFactory DBSessionFactory) throws Exception {
|
||||||
executeHQLUpdate(query, DBSessionFactory, true);
|
executeHQLUpdate(query, DBSessionFactory, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Object> executeSQLQuery(String query, SessionFactory DBSessionFactory) {
|
public static List<Object> executeSQLQuery(String query, SessionFactory DBSessionFactory) {
|
||||||
// System.out.println("QUERY: "+query);
|
// System.out.println("QUERY: "+query);
|
||||||
return executeHQLQuery(query, DBSessionFactory, true);
|
return executeHQLQuery(query, DBSessionFactory, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue