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 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  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    private static final Logger LOGGER =
32  	  Logger.getLogger(EnzymeClassMapper.class.getName());
33  
34    public EnzymeClassMapper() {
35    }
36  
37    private String findStatement() {
38      return "SELECT " + COLUMNS + " FROM classes WHERE ec1 = ?";
39    }
40  
41    private String findNumberOfClassesStatement() {
42      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      if (ec1 == null) throw new NullPointerException("Parameter 'ec1' must not be null.");
57      if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
58      PreparedStatement findStatement = null;
59      ResultSet rs = null;
60      EnzymeClass result = null;
61      List<EnzymeSubclass> subclasses;
62  
63      // Get subclasses.
64      EnzymeSubclassMapper subclassMapper = new EnzymeSubclassMapper();
65      subclasses = subclassMapper.findList(ec1, con);
66  
67      try {
68        findStatement = con.prepareStatement(findStatement());
69        findStatement.setString(1, ec1);
70        rs = findStatement.executeQuery();
71        if (rs.next()) {
72          result = doLoad(rs, subclasses);
73        } else {
74          LOGGER.warn("No class information found for EC " + ec1);
75        }
76      } finally {
77      	if (rs != null) rs.close();
78        if (findStatement != null) findStatement.close();
79      }
80  
81      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        List<EnzymeClass> result = new ArrayList<EnzymeClass>();
94        Statement stm = null;
95        ResultSet rs = null;
96        try {
97            stm = con.createStatement();
98            rs = stm.executeQuery("SELECT ec1 FROM classes");
99            while (rs.next()){
100               result.add(find(rs.getString(1), con));
101           }
102       } finally {
103       	if (rs != null) rs.close();
104           if (stm != null) stm.close();
105       }
106       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     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
118     PreparedStatement findStatement = null;
119     ResultSet rs = null;
120     int number = 0;
121 
122     try {
123       findStatement = con.prepareStatement(findNumberOfClassesStatement());
124       rs = findStatement.executeQuery();
125       if (rs.next()) {
126         number = Integer.parseInt(rs.getString("count"));
127       }
128     } finally {
129       	if (rs != null) rs.close();
130       	if (findStatement != null) findStatement.close();
131     }
132 
133     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     if (ec1 == null) throw new NullPointerException("Parameter 'ec1' must not be null.");
146     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
147     PreparedStatement findStatement = null;
148     ResultSet rs = null;
149     try {
150       findStatement = con.prepareStatement(findStatement());
151       findStatement.setString(1, ec1);
152       rs = findStatement.executeQuery();
153       if (rs.next()) {
154         return true;
155       }
156     } finally {
157       	if (rs != null) rs.close();
158       	if (findStatement != null) findStatement.close();
159     }
160 
161     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     assert rs != null;
176     
177     int ec1 = 0;
178     String name = "";
179     String description = "";
180 
181     if (rs.getInt("ec1") > 0) ec1 = rs.getInt("ec1");
182     if (rs.getString("name") != null) name = rs.getString("name");
183     if (rs.getString("description") != null) description = rs.getString("description");
184 
185     EnzymeCommissionNumber ec = EnzymeCommissionNumber.valueOf(ec1);
186     return EnzymeClass.valueOf(ec, name, description, subclasses);
187   }
188 }