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.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  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    public EnzymeSubSubclassMapper() {
30    }
31  
32    private String findStatement() {
33      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      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      return "SELECT ec1 FROM subsubclasses WHERE ec1 = ? AND ec2 = ? AND ec3 = ?";
46    }
47  
48    private String insertStatement() {
49      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      if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
67  
68      PreparedStatement findStatement = null;
69      ResultSet rs = null;
70      EnzymeSubSubclass result = null;
71      List<EnzymeEntry> entries;
72  
73      // Get entries.
74      EnzymeEntryMapper enzymeEntryMapper = new EnzymeEntryMapper();
75      entries = enzymeEntryMapper.findAllSubSubclassEntriesByEc(ec1, ec2, ec3, con);
76      if(entries == null) entries = new ArrayList<EnzymeEntry>();
77  
78      try {
79        findStatement = con.prepareStatement(findStatement());
80        findStatement.setInt(1, ec1);
81        findStatement.setInt(2, ec1);
82        findStatement.setInt(3, ec2);
83        findStatement.setInt(4, ec1);
84        findStatement.setInt(5, ec2);
85        findStatement.setInt(6, ec3);
86        rs = findStatement.executeQuery();
87        if (rs.next()) {
88          result = doLoad(rs, entries);
89        }
90      } finally {
91      	enzymeEntryMapper.close();
92      	if (rs != null) rs.close();
93        if (findStatement != null) findStatement.close();
94      }
95  
96      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     PreparedStatement findListStatement = null;
111     ResultSet rs = null;
112     List<EnzymeSubSubclass> result = new ArrayList<EnzymeSubSubclass>();
113     boolean noResult = true;
114 
115     try {
116       findListStatement = con.prepareStatement(findListStatement());
117       findListStatement.setString(1, ec1);
118       findListStatement.setString(2, ec1);
119       findListStatement.setString(3, ec2);
120       findListStatement.setString(4, ec1);
121       findListStatement.setString(5, ec2);
122       rs = findListStatement.executeQuery();
123 
124       while (rs.next()) {
125         noResult = false;
126         result.add(doLoad(rs, null));
127       }
128     } finally {
129     	if (rs != null) rs.close();
130       if (findListStatement != null) findListStatement.close();
131     }
132 
133     if (noResult) return null;
134     return result;
135   }
136   
137   public List<EnzymeSubSubclass> findAll(Connection con) throws SQLException, DomainException{
138       List<EnzymeSubSubclass> result = new ArrayList<EnzymeSubSubclass>();
139       Statement stm = null;
140       ResultSet rs = null;
141       try {
142           stm = con.createStatement();
143           rs = stm.executeQuery(SELECT_ALL);
144           while (rs.next()){
145               result.add(doLoad(rs, null));
146           }
147       } finally {
148       	if (rs != null) rs.close();
149           if (stm != null) stm.close();
150       }
151       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     if (ec == null) throw new NullPointerException("Parameter 'ec' must not be null.");
166     if (name == null) throw new NullPointerException("Parameter 'name' must not be null.");
167     if (description == null) throw new NullPointerException("Parameter 'description' must not be null.");
168     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
169 
170     PreparedStatement insertStatement = null;
171     try {
172       insertStatement = con.prepareStatement(insertStatement());
173       doInsert(ec, name, description, insertStatement);
174       insertStatement.execute();
175 //      con.commit();
176 //    } catch (SQLException e) {
177 //      con.rollback();
178 //      throw e;
179     } finally {
180       if (insertStatement != null) insertStatement.close();
181     }
182   }
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     PreparedStatement findStatement = null;
196     ResultSet rs = null;
197 
198     try {
199       findStatement = con.prepareStatement(findSubSubclassOnly());
200       findStatement.setString(1, ec1);
201       findStatement.setString(2, ec2);
202       findStatement.setString(3, ec3);
203       rs = findStatement.executeQuery();
204       if (rs.next()) {
205         return true;
206       }
207     } finally {
208     	if (rs != null) rs.close();
209       if (findStatement != null) findStatement.close();
210     }
211 
212     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     int ec1 = 0;
228     int ec2 = 0;
229     int ec3 = 0;
230     String name = "";
231     String subclassName = "";
232     String className = "";
233     String description = "";
234 
235     if (rs.getInt(1) > 0) ec1 = rs.getInt(1);
236     if (rs.getInt(2) > 0) ec2 = rs.getInt(2);
237     if (rs.getInt(3) > 0) ec3 = rs.getInt(3);
238     if (rs.getString(4) != null) name = rs.getString(4);
239     if (rs.getString(5) != null) subclassName = rs.getString(5);
240     if (rs.getString(6) != null) className = rs.getString(6);
241     if (rs.getString(7) != null) description = rs.getString(7);
242 
243     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     assert ec != null : "Parameter 'ec' must not be null.";
249     assert name != null : "Parameter 'name' must not be null.";
250     assert description != null : "Parameter 'description' must not be null.";
251     assert insertStatement != null : "Parameter 'insertStatement' must not be null.";
252 
253     insertStatement.setInt(1, ec.getEc1());
254     insertStatement.setInt(2, ec.getEc2());
255     insertStatement.setInt(3, ec.getEc3());
256     insertStatement.setString(4, name);
257     insertStatement.setString(5, description);
258     insertStatement.setString(6, "Y");
259   }
260 }