diff --git a/artemis_sqlmap/Dbxref.xml b/artemis_sqlmap/Dbxref.xml index 10a4181c1709c1167046bf2cd7edbf4a89645716..43d0f302bc6fa721ac2ef75f21359c8ec9616bea 100644 --- a/artemis_sqlmap/Dbxref.xml +++ b/artemis_sqlmap/Dbxref.xml @@ -8,7 +8,7 @@ <sqlMap namespace="Dbxref"> <select id="getDbxref" parameterClass="uk.ac.sanger.artemis.chado.ChadoFeature" - resultClass="uk.ac.sanger.artemis.chado.Dbxref"> + resultClass="uk.ac.sanger.artemis.chado.Dbxref"> SELECT db.name, dbx.accession, @@ -25,5 +25,37 @@ </isGreaterThan> ORDER BY f.type_id, uniquename </select> + + <select id="getDbId" parameterClass="uk.ac.sanger.artemis.chado.Dbxref" + resultClass="java.lang.Integer"> + SELECT db_id FROM db WHERE name=#name# + </select> + + <select id="getDbxrefId" parameterClass="uk.ac.sanger.artemis.chado.Dbxref" + resultClass="java.lang.Integer"> + SELECT dbxref_id FROM dbxref WHERE accession=#accession# + AND db_id=db_id; + </select> + + <!-- WRITE BACK --> + <delete id="deleteFeatureDbxref" parameterClass="uk.ac.sanger.artemis.chado.Dbxref"> + DELETE FROM $schema$.feature_dbxref + WHERE dbxref_id= + (SELECT dbxref_id FROM dbxref WHERE accession=#accession# + AND db_id=(SELECT db_id FROM db WHERE name=#name#)) + AND feature_id=$feature_id$ + </delete> + + <insert id="insertFeatureDbxref" parameterClass="uk.ac.sanger.artemis.chado.Dbxref"> + INSERT INTO $schema$.feature_dbxref + (feature_id, dbxref_id, is_current) + VALUES + ($feature_id$, $dbxref_id$, $current$) + </insert> + + <insert id="insertDbxref" parameterClass="uk.ac.sanger.artemis.chado.Dbxref"> + INSERT INTO dbxref ( db_id, accession ) + VALUES ($db_id$, #accession#) + </insert> </sqlMap> diff --git a/uk/ac/sanger/artemis/chado/ChadoDAO.java b/uk/ac/sanger/artemis/chado/ChadoDAO.java index a9fdfde433b72998c1a4275c939f6ceb9a312f62..00aaeb944d9c89e19d003e0891c73faeb1f1a4d2 100644 --- a/uk/ac/sanger/artemis/chado/ChadoDAO.java +++ b/uk/ac/sanger/artemis/chado/ChadoDAO.java @@ -210,18 +210,20 @@ public interface ChadoDAO * Insert a dbxref for a feature. * @param schema schema/organism name or null * @param tsn the <code>ChadoTransaction</code> + * @return number of rows changed * @throws SQLException */ - public void insertFeatureDbxref(final String schema, final ChadoTransaction tsn) + public int insertFeatureDbxref(final String schema, final ChadoTransaction tsn) throws SQLException; /** * Delete a dbxref for a feature. * @param schema schema/organism name or null * @param tsn the <code>ChadoTransaction</code> + * @return number of rows changed * @throws SQLException */ - public void deleteFeatureDbxref(final String schema, final ChadoTransaction tsn) + public int deleteFeatureDbxref(final String schema, final ChadoTransaction tsn) throws SQLException; /** diff --git a/uk/ac/sanger/artemis/chado/Dbxref.java b/uk/ac/sanger/artemis/chado/Dbxref.java index a54aaf818580480b4720733c42b04f296bf9e911..6924e9f900c131b7f10261bffeb0fd4d0146955a 100644 --- a/uk/ac/sanger/artemis/chado/Dbxref.java +++ b/uk/ac/sanger/artemis/chado/Dbxref.java @@ -26,7 +26,7 @@ package uk.ac.sanger.artemis.chado; /** * - * Java Object used by ORM to represent the chado dbxref table. + * Java Object used by ORM to represent the chado feature_dbxref table. * */ public class Dbxref @@ -34,6 +34,12 @@ public class Dbxref private String name; private String accession; private int feature_id; + /** database id */ + private int db_id; + /** database cross reference id */ + private int dbxref_id; + private boolean current = true; + private String schema; public Dbxref() { @@ -69,5 +75,46 @@ public class Dbxref this.feature_id = feature_id; } + public boolean isCurrent() + { + return current; + } + + public void setCurrent(boolean current) + { + this.current = current; + } + + public String getSchema() + { + return schema; + } + + public void setSchema(String schema) + { + this.schema = schema; + } + + public int getDb_id() + { + return db_id; + } + + public void setDb_id(int db_id) + { + this.db_id = db_id; + } + + public int getDbxref_id() + { + return dbxref_id; + } + + public void setDbxref_id(int dbxref_id) + { + this.dbxref_id = dbxref_id; + } + + } diff --git a/uk/ac/sanger/artemis/chado/IBatisDAO.java b/uk/ac/sanger/artemis/chado/IBatisDAO.java index 31db1e3bf5c17f4bd4ad3253fedf827f2afbde0f..9df0b7a3a8b3978a2025c08be08601ab60728710 100644 --- a/uk/ac/sanger/artemis/chado/IBatisDAO.java +++ b/uk/ac/sanger/artemis/chado/IBatisDAO.java @@ -287,7 +287,6 @@ public class IBatisDAO implements ChadoDAO } /** - * * Insert attributes defined by the <code>ChadoTransaction</code>. * @param schema schema/organism name or null * @param tsn the <code>ChadoTransaction</code> @@ -311,7 +310,6 @@ public class IBatisDAO implements ChadoDAO } /** - * * Delete attributes defined by the <code>ChadoTransaction</code>. * @param schema schema/organism name or null * @param tsn the <code>ChadoTransaction</code> @@ -335,7 +333,6 @@ public class IBatisDAO implements ChadoDAO } /** - * * Insert a feature into the database defined by the <code>ChadoTransaction</code>. * @param schema schema/organism name or null * @param tsn the <code>ChadoTransaction</code> @@ -398,28 +395,63 @@ public class IBatisDAO implements ChadoDAO * Insert a dbxref for a feature. * @param schema schema/organism name or null * @param tsn the <code>ChadoTransaction</code> + * @return number of rows changed * @throws SQLException */ - public void insertFeatureDbxref(final String schema, final ChadoTransaction tsn) + public int insertFeatureDbxref(final String schema, final ChadoTransaction tsn) throws SQLException { Dbxref dbxref = tsn.getFeatureDbxref(); + SqlMapClient sqlMap = DbSqlConfig.getSqlMapInstance(); + Integer db_id = (Integer)sqlMap.queryForObject("getDbId", dbxref); + + dbxref.setDb_id(db_id.intValue()); + + Integer dbxref_id = (Integer)sqlMap.queryForObject("getDbxrefId", dbxref); + if(dbxref_id == null) + { + // create a new accession entry in dbxref + sqlMap.insert("insertDbxref", dbxref); + // now get the new dbxref_id + dbxref_id = (Integer)sqlMap.queryForObject("getDbxrefId", dbxref); + } + + dbxref.setDbxref_id(dbxref_id.intValue()); + + // get the feature id's + tsn.setSchema(schema); + List feature_ids = sqlMap.queryForList("getFeatureID", tsn); + dbxref.setFeature_id( ((Integer)feature_ids.get(0)).intValue() ); + + dbxref.setSchema(schema); + sqlMap.insert("insertFeatureDbxref", dbxref); + + return 1; } /** * Delete a dbxref for a feature. * @param schema schema/organism name or null * @param tsn the <code>ChadoTransaction</code> + * @return number of rows changed * @throws SQLException */ - public void deleteFeatureDbxref(final String schema, final ChadoTransaction tsn) + public int deleteFeatureDbxref(final String schema, final ChadoTransaction tsn) throws SQLException { Dbxref dbxref = tsn.getFeatureDbxref(); + + SqlMapClient sqlMap = DbSqlConfig.getSqlMapInstance(); + tsn.setSchema(schema); + dbxref.setSchema(schema); + // get the feature id's + List feature_ids = sqlMap.queryForList("getFeatureID", tsn); + + dbxref.setFeature_id( ((Integer)feature_ids.get(0)).intValue() ); + return sqlMap.delete("deleteFeatureDbxref", dbxref); } /** - * * Write the time a feature was last modified * @param schema schema/organism name or null * @param uniquename the unique name of the feature @@ -437,7 +469,6 @@ public class IBatisDAO implements ChadoDAO } /** - * * Write the time a feature was last accessed * @param schema schema/organism name or null * @param uniquename the unique name of the feature diff --git a/uk/ac/sanger/artemis/chado/JdbcDAO.java b/uk/ac/sanger/artemis/chado/JdbcDAO.java index 4c71b56ded073a1cc4b1e16b5577375ce1663ebe..151d89880cb5fb76352b83fdae5dc88664c8e7b3 100644 --- a/uk/ac/sanger/artemis/chado/JdbcDAO.java +++ b/uk/ac/sanger/artemis/chado/JdbcDAO.java @@ -730,24 +730,66 @@ public class JdbcDAO * Insert a dbxref for a feature. * @param schema schema/organism name or null * @param tsn the <code>ChadoTransaction</code> + * @return number of rows changed * @throws SQLException */ - public void insertFeatureDbxref(final String schema, final ChadoTransaction tsn) + public int insertFeatureDbxref(final String schema, final ChadoTransaction tsn) throws SQLException { - Dbxref dbxref = tsn.getFeatureDbxref(); + final Dbxref dbxref = tsn.getFeatureDbxref(); + final String uniquename = tsn.getUniqueName(); + + // find database id + String sql = "SELECT db_id FROM db WHERE name='"+dbxref.getName()+"'"; + + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(sql); + boolean exists = rs.next(); + + final int db_id = rs.getInt("db_id"); + // find if accession exists already + String sqlDbxrefId = "SELECT dbxref_id FROM dbxref WHERE accession='"+ + dbxref.getAccession()+"' AND db_id="+db_id; + appendToLogFile(sqlDbxrefId, sqlLog); + rs = st.executeQuery(sqlDbxrefId); + exists = rs.next(); + + if(!exists) + { + // create a new accession entry in dbxref + sql = "INSERT INTO dbxref ( db_id, accession ) "+ + "VALUES ("+db_id+", "+dbxref.getAccession()+" )"; + appendToLogFile(sql, sqlLog); + int rowCount = st.executeUpdate(new String(sql)); + + // now get the new dbxref_id + appendToLogFile(sqlDbxrefId, sqlLog); + rs = st.executeQuery(sqlDbxrefId); + rs.next(); + } + + final int dbxref_id = rs.getInt("dbxref_id"); + sql = "INSERT INTO "+schema+".feature_dbxref "+ + "(feature_id, dbxref_id, is_current)"+ + " VALUES "+ + "( (SELECT feature_id FROM "+schema+ + ".feature WHERE uniquename='"+uniquename+"'), "+ + dbxref_id+", "+ Boolean.toString(dbxref.isCurrent())+")"; + appendToLogFile(sql, sqlLog); + return st.executeUpdate(new String(sql)); } /** * Delete a dbxref for a feature. * @param schema schema/organism name or null * @param tsn the <code>ChadoTransaction</code> + * @return number of rows changed * @throws SQLException */ - public void deleteFeatureDbxref(final String schema, final ChadoTransaction tsn) + public int deleteFeatureDbxref(final String schema, final ChadoTransaction tsn) throws SQLException { - Dbxref dbxref = tsn.getFeatureDbxref(); + final Dbxref dbxref = tsn.getFeatureDbxref(); final String uniquename = tsn.getUniqueName(); final String sql = @@ -757,8 +799,9 @@ public class JdbcDAO "AND db_id=(SELECT db_id FROM db WHERE name='"+dbxref.getName()+"'))"+ "AND feature_id=(SELECT feature_id FROM "+schema+ ".feature WHERE uniquename='"+uniquename+"')"; - - System.out.println(sql); + + Statement st = conn.createStatement(); + return st.executeUpdate(sql); } /** diff --git a/uk/ac/sanger/artemis/util/DatabaseDocument.java b/uk/ac/sanger/artemis/util/DatabaseDocument.java index 0ee5c1304750258aef8b843b0a60bf54a7502133..f4cafbdd63985e8f8218889cb40d6424e873a9dc 100644 --- a/uk/ac/sanger/artemis/util/DatabaseDocument.java +++ b/uk/ac/sanger/artemis/util/DatabaseDocument.java @@ -712,6 +712,8 @@ public class DatabaseDocument extends Document dao.deleteFeature(schema, tsn); else if(tsn.getType() == ChadoTransaction.DELETE_DBXREF) dao.deleteFeatureDbxref(schema, tsn); + else if(tsn.getType() == ChadoTransaction.INSERT_DBXREF) + dao.insertFeatureDbxref(schema, tsn); } } catch (java.sql.SQLException sqlExp)