| 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 | 0 | 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 | 0 | private static final Logger LOGGER = |
| 32 | |
Logger.getLogger(EnzymeClassMapper.class.getName()); |
| 33 | |
|
| 34 | 0 | public EnzymeClassMapper() { |
| 35 | 0 | } |
| 36 | |
|
| 37 | |
private String findStatement() { |
| 38 | 0 | return "SELECT " + COLUMNS + " FROM classes WHERE ec1 = ?"; |
| 39 | |
} |
| 40 | |
|
| 41 | |
private String findNumberOfClassesStatement() { |
| 42 | 0 | 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 | 0 | if (ec1 == null) throw new NullPointerException("Parameter 'ec1' must not be null."); |
| 57 | 0 | if (con == null) throw new NullPointerException("Parameter 'con' must not be null."); |
| 58 | 0 | PreparedStatement findStatement = null; |
| 59 | 0 | ResultSet rs = null; |
| 60 | 0 | EnzymeClass result = null; |
| 61 | |
List<EnzymeSubclass> subclasses; |
| 62 | |
|
| 63 | |
|
| 64 | 0 | EnzymeSubclassMapper subclassMapper = new EnzymeSubclassMapper(); |
| 65 | 0 | subclasses = subclassMapper.findList(ec1, con); |
| 66 | |
|
| 67 | |
try { |
| 68 | 0 | findStatement = con.prepareStatement(findStatement()); |
| 69 | 0 | findStatement.setString(1, ec1); |
| 70 | 0 | rs = findStatement.executeQuery(); |
| 71 | 0 | if (rs.next()) { |
| 72 | 0 | result = doLoad(rs, subclasses); |
| 73 | |
} else { |
| 74 | 0 | LOGGER.warn("No class information found for EC " + ec1); |
| 75 | |
} |
| 76 | |
} finally { |
| 77 | 0 | if (rs != null) rs.close(); |
| 78 | 0 | if (findStatement != null) findStatement.close(); |
| 79 | |
} |
| 80 | |
|
| 81 | 0 | return result; |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
public List<EnzymeClass> findAll(Connection con) throws SQLException, DomainException{ |
| 93 | 0 | List<EnzymeClass> result = new ArrayList<EnzymeClass>(); |
| 94 | 0 | Statement stm = null; |
| 95 | 0 | ResultSet rs = null; |
| 96 | |
try { |
| 97 | 0 | stm = con.createStatement(); |
| 98 | 0 | rs = stm.executeQuery("SELECT ec1 FROM classes"); |
| 99 | 0 | while (rs.next()){ |
| 100 | 0 | result.add(find(rs.getString(1), con)); |
| 101 | |
} |
| 102 | |
} finally { |
| 103 | 0 | if (rs != null) rs.close(); |
| 104 | 0 | if (stm != null) stm.close(); |
| 105 | |
} |
| 106 | 0 | return result; |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
public int findNumberOfClasses(Connection con) throws SQLException { |
| 117 | 0 | if (con == null) throw new NullPointerException("Parameter 'con' must not be null."); |
| 118 | 0 | PreparedStatement findStatement = null; |
| 119 | 0 | ResultSet rs = null; |
| 120 | 0 | int number = 0; |
| 121 | |
|
| 122 | |
try { |
| 123 | 0 | findStatement = con.prepareStatement(findNumberOfClassesStatement()); |
| 124 | 0 | rs = findStatement.executeQuery(); |
| 125 | 0 | if (rs.next()) { |
| 126 | 0 | number = Integer.parseInt(rs.getString("count")); |
| 127 | |
} |
| 128 | |
} finally { |
| 129 | 0 | if (rs != null) rs.close(); |
| 130 | 0 | if (findStatement != null) findStatement.close(); |
| 131 | |
} |
| 132 | |
|
| 133 | 0 | return number; |
| 134 | |
} |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
public boolean classExists(String ec1, Connection con) throws SQLException { |
| 145 | 0 | if (ec1 == null) throw new NullPointerException("Parameter 'ec1' must not be null."); |
| 146 | 0 | if (con == null) throw new NullPointerException("Parameter 'con' must not be null."); |
| 147 | 0 | PreparedStatement findStatement = null; |
| 148 | 0 | ResultSet rs = null; |
| 149 | |
try { |
| 150 | 0 | findStatement = con.prepareStatement(findStatement()); |
| 151 | 0 | findStatement.setString(1, ec1); |
| 152 | 0 | rs = findStatement.executeQuery(); |
| 153 | 0 | if (rs.next()) { |
| 154 | 0 | return true; |
| 155 | |
} |
| 156 | |
} finally { |
| 157 | 0 | if (rs != null) rs.close(); |
| 158 | 0 | if (findStatement != null) findStatement.close(); |
| 159 | |
} |
| 160 | |
|
| 161 | 0 | 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 | 0 | assert rs != null; |
| 176 | |
|
| 177 | 0 | int ec1 = 0; |
| 178 | 0 | String name = ""; |
| 179 | 0 | String description = ""; |
| 180 | |
|
| 181 | 0 | if (rs.getInt("ec1") > 0) ec1 = rs.getInt("ec1"); |
| 182 | 0 | if (rs.getString("name") != null) name = rs.getString("name"); |
| 183 | 0 | if (rs.getString("description") != null) description = rs.getString("description"); |
| 184 | |
|
| 185 | 0 | EnzymeCommissionNumber ec = EnzymeCommissionNumber.valueOf(ec1); |
| 186 | 0 | return EnzymeClass.valueOf(ec, name, description, subclasses); |
| 187 | |
} |
| 188 | |
} |