Coverage Report - uk.ac.ebi.intenz.mapper.EnzymeEntryMapper
 
Classes in this File Line Coverage Branch Coverage Complexity
EnzymeEntryMapper
20%
140/670
12%
43/339
3.864
EnzymeEntryMapper$1
100%
2/2
N/A
3.864
 
 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.sql.Statement;
 8  
 import java.util.ArrayList;
 9  
 import java.util.List;
 10  
 import java.util.Map;
 11  
 import java.util.Set;
 12  
 import java.util.TreeMap;
 13  
 import java.util.TreeSet;
 14  
 
 15  
 import org.apache.log4j.Logger;
 16  
 
 17  
 import uk.ac.ebi.intenz.domain.constants.EnzymeNameTypeConstant;
 18  
 import uk.ac.ebi.intenz.domain.constants.EnzymeSourceConstant;
 19  
 import uk.ac.ebi.intenz.domain.constants.Status;
 20  
 import uk.ac.ebi.intenz.domain.enzyme.EnzymaticReactions;
 21  
 import uk.ac.ebi.intenz.domain.enzyme.EnzymeComment;
 22  
 import uk.ac.ebi.intenz.domain.enzyme.EnzymeCommissionNumber;
 23  
 import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry;
 24  
 import uk.ac.ebi.intenz.domain.enzyme.EnzymeLink;
 25  
 import uk.ac.ebi.intenz.domain.enzyme.EnzymeName;
 26  
 import uk.ac.ebi.intenz.domain.exceptions.DomainException;
 27  
 import uk.ac.ebi.intenz.domain.exceptions.EcException;
 28  
 import uk.ac.ebi.intenz.domain.exceptions.EnzymeReactionException;
 29  
 import uk.ac.ebi.intenz.domain.exceptions.EnzymeReferenceException;
 30  
 import uk.ac.ebi.intenz.domain.history.HistoryGraph;
 31  
 import uk.ac.ebi.intenz.domain.reference.Reference;
 32  
 import uk.ac.ebi.intenz.domain.stats.EnzymeStatistics;
 33  
 import uk.ac.ebi.rhea.mapper.MapperException;
 34  
 
 35  
 /**
 36  
  * Maps enzyme entry information to the corresponding database tables.
 37  
  * <br>
 38  
  * <b>IMPORTANT</b>: after using instances of this class, call the
 39  
  * {@link #close()} method, otherwise the underlying Rhea mapper objects will
 40  
  * keep their statements open.
 41  
  * @author Michael Darsow
 42  
  * @version $Revision: 1.5 $ $Date: 2009/05/26 14:59:09 $
 43  
  */
 44  1
 public class EnzymeEntryMapper {
 45  
         
 46  1
   private static final Logger LOGGER = Logger.getLogger(EnzymeEntryMapper.class);
 47  
 
 48  
   /**
 49  
    * The names are used for the heading of each enzyme, that is to which class,
 50  
    * sub- and sub-subclass it belongs to.
 51  
    */
 52  
   private static final String COLUMNS =
 53  
           "e.enzyme_id, e.ec1, e.ec2, e.ec3, e.ec4, e.history, e.note, e.status, " +
 54  
       "e.source, e.active, s1.name, s2.name, s3.name";
 55  
 
 56  
   private static final String LIST_COLUMNS =
 57  
           "enzyme_id, ec1, ec2, ec3, ec4, history, note, status, source, active";
 58  
 
 59  
   private EnzymeReactionMapper enzymeReactionMapper;
 60  
 
 61  12
   public EnzymeEntryMapper() {
 62  12
           enzymeReactionMapper = new EnzymeReactionMapper();
 63  12
   }
 64  
 
 65  0
   public EnzymeEntryMapper(EnzymeReactionMapper enzymeReactionMapper){
 66  0
           this.enzymeReactionMapper = enzymeReactionMapper;        
 67  0
   }
 68  
   
 69  
   @Override
 70  
         protected void finalize() throws Throwable {
 71  4
                 close();
 72  4
         }
 73  
 
 74  
         public void close() {
 75  6
                 if (enzymeReactionMapper != null){
 76  
                         try {
 77  6
                                 enzymeReactionMapper.close();
 78  0
                         } catch (MapperException e) {
 79  0
                                 LOGGER.error("Closing enzymeReactionMapper", e);
 80  6
                         }
 81  
                 }
 82  6
         }
 83  
 
 84  
   /**
 85  
    * Returns the SQL statement used for loading an enzyme by the given EC.
 86  
    *
 87  
    * @return the SQL statement.
 88  
    */
 89  
   private String findByEcStatement() {
 90  0
     return new StringBuilder("SELECT ").append(COLUMNS)
 91  
        .append(" FROM enzymes e, classes s1, subclasses s2, subsubclasses s3")
 92  
            .append(" WHERE e.ec1 = ? AND e.ec2 = ? AND e.ec3 = ? AND e.ec4 = ?")
 93  
            .append(" AND s1.ec1 = ? AND s2.ec1 = ? AND s2.ec2 = ?")
 94  
            .append(" AND s3.ec1 = ? AND s3.ec2 = ? AND s3.ec3 = ?")
 95  
            .append(" AND e.status = ? AND enzyme_id NOT IN")
 96  
        .append(" (SELECT before_id FROM history_events WHERE event_class = 'MOD')")
 97  
            .toString();
 98  
   }
 99  
 
 100  
   /**
 101  
    * Returns the SQL statement used for loading all enzymes by the given EC.
 102  
    * @return the SQL statement.
 103  
    */
 104  
   private String findAllSubSubclassEntriesByEcStatement() {
 105  0
     return new StringBuilder("SELECT ").append(COLUMNS)
 106  
        .append(" FROM enzymes e, classes s1, subclasses s2, subsubclasses s3")
 107  
        .append(" WHERE e.ec1 = ? AND e.ec2 = ? AND e.ec3 = ?")
 108  
        .append(" AND s1.ec1 = ? AND s2.ec1 = ? AND s2.ec2 = ?")
 109  
        .append(" AND s3.ec1 = ? AND s3.ec2 = ? AND s3.ec3 = ? AND e.source = 'INTENZ'")
 110  
        .append(" ORDER BY e.ec1, e.ec2, e.ec3, e.ec4").toString();
 111  
   }
 112  
   
 113  
   /**
 114  
    * @param preliminary Search only for preliminary (<code>true</code>)
 115  
    *                 or only non-preliminary (<code>false</code>)? If <code>null</code>,
 116  
    *                 both are included in the search.
 117  
    * @return
 118  
    */
 119  
   private String findAllByEcStatement(Boolean preliminary){
 120  0
             StringBuilder sb = new StringBuilder("SELECT ").append(COLUMNS)
 121  
                .append(" FROM enzymes e, classes s1, subclasses s2, subsubclasses s3")
 122  
                .append(" WHERE e.ec1 = ? AND e.ec2 = ? AND e.ec3 = ? AND e.ec4 = ?")
 123  
                .append(" AND s1.ec1 = ? AND s2.ec1 = ? AND s2.ec2 = ?")
 124  
                .append(" AND s3.ec1 = ? AND s3.ec2 = ? AND s3.ec3 = ?");
 125  0
             if (preliminary != null){
 126  0
                     sb.append(" AND e.status ").append(preliminary? "=" : "!=").append(" 'PM'");
 127  
             }
 128  0
             return sb.toString();
 129  
   }
 130  
 
 131  
   /**
 132  
    * Returns the SQL statement used for loading an enzyme by the given ID.
 133  
    *
 134  
    * @return the SQL statement.
 135  
    */
 136  
   private String findByIdStatement() {
 137  1
     return new StringBuilder("SELECT ").append(COLUMNS)
 138  
        .append(" FROM enzymes e, classes s1, subclasses s2, subsubclasses s3")
 139  
        .append(" WHERE e.enzyme_id = ? AND s1.ec1 = ? AND s2.ec1 = ? AND s2.ec2 = ?")
 140  
        .append(" AND s3.ec1 = ? AND s3.ec2 = ? AND s3.ec3 = ?").toString();
 141  
   }
 142  
 
 143  
   /**
 144  
    * Returns the SQL statement used for loading a list of public enzymes.
 145  
    * <p/>
 146  
    * Because of modified enzymes which still have the same EC number
 147  
    * (and are both approved) only the most up-to-date
 148  
    * version should be loaded. This is done by the substatement.
 149  
    * @return the SQL statement.
 150  
    */
 151  
   private String findListStatement() {
 152  0
     return new StringBuilder("SELECT ").append(LIST_COLUMNS)
 153  
        .append(" FROM enzymes WHERE ec1 = ? AND ec2 = ? AND ec3 = ?")
 154  
        .append(" AND status IN ('OK','PM') AND enzyme_id NOT IN")
 155  
        .append(" (SELECT before_id FROM history_events WHERE event_class = 'MOD')")
 156  
        .append(" ORDER BY status, ec1, ec2, ec3, ec4").toString();
 157  
   }
 158  
 
 159  
   /**
 160  
    * Returns the SQL statement used for loading the list of all public enzymes,
 161  
    * be it accepted by NC-IUBMB or preliminary EC numbers used in UniProt.
 162  
    * <p/>
 163  
    * Because of modified enzymes which still have the same EC number (and are both approved) only the most up-to-date
 164  
    * version should be loaded. This is done by the substatement.
 165  
    *
 166  
    * @return the SQL statement.
 167  
    */
 168  
   private String findAllStatement() {
 169  0
     return new StringBuilder("SELECT ").append(LIST_COLUMNS)
 170  
        .append(" FROM enzymes WHERE status IN ('OK','PM') AND enzyme_id NOT IN")
 171  
            .append(" (SELECT before_id FROM history_events WHERE event_class = 'MOD')")
 172  
            .append(" ORDER BY ec1, ec2, ec3, status, ec4").toString();
 173  
   }
 174  
 
 175  
   /**
 176  
    * Returns the SQL statement used for loading the list of enzymes with a
 177  
    * given status.
 178  
    * @return the SQL statement.
 179  
    */
 180  
   private String findCoreListStatement() {
 181  0
     return new StringBuilder("SELECT ").append(LIST_COLUMNS)
 182  
        .append(" FROM enzymes WHERE status = ?")
 183  
        .append(" ORDER BY ec1, ec2, ec3, ec4").toString();
 184  
   }
 185  
 
 186  
   /**
 187  
    * Returns the SQL statement used for loading an EC number by a given enzyme ID.
 188  
    * @return the SQL statement.
 189  
    */
 190  
   private String findEcStatement() {
 191  1
     return "SELECT ec1, ec2, ec3, ec4, status FROM enzymes WHERE enzyme_id = ?";
 192  
   }
 193  
 
 194  
   private String findIDInMappingTable() {
 195  0
     return "SELECT enzyme_id FROM id2ec WHERE ec = ? and status = ?";
 196  
   }
 197  
 
 198  
   /**
 199  
    * Returns the SQL statement used for loading the history line by a given enzyme ID.
 200  
    *
 201  
    * @return the SQL statement.
 202  
    */
 203  
   private String findHistoryLineStatement() {
 204  0
     return "SELECT history FROM enzymes WHERE enzyme_id = ?";
 205  
   }
 206  
 
 207  
   /**
 208  
    * Returns the SQL statement used for loading the note by a given enzyme ID.
 209  
    *
 210  
    * @return the SQL statement.
 211  
    */
 212  
   private String findNoteStatement() {
 213  0
     return "SELECT note FROM enzymes WHERE enzyme_id = ?";
 214  
   }
 215  
 
 216  
   /**
 217  
    * Returns the SQL statement to fetch the next available enzyme ID.
 218  
    *
 219  
    * @return the SQL statement.
 220  
    */
 221  
   private String findNextEnzymeIdStatement() {
 222  0
     return "SELECT s_enzyme_id.nextval from DUAL";
 223  
   }
 224  
 
 225  
   private String countIUBMBEnzymesStatement() {
 226  0
           return new StringBuilder("SELECT status, active FROM enzymes")
 227  
        .append(" WHERE source = 'IUBMB' AND enzyme_id NOT IN")
 228  
        .append(" (SELECT before_id FROM history_events WHERE event_class = 'MOD')")
 229  
        .append(" ORDER BY ec1, ec2, ec3, ec4").toString();
 230  
   }
 231  
 
 232  
   private String countClassesStatement() {
 233  0
     return "SELECT count(*) FROM classes";
 234  
   }
 235  
 
 236  
   private String countSubclassesStatement() {
 237  0
     return "SELECT count(*) FROM subclasses";
 238  
   }
 239  
 
 240  
   private String countSubSubclassesStatement() {
 241  0
     return "SELECT count(*) FROM subsubclasses";
 242  
   }
 243  
 
 244  
   /**
 245  
    * Returns the INSERT statement for inserting an enzyme.
 246  
    *
 247  
    * @return the SQL statement.
 248  
    */
 249  
   private String insertStatement() {
 250  15
     return "INSERT INTO enzymes (enzyme_id, ec1, ec2, ec3, ec4, history, note, status, source, active)" +
 251  
                     " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
 252  
   }
 253  
 
 254  
   /**
 255  
    * Creates the SQL statement to be used to update the core data of an enzyme.
 256  
    *
 257  
    * @return the SQL statement.
 258  
    */
 259  
   private String updateStatement() {
 260  0
     return "UPDATE enzymes SET ec1 = ?, ec2 = ?, ec3 = ?, ec4 = ?, status = ?," +
 261  
                     " source = ?, note = ?, history = ?, active = ? WHERE enzyme_id = ?";
 262  
   }
 263  
 
 264  
   /**
 265  
    * Updates the EC.
 266  
    *
 267  
    * @return the SQL statement.
 268  
    */
 269  
   private String updateEcStatement() {
 270  0
     return "UPDATE enzymes SET ec1 = ?, ec2 = ?, ec3 = ?, ec4 = ? WHERE enzyme_id = ?";
 271  
   }
 272  
 
 273  
   /**
 274  
    * Updates the history line.
 275  
    *
 276  
    * @return the SQL statement.
 277  
    */
 278  
   private String updateHistoryLineStatement() {
 279  0
     return "UPDATE enzymes SET history = ? WHERE enzyme_id = ?";
 280  
   }
 281  
 
 282  
   /**
 283  
    * Updates the note.
 284  
    *
 285  
    * @return the SQL statement.
 286  
    */
 287  
   private String updateNoteStatement() {
 288  0
     return "UPDATE enzymes SET note = ? WHERE enzyme_id = ?";
 289  
   }
 290  
 
 291  
   /**
 292  
    * Updates the status.
 293  
    *
 294  
    * @return the SQL statement.
 295  
    */
 296  
   private String updateStatusStatement() {
 297  0
     return "UPDATE enzymes SET status = ? WHERE enzyme_id = ?";
 298  
   }
 299  
 
 300  
   /**
 301  
    * Tries to find entry information about an enzyme.
 302  
    * @param ec1 Number of class to search for.
 303  
    * @param ec2 Number of subclass to search for.
 304  
    * @param ec3 Number of sub-subclass to search for.
 305  
    * @param ec4 Number of entry to search for.
 306  
    * @param status the status of the searched enzyme.
 307  
    *         If <code>null</code>, it defaults to {@link Status#APPROVED}.
 308  
    * @param con The logical connection.
 309  
    * @return an <code>EnzymeEntry</code> instance or <code>null</code> if
 310  
    * nothing has been found.
 311  
    * @throws java.sql.SQLException 
 312  
    * @throws uk.ac.ebi.intenz.domain.exceptions.DomainException
 313  
  * @throws MapperException in case of problem retrieving reaction/cofactor info.
 314  
    */
 315  
   public EnzymeEntry findByEc(int ec1, int ec2, int ec3, int ec4, Status status,
 316  
                   Connection con)
 317  
   throws SQLException, DomainException, MapperException {
 318  0
     PreparedStatement findStatement = null;
 319  0
     ResultSet rs = null;
 320  0
     EnzymeEntry result = null;
 321  
     try {
 322  
       // Core information.
 323  0
       findStatement = con.prepareStatement(findByEcStatement());
 324  0
       findStatement.setInt(1, ec1);
 325  0
       findStatement.setInt(2, ec2);
 326  0
       findStatement.setInt(3, ec3);
 327  0
       findStatement.setInt(4, ec4);
 328  0
       findStatement.setInt(5, ec1);
 329  0
       findStatement.setInt(6, ec1);
 330  0
       findStatement.setInt(7, ec2);
 331  0
       findStatement.setInt(8, ec1);
 332  0
       findStatement.setInt(9, ec2);
 333  0
       findStatement.setInt(10, ec3);
 334  0
       findStatement.setString(11, status == null? "OK" : status.getCode());
 335  0
       rs = findStatement.executeQuery();
 336  0
       if (rs.next()) {
 337  0
         result = doLoad(rs);
 338  0
         findEnzymeData(result, con);
 339  
       }
 340  
     } finally {
 341  0
             if (rs != null) rs.close();
 342  0
       if (findStatement != null) findStatement.close();
 343  
     }
 344  
 
 345  0
     return result;
 346  
   }
 347  
   
 348  
   /**
 349  
    * Finds an enzyme by EC number (as String) and status.
 350  
    * @param ecString the EC number.
 351  
    * @param status the enzyme status.
 352  
    * @param con a database connection.
 353  
    * @return an enzyme entry, or <code>null</code> if not found.
 354  
    * @throws NumberFormatException
 355  
    * @throws SQLException
 356  
    * @throws DomainException
 357  
    * @throws MapperException
 358  
    */
 359  
   public EnzymeEntry findByEc(String ecString, Status status, Connection con)
 360  
   throws NumberFormatException, SQLException, DomainException, MapperException{
 361  0
           EnzymeCommissionNumber ec = EnzymeCommissionNumber.valueOf(ecString);
 362  0
           if (EnzymeCommissionNumber.isPreliminary(ecString)){
 363  0
                   status = Status.PRELIMINARY;
 364  
           }
 365  0
           return findByEc(ec.getEc1(), ec.getEc2(), ec.getEc3(), ec.getEc4(),
 366  
                           status, con);
 367  
   }
 368  
   
 369  
   public List<EnzymeEntry> findAllByEc(int ec1, int ec2, int ec3, int ec4,
 370  
                   Boolean preliminary, Connection con)
 371  
   throws SQLException, DomainException {
 372  0
             if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 373  0
             PreparedStatement findAllByEcStatement = null;
 374  0
             ResultSet rs = null;
 375  0
             List<EnzymeEntry> result = new ArrayList<EnzymeEntry>();
 376  
 
 377  
             try {
 378  
               // Core information.
 379  0
               findAllByEcStatement = con.prepareStatement(findAllByEcStatement(preliminary));
 380  0
               findAllByEcStatement.setInt(1, ec1);
 381  0
               findAllByEcStatement.setInt(2, ec2);
 382  0
               findAllByEcStatement.setInt(3, ec3);
 383  0
               findAllByEcStatement.setInt(4, ec4);
 384  0
               findAllByEcStatement.setInt(5, ec1);
 385  0
               findAllByEcStatement.setInt(6, ec1);
 386  0
               findAllByEcStatement.setInt(7, ec2);
 387  0
               findAllByEcStatement.setInt(8, ec1);
 388  0
               findAllByEcStatement.setInt(9, ec2);
 389  0
               findAllByEcStatement.setInt(10, ec3);
 390  0
               rs = findAllByEcStatement.executeQuery();
 391  0
               while (rs.next()) {
 392  0
                 EnzymeEntry enzymeEntry = doLoadGhost(rs);
 393  
 
 394  
                 // History information
 395  0
                 loadHistoryGraph(enzymeEntry, con);
 396  
 
 397  
                 // Common name
 398  0
                 EnzymeNameMapper nameMapper = new EnzymeNameMapper();
 399  0
                 List<EnzymeName> commonNames = nameMapper.findCommonNames(enzymeEntry.getId(), con);
 400  0
                 if (commonNames != null) enzymeEntry.setCommonNames(commonNames);
 401  
 
 402  0
                 result.add(enzymeEntry);
 403  0
               }
 404  
             } finally {
 405  0
                     if (rs != null) rs.close();
 406  0
               if (findAllByEcStatement != null) findAllByEcStatement.close();
 407  
             }
 408  
 
 409  0
             if (result.size() == 0) return null;
 410  0
             return result;
 411  
           
 412  
           
 413  
   }
 414  
 
 415  
   /**
 416  
    * Tries to find entry information about all enzymes specified by the given EC.
 417  
    *
 418  
    * @param ec1 Number of class to search for.
 419  
    * @param ec2 Number of subclass to search for.
 420  
    * @param ec3 Number of sub-subclass to search for.
 421  
    * @param ec4 Number of entry to search for.
 422  
    * @param con The logical connection.
 423  
    * @return an <code>EnzymeEntry</code> instance or <code>null</code>
 424  
    *                 if nothing has been found.
 425  
    * @throws NullPointerException if parameter <code>con</code> is <code>null</code>.
 426  
    * @throws SQLException if a database error occurs.
 427  
    * @throws DomainException if any error related to domain information occurs.
 428  
    */
 429  
   public List<EnzymeEntry> findAllByEc(int ec1, int ec2, int ec3, int ec4, Connection con)
 430  
   throws SQLException, DomainException {
 431  0
           return findAllByEc(ec1, ec2, ec3, ec4, null, con);
 432  
   }
 433  
 
 434  
         private HistoryGraph getHistoryGraph(Connection con, EnzymeEntry enzymeEntry)
 435  
                         throws SQLException, DomainException {
 436  
                 HistoryGraph historyGraph;
 437  1
                 switch (enzymeEntry.getStatus()){
 438  
                 case PRELIMINARY:
 439  0
                         if (enzymeEntry.isActive()){
 440  
                                 // look into the future in case it is being transferred:
 441  0
                             EnzymeFutureMapper futureEventsMapper = new EnzymeFutureMapper();
 442  0
                             historyGraph = futureEventsMapper.find(enzymeEntry, con);
 443  0
                                 break;
 444  
                         }
 445  
                         // else it has been transferred, look past like for APPROVED:
 446  
                 case APPROVED:
 447  1
                     EnzymeHistoryMapper historyEventsMapper = new EnzymeHistoryMapper();
 448  1
                     historyGraph = historyEventsMapper.find(enzymeEntry, con);
 449  1
                         break;
 450  
                 default:
 451  0
                     EnzymeFutureMapper futureEventsMapper = new EnzymeFutureMapper();
 452  0
                     historyGraph = futureEventsMapper.find(enzymeEntry, con);
 453  
                 }
 454  1
                 return historyGraph;
 455  
         }
 456  
 
 457  
   public List<EnzymeEntry> findAllSubSubclassEntriesByEc(int ec1, int ec2, int ec3, Connection con)
 458  
   throws SQLException, DomainException {
 459  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 460  0
     PreparedStatement findAllByEcStatement = null;
 461  0
     ResultSet rs = null;
 462  0
     Map<EnzymeCommissionNumber, EnzymeEntry> map =
 463  
             new TreeMap<EnzymeCommissionNumber, EnzymeEntry>();
 464  
 
 465  
     try {
 466  
       // Core information.
 467  0
       findAllByEcStatement = con.prepareStatement(findAllSubSubclassEntriesByEcStatement());
 468  0
       findAllByEcStatement.setInt(1, ec1);
 469  0
       findAllByEcStatement.setInt(2, ec2);
 470  0
       findAllByEcStatement.setInt(3, ec3);
 471  0
       findAllByEcStatement.setInt(4, ec1);
 472  0
       findAllByEcStatement.setInt(5, ec1);
 473  0
       findAllByEcStatement.setInt(6, ec2);
 474  0
       findAllByEcStatement.setInt(7, ec1);
 475  0
       findAllByEcStatement.setInt(8, ec2);
 476  0
       findAllByEcStatement.setInt(9, ec3);
 477  0
       rs = findAllByEcStatement.executeQuery();
 478  0
       while (rs.next()) {
 479  0
         EnzymeEntry enzymeEntry = doLoadGhost(rs);
 480  0
         if (map.containsKey(enzymeEntry.getEc())){
 481  0
                 EnzymeEntry existing = (EnzymeEntry) map.get(enzymeEntry.getEc());
 482  
                 // From several entries with the same EC number,
 483  
                 // we choose the most recent one (higher id):
 484  0
                 if (existing.getId().longValue() > enzymeEntry.getId().longValue())
 485  0
                         continue;
 486  
         }
 487  
         // History information
 488  0
         loadHistoryGraph(enzymeEntry, con);
 489  
 
 490  
         // Common name
 491  0
         EnzymeNameMapper nameMapper = new EnzymeNameMapper();
 492  0
         List<EnzymeName> commonNames = nameMapper.findCommonNames(enzymeEntry.getId(), con);
 493  0
         if (commonNames != null) enzymeEntry.setCommonNames(commonNames);
 494  
 
 495  0
         map.put(enzymeEntry.getEc(), enzymeEntry);
 496  0
       }
 497  
     } finally {
 498  0
             if (rs != null) rs.close();
 499  0
       if (findAllByEcStatement != null) findAllByEcStatement.close();
 500  
     }
 501  
 
 502  0
     return map.isEmpty()? null : new ArrayList<EnzymeEntry>(map.values());
 503  
   }
 504  
 
 505  
   /**
 506  
    * Tries to find entry information about an enzyme.
 507  
    * @param id  The enzyme ID.
 508  
    * @param con The logical connection.
 509  
    * @param ghost return a ghost (minimal info)?
 510  
    * @return an <code>EnzymeEntry</code> instance
 511  
    *                 or <code>null</code> if nothing has been found.
 512  
    * @throws java.sql.SQLException
 513  
    * @throws uk.ac.ebi.intenz.domain.exceptions.DomainException 
 514  
    * @throws MapperException in case of problem retrieving reaction/cofactor
 515  
    *                 info for non-ghosts.
 516  
    */
 517  
   public EnzymeEntry findById(Long id, Connection con, boolean ghost)
 518  
   throws SQLException, DomainException, MapperException {
 519  1
     PreparedStatement findStatement = null;
 520  1
     EnzymeEntry result = null;
 521  1
     ResultSet rsEc = null, rs = null;
 522  
     int ec1, ec2, ec3;
 523  
 
 524  
     try {
 525  
       // EC number (necessary for the heading)
 526  1
       findStatement = con.prepareStatement(findEcStatement());
 527  1
       findStatement.setLong(1, id.longValue());
 528  1
       rsEc = findStatement.executeQuery();
 529  1
       if (rsEc.next()) {
 530  1
         if (rsEc.getInt("ec1") != 0)
 531  1
           ec1 = rsEc.getInt("ec1");
 532  
         else
 533  0
           return result;
 534  1
         if (rsEc.getInt("ec2") != 0)
 535  1
           ec2 = rsEc.getInt("ec2");
 536  
         else
 537  0
           return result;
 538  1
         if (rsEc.getInt("ec3") != 0)
 539  1
           ec3 = rsEc.getInt("ec3");
 540  
         else
 541  0
           return result;
 542  
       } else
 543  0
         return result;
 544  
 
 545  1
       if (findStatement != null) findStatement.close();
 546  
 
 547  
       // Core information.
 548  1
       findStatement = con.prepareStatement(findByIdStatement());
 549  1
       findStatement.setLong(1, id.longValue());
 550  1
       findStatement.setInt(2, ec1);
 551  1
       findStatement.setInt(3, ec1);
 552  1
       findStatement.setInt(4, ec2);
 553  1
       findStatement.setInt(5, ec1);
 554  1
       findStatement.setInt(6, ec2);
 555  1
       findStatement.setInt(7, ec3);
 556  1
       rs = findStatement.executeQuery();
 557  1
       if (rs.next()) {
 558  1
               if (ghost){
 559  0
                       result = doLoadGhost(rs);
 560  
               } else {
 561  1
                   result = doLoad(rs);
 562  1
                   findEnzymeData(result, con);
 563  
               }
 564  
       }
 565  
     } finally {
 566  1
             if (rsEc != null) rsEc.close();
 567  1
             if (rs != null) rs.close();
 568  1
             if (findStatement != null) findStatement.close();
 569  
     }
 570  
 
 571  1
     return result;
 572  
   }
 573  
   
 574  
   /**
 575  
    * Loads a complete version of the enzyme with the given ID.
 576  
    * @param id
 577  
    * @param con
 578  
    * @return an <code>EnzymeEntry</code> instance
 579  
    *         or <code>null</code> if nothing has been found.
 580  
    * @throws SQLException
 581  
    * @throws DomainException
 582  
  * @throws MapperException in case of problem retrieving reaction/cofactor info.
 583  
    */
 584  
   public EnzymeEntry findById(long id, Connection con)
 585  
   throws SQLException, DomainException, MapperException{
 586  1
           return findById(id, con, false);
 587  
   }
 588  
 
 589  
   /**
 590  
    * Loads a ghost version of the enzyme with the given ID.
 591  
    *
 592  
    * @param id  The enzyme ID.
 593  
    * @param con The logical connection.
 594  
    * @return an <code>EnzymeEntry</code> instance or <code>null</code> if nothing has been found.
 595  
    * @throws SQLException
 596  
    * @throws uk.ac.ebi.intenz.domain.exceptions.DomainException
 597  
    */
 598  
   public EnzymeEntry findGhostById(int id, Connection con)
 599  
   throws SQLException, DomainException {
 600  0
           EnzymeEntry entry = null;
 601  
           try {
 602  0
                 entry = findById((long) id, con, true);
 603  0
         } catch (MapperException e) {
 604  
                 // Never thrown for ghosts
 605  0
         }
 606  0
         return entry;
 607  
   }
 608  
 
 609  
   public Long findIDInMappingTable(String ec, Status status, Connection con)
 610  
   throws SQLException {
 611  0
     if (con == null || ec == null) throw new NullPointerException();
 612  0
     PreparedStatement findStatement = null;
 613  0
     ResultSet rs = null;
 614  
     try {
 615  
       // Get id
 616  0
       findStatement = con.prepareStatement(findIDInMappingTable());
 617  0
       findStatement.setString(1, ec);
 618  0
       findStatement.setString(2, status.getCode());
 619  0
       rs = findStatement.executeQuery();
 620  0
       if (rs.next()) {
 621  0
         return new Long(rs.getLong(1));
 622  
       }
 623  0
       return new Long(-1);
 624  
     } finally {
 625  0
             if (rs != null) rs.close();
 626  0
       if (findStatement != null) findStatement.close();
 627  
     }
 628  
   }
 629  
 
 630  
   /**
 631  
    * Tries to find all public entries but loads the entries only with minimum
 632  
    * information.
 633  
    * @param con The logical connection.
 634  
    * @return a <code>List</code> of <code>EnzymeEntry</code> instances
 635  
    *                 or <code>null</code> if nothing has been found.
 636  
    * @throws SQLException
 637  
    * @throws uk.ac.ebi.intenz.domain.exceptions.DomainException 
 638  
    */
 639  
   public List<EnzymeEntry> findAll(Connection con) throws SQLException, DomainException {
 640  0
     Statement findListStatement = null;
 641  0
     ResultSet rs = null;
 642  0
     List<EnzymeEntry> result = new ArrayList<EnzymeEntry>();
 643  0
     boolean noResult = true;
 644  
 
 645  
     try {
 646  0
       findListStatement = con.createStatement();
 647  0
       rs = findListStatement.executeQuery(findAllStatement());
 648  
 
 649  0
       while (rs.next()) {
 650  0
         noResult = false;
 651  0
         EnzymeEntry enzymeEntry = doLoadGhost(rs);
 652  
 
 653  
         // Common name
 654  0
         EnzymeNameMapper nameMapper = new EnzymeNameMapper();
 655  0
         List<EnzymeName> commonNames = nameMapper.findCommonNames(enzymeEntry.getId(), con);
 656  0
         if (commonNames != null) enzymeEntry.setCommonNames(commonNames);
 657  0
         result.add(enzymeEntry);
 658  0
       }
 659  
     } finally {
 660  0
             if (rs != null) rs.close();
 661  0
       if (findListStatement != null) findListStatement.close();
 662  
     }
 663  
 
 664  0
     if (noResult) return null;
 665  0
     return result;
 666  
   }
 667  
 
 668  
   /**
 669  
    * Tries to find all entries requested but loads the entries only with
 670  
    * minimum information.
 671  
    * @param ec1 Number of class to search for.
 672  
    * @param ec2 Number of subclass to search for.
 673  
    * @param ec3 Number of sub-subclass to search for.
 674  
    * @param con The logical connection.
 675  
    * @return a <code>List</code> of <code>EnzymeEntry</code> instances or <code>null</code>
 676  
    * if nothing has been found.
 677  
    * @throws java.sql.SQLException
 678  
    * @throws uk.ac.ebi.intenz.domain.exceptions.DomainException 
 679  
    */
 680  
   public List<EnzymeEntry> findList(String ec1, String ec2, String ec3,
 681  
                   Connection con)
 682  
   throws SQLException, DomainException {
 683  0
     PreparedStatement findListStatement = null;
 684  0
     ResultSet rs = null;
 685  0
     List<EnzymeEntry> result = new ArrayList<EnzymeEntry>();
 686  0
     boolean noResult = true;
 687  
 
 688  
     try {
 689  0
       findListStatement = con.prepareStatement(findListStatement());
 690  0
       findListStatement.setString(1, ec1);
 691  0
       findListStatement.setString(2, ec2);
 692  0
       findListStatement.setString(3, ec3);
 693  0
       rs = findListStatement.executeQuery();
 694  
 
 695  0
       while (rs.next()) {
 696  0
         noResult = false;
 697  0
         EnzymeEntry enzymeEntry = doLoadGhost(rs);
 698  
 
 699  
         // History information
 700  0
         loadHistoryGraph(enzymeEntry, con);
 701  
 
 702  
         // Common name
 703  0
         EnzymeNameMapper nameMapper = new EnzymeNameMapper();
 704  0
         List<EnzymeName> commonNames = nameMapper.findCommonNames(enzymeEntry.getId(), con);
 705  0
         if (commonNames != null) enzymeEntry.setCommonNames(commonNames);
 706  0
         result.add(enzymeEntry);
 707  0
       }
 708  
     } finally {
 709  0
             if (rs != null) rs.close();
 710  0
       if (findListStatement != null) findListStatement.close();
 711  
     }
 712  
 
 713  0
     if (noResult) return null;
 714  0
     return result;
 715  
   }
 716  
 
 717  
   /**
 718  
    * Retrieves the list of enzymes with a given status.
 719  
    * @param con
 720  
    * @param status
 721  
    * @return the list of enzymes with the given status.
 722  
    * @throws SQLException
 723  
    * @throws DomainException
 724  
    */
 725  
   public List<EnzymeEntry> findByStatus(Connection con, Status status)
 726  
   throws SQLException, DomainException{
 727  0
             PreparedStatement findListStatement = null;
 728  0
             ResultSet rs = null;
 729  0
             List<EnzymeEntry> result = new ArrayList<EnzymeEntry>();
 730  
             try {
 731  0
               findListStatement = con.prepareStatement(findCoreListStatement());
 732  0
               findListStatement.setString(1, status.getCode());
 733  0
               rs = findListStatement.executeQuery();
 734  
 
 735  0
               while (rs.next()) {
 736  0
                 EnzymeEntry enzymeEntry = doLoadGhost(rs);
 737  
                 // History information
 738  0
                 loadHistoryGraph(enzymeEntry, con);
 739  
                 // Common name
 740  0
                 EnzymeNameMapper nameMapper = new EnzymeNameMapper();
 741  0
                 List<EnzymeName> commonNames = nameMapper.findCommonNames(enzymeEntry.getId(), con);
 742  0
                 if (commonNames != null) enzymeEntry.setCommonNames(commonNames);
 743  0
                 result.add(enzymeEntry);
 744  0
               }
 745  
             } finally {
 746  0
                     if (rs != null) rs.close();
 747  0
                 if (findListStatement != null) findListStatement.close();
 748  
             }
 749  0
             return result;
 750  
   }
 751  
   
 752  
   /**
 753  
    * Tries to find all proposed entries but loads the entries only with
 754  
    * minimum information.
 755  
    * @param con The logical connection.
 756  
    * @return a {@link java.util.List} of
 757  
    *         {@link uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry} instances.
 758  
    * @throws SQLException    if a database error occurs.
 759  
    * @throws DomainException if a domain related error occurs.
 760  
    */
 761  
   public List<EnzymeEntry> findProposedList(Connection con)
 762  
   throws SQLException, DomainException {
 763  0
     return findByStatus(con, Status.PROPOSED);
 764  
   }
 765  
 
 766  
   /**
 767  
    * Tries to find all suggested entries but loads the entries only with
 768  
    * minimum information.
 769  
    * @param con The logical connection.
 770  
    * @return a <code>Vector</code> of <code>EnzymeEntry</code> instances
 771  
    *         or <code>null</code> if nothing has been found.
 772  
    * @throws SQLException
 773  
    * @throws uk.ac.ebi.intenz.domain.exceptions.DomainException 
 774  
    */
 775  
   public List<EnzymeEntry> findSuggestedList(Connection con)
 776  
   throws SQLException, DomainException {
 777  0
     return findByStatus(con, Status.SUGGESTED);
 778  
   }
 779  
 
 780  
   /**
 781  
    * Gets the list of preliminary EC numbers issued by UniProt.
 782  
    * @param con
 783  
    * @return A list of preliminary EC numbers issued by UniProt.
 784  
    * @throws SQLException
 785  
    * @throws DomainException
 786  
    */
 787  
   public List<EnzymeEntry> findPreliminaryEcsList(Connection con)
 788  
   throws SQLException, DomainException{
 789  0
       return findByStatus(con, Status.PRELIMINARY);
 790  
   }
 791  
   
 792  
   /**
 793  
    * Exports every piece of data for publicly available entries.
 794  
    * @param con
 795  
    * @return A <code>List</code> of <code>EnzymeEntry</code>s
 796  
    * @throws SQLException 
 797  
    * @throws DomainException 
 798  
  * @throws MapperException in case of problem retrieving reaction/cofactor info.
 799  
    */
 800  
   public List<EnzymeEntry> exportAllEntries(Connection con)
 801  
   throws SQLException, DomainException, MapperException{
 802  0
           List<EnzymeEntry> entries = new ArrayList<EnzymeEntry>();
 803  0
           Statement findListStatement = null;
 804  0
             ResultSet rs = null;
 805  
           try {
 806  0
                   findListStatement = con.createStatement();
 807  0
                   rs = findListStatement.executeQuery(findAllStatement());
 808  0
                   while (rs.next()){
 809  0
                           EnzymeEntry entry = doLoadCore(rs);
 810  0
                           findEnzymeData(entry, con);
 811  0
                           entries.add(entry);
 812  0
                   }
 813  
           } finally {
 814  0
                     if (rs != null) rs.close();
 815  0
                 if (findListStatement != null) findListStatement.close();
 816  
           }
 817  0
           return entries.isEmpty()? null : entries;
 818  
   }
 819  
 
 820  
   /**
 821  
    * Tries to export all approved entries containing only ENZYME relevant information.
 822  
    * <p/>
 823  
    * Affected table rows will be locked.
 824  
    *
 825  
    * @param con The logical connection.
 826  
    * @return an <code>ArrayList</code> of <code>EnzymeEntry</code> instances or <code>null</code>
 827  
    *         if nothing has been found.
 828  
    * @throws SQLException
 829  
    * @throws uk.ac.ebi.intenz.domain.exceptions.DomainException 
 830  
  * @throws MapperException in case of problem retrieving reaction/cofactor info.
 831  
    */
 832  
   public List<EnzymeEntry> exportApprovedSibEntries(Connection con) 
 833  
   throws SQLException, DomainException, MapperException {
 834  0
     Statement findListStatement = null;
 835  0
     ResultSet rs = null;
 836  0
     List<EnzymeEntry> result = new ArrayList<EnzymeEntry>();
 837  
     try {
 838  0
       findListStatement = con.createStatement();
 839  0
       rs = findListStatement.executeQuery(findAllStatement());
 840  0
       while (rs.next()) {
 841  0
         EnzymeEntry enzymeEntry = doLoadCore(rs);
 842  0
         exportSIBEnzymeData(enzymeEntry, con);
 843  0
         result.add(enzymeEntry);
 844  0
       }
 845  
     } finally {
 846  0
             if (rs != null) rs.close();
 847  0
         if (findListStatement != null) findListStatement.close();
 848  
     }
 849  0
     return result.isEmpty()? null: result;
 850  
   }
 851  
 
 852  
   /**
 853  
    * Tries to find all proposed entries with all information.
 854  
    * <p/>
 855  
    * This feature is just needed to export a list of all proposed entries.
 856  
    *
 857  
    * @param con The logical connection.
 858  
    * @return a <code>Vector</code> of <code>EnzymeEntry</code> instances or <code>null</code>
 859  
    *         if nothing has been found.
 860  
    * @throws SQLException
 861  
    * @throws uk.ac.ebi.intenz.domain.exceptions.DomainException 
 862  
  * @throws MapperException in case of problem retrieving reaction/cofactor info.
 863  
    */
 864  
   public List<EnzymeEntry> findFullProposedList(Connection con)
 865  
   throws SQLException, DomainException, MapperException {
 866  0
     PreparedStatement findListStatement = null;
 867  0
     ResultSet rs = null;
 868  0
     List<EnzymeEntry> result = new ArrayList<EnzymeEntry>();
 869  0
     boolean noResult = true;
 870  
 
 871  
     try {
 872  0
       findListStatement = con.prepareStatement(findCoreListStatement());
 873  0
       findListStatement.setString(1, "PR");
 874  0
       rs = findListStatement.executeQuery();
 875  
 
 876  0
       while (rs.next()) {
 877  0
         noResult = false;
 878  0
         EnzymeEntry enzymeEntry = doLoadCore(rs);
 879  0
         findEnzymeData(enzymeEntry, con);
 880  0
         result.add(enzymeEntry);
 881  0
       }
 882  
     } finally {
 883  0
             if (rs != null) rs.close();
 884  0
         if (findListStatement != null) findListStatement.close();
 885  
     }
 886  
 
 887  0
     if (noResult) return null;
 888  0
     return result;
 889  
   }
 890  
 
 891  
   /**
 892  
    * Tries to find all suggested entries with all information.
 893  
    * <p/>
 894  
    * This feature is just needed to export a list of all suggested entries.
 895  
    *
 896  
    * @param con The logical connection.
 897  
    * @return a <code>Vector</code> of <code>EnzymeEntry</code> instances or <code>null</code>
 898  
    *         if nothing has been found.
 899  
    * @throws SQLException
 900  
    * @throws uk.ac.ebi.intenz.domain.exceptions.DomainException 
 901  
  * @throws MapperException in case of problem retrieving reaction/cofactor info.
 902  
    */
 903  
   public List<EnzymeEntry> findFullSuggestedList(Connection con)
 904  
   throws SQLException, DomainException, MapperException {
 905  0
     PreparedStatement findListStatement = null;
 906  0
     ResultSet rs = null;
 907  0
     List<EnzymeEntry> result = new ArrayList<EnzymeEntry>();
 908  0
     boolean noResult = true;
 909  
 
 910  
     try {
 911  0
       findListStatement = con.prepareStatement(findCoreListStatement());
 912  0
       findListStatement.setString(1, "SU");
 913  0
       rs = findListStatement.executeQuery();
 914  
 
 915  0
       while (rs.next()) {
 916  0
         noResult = false;
 917  0
         EnzymeEntry enzymeEntry = doLoadCore(rs);
 918  0
         findEnzymeData(enzymeEntry, con);
 919  0
         result.add(enzymeEntry);
 920  0
       }
 921  
     } finally {
 922  0
             if (rs != null) rs.close();
 923  0
         if (findListStatement != null) findListStatement.close();
 924  
     }
 925  
 
 926  0
     if (noResult) return null;
 927  0
     return result;
 928  
   }
 929  
 
 930  
   /**
 931  
    * Returns the EC number of the given enzyme ID.
 932  
    *
 933  
    * @param id  The enzyme ID.
 934  
    * @param con The logical connection.
 935  
    * @return The EC, if found, otherwise <code>null</code>.
 936  
    * @throws SQLException
 937  
    * @throws uk.ac.ebi.intenz.domain.exceptions.DomainException 
 938  
    */
 939  
   public EnzymeCommissionNumber findEC(String id, Connection con)
 940  
   throws SQLException, DomainException {
 941  0
     int ec1 = 0, ec2 = 0, ec3 = 0, ec4 = 0;
 942  0
     Status status = null;
 943  0
     PreparedStatement findEcStatement = null;
 944  0
     ResultSet rs = null;
 945  
     try {
 946  0
       findEcStatement = con.prepareStatement(findEcStatement());
 947  0
       findEcStatement.setString(1, id);
 948  0
       rs = findEcStatement.executeQuery();
 949  
 
 950  0
       while (rs.next()) {
 951  0
         ec1 = rs.getInt("ec1");
 952  0
         ec2 = rs.getInt("ec2");
 953  0
         ec3 = rs.getInt("ec3");
 954  0
         ec4 = rs.getInt("ec4");
 955  0
         status = Status.fromCode(rs.getString("status"));
 956  
       }
 957  
     } finally {
 958  0
             if (rs != null) rs.close();
 959  0
       if (findEcStatement != null) findEcStatement.close();
 960  
     }
 961  
 
 962  0
     return EnzymeCommissionNumber.valueOf(
 963  
                     ec1, ec2, ec3, ec4, status.equals(Status.PRELIMINARY));
 964  
   }
 965  
 
 966  
   /**
 967  
    * Checks whether a given EC number already exists in the database.
 968  
    *
 969  
    * @param ec  The EC number to be checked.
 970  
    * @param con The database connection.
 971  
    * @return <code>true</code> if the given EC number exists in the database.
 972  
    * @throws SQLException         if a database error occurs.
 973  
    * @throws NullPointerException if any of the parameters is <code>null</code>
 974  
    */
 975  
   public static boolean ecExists(EnzymeCommissionNumber ec, Connection con)
 976  
   throws SQLException {
 977  0
     if (ec == null) throw new NullPointerException("Parameter 'ec' must not be null.");
 978  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 979  
 
 980  0
     PreparedStatement findStatement = null;
 981  0
     ResultSet rs = null;
 982  
     try {
 983  1
       switch (ec.getType()) {
 984  
         case CLASS:
 985  0
           findStatement = con.prepareStatement("SELECT ec1 FROM classes WHERE ec1 = ?");
 986  0
           findStatement.setInt(1, ec.getEc1());
 987  0
           break;
 988  
         case SUBCLASS:
 989  0
           findStatement = con.prepareStatement("SELECT ec1 FROM subclasses" +
 990  
                           " WHERE ec1 = ? AND ec2 = ?");
 991  0
           findStatement.setInt(1, ec.getEc1());
 992  0
           findStatement.setInt(2, ec.getEc2());
 993  0
           break;
 994  
         case SUBSUBCLASS:
 995  0
           findStatement = con.prepareStatement("SELECT ec1 FROM subsubclasses" +
 996  
                           " WHERE ec1 = ? AND ec2 = ? AND ec3 = ?");
 997  0
           findStatement.setInt(1, ec.getEc1());
 998  0
           findStatement.setInt(2, ec.getEc2());
 999  0
           findStatement.setInt(3, ec.getEc3());
 1000  0
           break;
 1001  
         case ENZYME:
 1002  0
           findStatement = con.prepareStatement("SELECT ec1 FROM enzymes " +
 1003  
                           "WHERE ec1 = ? AND ec2 = ? AND ec3 = ? AND ec4 = ? AND status != 'PM'");
 1004  0
           findStatement.setInt(1, ec.getEc1());
 1005  0
           findStatement.setInt(2, ec.getEc2());
 1006  0
           findStatement.setInt(3, ec.getEc3());
 1007  0
           findStatement.setInt(4, ec.getEc4());
 1008  0
           break;
 1009  
         case PRELIMINARY:
 1010  0
             findStatement = con.prepareStatement("SELECT ec1 FROM enzymes " +
 1011  
                               "WHERE ec1 = ? AND ec2 = ? AND ec3 = ? AND ec4 = ? AND status = 'PM'");
 1012  0
                       findStatement.setInt(1, ec.getEc1());
 1013  0
                       findStatement.setInt(2, ec.getEc2());
 1014  0
                       findStatement.setInt(3, ec.getEc3());
 1015  0
                       findStatement.setInt(4, ec.getEc4());
 1016  
           break;
 1017  
       }
 1018  0
       rs = findStatement.executeQuery();
 1019  0
       return rs.next();
 1020  
     } finally {
 1021  0
             if (rs != null) rs.close();
 1022  0
       if (findStatement != null) findStatement.close();
 1023  
     }
 1024  
   }
 1025  
 
 1026  
   /**
 1027  
    * Returns the history line of the enzyme with the given ID.
 1028  
    *
 1029  
    * @param id  The enzyme ID.
 1030  
    * @param con The logical connection.
 1031  
    * @return The history line, if found, otherwise <code>null</code>.
 1032  
    * @throws SQLException
 1033  
    */
 1034  
   public String findHistoryLine(Long id, Connection con) throws SQLException {
 1035  0
     String historyLine = "";
 1036  0
     PreparedStatement findHistoryLineStatement = null;
 1037  0
     ResultSet rs = null;
 1038  
     try {
 1039  0
       findHistoryLineStatement = con.prepareStatement(findHistoryLineStatement());
 1040  0
       findHistoryLineStatement.setLong(1, id.longValue());
 1041  0
       rs = findHistoryLineStatement.executeQuery();
 1042  
 
 1043  0
       while (rs.next()) {
 1044  0
         if (rs.getString("history") != null) historyLine = rs.getString("history");
 1045  
       }
 1046  
     } finally {
 1047  0
             if (rs != null) rs.close();
 1048  0
       if (findHistoryLineStatement != null) findHistoryLineStatement.close();
 1049  
     }
 1050  
 
 1051  0
     return historyLine;
 1052  
   }
 1053  
 
 1054  
   /**
 1055  
    * Returns the note of the enzyme with the given ID.
 1056  
    *
 1057  
    * @param id  The enzyme ID.
 1058  
    * @param con The logical connection.
 1059  
    * @return The note, if found, otherwise <code>null</code>.
 1060  
    * @throws SQLException
 1061  
    */
 1062  
   public String findNote(Long id, Connection con) throws SQLException {
 1063  0
     String note = "";
 1064  0
     PreparedStatement findNoteStatement = null;
 1065  0
     ResultSet rs = null;
 1066  
 
 1067  
     try {
 1068  0
       findNoteStatement = con.prepareStatement(findNoteStatement());
 1069  0
       findNoteStatement.setLong(1, id.longValue());
 1070  0
       rs = findNoteStatement.executeQuery();
 1071  
 
 1072  0
       while (rs.next()) {
 1073  0
         if (rs.getString("note") != null) note = rs.getString("note");
 1074  
       }
 1075  
     } finally {
 1076  0
             if (rs != null) rs.close();
 1077  0
         if (findNoteStatement != null) findNoteStatement.close();
 1078  
     }
 1079  
 
 1080  0
     return note;
 1081  
   }
 1082  
 
 1083  
   /**
 1084  
    * @param con The logical connection.
 1085  
    * @return
 1086  
    * @throws SQLException
 1087  
    */
 1088  
   public EnzymeStatistics findStats(Connection con) throws SQLException {
 1089  0
     PreparedStatement countIUBMBEnzymes = null, countClasses = null,
 1090  0
             countSubclasses = null, countSubSubclasses = null;
 1091  0
     EnzymeStatistics result = new EnzymeStatistics();
 1092  0
     ResultSet rs = null;
 1093  
 
 1094  0
     int allEnzymes = 0;
 1095  0
     int approved = 0;
 1096  0
     int proposed = 0;
 1097  0
     int suggested = 0;
 1098  0
     int approvedDeleted = 0;
 1099  0
     int proposedDeleted = 0;
 1100  0
     int suggestedDeleted = 0;
 1101  
 
 1102  
     try {
 1103  0
       countIUBMBEnzymes = con.prepareStatement(countIUBMBEnzymesStatement());
 1104  0
       rs = countIUBMBEnzymes.executeQuery();
 1105  0
       while (rs.next()) {
 1106  0
         allEnzymes++;
 1107  0
         if (rs.getString(1).equals("OK")) {
 1108  0
           approved++;
 1109  0
           if (rs.getString(2).equals("N")) approvedDeleted++;
 1110  
         }
 1111  0
         if (rs.getString(1).equals("PR")) {
 1112  0
           proposed++;
 1113  0
           if (rs.getString(2).equals("N")) proposedDeleted++;
 1114  
         }
 1115  0
         if (rs.getString(1).equals("SU")) {
 1116  0
           suggested++;
 1117  0
           if (rs.getString(2).equals("N")) suggestedDeleted++;
 1118  
         }
 1119  
       }
 1120  
 
 1121  0
       result.setAllEnzymes(allEnzymes);
 1122  0
       result.setApprovedEnzymes(approved);
 1123  0
       result.setApprovedDeletedEnzymes(approvedDeleted);
 1124  0
       result.setProposedEnzymes(proposed);
 1125  0
       result.setProposedDeletedEnzymes(proposedDeleted);
 1126  0
       result.setSuggestedEnzymes(suggested);
 1127  0
       result.setSuggestedDeletedEnzymes(suggestedDeleted);
 1128  
 
 1129  0
       countClasses = con.prepareStatement(countClassesStatement());
 1130  0
       rs = countClasses.executeQuery();
 1131  0
       if (rs.next()) {
 1132  0
         result.setClasses(rs.getInt(1));
 1133  
       }
 1134  
 
 1135  0
       countSubclasses = con.prepareStatement(countSubclassesStatement());
 1136  0
       rs = countSubclasses.executeQuery();
 1137  0
       if (rs.next()) {
 1138  0
         result.setSubclasses(rs.getInt(1));
 1139  
       }
 1140  
 
 1141  0
       countSubSubclasses = con.prepareStatement(countSubSubclassesStatement());
 1142  0
       rs = countSubSubclasses.executeQuery();
 1143  0
       if (rs.next()) {
 1144  0
         result.setSubsubclasses(rs.getInt(1));
 1145  
       }
 1146  
 
 1147  
     } finally {
 1148  0
             if (rs != null) rs.close();
 1149  0
       countIUBMBEnzymes.close();
 1150  0
       countClasses.close();
 1151  0
       countSubclasses.close();
 1152  0
       countSubSubclasses.close();
 1153  0
     }
 1154  
 
 1155  0
     return result;
 1156  
   }
 1157  
 
 1158  
 
 1159  
   /**
 1160  
    * Inserts the core data of a new enzyme entry.
 1161  
    * <p/>
 1162  
    * A new enzyme ID must already be available. It can be obtained via
 1163  
    * {@link EnzymeEntryMapper#findNextEnzymeId(java.sql.Connection)}
 1164  
    *
 1165  
    * @param enzymeId 
 1166  
    * @param ec 
 1167  
    * @param status 
 1168  
    * @param source
 1169  
    * @param isActive
 1170  
    * @param note 
 1171  
    * @param historyLine 
 1172  
    * @param con
 1173  
    * @throws java.sql.SQLException 
 1174  
    */
 1175  
   public void insert(Long enzymeId, EnzymeCommissionNumber ec, Status status,
 1176  
                      EnzymeSourceConstant source, String note, String historyLine,
 1177  
                      boolean isActive, Connection con)
 1178  
           throws SQLException {
 1179  15
     PreparedStatement insertStatement = null;
 1180  
     try {
 1181  15
       insertStatement = con.prepareStatement(insertStatement());
 1182  15
       insertStatement.setLong(1, enzymeId.longValue());
 1183  15
       insertStatement.setInt(2, ec.getEc1());
 1184  15
       insertStatement.setInt(3, ec.getEc2());
 1185  15
       insertStatement.setInt(4, ec.getEc3());
 1186  15
       insertStatement.setInt(5, ec.getEc4());
 1187  15
       insertStatement.setString(6, historyLine);
 1188  15
       insertStatement.setString(7, note);
 1189  15
       insertStatement.setString(8, status.getCode());
 1190  15
       insertStatement.setString(9, source.toString());
 1191  15
       insertStatement.setString(10, isActive ? "Y" : "N");
 1192  15
       insertStatement.execute();
 1193  
     } finally {
 1194  15
       insertStatement.close();
 1195  15
     }
 1196  15
   }
 1197  
 
 1198  
 
 1199  
   // ----------------- UPDATE PROCEDURES ----------------------
 1200  
 
 1201  
   /**
 1202  
    * Updates the core data of an enzyme stored in the table <b><code>ENZYMES</code></b>.
 1203  
    * <p/>
 1204  
    * <b>NOTE:</b> The <b><code>ACTIVE</code></b> column of this table is cannot be updated
 1205  
    * using this method since this column
 1206  
    * is managed by the event package.
 1207  
    *
 1208  
    * @param enzymeId    The enzyme's ID.
 1209  
    * @param ec          The EC number.
 1210  
    * @param status      The current status of the enzyme.
 1211  
    * @param source      The source of this enzyme.
 1212  
    * @param note        A note the curator might have added.
 1213  
    * @param historyLine The enzyme's history line.
 1214  
    * @param active The enzyme's active status.
 1215  
    * @param con         A database connection.
 1216  
    * @throws java.sql.SQLException 
 1217  
    */
 1218  
   public void update(Long enzymeId, EnzymeCommissionNumber ec, Status status,
 1219  
                   EnzymeSourceConstant source, String note, String historyLine,
 1220  
                   boolean active, Connection con)
 1221  
   throws SQLException {
 1222  0
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 1223  0
     if (ec == null) throw new NullPointerException("Parameter 'ec' must not be null.");
 1224  0
     if (status == null) throw new NullPointerException("Parameter 'status' must not be null.");
 1225  0
     if (source == null) throw new NullPointerException("Parameter 'source' must not be null.");
 1226  0
     if (note == null) throw new NullPointerException("Parameter 'note' must not be null.");
 1227  0
     if (historyLine == null) throw new NullPointerException("Parameter 'historyLine' must not be null.");
 1228  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 1229  
 
 1230  0
     PreparedStatement updateStatement = null;
 1231  
     try {
 1232  0
       updateStatement = con.prepareStatement(updateStatement());
 1233  0
       updateStatement.setInt(1, ec.getEc1());
 1234  0
       updateStatement.setInt(2, ec.getEc2());
 1235  0
       updateStatement.setInt(3, ec.getEc3());
 1236  0
       updateStatement.setInt(4, ec.getEc4());
 1237  0
       updateStatement.setString(5, status.getCode());
 1238  0
       updateStatement.setString(6, source.toString());
 1239  0
       updateStatement.setString(7, note);
 1240  0
       updateStatement.setString(8, historyLine);
 1241  0
       updateStatement.setString(9, active? "Y" : "N");
 1242  0
       updateStatement.setLong(10, enzymeId.longValue());
 1243  0
       updateStatement.execute();
 1244  
     } finally {
 1245  0
       updateStatement.close();
 1246  0
     }
 1247  0
   }
 1248  
 
 1249  
   /**
 1250  
    * Updates the EC of the given enzyme.
 1251  
    *
 1252  
    * @param enzymeId The enzyme's ID.
 1253  
    * @param newEc    The new EC.
 1254  
    * @param con      The logical connection.
 1255  
    * @throws SQLException
 1256  
    */
 1257  
   public void updateEc(Long enzymeId, EnzymeCommissionNumber newEc, Connection con) throws SQLException {
 1258  0
     PreparedStatement updateEcStatement = null;
 1259  
 
 1260  
     try {
 1261  0
       updateEcStatement = con.prepareStatement(updateEcStatement());
 1262  0
       updateEcStatement.setInt(1, newEc.getEc1());
 1263  0
       updateEcStatement.setInt(2, newEc.getEc2());
 1264  0
       updateEcStatement.setInt(3, newEc.getEc3());
 1265  0
       updateEcStatement.setInt(4, newEc.getEc4());
 1266  0
       updateEcStatement.setLong(5, enzymeId.longValue());
 1267  0
       updateEcStatement.execute();
 1268  
     } finally {
 1269  0
       updateEcStatement.close();
 1270  0
     }
 1271  0
   }
 1272  
 
 1273  
   /**
 1274  
    * Updates the notes of the given enzyme.
 1275  
    *
 1276  
    * @param enzymeId The enzyme's ID.
 1277  
    * @param note     The note.
 1278  
    * @param con      The logical connection.
 1279  
    * @throws SQLException
 1280  
    */
 1281  
   public void updateNote(Long enzymeId, String note, Connection con) throws SQLException {
 1282  0
     PreparedStatement updateNoteStatement = null;
 1283  
 
 1284  
     try {
 1285  0
       updateNoteStatement = con.prepareStatement(updateNoteStatement());
 1286  0
       updateNoteStatement.setString(1, note);
 1287  0
       updateNoteStatement.setLong(2, enzymeId.longValue());
 1288  0
       updateNoteStatement.execute();
 1289  
     } finally {
 1290  0
       updateNoteStatement.close();
 1291  0
     }
 1292  0
   }
 1293  
 
 1294  
   /**
 1295  
    * Updates the status of the given enzyme.
 1296  
    *
 1297  
    * @param enzymeId The enzyme's ID.
 1298  
    * @param status   The status.
 1299  
    * @param con      The logical connection.
 1300  
    * @throws SQLException
 1301  
    */
 1302  
   public void updateStatus(Long enzymeId, Status status, Connection con) throws SQLException {
 1303  0
     PreparedStatement updateStatusStatement = null;
 1304  
 
 1305  
     try {
 1306  0
       updateStatusStatement = con.prepareStatement(updateStatusStatement());
 1307  0
       updateStatusStatement.setString(1, status.getCode());
 1308  0
       updateStatusStatement.setLong(2, enzymeId.longValue());
 1309  0
       updateStatusStatement.execute();
 1310  
     } finally {
 1311  0
       updateStatusStatement.close();
 1312  0
     }
 1313  0
   }
 1314  
 
 1315  
   /**
 1316  
    * Updates the history line of the given enzyme.
 1317  
    *
 1318  
    * @param enzymeId    The enzyme's ID.
 1319  
    * @param historyLine The new history line.
 1320  
    * @param con         The logical connection.
 1321  
    * @throws SQLException
 1322  
    */
 1323  
   public void updateHistoryLine(Long enzymeId, String historyLine, Connection con) throws SQLException {
 1324  0
     PreparedStatement updateHistoryLineStatement = null;
 1325  
 
 1326  
     try {
 1327  0
       updateHistoryLineStatement = con.prepareStatement(updateHistoryLineStatement());
 1328  0
       updateHistoryLineStatement.setString(1, historyLine);
 1329  0
       updateHistoryLineStatement.setLong(2, enzymeId.longValue());
 1330  0
       updateHistoryLineStatement.execute();
 1331  
     } finally {
 1332  0
       updateHistoryLineStatement.close();
 1333  0
     }
 1334  0
   }
 1335  
 
 1336  
   /**
 1337  
    * Checks if a clone of an enzyme already exists.
 1338  
    *
 1339  
    * @param id  The enzyme ID.
 1340  
    * @param con The logical connection.
 1341  
    * @return <code>true</code>, if a clone already exists.
 1342  
    * @throws SQLException
 1343  
    */
 1344  
   public boolean cloneExists(Long id, Connection con) throws SQLException {
 1345  0
     EnzymeFutureMapper futureEventsMapper = new EnzymeFutureMapper();
 1346  0
     return futureEventsMapper.futureEventExists(id, con);
 1347  
   }
 1348  
 
 1349  
   // ------------------- PRIVATE METHODS ------------------------
 1350  
 
 1351  
   /**
 1352  
    * Creates the <code>EnzymeEntry</code> object from the given result set.
 1353  
    * <p/>
 1354  
    * This object includes information about its corresponding class, subclass and sub-subclass.
 1355  
    *
 1356  
    * @param rs The result set object.
 1357  
    * @return a <code>EnzymeEntry</code> instance containing the core data of an enzyme.
 1358  
    * @throws java.sql.SQLException if a database error occurs.
 1359  
    */
 1360  
   private EnzymeEntry doLoad(ResultSet rs) throws SQLException, EcException {
 1361  1
     Long enzymeId = null;
 1362  1
     int ec1 = 0;
 1363  1
     int ec2 = 0;
 1364  1
     int ec3 = 0;
 1365  1
     int ec4 = 0;
 1366  1
     String history = "";
 1367  1
     String note = "";
 1368  1
     String status = "";
 1369  1
     String source = "";
 1370  1
     String active = "";
 1371  1
     String className = "";
 1372  1
     String subclassName = "";
 1373  1
     String subSubclassName = "";
 1374  
 
 1375  1
     if (rs.getInt(1) > 0) enzymeId = new Long(rs.getLong(1));
 1376  1
     if (rs.getInt(2) > 0) ec1 = rs.getInt(2);
 1377  1
     if (rs.getInt(3) > 0) ec2 = rs.getInt(3);
 1378  1
     if (rs.getInt(4) > 0) ec3 = rs.getInt(4);
 1379  1
     if (rs.getInt(5) > 0) ec4 = rs.getInt(5);
 1380  1
     if (rs.getString(6) != null) history = rs.getString(6);
 1381  1
     if (rs.getString(7) != null) note = rs.getString(7);
 1382  1
     if (rs.getString(8) != null) status = rs.getString(8);
 1383  1
     if (rs.getString(9) != null) source = rs.getString(9);
 1384  1
     if (rs.getString(10) != null) active = rs.getString(10);
 1385  1
     if (rs.getString(11) != null) className = rs.getString(11);
 1386  1
     if (rs.getString(12) != null) subclassName = rs.getString(12);
 1387  1
     if (rs.getString(13) != null) subSubclassName = rs.getString(13);
 1388  
 
 1389  1
     EnzymeEntry enzymeEntry = new EnzymeEntry();
 1390  1
     Status st = Status.fromCode(status);
 1391  1
     enzymeEntry.setId(enzymeId);
 1392  1
     EnzymeCommissionNumber ec = EnzymeCommissionNumber.valueOf(
 1393  
                     ec1, ec2, ec3, ec4, st.equals(Status.PRELIMINARY));
 1394  1
     enzymeEntry.setEc(ec);
 1395  1
     enzymeEntry.getHistory().getRootNode().setHistoryLine(history);
 1396  1
     enzymeEntry.setNote(note);
 1397  1
         enzymeEntry.setStatus(st);
 1398  1
     enzymeEntry.setSource(EnzymeSourceConstant.valueOf(source));
 1399  1
     enzymeEntry.setActive(active.charAt(0) == 'Y');
 1400  1
     enzymeEntry.setSubSubclassName(subSubclassName);
 1401  1
     enzymeEntry.setSubclassName(subclassName);
 1402  1
     enzymeEntry.setClassName(className);
 1403  
 
 1404  1
     return enzymeEntry;
 1405  
   }
 1406  
 
 1407  
 
 1408  
   /**
 1409  
    * Creates the <code>EnzymeEntry</code> object from the given result set.
 1410  
    *
 1411  
    * @param rs The result set object.
 1412  
    * @return an <code>EnzymeEntry</code> instance.
 1413  
    * @throws java.sql.SQLException
 1414  
    */
 1415  
   private EnzymeEntry doLoadCore(ResultSet rs) throws SQLException, DomainException {
 1416  0
     Long enzymeId = null;
 1417  0
     int ec1 = 0;
 1418  0
     int ec2 = 0;
 1419  0
     int ec3 = 0;
 1420  0
     int ec4 = 0;
 1421  0
     String history = "";
 1422  0
     String note = "";
 1423  0
     String status = "";
 1424  0
     String source = "";
 1425  0
     String active = "";
 1426  
 
 1427  0
     if (rs.getInt(1) > 0) enzymeId = new Long(rs.getLong(1));
 1428  0
     if (rs.getInt(2) > 0) ec1 = rs.getInt(2);
 1429  0
     if (rs.getInt(3) > 0) ec2 = rs.getInt(3);
 1430  0
     if (rs.getInt(4) > 0) ec3 = rs.getInt(4);
 1431  0
     if (rs.getInt(5) > 0) ec4 = rs.getInt(5);
 1432  0
     if (rs.getString(6) != null) history = rs.getString(6);
 1433  0
     if (rs.getString(7) != null) note = rs.getString(7);
 1434  0
     if (rs.getString(8) != null) status = rs.getString(8);
 1435  0
     if (rs.getString(9) != null) source = rs.getString(9);
 1436  0
     if (rs.getString(10) != null) active = rs.getString(10);
 1437  
 
 1438  0
     EnzymeEntry enzymeEntry = new EnzymeEntry();
 1439  0
     enzymeEntry.setId(enzymeId);
 1440  0
     Status st = Status.fromCode(status);
 1441  0
     EnzymeCommissionNumber ec = EnzymeCommissionNumber.valueOf(
 1442  
                     ec1, ec2, ec3, ec4, st.equals(Status.PRELIMINARY));
 1443  0
     enzymeEntry.setEc(ec);
 1444  0
     enzymeEntry.getHistory().getRootNode().setHistoryLine(history);
 1445  0
     enzymeEntry.setNote(note);
 1446  0
         enzymeEntry.setStatus(st);
 1447  0
     enzymeEntry.setSource(EnzymeSourceConstant.valueOf(source));
 1448  0
     enzymeEntry.setActive(active.charAt(0) == 'Y');
 1449  
 
 1450  0
     return enzymeEntry;
 1451  
   }
 1452  
 
 1453  
   /**
 1454  
    * Checks the list of loaded names and stores them in an entry object according to its type
 1455  
    * (common, systematic or other name).
 1456  
    *
 1457  
    * @param entry The entry where the names will be stored.
 1458  
    * @param names The list of names.
 1459  
    */
 1460  
   private void doLoadNames(EnzymeEntry entry, List<EnzymeName> names) throws DomainException {
 1461  14
     for (int iii = 0; iii < names.size(); iii++) {
 1462  13
       EnzymeName enzymeName = names.get(iii);
 1463  13
       if (enzymeName.getType().equals(EnzymeNameTypeConstant.COMMON_NAME)) {
 1464  1
         entry.addCommonName(enzymeName);
 1465  
       }
 1466  13
       if (enzymeName.getType().equals(EnzymeNameTypeConstant.SYSTEMATIC_NAME)) {
 1467  1
         entry.setSystematicName(enzymeName);
 1468  
       }
 1469  13
       if (enzymeName.getType().equals(EnzymeNameTypeConstant.OTHER_NAME)) {
 1470  11
         entry.addSynonym(enzymeName);
 1471  
       }
 1472  
     }
 1473  1
   }
 1474  
 
 1475  
   /**
 1476  
    * Creates the <code>EnzymeEntry</code> ghost (light-weight) object from the given result set.
 1477  
    *
 1478  
    * @param rs The result set object.
 1479  
    * @return an <code>EnzymeEntry</code> instance.
 1480  
    * @throws java.sql.SQLException
 1481  
    */
 1482  
   private EnzymeEntry doLoadGhost(ResultSet rs) throws SQLException, DomainException {
 1483  0
     Long enzymeId = null;
 1484  0
     int ec1 = 0;
 1485  0
     int ec2 = 0;
 1486  0
     int ec3 = 0;
 1487  0
     int ec4 = 0;
 1488  0
     String status = "";
 1489  0
     String source = "";
 1490  0
     String active = "";
 1491  0
     String history = "";
 1492  
 
 1493  0
     if (rs.getInt("enzyme_id") > 0) enzymeId = new Long(rs.getLong("enzyme_id"));
 1494  0
     if (rs.getInt("ec1") > 0) ec1 = rs.getInt("ec1");
 1495  0
     if (rs.getInt("ec2") > 0) ec2 = rs.getInt("ec2");
 1496  0
     if (rs.getInt("ec3") > 0) ec3 = rs.getInt("ec3");
 1497  0
     if (rs.getInt("ec4") > 0) ec4 = rs.getInt("ec4");
 1498  0
     if (rs.getString("history") != null) history = rs.getString("history");
 1499  0
     if (rs.getString("status") != null) status = rs.getString("status");
 1500  0
     if (rs.getString("source") != null) source = rs.getString("source");
 1501  0
     if (rs.getString("active") != null) active = rs.getString("active");
 1502  
 
 1503  0
     EnzymeEntry enzymeEntry = new EnzymeEntry();
 1504  0
         Status st = Status.fromCode(status);
 1505  0
     enzymeEntry.setId(enzymeId);
 1506  0
     EnzymeCommissionNumber ec = EnzymeCommissionNumber.valueOf(
 1507  
                     ec1, ec2, ec3, ec4, st.equals(Status.PRELIMINARY));
 1508  0
     enzymeEntry.setEc(ec);
 1509  0
     enzymeEntry.getHistory().getRootNode().setHistoryLine(history);
 1510  0
         enzymeEntry.setStatus(st);
 1511  0
     if (!source.equals("")) enzymeEntry.setSource(EnzymeSourceConstant.valueOf(source));
 1512  0
     if (!active.equals("")) enzymeEntry.setActive(active.charAt(0) == 'Y');
 1513  0
     enzymeEntry.setGhost(true);
 1514  0
     return enzymeEntry;
 1515  
   }
 1516  
 
 1517  
   /**
 1518  
    * Loads the enzyme data except for the core information (see <code>findBy</code> methods).
 1519  
    *
 1520  
    * @param enzymeEntry The <code>EnzymeEntry</code> instance to be populated.
 1521  
    * @param con         The logical connection.
 1522  
    * @throws SQLException if a generic database error occurs.
 1523  
    * @throws MapperException in case of problem retrieving reaction/cofactor data.
 1524  
    */
 1525  
   private void findEnzymeData(EnzymeEntry enzymeEntry, Connection con)
 1526  
   throws SQLException, DomainException, EnzymeReactionException,
 1527  
                   EnzymeReferenceException, MapperException {
 1528  1
     loadHistoryGraph(enzymeEntry, con);
 1529  
 
 1530  
     // Names
 1531  1
     EnzymeNameMapper nameMapper = new EnzymeNameMapper();
 1532  1
     List<EnzymeName> names = nameMapper.find(enzymeEntry.getId(), con);
 1533  1
     if (names != null) doLoadNames(enzymeEntry, names);
 1534  
 
 1535  
     // Reaction
 1536  1
     EnzymaticReactions reactions =
 1537  
                     enzymeReactionMapper.find(enzymeEntry.getId(), con);
 1538  1
     if (reactions != null) enzymeEntry.setEnzymaticReactions(reactions);
 1539  
 
 1540  
     // Cofactors
 1541  1
     EnzymeCofactorMapper cofactorMapper = new EnzymeCofactorMapper();
 1542  1
     Set<Object> cofactors = cofactorMapper.find(enzymeEntry.getId(), con);
 1543  1
     cofactorMapper.close();
 1544  1
     if (cofactors != null) enzymeEntry.setCofactors(cofactors);
 1545  
 
 1546  
     // Links
 1547  1
     EnzymeLinkMapper linkMapper = new EnzymeLinkMapper();
 1548  1
     List<EnzymeLink> links = linkMapper.find(enzymeEntry.getId(), con);
 1549  1
     if (links != null) {
 1550  1
                 enzymeEntry.setLinks(new TreeSet<EnzymeLink>(links));
 1551  
         }
 1552  
 
 1553  
     // Comments
 1554  1
     EnzymeCommentMapper commentMapper = new EnzymeCommentMapper();
 1555  1
     List<EnzymeComment> comments = commentMapper.find(enzymeEntry.getId(), con);
 1556  1
     if (comments != null) enzymeEntry.setComments(comments);
 1557  
 
 1558  
     // References
 1559  1
     EnzymeReferenceMapper referenceMapper = new EnzymeReferenceMapper();
 1560  1
     List<Reference> references = referenceMapper.find(enzymeEntry.getId(), con);
 1561  1
     if (references != null) enzymeEntry.setReferences(references);
 1562  1
   }
 1563  
 
 1564  
   /**
 1565  
    * Exports all data available for this ENZYME entry.
 1566  
    * <p/>
 1567  
    * Affected table rows will be locked.
 1568  
    *
 1569  
    * @param enzymeEntry The instance storing the data to be gathered.
 1570  
    * @param con         The logical connection.
 1571  
    * @throws SQLException            if a database error occurs.
 1572  
    * @throws DomainException         if a domain related error occurs.
 1573  
    * @throws EnzymeReactionException if no reaction has been found; one reaction is mandatory.
 1574  
  * @throws MapperException in case of a problem getting reaction/cofactor info.
 1575  
    */
 1576  
   private void exportSIBEnzymeData(EnzymeEntry enzymeEntry, Connection con)
 1577  
   throws SQLException, DomainException,
 1578  
           EnzymeReactionException, MapperException {
 1579  0
     assert enzymeEntry != null : "Parameter 'enzymeEntry' must not be null.";
 1580  0
     assert con != null : "Parameter 'con' must not be null.";
 1581  
 
 1582  0
     loadHistoryGraph(enzymeEntry, con);
 1583  
     
 1584  
     // Names
 1585  0
     EnzymeNameMapper nameMapper = new EnzymeNameMapper();
 1586  0
     List<EnzymeName> names = nameMapper.exportSibNames(enzymeEntry.getId(), con);
 1587  0
     if (names != null) doLoadNames(enzymeEntry, names);
 1588  
 
 1589  
     // Reaction
 1590  0
     EnzymaticReactions reactions =
 1591  
                     enzymeReactionMapper.exportSibReactions(enzymeEntry.getId(), con);
 1592  0
     if (reactions != null) enzymeEntry.setEnzymaticReactions(reactions);
 1593  
 
 1594  
     // Cofactors
 1595  0
     EnzymeCofactorMapper cofactorMapper = new EnzymeCofactorMapper();
 1596  0
     Set<Object> cofactors = cofactorMapper.exportSibCofactors(enzymeEntry.getId(), con);
 1597  0
     cofactorMapper.close();
 1598  0
     if (cofactors != null) enzymeEntry.setCofactors(cofactors);
 1599  
 
 1600  
     // Links
 1601  0
     EnzymeLinkMapper linkMapper = new EnzymeLinkMapper();
 1602  0
     List<EnzymeLink> links = linkMapper.exportSibLinks(enzymeEntry.getId(), con);
 1603  0
     if (links != null)
 1604  0
       enzymeEntry.setLinks(new TreeSet<EnzymeLink>(links));
 1605  
 
 1606  
     // Comments
 1607  0
     EnzymeCommentMapper commentMapper = new EnzymeCommentMapper();
 1608  0
     List<EnzymeComment> comments = commentMapper.exportSibComments(enzymeEntry.getId(), con);
 1609  0
     if (comments != null) enzymeEntry.setComments(comments);
 1610  0
   }
 1611  
 
 1612  
   /**
 1613  
    * Returns the next available enzyme ID.
 1614  
    *
 1615  
    * @param con The connection.
 1616  
    * @return The next enzyme ID.
 1617  
    * @throws SQLException
 1618  
    */
 1619  
   public Long findNextEnzymeId(Connection con) throws SQLException {
 1620  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 1621  0
     long enzymeId = 0;
 1622  0
     PreparedStatement findNextEnzymeId = null;
 1623  
 
 1624  
     try {
 1625  0
       findNextEnzymeId = con.prepareStatement(findNextEnzymeIdStatement());
 1626  0
       ResultSet rs = findNextEnzymeId.executeQuery();
 1627  0
       if (rs.next()) {
 1628  0
         enzymeId = rs.getLong(1);
 1629  
       }
 1630  
     } finally {
 1631  0
       findNextEnzymeId.close();
 1632  0
     }
 1633  
 
 1634  0
     return new Long(enzymeId);
 1635  
   }
 1636  
 
 1637  
         private void loadHistoryGraph(EnzymeEntry enzymeEntry, Connection con)
 1638  
     throws SQLException, DomainException {
 1639  1
                 HistoryGraph historyGraph = getHistoryGraph(con, enzymeEntry);
 1640  1
             if (historyGraph != null) enzymeEntry.setHistory(historyGraph);
 1641  1
         }
 1642  
 
 1643  
 }