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
18
19
20
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
54
55
56
57
58
59
60
61
62
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
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
101
102
103
104
105
106
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
156
157
158
159
160
161
162
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
176
177
178
179 } finally {
180 if (insertStatement != null) insertStatement.close();
181 }
182 }
183
184
185
186
187
188
189
190
191
192
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
217
218
219
220
221
222
223
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 }