Coverage Report - uk.ac.ebi.intenz.mapper.EnzymeSubSubclassMapper
 
Classes in this File Line Coverage Branch Coverage Complexity
EnzymeSubSubclassMapper
0%
0/104
0%
0/74
3.833
 
 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.EnzymeEntry;
 13  
 import uk.ac.ebi.intenz.domain.enzyme.EnzymeSubSubclass;
 14  
 import uk.ac.ebi.intenz.domain.exceptions.DomainException;
 15  
 
 16  
 /**
 17  
  * Maps enzyme Sub-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  0
 public class EnzymeSubSubclassMapper {
 23  
 
 24  
   private static final String COLUMNS = "s1.ec1, s1.ec2, s1.ec3, s1.name, s2.name, s3.name, s1.description";
 25  
 
 26  
   private static final String SELECT_ALL = "SELECT " + COLUMNS
 27  
       + " FROM subsubclasses s1, subclasses s2, classes s3";
 28  
   
 29  0
   public EnzymeSubSubclassMapper() {
 30  0
   }
 31  
 
 32  
   private String findStatement() {
 33  0
     return "SELECT " + COLUMNS +
 34  
            " FROM subsubclasses s1, subclasses s2, classes s3 WHERE s3.ec1 = ? AND s2.ec1 = ? AND s2.ec2 = ? AND s1.ec1 = ? AND s1.ec2 = ? AND s1.ec3 = ?" +
 35  
            " ORDER BY s1.ec1, s1.ec2, s1.ec3";
 36  
   }
 37  
 
 38  
   private String findListStatement() {
 39  0
     return "SELECT " + COLUMNS +
 40  
            " FROM subsubclasses s1, subclasses s2, classes s3 WHERE s3.ec1 = ? AND s2.ec1 = ? AND s2.ec2 = ? AND s1.ec1 = ? AND s1.ec2 = ?" +
 41  
            " ORDER BY s1.ec1, s1.ec2, s1.ec3";
 42  
   }
 43  
 
 44  
   private String findSubSubclassOnly() {
 45  0
     return "SELECT ec1 FROM subsubclasses WHERE ec1 = ? AND ec2 = ? AND ec3 = ?";
 46  
   }
 47  
 
 48  
   private String insertStatement() {
 49  0
     return "INSERT INTO subsubclasses (ec1, ec2, ec3, name, description, active ) VALUES (?, ?, ?, ?, ?, ?)";
 50  
   }
 51  
 
 52  
   /**
 53  
    * Tries to find Sub-subclass information about an enzyme.
 54  
    *
 55  
    * @param ec1 Number of class to search for.
 56  
    * @param ec2 Number of subclass to search for.
 57  
    * @param ec3 Number of sub-subclass to search for.
 58  
    * @param con The logical connection.
 59  
    * @return an <code>EnzymeClass</code> instance or <code>null</code> if nothing has been found.
 60  
    * @throws NullPointerException if any of the parameters is <code>null</code>.
 61  
    * @throws SQLException         if a database error occurs.
 62  
    * @throws DomainException      if any error related to domain information occurs.
 63  
    */
 64  
   public EnzymeSubSubclass find(int ec1, int ec2, int ec3, Connection con)
 65  
   throws SQLException, DomainException {
 66  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 67  
 
 68  0
     PreparedStatement findStatement = null;
 69  0
     ResultSet rs = null;
 70  0
     EnzymeSubSubclass result = null;
 71  
     List<EnzymeEntry> entries;
 72  
 
 73  
     // Get entries.
 74  0
     EnzymeEntryMapper enzymeEntryMapper = new EnzymeEntryMapper();
 75  0
     entries = enzymeEntryMapper.findAllSubSubclassEntriesByEc(ec1, ec2, ec3, con);
 76  0
     if(entries == null) entries = new ArrayList<EnzymeEntry>();
 77  
 
 78  
     try {
 79  0
       findStatement = con.prepareStatement(findStatement());
 80  0
       findStatement.setInt(1, ec1);
 81  0
       findStatement.setInt(2, ec1);
 82  0
       findStatement.setInt(3, ec2);
 83  0
       findStatement.setInt(4, ec1);
 84  0
       findStatement.setInt(5, ec2);
 85  0
       findStatement.setInt(6, ec3);
 86  0
       rs = findStatement.executeQuery();
 87  0
       if (rs.next()) {
 88  0
         result = doLoad(rs, entries);
 89  
       }
 90  
     } finally {
 91  0
             enzymeEntryMapper.close();
 92  0
             if (rs != null) rs.close();
 93  0
       if (findStatement != null) findStatement.close();
 94  
     }
 95  
 
 96  0
     return result;
 97  
   }
 98  
 
 99  
   /**
 100  
    * Tries to find all Sub-subclasses requested.
 101  
    *
 102  
    * @param ec1 Number/Wildcard of class to search for.
 103  
    * @param ec2 Number/Wildcard of subclass to search for.
 104  
    * @param con The logical connection.
 105  
    * @return a <code>Vector</code> of <code>SubSubClass</code> instances or <code>null</code> if nothing has been found.
 106  
    * @throws SQLException
 107  
    */
 108  
   public List<EnzymeSubSubclass> findList(String ec1, String ec2, Connection con)
 109  
   throws SQLException, DomainException {
 110  0
     PreparedStatement findListStatement = null;
 111  0
     ResultSet rs = null;
 112  0
     List<EnzymeSubSubclass> result = new ArrayList<EnzymeSubSubclass>();
 113  0
     boolean noResult = true;
 114  
 
 115  
     try {
 116  0
       findListStatement = con.prepareStatement(findListStatement());
 117  0
       findListStatement.setString(1, ec1);
 118  0
       findListStatement.setString(2, ec1);
 119  0
       findListStatement.setString(3, ec2);
 120  0
       findListStatement.setString(4, ec1);
 121  0
       findListStatement.setString(5, ec2);
 122  0
       rs = findListStatement.executeQuery();
 123  
 
 124  0
       while (rs.next()) {
 125  0
         noResult = false;
 126  0
         result.add(doLoad(rs, null));
 127  
       }
 128  
     } finally {
 129  0
             if (rs != null) rs.close();
 130  0
       if (findListStatement != null) findListStatement.close();
 131  
     }
 132  
 
 133  0
     if (noResult) return null;
 134  0
     return result;
 135  
   }
 136  
   
 137  
   public List<EnzymeSubSubclass> findAll(Connection con) throws SQLException, DomainException{
 138  0
       List<EnzymeSubSubclass> result = new ArrayList<EnzymeSubSubclass>();
 139  0
       Statement stm = null;
 140  0
       ResultSet rs = null;
 141  
       try {
 142  0
           stm = con.createStatement();
 143  0
           rs = stm.executeQuery(SELECT_ALL);
 144  0
           while (rs.next()){
 145  0
               result.add(doLoad(rs, null));
 146  
           }
 147  
       } finally {
 148  0
               if (rs != null) rs.close();
 149  0
           if (stm != null) stm.close();
 150  
       }
 151  0
       return result;
 152  
   }
 153  
 
 154  
   /**
 155  
    * Creates a new row in the <code>SUBSUBCLASSES</code> table.
 156  
    *
 157  
    * @param ec EC number of this sub-subclass.
 158  
    * @param name The name of the sub-subclass.
 159  
    * @param description The description of the sub-subclass.
 160  
    * @param con The database connection.
 161  
    * @throws SQLException if a database error occurs.
 162  
    * @throws NullPointerException if any of the parameters is <code>null</code>.
 163  
    */
 164  
   public synchronized void insertSubSubclass(EnzymeCommissionNumber ec, String name, String description, Connection con) throws SQLException {
 165  0
     if (ec == null) throw new NullPointerException("Parameter 'ec' must not be null.");
 166  0
     if (name == null) throw new NullPointerException("Parameter 'name' must not be null.");
 167  0
     if (description == null) throw new NullPointerException("Parameter 'description' must not be null.");
 168  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 169  
 
 170  0
     PreparedStatement insertStatement = null;
 171  
     try {
 172  0
       insertStatement = con.prepareStatement(insertStatement());
 173  0
       doInsert(ec, name, description, insertStatement);
 174  0
       insertStatement.execute();
 175  
 //      con.commit();
 176  
 //    } catch (SQLException e) {
 177  
 //      con.rollback();
 178  
 //      throw e;
 179  
     } finally {
 180  0
       if (insertStatement != null) insertStatement.close();
 181  
     }
 182  0
   }
 183  
 
 184  
   /**
 185  
    * Checks if the given sub-subclass numbers are valid.
 186  
    *
 187  
    * @param ec1 The class number.
 188  
    * @param ec2 The subclass number.
 189  
    * @param ec3 The sub-subclass number.
 190  
    * @param con The connection.
 191  
    * @return <code>true</code> if the class exists.
 192  
    * @throws SQLException
 193  
    */
 194  
   public boolean subSubclassExists(String ec1, String ec2, String ec3, Connection con) throws SQLException {
 195  0
     PreparedStatement findStatement = null;
 196  0
     ResultSet rs = null;
 197  
 
 198  
     try {
 199  0
       findStatement = con.prepareStatement(findSubSubclassOnly());
 200  0
       findStatement.setString(1, ec1);
 201  0
       findStatement.setString(2, ec2);
 202  0
       findStatement.setString(3, ec3);
 203  0
       rs = findStatement.executeQuery();
 204  0
       if (rs.next()) {
 205  0
         return true;
 206  
       }
 207  
     } finally {
 208  0
             if (rs != null) rs.close();
 209  0
       if (findStatement != null) findStatement.close();
 210  
     }
 211  
 
 212  0
     return false;
 213  
   }
 214  
 
 215  
   
 216  
   // ------------------- PRIVATE METHODS ------------------------
 217  
 
 218  
   /**
 219  
    * Creates the <code>EnzymeSubSubclass</code> object from the given result set.
 220  
    *
 221  
    * @param rs The result set object.
 222  
    * @return an <code>EnzymeSubSubclass</code> instance.
 223  
    * @throws SQLException
 224  
    */
 225  
   private EnzymeSubSubclass doLoad(ResultSet rs, List<EnzymeEntry> entries)
 226  
   throws SQLException, DomainException {
 227  0
     int ec1 = 0;
 228  0
     int ec2 = 0;
 229  0
     int ec3 = 0;
 230  0
     String name = "";
 231  0
     String subclassName = "";
 232  0
     String className = "";
 233  0
     String description = "";
 234  
 
 235  0
     if (rs.getInt(1) > 0) ec1 = rs.getInt(1);
 236  0
     if (rs.getInt(2) > 0) ec2 = rs.getInt(2);
 237  0
     if (rs.getInt(3) > 0) ec3 = rs.getInt(3);
 238  0
     if (rs.getString(4) != null) name = rs.getString(4);
 239  0
     if (rs.getString(5) != null) subclassName = rs.getString(5);
 240  0
     if (rs.getString(6) != null) className = rs.getString(6);
 241  0
     if (rs.getString(7) != null) description = rs.getString(7);
 242  
 
 243  0
     return new EnzymeSubSubclass(EnzymeCommissionNumber.valueOf(ec1, ec2, ec3),
 244  
                                  className, subclassName, name, description, entries);
 245  
   }
 246  
 
 247  
   private void doInsert(EnzymeCommissionNumber ec, String name, String description, PreparedStatement insertStatement) throws SQLException {
 248  0
     assert ec != null : "Parameter 'ec' must not be null.";
 249  0
     assert name != null : "Parameter 'name' must not be null.";
 250  0
     assert description != null : "Parameter 'description' must not be null.";
 251  0
     assert insertStatement != null : "Parameter 'insertStatement' must not be null.";
 252  
 
 253  0
     insertStatement.setInt(1, ec.getEc1());
 254  0
     insertStatement.setInt(2, ec.getEc2());
 255  0
     insertStatement.setInt(3, ec.getEc3());
 256  0
     insertStatement.setString(4, name);
 257  0
     insertStatement.setString(5, description);
 258  0
     insertStatement.setString(6, "Y");
 259  0
   }
 260  
 }