View Javadoc

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    public EnzymeSubclassMapper() {
29    }
30  
31    private String findStatement() {
32      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      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      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      if (ec1 == null) throw new NullPointerException("Parameter 'ec1' must not be null.");
56      if (ec2 == null) throw new NullPointerException("Parameter 'ec2' must not be null.");
57      if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
58  
59      PreparedStatement findStatement = null;
60      ResultSet rs = null;
61      EnzymeSubclass result = null;
62      List<EnzymeSubSubclass> subSubclasses;
63  
64      // Get sub-subclasses.
65      EnzymeSubSubclassMapper subSubclassMapper = new EnzymeSubSubclassMapper();
66      subSubclasses = subSubclassMapper.findList(ec1, ec2, con);
67  
68      try {
69        findStatement = con.prepareStatement(findStatement());
70        findStatement.setString(1, ec1);
71        findStatement.setString(2, ec1);
72        findStatement.setString(3, ec2);
73        rs = findStatement.executeQuery();
74        if (rs.next()) {
75          result = doLoad(rs, subSubclasses);
76        }
77      } finally {
78      	if (rs != null) rs.close();
79        if (findStatement != null) findStatement.close();
80      }
81  
82      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      PreparedStatement findListStatement = null;
95      ResultSet rs = null;
96      List<EnzymeSubclass> result = new ArrayList<EnzymeSubclass>();
97      boolean noResult = true;
98  
99      try {
100       findListStatement = con.prepareStatement(findListStatement());
101       findListStatement.setString(1, ec1);
102       findListStatement.setString(2, ec1);
103       rs = findListStatement.executeQuery();
104       while (rs.next()) {
105         noResult = false;
106         result.add(doLoad(rs, null));
107       }
108     } finally {
109     	if (rs != null) rs.close();
110       if (findListStatement != null) findListStatement.close();
111     }
112 
113     if (noResult) return null;
114     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       List<EnzymeSubclass> result = new ArrayList<EnzymeSubclass>();
127       Statement stm = null;
128       ResultSet rs = null;
129       try {
130           stm = con.createStatement();
131           rs = stm.executeQuery(SELECT_ALL);
132           while (rs.next()){
133               result.add(doLoad(rs, null));
134           }
135       } finally {
136       	if (rs != null) rs.close();
137           if (stm != null) stm.close();
138       }
139       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     PreparedStatement findStatement = null;
153     ResultSet rs = null;
154     try {
155       findStatement = con.prepareStatement(findSubclassOnlyStatement());
156       findStatement.setString(1, ec1);
157       findStatement.setString(2, ec2);
158       rs = findStatement.executeQuery();
159       if (rs.next()) {
160         return true;
161       }
162     } finally {
163     	if (rs != null) rs.close();
164       if (findStatement != null) findStatement.close();
165     }
166 
167     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     int ec1 = 0;
183     int ec2 = 0;
184     String className = "";
185     String name = "";
186     String description = "";
187 
188     if (rs.getInt(1) > 0) ec1 = rs.getInt(1);
189     if (rs.getInt(2) > 0) ec2 = rs.getInt(2);
190     if (rs.getString(3) != null) name = rs.getString(3);
191     if (rs.getString(4) != null) className = rs.getString(4);
192     if (rs.getString(5) != null) description = rs.getString(5);
193 
194     return new EnzymeSubclass(EnzymeCommissionNumber.valueOf(ec1, ec2), className, name, description, subSubclasses);
195   }
196 }