Coverage Report - uk.ac.ebi.intenz.mapper.EnzymeCommentMapper
 
Classes in this File Line Coverage Branch Coverage Complexity
EnzymeCommentMapper
55%
65/117
28%
30/106
4.133
 
 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.util.ArrayList;
 8  
 import java.util.List;
 9  
 
 10  
 import org.apache.log4j.Logger;
 11  
 
 12  
 import uk.ac.ebi.intenz.domain.constants.EnzymeSourceConstant;
 13  
 import uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant;
 14  
 import uk.ac.ebi.intenz.domain.constants.Status;
 15  
 import uk.ac.ebi.intenz.domain.enzyme.EnzymeComment;
 16  
 
 17  
 /**
 18  
  * Maps link information to the corresponding database tables.
 19  
  * <p/>
 20  
  *
 21  
  * @author Michael Darsow
 22  
  * @version $Revision: 1.3 $ $Date: 2009/05/26 14:59:09 $
 23  
  */
 24  1
 public class EnzymeCommentMapper {
 25  
 
 26  
   private static final String COLUMNS = "enzyme_id, comment_text, order_in, status, source, web_view";
 27  
 
 28  1
   private static final Logger LOGGER =
 29  
           Logger.getLogger(EnzymeCommentMapper.class.getName());
 30  
 
 31  2
   public EnzymeCommentMapper() {
 32  2
   }
 33  
 
 34  
   private String findStatement() {
 35  2
     return "SELECT " + COLUMNS +
 36  
            " FROM comments" +
 37  
            " WHERE enzyme_id = ? ORDER BY order_in";
 38  
   }
 39  
 
 40  
   private String exportSibCommentsStatement() {
 41  0
     return "SELECT " + COLUMNS +
 42  
            " FROM comments WHERE enzyme_id = ?" +
 43  
            " AND (web_view = ? OR web_view = ? OR web_view = ? OR web_view = ?)" +
 44  
            " FOR UPDATE ORDER BY order_in";
 45  
   }
 46  
 
 47  
   private String insertStatement() {
 48  1
     return "INSERT INTO comments (enzyme_id, comment_text, order_in, status, source, web_view) VALUES (?, ?, ?, ?, ?, ?)";
 49  
   }
 50  
 
 51  
   private String updateStatement() {
 52  0
     return "UPDATE comments SET comment_text = ?, order_in = ?, status = ?, source = ?, web_view = ? WHERE enzyme_id = ?";
 53  
   }
 54  
 
 55  
   private String deleteAllStatement() {
 56  2
     return "DELETE comments WHERE enzyme_id = ?";
 57  
   }
 58  
 
 59  
   /**
 60  
    * Tries to find comment information about an enzyme.
 61  
    *
 62  
    * @param enzymeId Enzyme ID of entry.
 63  
    * @param con      The logical connection.
 64  
    * @return a <code>String</code> representing the comment.
 65  
    * @throws SQLException
 66  
    */
 67  
   public List<EnzymeComment> find(Long enzymeId, Connection con) throws SQLException {
 68  2
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 69  2
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 70  
 
 71  2
     PreparedStatement findStatement = null;
 72  2
     ResultSet rs = null;
 73  2
     List<EnzymeComment> result = new ArrayList<EnzymeComment>();
 74  2
     boolean noResult = true;
 75  
 
 76  
     try {
 77  2
       findStatement = con.prepareStatement(findStatement());
 78  2
       findStatement.setLong(1, enzymeId.longValue());
 79  2
       rs = findStatement.executeQuery();
 80  5
       while (rs.next()) {
 81  3
         noResult = false;
 82  3
         EnzymeComment enzymeComment = doLoad(rs);
 83  3
         result.add(enzymeComment);
 84  3
       }
 85  
     } finally {
 86  2
             if (rs != null) rs.close();
 87  2
       if (findStatement != null) findStatement.close();
 88  
     }
 89  
 
 90  2
     if (noResult) {
 91  0
       LOGGER.debug("No comment information found for the enzyme with ID "
 92  
               + enzymeId);
 93  0
       return null;
 94  
     }
 95  2
     return result;
 96  
   }
 97  
 
 98  
   /**
 99  
    * Exports all comments which are displayed in the ENZYME view.
 100  
    *
 101  
    * Affected table rows will be locked.
 102  
    *
 103  
    * @param enzymeId The enzyme ID used to retreive the related comments.
 104  
    * @param con The database connection.
 105  
    * @return an {@link java.util.ArrayList} of comments or <code>null</code> if no comment could be found.
 106  
    * @throws SQLException if a database error occured.
 107  
    * @throws NullPointerException if either of the parameters is <code>null</code>.
 108  
    */
 109  
   public List<EnzymeComment> exportSibComments(Long enzymeId, Connection con) throws SQLException {
 110  0
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 111  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 112  
 
 113  0
     PreparedStatement findStatement = null;
 114  0
     ResultSet rs = null;
 115  0
     List<EnzymeComment> result = new ArrayList<EnzymeComment>();
 116  0
     boolean noResult = true;
 117  
 
 118  
     try {
 119  0
       findStatement = con.prepareStatement(exportSibCommentsStatement());
 120  0
       findStatement.setLong(1, enzymeId.longValue());
 121  0
       findStatement.setString(2, EnzymeViewConstant.INTENZ.toString());
 122  0
       findStatement.setString(3, EnzymeViewConstant.IUBMB_SIB.toString());
 123  0
       findStatement.setString(4, EnzymeViewConstant.SIB.toString());
 124  0
       findStatement.setString(5, EnzymeViewConstant.SIB_INTENZ.toString());
 125  0
       rs = findStatement.executeQuery();
 126  0
       while (rs.next()) {
 127  0
         noResult = false;
 128  0
         EnzymeComment enzymeComment = doLoad(rs);
 129  0
         result.add(enzymeComment);
 130  0
       }
 131  
     } finally {
 132  0
             if (rs != null) rs.close();
 133  0
         if (findStatement != null) findStatement.close();
 134  
     }
 135  
 
 136  0
     if (noResult) return null;
 137  0
     return result;
 138  
   }
 139  
 
 140  
   public void reload(List<EnzymeComment> comments, Long enzymeId, Status status, Connection con)
 141  
           throws SQLException {
 142  0
     deleteAll(enzymeId, con);
 143  0
     insert(comments, enzymeId, status, con);
 144  0
   }
 145  
 
 146  
   /**
 147  
    * Deletes all comments related to an enzyme instance.
 148  
    *
 149  
    * @param enzymeId Enzyme ID of the enzyme instance.
 150  
    * @param con      A database connection.
 151  
    * @throws SQLException if a database error occurs.
 152  
    * @throws NullPointerException if any of the parameter is <code>null</code>.
 153  
    */
 154  
   public void deleteAll(Long enzymeId, Connection con) throws SQLException {
 155  2
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 156  2
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 157  
 
 158  2
     PreparedStatement deleteAllStatement = null;
 159  
 
 160  
     try {
 161  2
       deleteAllStatement = con.prepareStatement(deleteAllStatement());
 162  2
       deleteAllStatement.setLong(1, enzymeId.longValue());
 163  2
       deleteAllStatement.execute();
 164  
     } finally {
 165  2
       if (deleteAllStatement != null) deleteAllStatement.close();
 166  
     }
 167  2
   }
 168  
 
 169  
   /**
 170  
    * Stores the given comment into the database.
 171  
    *
 172  
    * @param comments The enzyme's comment.
 173  
    * @param enzymeId The enzyme ID.
 174  
    * @param status   ...
 175  
    * @param con      ...
 176  
    * @throws SQLException
 177  
    */
 178  
   public void insert(List<EnzymeComment> comments, Long enzymeId, Status status, Connection con)
 179  
           throws SQLException {
 180  1
     if (comments == null) throw new NullPointerException("Parameter 'comments' must not be null.");
 181  1
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 182  1
     if (status == null) throw new NullPointerException("Parameter 'status' must not be null.");
 183  1
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 184  
 
 185  1
     PreparedStatement insertStatement = null;
 186  
     try {
 187  1
       insertStatement = con.prepareStatement(insertStatement());
 188  2
       for (int iii = 0; iii < comments.size(); iii++) {
 189  1
         EnzymeComment enzymeComment = comments.get(iii);
 190  1
         doInsert(enzymeComment, enzymeId, (iii+1), status, insertStatement);
 191  1
         insertStatement.execute();
 192  
       }
 193  
     } finally {
 194  1
       if (insertStatement != null) insertStatement.close();
 195  
     }
 196  1
   }
 197  
 
 198  
   public void update(EnzymeComment comment, Long enzymeId, Status status, int orderIn, Connection con) throws SQLException {
 199  0
     if (comment == null) throw new NullPointerException("Parameter 'comment' must not be null.");
 200  0
     if (enzymeId == null) throw new NullPointerException("Parameter 'enzymeId' must not be null.");
 201  0
     if (status == null) throw new NullPointerException("Parameter 'status' must not be null.");
 202  0
     if (orderIn < 1) throw new IllegalArgumentException("Parameter 'orderIn' must not be > 0.");
 203  0
     if (con == null) throw new NullPointerException("Parameter 'con' must not be null.");
 204  
 
 205  0
     PreparedStatement updateStatement = null;
 206  
     try {
 207  0
       updateStatement = con.prepareStatement(updateStatement());
 208  0
       doUpdate(comment, enzymeId, status, orderIn, updateStatement);
 209  0
       updateStatement.execute();
 210  
     } finally {
 211  0
       if (updateStatement != null) updateStatement.close();
 212  
     }
 213  0
   }
 214  
 
 215  
   // ------------------- PRIVATE METHODS ------------------------
 216  
 
 217  
   /**
 218  
    * Creates the <code>EnzymeComment</code> object from the given result set.
 219  
    *
 220  
    * @param rs The result set object.
 221  
    * @return an <code>EnzymeComment</code> instance.
 222  
    * @throws SQLException
 223  
    */
 224  
   private EnzymeComment doLoad(ResultSet rs) throws SQLException {
 225  3
     assert rs != null : "Parameter 'rs' must not be null.";
 226  
 
 227  3
     String comment = "";
 228  3
     String source = "";
 229  3
     String webView = "";
 230  3
     if (rs.getString("comment_text") != null) comment = rs.getString("comment_text");
 231  3
     if (rs.getString("source") != null) source = rs.getString("source");
 232  3
     if (rs.getString("web_view") != null) webView = rs.getString("web_view");
 233  
 
 234  3
     EnzymeComment enzymeComment = new EnzymeComment(comment, EnzymeSourceConstant.valueOf(source),
 235  
                                                     EnzymeViewConstant.valueOf(webView));
 236  
 
 237  3
     return enzymeComment;
 238  
   }
 239  
 
 240  
   /**
 241  
    * Sets the parameters of the prepared statement.
 242  
    *
 243  
    * @param comment         ...
 244  
    * @param enzymeId        ...
 245  
    * @param status          ...
 246  
    * @param insertStatement ...
 247  
    * @throws SQLException
 248  
    */
 249  
   private void doInsert(EnzymeComment comment, Long enzymeId, int orderIn, Status status,
 250  
                         PreparedStatement insertStatement)
 251  
           throws SQLException {
 252  1
     assert comment != null : "Parameter 'comment' must not be null.";
 253  1
     assert enzymeId != null : "Parameter 'enzymeId' must not be null.";
 254  1
     assert status != null : "Parameter 'status' must not be null.";
 255  1
     assert insertStatement != null : "Parameter 'insertStatement' must not be null.";
 256  
 
 257  1
     insertStatement.setLong(1, enzymeId.longValue());
 258  1
     insertStatement.setString(2, comment.getCommentText());
 259  1
     insertStatement.setInt(3, orderIn);
 260  1
     insertStatement.setString(4, status.getCode());
 261  1
     insertStatement.setString(5, comment.getSource().toString());
 262  1
     insertStatement.setString(6, comment.getView().toString());
 263  1
   }
 264  
 
 265  
   private void doUpdate(EnzymeComment comment, Long enzymeId, Status status, int orderIn,
 266  
                         PreparedStatement updateStatement)
 267  
           throws SQLException {
 268  0
     assert comment != null : "Parameter 'comment' must not be null.";
 269  0
     assert enzymeId != null : "Parameter 'enzymeId' must not be null.";
 270  0
     assert status != null : "Parameter 'status' must not be null.";
 271  0
     assert orderIn > 0 : "Parameter 'orderIn' must not be > 0.";
 272  0
     assert updateStatement != null : "Parameter 'insertStatement' must not be null.";
 273  
 
 274  0
     updateStatement.setString(1, comment.getCommentText());
 275  0
     updateStatement.setInt(2, orderIn);
 276  0
     updateStatement.setString(3, status.getCode());
 277  0
     updateStatement.setString(4, comment.getSource().toString());
 278  0
     updateStatement.setString(5, comment.getView().toString());
 279  0
     updateStatement.setLong(6, enzymeId.longValue());
 280  0
   }
 281  
 }