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