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
18
19
20
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
45
46
47
48
49
50
51
52
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
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
87
88
89
90
91
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
120
121
122
123
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
144
145
146
147
148
149
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
172
173
174
175
176
177
178
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 }