| 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.Types; |
| 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.constants.EnzymeNameQualifierConstant; |
| 14 | |
import uk.ac.ebi.intenz.domain.constants.EnzymeNameTypeConstant; |
| 15 | |
import uk.ac.ebi.intenz.domain.constants.EnzymeSourceConstant; |
| 16 | |
import uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant; |
| 17 | |
import uk.ac.ebi.intenz.domain.constants.Status; |
| 18 | |
import uk.ac.ebi.intenz.domain.enzyme.EnzymeName; |
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | 1 | public class EnzymeNameMapper { |
| 27 | |
|
| 28 | 1 | private static final Logger LOGGER = |
| 29 | |
Logger.getLogger(EnzymeNameMapper.class.getName()); |
| 30 | |
|
| 31 | |
private static final String COLUMNS = "enzyme_id, name, name_class, warning, status, source, note, order_in, web_view"; |
| 32 | |
|
| 33 | 2 | public EnzymeNameMapper() { |
| 34 | 2 | } |
| 35 | |
|
| 36 | |
private String exportSibNamesStatement() { |
| 37 | 0 | return "SELECT " + COLUMNS + |
| 38 | |
" FROM names WHERE enzyme_id = ?" + |
| 39 | |
" AND (web_view = ? OR web_view = ? OR web_view = ? OR web_view = ?)" + |
| 40 | |
" FOR UPDATE ORDER BY UPPER(name)"; |
| 41 | |
} |
| 42 | |
|
| 43 | |
private String findStatement() { |
| 44 | 2 | return "SELECT " + COLUMNS + |
| 45 | |
" FROM names" + |
| 46 | |
" WHERE enzyme_id = ?" + |
| 47 | |
" ORDER BY order_in"; |
| 48 | |
} |
| 49 | |
|
| 50 | |
private String findByClassStatement() { |
| 51 | 2 | return "SELECT " + COLUMNS + |
| 52 | |
" FROM names" + |
| 53 | |
" WHERE enzyme_id = ? AND name_class = ?" + |
| 54 | |
" ORDER BY order_in"; |
| 55 | |
} |
| 56 | |
|
| 57 | |
private String insertStatement() { |
| 58 | 2 | return "INSERT INTO names (enzyme_id, name, name_class, warning, status, source, note, order_in, web_view) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; |
| 59 | |
} |
| 60 | |
|
| 61 | |
private String updateNameStatement() { |
| 62 | 0 | return "UPDATE names SET name = ?, warning = ?, status = ?, source = ?, web_view = ? " + |
| 63 | |
"WHERE enzyme_id = ? AND name_class = ?"; |
| 64 | |
} |
| 65 | |
|
| 66 | |
private String deleteNamesStatement() { |
| 67 | 4 | return "DELETE names WHERE enzyme_id = ? AND name_class = ?"; |
| 68 | |
} |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public List<EnzymeName> find(Long enzymeId, Connection con) throws SQLException { |
| 79 | 2 | if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null."); |
| 80 | 2 | if (con == null) throw new NullPointerException("Parameter 'con' must not be null."); |
| 81 | |
|
| 82 | 2 | PreparedStatement findStatement = null; |
| 83 | 2 | ResultSet rs = null; |
| 84 | 2 | List<EnzymeName> result = new ArrayList<EnzymeName>(); |
| 85 | 2 | boolean noResult = true; |
| 86 | |
|
| 87 | |
try { |
| 88 | |
|
| 89 | 2 | findStatement = con.prepareStatement(findStatement()); |
| 90 | 2 | findStatement.setLong(1, enzymeId.longValue()); |
| 91 | 2 | rs = findStatement.executeQuery(); |
| 92 | 17 | while (rs.next()) { |
| 93 | 15 | EnzymeName name = doLoad(rs); |
| 94 | 15 | if (name != null) { |
| 95 | 15 | noResult = false; |
| 96 | 15 | result.add(name); |
| 97 | |
} |
| 98 | 15 | } |
| 99 | |
} finally { |
| 100 | 2 | if (rs != null) rs.close(); |
| 101 | 2 | if (findStatement != null) findStatement.close(); |
| 102 | |
} |
| 103 | |
|
| 104 | 2 | if (noResult) return null; |
| 105 | 2 | return result; |
| 106 | |
} |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
public List<EnzymeName> exportSibNames(Long enzymeId, Connection con) throws SQLException { |
| 120 | 0 | if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null."); |
| 121 | 0 | if (con == null) throw new NullPointerException("Parameter 'con' must not be null."); |
| 122 | |
|
| 123 | 0 | PreparedStatement findStatement = null; |
| 124 | 0 | ResultSet rs = null; |
| 125 | 0 | List<EnzymeName> result = new ArrayList<EnzymeName>(); |
| 126 | 0 | boolean noResult = true; |
| 127 | |
|
| 128 | |
try { |
| 129 | |
|
| 130 | 0 | findStatement = con.prepareStatement(exportSibNamesStatement()); |
| 131 | 0 | findStatement.setLong(1, enzymeId.longValue()); |
| 132 | 0 | findStatement.setString(2, EnzymeViewConstant.INTENZ.toString()); |
| 133 | 0 | findStatement.setString(3, EnzymeViewConstant.IUBMB_SIB.toString()); |
| 134 | 0 | findStatement.setString(4, EnzymeViewConstant.SIB.toString()); |
| 135 | 0 | findStatement.setString(5, EnzymeViewConstant.SIB_INTENZ.toString()); |
| 136 | 0 | rs = findStatement.executeQuery(); |
| 137 | 0 | while (rs.next()) { |
| 138 | 0 | EnzymeName name = doLoad(rs); |
| 139 | 0 | if (name != null) { |
| 140 | 0 | noResult = false; |
| 141 | 0 | result.add(name); |
| 142 | |
} |
| 143 | 0 | } |
| 144 | |
} finally { |
| 145 | 0 | if (rs != null) rs.close(); |
| 146 | 0 | if (findStatement != null) findStatement.close(); |
| 147 | |
} |
| 148 | |
|
| 149 | 0 | if (noResult) return null; |
| 150 | 0 | return result; |
| 151 | |
} |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
public List<EnzymeName> findCommonNames(Long enzymeId, Connection con) throws SQLException { |
| 164 | 1 | if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null."); |
| 165 | 1 | if (con == null) throw new NullPointerException("Parameter 'con' must not be null."); |
| 166 | |
|
| 167 | 1 | PreparedStatement findStatement = null; |
| 168 | 1 | ResultSet rs = null; |
| 169 | 1 | boolean noResult = true; |
| 170 | 1 | List<EnzymeName> commonNames = new ArrayList<EnzymeName>(); |
| 171 | 1 | EnzymeName result = null; |
| 172 | |
|
| 173 | |
try { |
| 174 | |
|
| 175 | 1 | findStatement = con.prepareStatement(findByClassStatement()); |
| 176 | 1 | findStatement.setLong(1, enzymeId.longValue()); |
| 177 | 1 | findStatement.setString(2, EnzymeNameTypeConstant.COMMON_NAME.toString()); |
| 178 | 1 | rs = findStatement.executeQuery(); |
| 179 | 2 | while (rs.next()) { |
| 180 | 1 | result = doLoad(rs); |
| 181 | 1 | if (result != null) { |
| 182 | 1 | noResult = false; |
| 183 | 1 | commonNames.add(result); |
| 184 | |
} |
| 185 | |
} |
| 186 | |
} finally { |
| 187 | 1 | if (rs != null) rs.close(); |
| 188 | 1 | if (findStatement != null) findStatement.close(); |
| 189 | |
} |
| 190 | |
|
| 191 | 1 | if (noResult) return null; |
| 192 | 1 | return commonNames; |
| 193 | |
} |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
public EnzymeName findSystematicName(Long enzymeId, Connection con) throws SQLException { |
| 204 | 1 | if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null."); |
| 205 | 1 | if (con == null) throw new NullPointerException("Parameter 'con' must not be null."); |
| 206 | |
|
| 207 | 1 | PreparedStatement findStatement = null; |
| 208 | 1 | ResultSet rs = null; |
| 209 | 1 | EnzymeName result = null; |
| 210 | |
try { |
| 211 | |
|
| 212 | 1 | findStatement = con.prepareStatement(findByClassStatement()); |
| 213 | 1 | findStatement.setLong(1, enzymeId.longValue()); |
| 214 | 1 | findStatement.setString(2, EnzymeNameTypeConstant.SYSTEMATIC_NAME.toString()); |
| 215 | 1 | rs = findStatement.executeQuery(); |
| 216 | 1 | if (rs.next()) { |
| 217 | 1 | result = doLoad(rs); |
| 218 | |
} |
| 219 | |
} finally { |
| 220 | 1 | if (rs != null) rs.close(); |
| 221 | 1 | if (findStatement != null) findStatement.close(); |
| 222 | |
} |
| 223 | |
|
| 224 | 1 | return result; |
| 225 | |
} |
| 226 | |
|
| 227 | |
public void update(EnzymeName name, Long enzymeId, Status status, int orderIn, Connection con) |
| 228 | |
throws SQLException { |
| 229 | 0 | if (name == null) throw new NullPointerException("Parameter 'name' must not be null."); |
| 230 | 0 | if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null."); |
| 231 | 0 | if (status == null) throw new NullPointerException("Parameter 'status' must not be null."); |
| 232 | 0 | if (orderIn < 1) throw new IllegalArgumentException("Parameter 'orderIn' must not be < 1."); |
| 233 | 0 | if (con == null) throw new NullPointerException("Parameter 'con' must not be null."); |
| 234 | |
|
| 235 | 0 | PreparedStatement updateNameStatement = null; |
| 236 | |
try { |
| 237 | 0 | updateNameStatement = con.prepareStatement(updateNameStatement()); |
| 238 | 0 | doUpdate(name, enzymeId, status, orderIn, updateNameStatement); |
| 239 | 0 | updateNameStatement.execute(); |
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
} finally { |
| 245 | 0 | if (updateNameStatement != null) updateNameStatement.close(); |
| 246 | |
} |
| 247 | 0 | } |
| 248 | |
|
| 249 | |
public void insertNames(List<EnzymeName> names, Long enzymeId, Status status, Connection con) |
| 250 | |
throws SQLException { |
| 251 | 0 | if (names == null) throw new NullPointerException("Parameter 'names' must not be null."); |
| 252 | 0 | if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null."); |
| 253 | 0 | if (status == null) throw new NullPointerException("Parameter 'status' must not be null."); |
| 254 | 0 | if (con == null) throw new NullPointerException("Parameter 'con' must not be null."); |
| 255 | 0 | for (int iii = 0; iii < names.size(); iii++) { |
| 256 | 0 | insert(names.get(iii), enzymeId, status, (iii+1), con); |
| 257 | |
} |
| 258 | 0 | } |
| 259 | |
|
| 260 | |
public void insert(EnzymeName enzymeName, Long enzymeId, Status status, int orderIn, Connection con) |
| 261 | |
throws SQLException { |
| 262 | 2 | if (enzymeName == null) throw new NullPointerException("Parameter 'enzymeName' must not be null."); |
| 263 | 2 | if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null."); |
| 264 | 2 | if (status == null) throw new NullPointerException("Parameter 'status' must not be null."); |
| 265 | 2 | if (con == null) throw new NullPointerException("Parameter 'con' must not be null."); |
| 266 | |
|
| 267 | 2 | PreparedStatement insertStatement = null; |
| 268 | |
try { |
| 269 | 2 | insertStatement = con.prepareStatement(insertStatement()); |
| 270 | 2 | doInsert(enzymeName, enzymeId, status, orderIn, insertStatement); |
| 271 | 2 | insertStatement.execute(); |
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
} finally { |
| 277 | 2 | if (insertStatement != null) insertStatement.close(); |
| 278 | |
} |
| 279 | 2 | } |
| 280 | |
|
| 281 | |
public void reload(List<EnzymeName> names, Long enzymeId, EnzymeNameTypeConstant type, Status status, |
| 282 | |
Connection con) |
| 283 | |
throws SQLException { |
| 284 | 0 | deleteNames(enzymeId, type, con); |
| 285 | 0 | insertNames(names, enzymeId, status, con); |
| 286 | 0 | } |
| 287 | |
|
| 288 | |
public void deleteNames(Long enzymeId, EnzymeNameTypeConstant nameType, Connection con) throws SQLException { |
| 289 | 4 | if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null."); |
| 290 | 4 | if (con == null) throw new NullPointerException("Parameter 'con' must not be null."); |
| 291 | |
|
| 292 | 4 | PreparedStatement deleteNamesStatement = null; |
| 293 | |
try { |
| 294 | 4 | deleteNamesStatement = con.prepareStatement(deleteNamesStatement()); |
| 295 | 4 | deleteNamesStatement.setLong(1, enzymeId.longValue()); |
| 296 | 4 | deleteNamesStatement.setString(2, nameType.toString()); |
| 297 | 4 | deleteNamesStatement.execute(); |
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
} finally { |
| 303 | 4 | if (deleteNamesStatement != null) deleteNamesStatement.close(); |
| 304 | |
} |
| 305 | 4 | } |
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
private EnzymeName doLoad(ResultSet rs) throws SQLException { |
| 319 | 17 | assert rs != null : "Parameter 'rs' must not be null."; |
| 320 | |
|
| 321 | 17 | String name = ""; |
| 322 | 17 | String nameClass = ""; |
| 323 | 17 | String warning = ""; |
| 324 | 17 | String source = ""; |
| 325 | 17 | String webView = ""; |
| 326 | |
|
| 327 | 17 | if (rs.getString("name") != null) name = rs.getString("name"); |
| 328 | 17 | if (rs.getString("name_class") != null) nameClass = rs.getString("name_class"); |
| 329 | 17 | if (rs.getString("warning") != null) warning = rs.getString("warning"); |
| 330 | 17 | if (rs.getString("source") != null) source = rs.getString("source"); |
| 331 | 17 | if (rs.getString("web_view") != null) webView = rs.getString("web_view"); |
| 332 | |
|
| 333 | 17 | return EnzymeName.valueOf(name, EnzymeNameTypeConstant.valueOf(nameClass.toUpperCase()), |
| 334 | |
EnzymeNameQualifierConstant.valueOf(warning.toUpperCase()), |
| 335 | |
EnzymeSourceConstant.valueOf(source), EnzymeViewConstant.valueOf(webView)); |
| 336 | |
} |
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
private void doInsert(EnzymeName name, Long enzymeId, Status status, int orderIn, |
| 348 | |
PreparedStatement insertStatement) throws SQLException { |
| 349 | 2 | assert name != null : "Parameter 'name' must not be null."; |
| 350 | 2 | assert enzymeId != null : "Parameter 'enzymeId' must not be null."; |
| 351 | 2 | assert status != null : "Parameter 'status' must not be null."; |
| 352 | 2 | assert orderIn > 0 : "Parameter 'orderIn' must not be < 1."; |
| 353 | 2 | assert insertStatement != null : "Parameter 'insertStatement' must not be null."; |
| 354 | |
|
| 355 | 2 | insertStatement.setLong(1, enzymeId.longValue()); |
| 356 | 2 | insertStatement.setString(2, name.getName()); |
| 357 | 2 | insertStatement.setString(3, name.getType().toString()); |
| 358 | 2 | if (name.getQualifier() == EnzymeNameQualifierConstant.UNDEF) |
| 359 | 2 | insertStatement.setNull(4, Types.VARCHAR); |
| 360 | |
else |
| 361 | 0 | insertStatement.setString(4, name.getQualifier().toString()); |
| 362 | 2 | insertStatement.setString(5, status.getCode()); |
| 363 | 2 | insertStatement.setString(6, name.getSource().toString()); |
| 364 | 2 | insertStatement.setNull(7, Types.VARCHAR); |
| 365 | 2 | insertStatement.setInt(8, orderIn); |
| 366 | 2 | insertStatement.setString(9, name.getView().toString()); |
| 367 | 2 | } |
| 368 | |
|
| 369 | |
private void doUpdate(EnzymeName name, Long enzymeId, Status status, int orderIn, |
| 370 | |
PreparedStatement updateStatement) throws SQLException { |
| 371 | 0 | assert name != null : "Parameter 'name' must not be null."; |
| 372 | 0 | assert enzymeId != null : "Parameter 'enzymeId' must not be null."; |
| 373 | 0 | assert status != null : "Parameter 'status' must not be null."; |
| 374 | 0 | assert orderIn > 0 : "Parameter 'orderIn' must not be < 1."; |
| 375 | 0 | assert updateStatement != null : "Parameter 'updateStatement' must not be null."; |
| 376 | |
|
| 377 | 0 | updateStatement.setString(1, name.getName()); |
| 378 | 0 | if (name.getQualifier() == EnzymeNameQualifierConstant.UNDEF) |
| 379 | 0 | updateStatement.setNull(2, Types.VARCHAR); |
| 380 | |
else |
| 381 | 0 | updateStatement.setString(2, name.getQualifier().toString()); |
| 382 | 0 | updateStatement.setString(3, status.getCode()); |
| 383 | 0 | updateStatement.setString(4, name.getSource().toString()); |
| 384 | 0 | updateStatement.setString(5, name.getView().toString()); |
| 385 | 0 | updateStatement.setLong(6, enzymeId.longValue()); |
| 386 | 0 | updateStatement.setString(7, name.getType().toString()); |
| 387 | 0 | } |
| 388 | |
} |