diff --git a/src/fr/inrialpes/exmo/align/service/DBService.java b/src/fr/inrialpes/exmo/align/service/DBService.java index fd55ae476f20d836562e69055c659a35aaac9ef2..8a938432c2043089b52fc778333fd4dbe087dea8 100644 --- a/src/fr/inrialpes/exmo/align/service/DBService.java +++ b/src/fr/inrialpes/exmo/align/service/DBService.java @@ -21,16 +21,17 @@ package fr.inrialpes.exmo.align.service; import java.net.URI; -import java.util.Vector; -import org.semanticweb.owl.align.Alignment; - +import java.sql.Connection; +import java.sql.SQLException; public interface DBService { - public long store(Alignment alignment); - public int connect(String password); // password in database - public int connect(String IPAdress, String id, String password); // with userID, password in database - public Alignment find(long id); // find alignment with alignmentID - public Vector find(URI uri); // find alignment list with an ontology uri - public Vector find(URI uri1, URI uri2); // find alignment list with two ontology uri - public int close(); + public void init(); + + public void connect(String password) throws SQLException; // password in database + public void connect(String user, String password) throws SQLException; // password in database + public void connect(String port, String user, String password) throws SQLException; // password in database + public void connect(String IPAdress, String port, String user, String password) throws SQLException; // with userID, password in database + public Connection getConnection(); + + public void close(); } diff --git a/src/fr/inrialpes/exmo/align/service/DBServiceImpl.java b/src/fr/inrialpes/exmo/align/service/DBServiceImpl.java index 40226e424a3bf0b23f35204748924588f901e647..2724590065392034cd7a9520dfdf2f11a842595b 100644 --- a/src/fr/inrialpes/exmo/align/service/DBServiceImpl.java +++ b/src/fr/inrialpes/exmo/align/service/DBServiceImpl.java @@ -20,223 +20,86 @@ package fr.inrialpes.exmo.align.service; +import java.lang.ClassNotFoundException; +import java.lang.IllegalAccessException; import java.util.Vector; -import java.util.Enumeration; import java.net.URI; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; +import java.sql.SQLException; -import org.semanticweb.owl.align.Alignment; -import org.semanticweb.owl.align.Cell; -import org.semanticweb.owl.io.owl_rdf.OWLRDFParser; -import org.semanticweb.owl.util.OWLManager; -import org.semanticweb.owl.model.OWLEntity; -import org.semanticweb.owl.model.OWLOntology; -import org.semanticweb.owl.model.OWLException; - -import fr.inrialpes.exmo.align.impl.BasicRelation; public class DBServiceImpl implements DBService{ - int id = 0; - Connection conn = null; - String url = "jdbc:mysql://localhost:3306/DBService"; - Statement st = null; - ResultSet rs = null; + int id = 0; + Connection conn = null; + static String IPAddress = "localhost"; + static String port = "3306"; + static String user = "root"; + static String database = ""; + String driverPrefix = "jdbc:mysql"; + Statement st = null; + ResultSet rs = null; - OWLOntology O1 = null; - OWLOntology O2 = null; - String type = null; - String level = null; + public DBServiceImpl() throws ClassNotFoundException, InstantiationException, IllegalAccessException { + Class.forName("com.mysql.jdbc.Driver").newInstance(); + } - String s_O1 = ""; - String s_O2 = ""; - String s_File1 = ""; - String s_File2 = ""; - String s_uri1 = ""; - String s_uri2 = ""; - String s_method = ""; - String s_cell = ""; - - final int CONNECTION_ERROR = 1; - final int SUCCESS = 2; - final int INIT_ERROR = 3; + public DBServiceImpl( String driver, String prefix ) throws ClassNotFoundException, InstantiationException, IllegalAccessException { + Class.forName(driver).newInstance(); + driverPrefix = prefix; + } - static Cache cache = null; - - public DBServiceImpl() { - try{ - Class.forName("com.mysql.jdbc.Driver").newInstance(); - } catch(Exception e){ - System.out.println(e.toString()); - } - } - - public int connect(String password){ - try { - conn = DriverManager.getConnection(url, "root", password); - st = (Statement) conn.createStatement(); - } catch(Exception ex) {return CONNECTION_ERROR;} - - if(init()) return SUCCESS; - else return INIT_ERROR; - } - - public int connect(String IPAddress, String id, String password) { - try { - conn = DriverManager.getConnection("jdbc:mysql://"+IPAddress+":3306/DBService", id, password); - st = (Statement) conn.createStatement(); - } catch(Exception ex) {return CONNECTION_ERROR;} - - if(init()) { - System.out.println("init success"); - return SUCCESS; - } - else return INIT_ERROR; - } - - private boolean init() { - try { - cache = new CacheImpl(conn); - if( cache.loading() == SUCCESS ) { - System.out.println("chche loading success"); - return true; - } - else return false; - } catch (Exception e) {return false;} - } + public void init() {}; - public long store(Alignment alignment){ - long id = 0; - String query = null; - id = nextID(); - - try { - O1 = (OWLOntology) alignment.getOntology1(); - O2 = (OWLOntology) alignment.getOntology2(); - s_O1 = O1.getLogicalURI().toString(); - s_O2 = O2.getLogicalURI().toString(); - - if (alignment.getFile1() != null) s_File1 = alignment.getFile1().toString(); - if (alignment.getFile2() != null) s_File2 = alignment.getFile2().toString(); - - s_uri1 = O1.getPhysicalURI().toString(); - s_uri2 = O2.getPhysicalURI().toString(); - - type = alignment.getType(); - level = alignment.getLevel(); - - query = "insert into alignment " + - "(id, owlontology1, owlontology2, type, level, file1, file2, uri1, uri2) " + - "values (" + id + ",'" + s_O1 + "','" + s_O2 + "','" + type + "','" + level + "','" + s_File1 + "','" + s_File2 + "','" + s_uri1 + "','" + s_uri2 + "')"; - st.executeUpdate(query); - for( Enumeration e = alignment.getExtensions().getNames() ; e.hasMoreElements() ; ){ - String tag = (String)e.nextElement(); - s_method = alignment.getExtension(tag); - query = "insert into extension " + - "(id, tag, method) " + - "values (" + id + ",'" + tag + "','" + s_method + "')"; - st.executeUpdate(query); - } + public void connect( String password ) throws SQLException { + connect( IPAddress, port, user, password ); + } + + public void connect( String user, String password ) throws SQLException { + connect( IPAddress, port, user, password ); + } + + public void connect( String port, String user, String password ) throws SQLException { + connect( IPAddress, port, user, password ); + } + + public void connect(String IPAddress, String port, String user, String password ) throws SQLException { + conn = DriverManager.getConnection(driverPrefix+"://"+IPAddress+":"+port+"/DBService", user, password); + st = (Statement) conn.createStatement(); + } - for( Enumeration e = alignment.getElements() ; e.hasMoreElements(); ){ - Cell c = (Cell)e.nextElement(); - String temp[] = new String[10]; - try { - if ( ((OWLEntity)c.getObject1()).getURI() != null && ((OWLEntity)c.getObject2()).getURI() != null ){ - if ( c.getId() != null ){ - temp[0] = c.getId(); - } - else temp[0] = ""; - temp[1] = ((OWLEntity)c.getObject1()).getURI().toString(); - temp[2] = ((OWLEntity)c.getObject2()).getURI().toString(); - temp[3] = c.getStrength() + ""; - if ( !c.getSemantics().equals("first-order") ) - temp[4] = c.getSemantics(); - else temp[4] = ""; - temp[5] = ((BasicRelation)c.getRelation()).getRelation(); - query = "insert into cell " + - "(id, cell_id, uri1, uri2, measure, semantics, relation) " + - "values (" + id + ",'" + temp[0] + "','" + temp[1] + "','" + temp[2] + "','" + temp[3] + "','" + temp[4] + "','" + temp[5] + "')"; - st.executeUpdate(query); - } - - } catch ( OWLException ex) { System.out.println( "getURI problem" + ex.toString() ); } - } - } - catch (Exception e) {System.out.println(e.toString());} - return id; - } + public Connection getConnection() { + return conn; + } - public Alignment find(long id) { - Alignment result = null; - try { - result = (Alignment) cache.get(id); - } catch (Exception e) {System.out.println("Alignment Finding Exception");} - return result; + // JE: I think that there is no interest now + public synchronized long nextID(){ + long id = 0; + try { + st.executeUpdate("insert into id_seq (aa) values ('a')"); + rs = (ResultSet) st.executeQuery("select max(id) from id_seq"); + //System.out.println(rs.toString()); + while(rs.next()) { + id = rs.getInt(1); + } + //id = rs.getBigDecimal(1).longValue(); + } catch(Exception ex){ + System.out.println(ex.toString()); + return -1; } + return id; + } - public Vector find(URI uri) { // find alignment list with an ontology uri - Vector result = null; - try { - result = (Vector) cache.get(uri); - } catch (Exception e) {System.out.println("Alignment Finding Exception");} - return result; - } - - public Vector find(URI uri1, URI uri2) { // find alignment list with two ontology uri - Vector result = null; - try { - result = (Vector) cache.get(uri1, uri2); - } catch (Exception e) {System.out.println("Alignment Finding Exception");} - return result; - } - - public synchronized long nextID(){ - long id = 0; - try { - st.executeUpdate("insert into id_seq (aa) values ('a')"); - rs = (ResultSet) st.executeQuery("select max(id) from id_seq"); - //System.out.println(rs.toString()); - while(rs.next()) { - id = rs.getInt(1); - } - //id = rs.getBigDecimal(1).longValue(); - } catch(Exception ex){ - System.out.println(ex.toString()); - return -1; - } - return id; - } - - public int open(String id, String password){ - try{ - String userid = null;; - String pwd; - userid = id; - pwd = password; - - conn = DriverManager.getConnection(url, userid, pwd); - } catch(Exception ex){System.out.println(ex.toString());} - return 1; - } - - public int close(){ - - try { - conn.close(); - st.close(); - rs.close(); - return 1; - } catch (Exception ex) { - return -1; - } + public void close() { + try { + conn.close(); + st.close(); + rs.close(); + } catch (Exception ex) { + // Do something } + } - public static OWLOntology loadOntology(URI uri) throws Exception { - OWLRDFParser parser = new OWLRDFParser(); - parser.setConnection(OWLManager.getOWLConnection()); - return parser.parseOntology(uri); - } }