Coverage Report - uk.ac.ebi.intenz.mapper.EnzymeLinkMapper
 
Classes in this File Line Coverage Branch Coverage Complexity
EnzymeLinkMapper
69%
216/312
42%
125/296
6.152
 
 1  
 package uk.ac.ebi.intenz.mapper;
 2  
 
 3  
 import java.sql.Connection;
 4  
 import java.sql.PreparedStatement;
 5  
 import java.sql.ResultSet;
 6  
 import java.sql.SQLException;
 7  
 import java.util.ArrayList;
 8  
 import java.util.List;
 9  
 
 10  
 import uk.ac.ebi.intenz.domain.constants.EnzymeSourceConstant;
 11  
 import uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant;
 12  
 import uk.ac.ebi.intenz.domain.constants.Status;
 13  
 import uk.ac.ebi.intenz.domain.constants.XrefDatabaseConstant;
 14  
 import uk.ac.ebi.intenz.domain.enzyme.EnzymeLink;
 15  
 import uk.ac.ebi.intenz.domain.exceptions.DomainException;
 16  
 
 17  
 /**
 18  
  * Maps link information to the corresponding database tables.
 19  
  * <p/>
 20  
  *
 21  
  * @author Michael Darsow
 22  
  * @version $Revision: 1.3 $ $Date: 2009/05/26 14:23:11 $
 23  
  */
 24  1
 public class EnzymeLinkMapper {
 25  
 
 26  
   private static final String COLUMNS =
 27  
     "enzyme_id, url, display_name, status, source, web_view, data_comment";
 28  
   private static final String TABLES = "links";
 29  
 
 30  
   private static final String XREF_COLUMNS =
 31  
     "enzyme_id, database_code, database_ac, name, status, source, web_view, data_comment";
 32  
   private static final String XREF_TABLES = "xrefs";
 33  
 
 34  
 //  private static final Logger LOGGER = Logger.getLogger(EnzymeLinkMapper.class);
 35  
 
 36  3
   public EnzymeLinkMapper() {
 37  3
   }
 38  
 
 39  
   private String findStatement() {
 40  6
     return "SELECT " + COLUMNS +
 41  
            " FROM " + TABLES +
 42  
            " WHERE links.enzyme_id = ?" +
 43  
            " ORDER BY links.display_name";
 44  
   }
 45  
 
 46  
   private String exportSibLinksStatement() {
 47  1
     return "SELECT " + COLUMNS +
 48  
            " FROM " + TABLES +
 49  
            " WHERE links.enzyme_id = ? AND (links.web_view = ? OR links.web_view = ? OR links.web_view = ? OR links.web_view = ?)" +
 50  
            " ORDER BY links.display_name";
 51  
   }
 52  
 
 53  
   private String findXrefsStatement() {
 54  8
     return "SELECT " + XREF_COLUMNS +
 55  
            " FROM " + XREF_TABLES +
 56  
            " WHERE xrefs.enzyme_id = ?" +
 57  
            " ORDER BY xrefs.name";
 58  
   }
 59  
 
 60  
   private String findSibXrefsStatement() {
 61  2
     return "SELECT " + XREF_COLUMNS +
 62  
            " FROM " + XREF_TABLES +
 63  
            " WHERE xrefs.enzyme_id = ? AND (xrefs.web_view = ? OR xrefs.web_view = ? OR xrefs.web_view = ? OR xrefs.web_view = ?)" +
 64  
            " ORDER BY name";
 65  
   }
 66  
 
 67  
   private String insertStatement() {
 68  2
     return "INSERT INTO links (enzyme_id, url, display_name, status, source, web_view, data_comment) VALUES (?, ?, ?, ?, ?, ?, ?)";
 69  
   }
 70  
 
 71  
   private String insertXrefStatement() {
 72  1
     return "INSERT INTO xrefs (enzyme_id, database_code, database_ac, name, status, source, web_view, data_comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
 73  
   }
 74  
 
 75  
   private String updateLinkUrlStatement() {
 76  0
     return "UPDATE links SET url = ?, status = ? WHERE enzyme_id = ? AND display_name = ?";
 77  
   }
 78  
 
 79  
   private String deleteStatement() {
 80  0
     return "DELETE links WHERE enzyme_id = ? AND display_name = ? AND url = ?";
 81  
   }
 82  
 
 83  
   private String deleteAllStatement() {
 84  3
     return "DELETE links WHERE enzyme_id = ?";
 85  
   }
 86  
 
 87  
   private String deleteXrefStatement() {
 88  0
     return "DELETE xrefs WHERE enzyme_id = ? AND database_code = ? AND database_ac = ? AND name = ?";
 89  
   }
 90  
 
 91  
   private String deleteAllXrefStatement() {
 92  1
     return "DELETE xrefs WHERE enzyme_id = ?";
 93  
   }
 94  
 
 95  
    private String deleteByCodeXrefStatement() {
 96  0
     return "DELETE xrefs WHERE enzyme_id = ? AND database_code = ?";
 97  
   }
 98  
 
 99  
   private String deleteByNameStatement() {
 100  0
     return "DELETE links WHERE enzyme_id = ? AND display_name = ?";
 101  
   }
 102  
 
 103  
   /**
 104  
    * Tries to find link information about an enzyme in tables LINKS and XREFS.
 105  
    *
 106  
    * @param enzymeId Enzyme ID of entry.
 107  
    * @param con      The logical connection.
 108  
    * @return a <code>Vector</code> containing <code>EnzymeLink</code>instances or <code>null</code> if nothing has been found.
 109  
    * @throws SQLException
 110  
    */
 111  
   public List<EnzymeLink> find(Long enzymeId, Connection con) throws SQLException, DomainException {
 112  6
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 113  6
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 114  
 
 115  6
     PreparedStatement findStatement = null;
 116  6
     ResultSet rs = null;
 117  6
     List<EnzymeLink> result = new ArrayList<EnzymeLink>();
 118  
 
 119  
     try {
 120  6
       findStatement = con.prepareStatement(findStatement());
 121  6
       findStatement.setLong(1, enzymeId.longValue());
 122  6
       rs = findStatement.executeQuery();
 123  14
       while (rs.next()) {
 124  8
         EnzymeLink link = doLoad(rs);
 125  8
         if (link == null) continue;
 126  8
         result.add(link);
 127  8
       }
 128  6
       List<EnzymeLink> xrefs = findXrefs(enzymeId, con);
 129  6
       if (xrefs != null) result.addAll(xrefs);
 130  
     } finally {
 131  6
             if (rs != null) rs.close();
 132  6
       if (findStatement != null) findStatement.close();
 133  
     }
 134  
 
 135  6
     if (result.size() == 0) return null;
 136  5
     return result;
 137  
   }
 138  
 
 139  
   /**
 140  
    * Exports all links which are displayed in the ENZYME view.
 141  
    *
 142  
    * Affected table rows will be locked.
 143  
    *
 144  
    * @param enzymeId The enzyme ID used to retreive the related links.
 145  
    * @param con The database connection.
 146  
    * @return an {@link java.util.ArrayList} of links or <code>null</code> if no link could be found.
 147  
    * @throws SQLException if a database error occured.
 148  
    * @throws NullPointerException if either of the parameters is <code>null</code>.
 149  
    */
 150  
   public List<EnzymeLink> exportSibLinks(Long enzymeId, Connection con) throws SQLException, DomainException {
 151  1
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 152  1
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 153  
 
 154  1
     PreparedStatement findStatement = null;
 155  1
     ResultSet rs = null;
 156  1
     List<EnzymeLink> result = new ArrayList<EnzymeLink>();
 157  
 
 158  
     try {
 159  1
       findStatement = con.prepareStatement(exportSibLinksStatement());
 160  1
       findStatement.setLong(1, enzymeId.longValue());
 161  1
       findStatement.setString(2, EnzymeViewConstant.INTENZ.toString());
 162  1
       findStatement.setString(3, EnzymeViewConstant.IUBMB_SIB.toString());
 163  1
       findStatement.setString(4, EnzymeViewConstant.SIB.toString());
 164  1
       findStatement.setString(5, EnzymeViewConstant.SIB_INTENZ.toString());
 165  1
       rs = findStatement.executeQuery();
 166  1
       while (rs.next()) {
 167  0
         EnzymeLink link = doLoad(rs);
 168  0
         if (link == null) continue;
 169  0
         result.add(link);
 170  0
       }
 171  1
       List<EnzymeLink> xrefs = findSibXrefs(enzymeId, con);
 172  1
       if (xrefs != null) result.addAll(xrefs);
 173  
     } finally {
 174  1
             if (rs != null) rs.close();
 175  1
       if (findStatement != null) findStatement.close();
 176  
     }
 177  
 
 178  1
     if (result.size() == 0) return null;
 179  1
     return result;
 180  
   }
 181  
 
 182  
   /**
 183  
    * Tries to find SwissProt xref information about an enzyme.
 184  
    *
 185  
    * @param enzymeId Enzyme ID of entry.
 186  
    * @param con      The logical connection.
 187  
    * @return a <code>Vector</code> containing <code>EnzymeLink</code>instances or <code>null</code> if nothing has been found.
 188  
    * @throws SQLException
 189  
    */
 190  
   public List<EnzymeLink> findXrefs(Long enzymeId, Connection con) throws SQLException, DomainException {
 191  8
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 192  8
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 193  
 
 194  8
     PreparedStatement findStatement = null;
 195  8
     ResultSet rs = null;
 196  8
     List<EnzymeLink> result = new ArrayList<EnzymeLink>();
 197  8
     boolean noResult = true;
 198  
 
 199  
     try {
 200  
       // Core information.
 201  8
       findStatement = con.prepareStatement(findXrefsStatement());
 202  8
       findStatement.setLong(1, enzymeId.longValue());
 203  8
       rs = findStatement.executeQuery();
 204  313
       while (rs.next()) {
 205  305
         noResult = false;
 206  305
         EnzymeLink link = doLoadXref(rs);
 207  305
         if (link == null) continue;
 208  305
         result.add(link);
 209  305
       }
 210  
     } finally {
 211  8
             if (rs != null) rs.close();
 212  8
         if (findStatement != null) findStatement.close();
 213  
     }
 214  
 
 215  8
     if (noResult) return null;
 216  5
     return result;
 217  
   }
 218  
 
 219  
   /**
 220  
    * Finds all xrefs which are displayed in the ENZYME view.
 221  
    *
 222  
    * @param enzymeId The enzyme ID used to retreive the related xrefs.
 223  
    * @param con The database connection.
 224  
    * @return an {@link java.util.ArrayList} of xrefs or <code>null</code> if no xref could be found.
 225  
    * @throws SQLException if a database error occured.
 226  
    * @throws NullPointerException if either of the parameters is <code>null</code>.
 227  
    */
 228  
   public List<EnzymeLink> findSibXrefs(Long enzymeId, Connection con) throws SQLException, DomainException {
 229  2
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 230  2
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 231  
 
 232  2
     PreparedStatement findStatement = null;
 233  2
     ResultSet rs = null;
 234  2
     List<EnzymeLink> result = new ArrayList<EnzymeLink>();
 235  2
     boolean noResult = true;
 236  
 
 237  
     try {
 238  
       // Core information.
 239  2
       findStatement = con.prepareStatement(findSibXrefsStatement());
 240  2
       findStatement.setLong(1, enzymeId.longValue());
 241  2
       findStatement.setString(2, EnzymeViewConstant.INTENZ.toString());
 242  2
       findStatement.setString(3, EnzymeViewConstant.IUBMB_SIB.toString());
 243  2
       findStatement.setString(4, EnzymeViewConstant.SIB.toString());
 244  2
       findStatement.setString(5, EnzymeViewConstant.SIB_INTENZ.toString());
 245  2
       rs = findStatement.executeQuery();
 246  62
       while (rs.next()) {
 247  60
         noResult = false;
 248  60
         EnzymeLink link = doLoadXref(rs);
 249  60
         if (link == null) continue;
 250  60
         result.add(link);
 251  60
       }
 252  
     } finally {
 253  2
             if (rs != null) rs.close();
 254  2
         if (findStatement != null) findStatement.close();
 255  
     }
 256  
 
 257  2
     if (noResult) return null;
 258  2
     return result;
 259  
   }
 260  
 
 261  
   /**
 262  
    * Stores the given list of links into the database.
 263  
    *
 264  
    * @param links    The vector of links.
 265  
    * @param enzymeId The enzyme ID.
 266  
    * @param status   ...
 267  
    * @param con      ...
 268  
    * @throws SQLException
 269  
    */
 270  
   public void insert(List<EnzymeLink> links, Long enzymeId, Status status, Connection con)
 271  
   throws SQLException {
 272  1
     if (links == null) throw new NullPointerException("Parameter 'links' must not be null.");
 273  1
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 274  1
     if (status == null) throw new NullPointerException("Parameter 'status' must not be null.");
 275  1
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 276  
 
 277  1
     PreparedStatement insertStatement = null, insertXrefStatement = null;
 278  
 
 279  
     try {
 280  1
       insertStatement = con.prepareStatement(insertStatement());
 281  1
       insertXrefStatement = con.prepareStatement(insertXrefStatement());
 282  3
       for (int iii = 0; iii < links.size(); iii++) {
 283  2
         EnzymeLink link = links.get(iii);
 284  2
         if (link.getXrefDatabaseConstant().isXref()) {
 285  1
           doInsertXref(link, enzymeId, status, insertXrefStatement);
 286  1
           insertXrefStatement.execute();
 287  
         } else {
 288  1
           doInsert(link, enzymeId, status, insertStatement);
 289  1
           insertStatement.execute();
 290  
         }
 291  
       }
 292  
     } finally {
 293  1
       if (insertStatement != null) insertStatement.close();
 294  1
       if (insertXrefStatement != null) insertXrefStatement.close();
 295  
     }
 296  1
   }
 297  
 
 298  
   public void insertLink(EnzymeLink link, Long enzymeId, Status status, Connection con) throws SQLException {
 299  1
     if (link == null) throw new NullPointerException("Parameter 'link' must not be null.");
 300  1
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 301  1
     if (status == null) throw new NullPointerException("Parameter 'status' must not be null.");
 302  1
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 303  
 
 304  1
     PreparedStatement insertStatement = null;
 305  
     try {
 306  1
       insertStatement = con.prepareStatement(insertStatement());
 307  1
       doInsert(link, enzymeId, status, insertStatement);
 308  1
       insertStatement.execute();
 309  
     } finally {
 310  1
       if (insertStatement != null) insertStatement.close();
 311  
     }
 312  1
   }
 313  
 
 314  
   public void updateLinkUrl(EnzymeLink link, Long enzymeId, Status status, Connection con) throws SQLException {
 315  0
     if (link == null) throw new NullPointerException("Parameter 'link' must not be null.");
 316  0
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 317  0
     if (status == null) throw new NullPointerException("Parameter 'status' must not be null.");
 318  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 319  
 
 320  0
     PreparedStatement updateStatement = null;
 321  
     try {
 322  0
       updateStatement = con.prepareStatement(updateLinkUrlStatement());
 323  0
       updateStatement.setString(1, link.getSpecificUrl());
 324  0
       updateStatement.setString(2, status.getCode());
 325  0
       updateStatement.setLong(3, enzymeId.longValue());
 326  0
       if (link.getXrefDatabaseConstant().equals("NIST74"))
 327  0
         updateStatement.setString(4, "GTD"); // TODO: Change to NIST 74 completely.
 328  
       else
 329  0
         updateStatement.setString(4, link.getXrefDatabaseConstant().toString());
 330  0
       updateStatement.execute();
 331  
     } finally {
 332  0
       if (updateStatement != null) updateStatement.close();
 333  
     }
 334  0
   }
 335  
 
 336  
   public void reloadLinks(List<EnzymeLink> links, Long enzymeId, Status status, Connection con) throws SQLException {
 337  0
     if (links == null) throw new NullPointerException("Parameter 'links' must not be null.");
 338  0
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 339  0
     if (status == null) throw new NullPointerException("Parameter 'status' must not be null.");
 340  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 341  
 
 342  0
     deleteOtherLinks(links, enzymeId, con);
 343  0
     insert(links, enzymeId, status, con);
 344  0
   }
 345  
 
 346  
   public void delete(Long enzymeId, EnzymeLink link, Connection con) throws SQLException {
 347  0
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 348  0
     if (link == null) throw new NullPointerException("Parameter 'link' must not be null.");
 349  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 350  
 
 351  0
     PreparedStatement deleteStatement = null;
 352  
     try {
 353  0
       deleteStatement = con.prepareStatement(deleteStatement());
 354  0
       deleteStatement.setLong(1, enzymeId.longValue());
 355  0
       if (link.getXrefDatabaseConstant().equals("NIST74"))
 356  0
         deleteStatement.setString(2, "GTD");
 357  
       else
 358  0
         deleteStatement.setString(2, link.getXrefDatabaseConstant().toString());
 359  0
       deleteStatement.setString(3, link.getSpecificUrl());
 360  0
       deleteStatement.execute();
 361  
 //      con.commit();
 362  
 //    } catch (SQLException e) {
 363  
 //      con.rollback();
 364  
 //      throw e;
 365  
     } finally {
 366  0
       if (deleteStatement != null) deleteStatement.close();
 367  
     }
 368  0
   }
 369  
 
 370  
   public void deleteAll(Long enzymeId, Connection con) throws SQLException {
 371  3
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 372  3
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 373  
 
 374  3
     PreparedStatement deleteStatement = null;
 375  
     try {
 376  3
       deleteStatement = con.prepareStatement(deleteAllStatement());
 377  3
       deleteStatement.setLong(1, enzymeId.longValue());
 378  3
       deleteStatement.execute();
 379  
 //      con.commit();
 380  
 //    } catch (SQLException e) {
 381  
 //      con.rollback();
 382  
 //      throw e;
 383  
     } finally {
 384  3
       if (deleteStatement != null) deleteStatement.close();
 385  
     }
 386  3
   }
 387  
 
 388  
   public void deleteXref(Long enzymeId, EnzymeLink xref, Connection con) throws SQLException {
 389  0
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 390  0
     if (xref == null) throw new NullPointerException("Parameter 'xref' must not be null.");
 391  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 392  
 
 393  0
     PreparedStatement deleteXrefStatement = null;
 394  0
     String dbCode = xref.getXrefDatabaseConstant().toString();
 395  
 
 396  0
     if (dbCode.equals("Swiss-Prot")) dbCode = "S";
 397  0
     if (dbCode.equals("PROSITE")) dbCode = "P";
 398  
 
 399  
     try {
 400  0
       deleteXrefStatement = con.prepareStatement(deleteXrefStatement());
 401  0
       deleteXrefStatement.setLong(1, enzymeId.longValue());
 402  0
       deleteXrefStatement.setString(2, dbCode);
 403  0
       deleteXrefStatement.setString(3, xref.getAccession());
 404  0
       deleteXrefStatement.setString(4, xref.getName());
 405  0
       deleteXrefStatement.execute();
 406  
 //      con.commit();
 407  
 //    } catch (SQLException e) {
 408  
 //      con.rollback();
 409  
 //      throw e;
 410  
     } finally {
 411  0
       if (deleteXrefStatement != null) deleteXrefStatement.close();
 412  
     }
 413  0
   }
 414  
 
 415  
   public void deleteAllXref(Long enzymeId, Connection con) throws SQLException {
 416  1
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 417  1
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 418  
 
 419  1
     PreparedStatement deleteXrefStatement = null;
 420  
     try {
 421  1
       deleteXrefStatement = con.prepareStatement(deleteAllXrefStatement());
 422  1
       deleteXrefStatement.setLong(1, enzymeId.longValue());
 423  1
       deleteXrefStatement.execute();
 424  
 //      con.commit();
 425  
 //    } catch (SQLException e) {
 426  
 //      con.rollback();
 427  
 //      throw e;
 428  
     } finally {
 429  1
       if (deleteXrefStatement != null) deleteXrefStatement.close();
 430  
     }
 431  1
   }
 432  
 
 433  
   public void deleteByName(Long enzymeId, String displayName, Connection con) throws SQLException {
 434  0
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 435  0
     if (displayName == null) throw new NullPointerException("Parameter 'displayName' must not be null.");
 436  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 437  
 
 438  0
     PreparedStatement deleteByNameStatement = null;
 439  
     try {
 440  0
       deleteByNameStatement = con.prepareStatement(deleteByNameStatement());
 441  0
       deleteByNameStatement.setLong(1, enzymeId.longValue());
 442  0
       deleteByNameStatement.setString(2, displayName);
 443  0
       deleteByNameStatement.execute();
 444  
 //      con.commit();
 445  
 //    } catch (SQLException e) {
 446  
 //      con.rollback();
 447  
 //      throw e;
 448  
     } finally {
 449  0
       if (deleteByNameStatement != null) deleteByNameStatement.close();
 450  
     }
 451  0
   }
 452  
 
 453  
    public void deleteByCodeXref(Long enzymeId, String xrefCode, Connection con) throws SQLException {
 454  0
        if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 455  0
        if (xrefCode == null) throw new NullPointerException("Parameter 'xrefCode' must not be null.");
 456  0
        if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 457  
 
 458  0
        PreparedStatement deleteByNameStatement = null;
 459  0
        if (xrefCode.equals(XrefDatabaseConstant.SWISSPROT.getDatabaseCode())) xrefCode = "S";
 460  0
        if (xrefCode.equals(XrefDatabaseConstant.PROSITE.getDatabaseCode())) xrefCode = "P";
 461  
 
 462  
        try {
 463  0
          deleteByNameStatement = con.prepareStatement(deleteByCodeXrefStatement());
 464  0
          deleteByNameStatement.setLong(1, enzymeId.longValue());
 465  0
          deleteByNameStatement.setString(2, xrefCode);
 466  0
          deleteByNameStatement.execute();
 467  
 //         con.commit();
 468  
 //       } catch (SQLException e) {
 469  
 //         con.rollback();
 470  
 //         throw e;
 471  
        } finally {
 472  0
          if (deleteByNameStatement != null) deleteByNameStatement.close();
 473  
        }
 474  0
      }
 475  
 
 476  
 
 477  
   public void deleteOtherLinks(List<EnzymeLink> links, Long enzymeId, Connection con) throws SQLException {
 478  0
     if (links == null) throw new NullPointerException("Parameter 'links' must not be null.");
 479  0
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 480  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 481  
 
 482  0
     deleteAll(enzymeId, con);
 483  0
     deleteAllXref(enzymeId, con);
 484  
 
 485  0
   }
 486  
 
 487  
 
 488  
   // ------------------- PRIVATE METHODS ------------------------
 489  
 
 490  
   /**
 491  
    * Creates the <code>EnzymeLink</code> object from the given result set.
 492  
    * <p/>
 493  
    *
 494  
    * @param rs The result set object.
 495  
    * @return an <code>EnzymeLink</code> instance.
 496  
    * @throws SQLException
 497  
    */
 498  
   private EnzymeLink doLoad(ResultSet rs) throws SQLException, DomainException {
 499  8
     assert rs != null : "Parameter 'rs' must not be null.";
 500  
 
 501  8
     EnzymeLink result = null;
 502  
 
 503  8
     String displayName = "";
 504  8
     String url = "";
 505  8
     String source = "";
 506  8
     String webView = "";
 507  8
     String dataComment = null;
 508  
 
 509  8
     if (rs.getString("display_name") != null) displayName = rs.getString("display_name");
 510  8
     if (rs.getString("url") != null) url = rs.getString("url");
 511  8
     if (rs.getString("source") != null) source = rs.getString("source");
 512  8
     if (rs.getString("web_view") != null) webView = rs.getString("web_view");
 513  8
     if (rs.getString("data_comment") != null) dataComment = rs.getString("data_comment");
 514  
 
 515  8
     if (displayName.equals("UM-BBD")) displayName = "UMBBD";
 516  8
     if (displayName.equals("NIST 74")) displayName = "NIST74";
 517  
 
 518  
         // Check for static links.
 519  8
     if (displayName.equals(EnzymeLink.BRENDA.getXrefDatabaseConstant().getDatabaseCode())){
 520  0
         result = EnzymeLink.BRENDA;
 521  8
     } else if (displayName.equals(EnzymeLink.KEGG.getXrefDatabaseConstant().getDatabaseCode())){
 522  2
         result = EnzymeLink.KEGG;
 523  6
     } else if (displayName.equals(EnzymeLink.EXPASY.getXrefDatabaseConstant().getDatabaseCode())){
 524  0
         return null;//return EnzymeLink.EXPASY; // These links are not necessary anymore.
 525  6
     } else if (displayName.equals(EnzymeLink.GO.getXrefDatabaseConstant().getDatabaseCode())){
 526  0
         result = EnzymeLink.GO;
 527  6
     } else if (displayName.equals(EnzymeLink.NIST74.getXrefDatabaseConstant().getDisplayName()) ||
 528  
             displayName.equals("GTD")){
 529  0
         result = EnzymeLink.NIST74;
 530  6
     } else if (displayName.equals(EnzymeLink.ERGO.getXrefDatabaseConstant().getDatabaseCode()) ||
 531  
             displayName.equals(EnzymeLink.WIT.getXrefDatabaseConstant().getDatabaseCode())) {
 532  0
         result = EnzymeLink.ERGO;
 533  6
     } else if (displayName.equals(XrefDatabaseConstant.CAS.getDatabaseCode())) {
 534  2
         if(url.trim().equals("")) return null;
 535  2
         result = EnzymeLink.valueOf(XrefDatabaseConstant.CAS, "", url, "",
 536  
                         EnzymeSourceConstant.valueOf(source), EnzymeViewConstant.valueOf(webView), dataComment);
 537  
     }
 538  
 
 539  8
     if (result == null){ // not a static link
 540  4
         result = EnzymeLink.valueOf(XrefDatabaseConstant.valueOf(displayName), url, "", "",
 541  
                 EnzymeSourceConstant.valueOf(source), EnzymeViewConstant.valueOf(webView), dataComment);    
 542  4
     } else if (dataComment != null){ // static link with a comment!
 543  2
         result = EnzymeLink.valueOf(XrefDatabaseConstant.valueOf(displayName), result.getSpecificUrl(),
 544  
                 result.getAccession(), result.getName(), result.getSource(), result.getView(), dataComment);
 545  
     }
 546  
 
 547  8
     return result;
 548  
   }
 549  
 
 550  
   /**
 551  
    * Creates the <code>EnzymeLink</code> object from the given xref result set.
 552  
    * <p/>
 553  
    *
 554  
    * @param rs The result set object.
 555  
    * @return an <code>EnzymeLink</code> instance.
 556  
    * @throws SQLException
 557  
    */
 558  
   private EnzymeLink doLoadXref(ResultSet rs) throws SQLException, DomainException {
 559  365
     assert rs != null : "Parameter 'rs' must not be null.";
 560  
 
 561  365
     String databaseCode = "";
 562  365
     String accession = "";
 563  365
     String name = "";
 564  365
     String source = "";
 565  365
     String webView = "";
 566  365
     String dataComment = null;
 567  
 
 568  365
     if (rs.getString("database_code") != null) databaseCode = rs.getString("database_code");
 569  365
     if (rs.getString("database_ac") != null) accession = rs.getString("database_ac");
 570  365
     if (rs.getString("name") != null) name = rs.getString("name");
 571  365
     if (rs.getString("source") != null) source = rs.getString("source");
 572  365
     if (rs.getString("web_view") != null) webView = rs.getString("web_view");
 573  365
     if (rs.getString("data_comment") != null) dataComment = rs.getString("data_comment");
 574  
 
 575  365
     if (databaseCode.equals("S")) databaseCode = "SWISSPROT";
 576  365
     if (databaseCode.equals("P")) databaseCode = "PROSITE";
 577  365
     if (databaseCode.equals("DIAGR")) {
 578  2
       databaseCode = "DIAGRAM";
 579  2
       return EnzymeLink.valueOf(XrefDatabaseConstant.DIAGRAM, accession, "", name,
 580  
                       EnzymeSourceConstant.valueOf(source), EnzymeViewConstant.valueOf(webView), dataComment); // Accession is the URL for DIAGRAMS.
 581  
     }
 582  
 
 583  363
     return EnzymeLink.valueOf(XrefDatabaseConstant.valueOf(databaseCode), "", accession, name,
 584  
         EnzymeSourceConstant.valueOf(source), EnzymeViewConstant.valueOf(webView), dataComment);
 585  
   }
 586  
 
 587  
   /**
 588  
    * Sets the parameters of the prepared statement.
 589  
    *
 590  
    * @param link            ...
 591  
    * @param enzymeId        ...
 592  
    * @param status          ...
 593  
    * @param insertStatement ...
 594  
    * @throws SQLException
 595  
    */
 596  
   private void doInsert(EnzymeLink link, Long enzymeId, Status status,
 597  
                         PreparedStatement insertStatement) throws SQLException {
 598  2
     assert link != null : "Parameter 'link' must not be null.";
 599  2
     assert enzymeId != null : "Parameter 'enzymeId' must not be null.";
 600  2
     assert status != null : "Parameter 'status' must not be null.";
 601  2
     assert insertStatement != null : "Parameter 'insertStatement' must not be null.";
 602  
 
 603  2
     insertStatement.setLong(1, enzymeId.longValue());
 604  2
     if (link.getXrefDatabaseConstant() == XrefDatabaseConstant.CAS) {
 605  0
       insertStatement.setString(2, link.getAccession());
 606  
     } else {
 607  2
       insertStatement.setString(2, link.getSpecificUrl());
 608  
     }
 609  2
     if (link.getXrefDatabaseConstant().equals("NIST74")) // TODO: Change to NIST 74 completely.
 610  0
       insertStatement.setString(3, "GTD");
 611  
     else
 612  2
       insertStatement.setString(3, link.getXrefDatabaseConstant().toString());
 613  2
     insertStatement.setString(4, status.getCode());
 614  2
     insertStatement.setString(5, link.getSource().toString());
 615  2
     insertStatement.setString(6, link.getView().toString());
 616  2
     if (link.getDataComment() == null || link.getDataComment().equals("")){
 617  1
         insertStatement.setNull(7, java.sql.Types.VARCHAR);
 618  
     } else {
 619  1
         insertStatement.setString(7, link.getDataComment());
 620  
     }
 621  2
   }
 622  
 
 623  
   private void doInsertXref(EnzymeLink xref, Long enzymeId, Status status,
 624  
                             PreparedStatement insertXrefStatement) throws SQLException {
 625  1
     assert xref != null : "Parameter 'xref' must not be null.";
 626  1
     assert enzymeId != null : "Parameter 'enzymeId' must not be null.";
 627  1
     assert status != null : "Parameter 'status' must not be null.";
 628  1
     assert insertXrefStatement != null : "Parameter 'insertXrefStatement' must not be null.";
 629  
     
 630  1
     String dbCode = xref.getXrefDatabaseConstant().getDatabaseCode();
 631  1
     if (dbCode.equals("SWISSPROT")) dbCode = "S";
 632  1
     if (dbCode.equals("PROSITE")) dbCode = "P";
 633  
 //    if (dbCode.equals("DIAGRAM")) dbCode = "DIAGR";
 634  1
     insertXrefStatement.setLong(1, enzymeId.longValue());
 635  1
     insertXrefStatement.setString(2, dbCode);
 636  1
     if (dbCode.equals("DIAGR"))
 637  0
       insertXrefStatement.setString(3, xref.getSpecificUrl());
 638  
     else
 639  1
       insertXrefStatement.setString(3, xref.getAccession());
 640  1
     insertXrefStatement.setString(4, xref.getName());
 641  1
     insertXrefStatement.setString(5, status.getCode());
 642  1
     insertXrefStatement.setString(6, xref.getSource().toString());
 643  1
     insertXrefStatement.setString(7, xref.getView().toString());
 644  1
     if (xref.getDataComment() == null || xref.getDataComment().equals("")){
 645  0
         insertXrefStatement.setNull(8, java.sql.Types.VARCHAR);
 646  
     } else {
 647  1
         insertXrefStatement.setString(8, xref.getDataComment());
 648  
     }
 649  1
   }
 650  
 }