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