| 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 | 0 | public EnzymeSubclassMapper() { |
| 29 | 0 | } |
| 30 | |
|
| 31 | |
private String findStatement() { |
| 32 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | if (ec1 == null) throw new NullPointerException("Parameter 'ec1' must not be null."); |
| 56 | 0 | if (ec2 == null) throw new NullPointerException("Parameter 'ec2' must not be null."); |
| 57 | 0 | if (con == null) throw new NullPointerException("Parameter 'con' must not be null."); |
| 58 | |
|
| 59 | 0 | PreparedStatement findStatement = null; |
| 60 | 0 | ResultSet rs = null; |
| 61 | 0 | EnzymeSubclass result = null; |
| 62 | |
List<EnzymeSubSubclass> subSubclasses; |
| 63 | |
|
| 64 | |
|
| 65 | 0 | EnzymeSubSubclassMapper subSubclassMapper = new EnzymeSubSubclassMapper(); |
| 66 | 0 | subSubclasses = subSubclassMapper.findList(ec1, ec2, con); |
| 67 | |
|
| 68 | |
try { |
| 69 | 0 | findStatement = con.prepareStatement(findStatement()); |
| 70 | 0 | findStatement.setString(1, ec1); |
| 71 | 0 | findStatement.setString(2, ec1); |
| 72 | 0 | findStatement.setString(3, ec2); |
| 73 | 0 | rs = findStatement.executeQuery(); |
| 74 | 0 | if (rs.next()) { |
| 75 | 0 | result = doLoad(rs, subSubclasses); |
| 76 | |
} |
| 77 | |
} finally { |
| 78 | 0 | if (rs != null) rs.close(); |
| 79 | 0 | if (findStatement != null) findStatement.close(); |
| 80 | |
} |
| 81 | |
|
| 82 | 0 | 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 | 0 | PreparedStatement findListStatement = null; |
| 95 | 0 | ResultSet rs = null; |
| 96 | 0 | List<EnzymeSubclass> result = new ArrayList<EnzymeSubclass>(); |
| 97 | 0 | boolean noResult = true; |
| 98 | |
|
| 99 | |
try { |
| 100 | 0 | findListStatement = con.prepareStatement(findListStatement()); |
| 101 | 0 | findListStatement.setString(1, ec1); |
| 102 | 0 | findListStatement.setString(2, ec1); |
| 103 | 0 | rs = findListStatement.executeQuery(); |
| 104 | 0 | while (rs.next()) { |
| 105 | 0 | noResult = false; |
| 106 | 0 | result.add(doLoad(rs, null)); |
| 107 | |
} |
| 108 | |
} finally { |
| 109 | 0 | if (rs != null) rs.close(); |
| 110 | 0 | if (findListStatement != null) findListStatement.close(); |
| 111 | |
} |
| 112 | |
|
| 113 | 0 | if (noResult) return null; |
| 114 | 0 | return result; |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
public List<EnzymeSubclass> findAll(Connection con) throws SQLException, DomainException{ |
| 126 | 0 | List<EnzymeSubclass> result = new ArrayList<EnzymeSubclass>(); |
| 127 | 0 | Statement stm = null; |
| 128 | 0 | ResultSet rs = null; |
| 129 | |
try { |
| 130 | 0 | stm = con.createStatement(); |
| 131 | 0 | rs = stm.executeQuery(SELECT_ALL); |
| 132 | 0 | while (rs.next()){ |
| 133 | 0 | result.add(doLoad(rs, null)); |
| 134 | |
} |
| 135 | |
} finally { |
| 136 | 0 | if (rs != null) rs.close(); |
| 137 | 0 | if (stm != null) stm.close(); |
| 138 | |
} |
| 139 | 0 | 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 | 0 | PreparedStatement findStatement = null; |
| 153 | 0 | ResultSet rs = null; |
| 154 | |
try { |
| 155 | 0 | findStatement = con.prepareStatement(findSubclassOnlyStatement()); |
| 156 | 0 | findStatement.setString(1, ec1); |
| 157 | 0 | findStatement.setString(2, ec2); |
| 158 | 0 | rs = findStatement.executeQuery(); |
| 159 | 0 | if (rs.next()) { |
| 160 | 0 | return true; |
| 161 | |
} |
| 162 | |
} finally { |
| 163 | 0 | if (rs != null) rs.close(); |
| 164 | 0 | if (findStatement != null) findStatement.close(); |
| 165 | |
} |
| 166 | |
|
| 167 | 0 | 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 | 0 | int ec1 = 0; |
| 183 | 0 | int ec2 = 0; |
| 184 | 0 | String className = ""; |
| 185 | 0 | String name = ""; |
| 186 | 0 | String description = ""; |
| 187 | |
|
| 188 | 0 | if (rs.getInt(1) > 0) ec1 = rs.getInt(1); |
| 189 | 0 | if (rs.getInt(2) > 0) ec2 = rs.getInt(2); |
| 190 | 0 | if (rs.getString(3) != null) name = rs.getString(3); |
| 191 | 0 | if (rs.getString(4) != null) className = rs.getString(4); |
| 192 | 0 | if (rs.getString(5) != null) description = rs.getString(5); |
| 193 | |
|
| 194 | 0 | return new EnzymeSubclass(EnzymeCommissionNumber.valueOf(ec1, ec2), className, name, description, subSubclasses); |
| 195 | |
} |
| 196 | |
} |