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
21
22
23
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
47
48
49
50
51
52
53
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
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
87
88
89
90
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
111
112
113
114
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
138
139
140
141
142
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
165
166
167
168
169
170
171
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 }