Coverage Report - uk.ac.ebi.intenz.mapper.EnzymeClassMapper
 
Classes in this File Line Coverage Branch Coverage Complexity
EnzymeClassMapper
0%
0/64
0%
0/46
4.25
 
 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  
 
 11  
 import org.apache.log4j.Logger;
 12  
 
 13  
 import uk.ac.ebi.intenz.domain.enzyme.EnzymeClass;
 14  
 import uk.ac.ebi.intenz.domain.enzyme.EnzymeCommissionNumber;
 15  
 import uk.ac.ebi.intenz.domain.enzyme.EnzymeSubclass;
 16  
 import uk.ac.ebi.intenz.domain.exceptions.DomainException;
 17  
 import uk.ac.ebi.intenz.domain.exceptions.EcException;
 18  
 
 19  
 /**
 20  
  * Maps enzyme class information to the corresponding database tables.
 21  
  *
 22  
  * @author Michael Darsow
 23  
  * @version $Revision: 1.3 $ $Date: 2009/05/26 14:59:09 $
 24  
  */
 25  0
 public class EnzymeClassMapper {
 26  
 
 27  
   private static final String COLUMNS = "ec1, name, description";
 28  
   
 29  
   private static final String SELECT_ALL = "SELECT " + COLUMNS + " FROM classes";
 30  
 
 31  0
   private static final Logger LOGGER =
 32  
           Logger.getLogger(EnzymeClassMapper.class.getName());
 33  
 
 34  0
   public EnzymeClassMapper() {
 35  0
   }
 36  
 
 37  
   private String findStatement() {
 38  0
     return "SELECT " + COLUMNS + " FROM classes WHERE ec1 = ?";
 39  
   }
 40  
 
 41  
   private String findNumberOfClassesStatement() {
 42  0
     return "SELECT count(*) AS count FROM classes";
 43  
   }
 44  
 
 45  
   /**
 46  
    * Tries to find Class information about an enzyme.
 47  
    *
 48  
    * @param ec1 Number of class to search for.
 49  
    * @param con The logical connection.
 50  
    * @return an <code>EnzymeClass</code> instance or <code>null</code> if nothing has been found.
 51  
    * @throws NullPointerException if any of the parameters is <code>null</code>.
 52  
    * @throws SQLException         if a database error occurs.
 53  
    * @throws DomainException      if any error related to domain information occurs (e.g. <code>ec1</code> is invalid).
 54  
    */
 55  
   public EnzymeClass find(String ec1, Connection con) throws SQLException, DomainException {
 56  0
     if (ec1 == null) throw new NullPointerException("Parameter 'ec1' must not be null.");
 57  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 58  0
     PreparedStatement findStatement = null;
 59  0
     ResultSet rs = null;
 60  0
     EnzymeClass result = null;
 61  
     List<EnzymeSubclass> subclasses;
 62  
 
 63  
     // Get subclasses.
 64  0
     EnzymeSubclassMapper subclassMapper = new EnzymeSubclassMapper();
 65  0
     subclasses = subclassMapper.findList(ec1, con);
 66  
 
 67  
     try {
 68  0
       findStatement = con.prepareStatement(findStatement());
 69  0
       findStatement.setString(1, ec1);
 70  0
       rs = findStatement.executeQuery();
 71  0
       if (rs.next()) {
 72  0
         result = doLoad(rs, subclasses);
 73  
       } else {
 74  0
         LOGGER.warn("No class information found for EC " + ec1);
 75  
       }
 76  
     } finally {
 77  0
             if (rs != null) rs.close();
 78  0
       if (findStatement != null) findStatement.close();
 79  
     }
 80  
 
 81  0
     return result;
 82  
   }
 83  
   
 84  
   /**
 85  
    * 
 86  
    * @param con
 87  
    * @return A <code>List</code> of all the <code>EnzymeClass</code>es
 88  
    *    (without subclass information)
 89  
  * @throws SQLException 
 90  
  * @throws EcException 
 91  
    */
 92  
   public List<EnzymeClass> findAll(Connection con) throws SQLException, DomainException{
 93  0
       List<EnzymeClass> result = new ArrayList<EnzymeClass>();
 94  0
       Statement stm = null;
 95  0
       ResultSet rs = null;
 96  
       try {
 97  0
           stm = con.createStatement();
 98  0
           rs = stm.executeQuery("SELECT ec1 FROM classes");
 99  0
           while (rs.next()){
 100  0
               result.add(find(rs.getString(1), con));
 101  
           }
 102  
       } finally {
 103  0
               if (rs != null) rs.close();
 104  0
           if (stm != null) stm.close();
 105  
       }
 106  0
       return result;
 107  
   }
 108  
 
 109  
   /**
 110  
    * Finds the number of classes.
 111  
    *
 112  
    * @param con The logical connection.
 113  
    * @return the number of classes.
 114  
    * @throws SQLException
 115  
    */
 116  
   public int findNumberOfClasses(Connection con) throws SQLException {
 117  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 118  0
     PreparedStatement findStatement = null;
 119  0
     ResultSet rs = null;
 120  0
     int number = 0;
 121  
 
 122  
     try {
 123  0
       findStatement = con.prepareStatement(findNumberOfClassesStatement());
 124  0
       rs = findStatement.executeQuery();
 125  0
       if (rs.next()) {
 126  0
         number = Integer.parseInt(rs.getString("count"));
 127  
       }
 128  
     } finally {
 129  0
               if (rs != null) rs.close();
 130  0
               if (findStatement != null) findStatement.close();
 131  
     }
 132  
 
 133  0
     return number;
 134  
   }
 135  
 
 136  
   /**
 137  
    * Checks if the given class number is valid.
 138  
    *
 139  
    * @param ec1 The class number.
 140  
    * @param con The connection.
 141  
    * @return <code>true</code> if the class exists.
 142  
    * @throws SQLException
 143  
    */
 144  
   public boolean classExists(String ec1, Connection con) throws SQLException {
 145  0
     if (ec1 == null) throw new NullPointerException("Parameter 'ec1' must not be null.");
 146  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 147  0
     PreparedStatement findStatement = null;
 148  0
     ResultSet rs = null;
 149  
     try {
 150  0
       findStatement = con.prepareStatement(findStatement());
 151  0
       findStatement.setString(1, ec1);
 152  0
       rs = findStatement.executeQuery();
 153  0
       if (rs.next()) {
 154  0
         return true;
 155  
       }
 156  
     } finally {
 157  0
               if (rs != null) rs.close();
 158  0
               if (findStatement != null) findStatement.close();
 159  
     }
 160  
 
 161  0
     return false;
 162  
   }
 163  
 
 164  
   // ------------------- PRIVATE METHODS ------------------------
 165  
 
 166  
   /**
 167  
    * Creates the <code>EnzymeClass</code> object.
 168  
    *
 169  
    * @param rs The result set object.
 170  
    * @return an <code>EnzymeClass</code> instance.
 171  
    * @throws SQLException
 172  
    */
 173  
   private EnzymeClass doLoad(ResultSet rs, List<EnzymeSubclass> subclasses)
 174  
   throws SQLException, EcException {
 175  0
     assert rs != null;
 176  
     
 177  0
     int ec1 = 0;
 178  0
     String name = "";
 179  0
     String description = "";
 180  
 
 181  0
     if (rs.getInt("ec1") > 0) ec1 = rs.getInt("ec1");
 182  0
     if (rs.getString("name") != null) name = rs.getString("name");
 183  0
     if (rs.getString("description") != null) description = rs.getString("description");
 184  
 
 185  0
     EnzymeCommissionNumber ec = EnzymeCommissionNumber.valueOf(ec1);
 186  0
     return EnzymeClass.valueOf(ec, name, description, subclasses);
 187  
   }
 188  
 }