Coverage Report - uk.ac.ebi.intenz.mapper.HistoryEventMapper
 
Classes in this File Line Coverage Branch Coverage Complexity
HistoryEventMapper
0%
0/46
0%
0/22
4.333
HistoryEventMapper$1
0%
0/1
N/A
4.333
 
 1  
 package uk.ac.ebi.intenz.mapper;
 2  
 
 3  
 import java.sql.Connection;
 4  
 import java.sql.PreparedStatement;
 5  
 import java.sql.SQLException;
 6  
 import java.sql.Timestamp;
 7  
 import java.sql.Types;
 8  
 
 9  
 import uk.ac.ebi.intenz.domain.constants.Event;
 10  
 
 11  0
 public class HistoryEventMapper {
 12  
 
 13  
         private static final String INSERT_EVENT_SQL =
 14  
                 "INSERT INTO history_events" +
 15  
                 " (event_id, group_id, event_class, before_id, after_id, event_note, event_year)" +
 16  
                 " VALUES (s_history_event_id.nextval, s_history_group_id.nextval, ?, ?, ?, ?, ?)";
 17  
         
 18  
         //TODO
 19  
         private static final String SELECT_EVENT_SQL =
 20  
                 "SELECT group_id, event_id, before_id, after_id, event_year," +
 21  
                 " event_note, event_class FROM history_events" +
 22  
                 " WHERE before_id = ? OR after_id = ?";
 23  
 
 24  
         private static final String UPDATE_EVENT_SQL =
 25  
                         "UPDATE history_events" +
 26  
                         " SET before_id = ?, after_id = ?, event_note = ?" +
 27  
                         " WHERE event_id = ? AND group_id = ?";
 28  
 
 29  
         private static final String UPDATE_EVENT_NOTE_SQL =
 30  
                         "UPDATE history_events SET event_note = ?" +
 31  
                         " WHERE event_id = ? AND group_id = ?";
 32  
 
 33  
     public void insertEvent(Event event, Long beforeId, Long afterId,
 34  
                         String note, Connection con) throws SQLException{
 35  0
             PreparedStatement stm = null;
 36  
             try {
 37  0
                     stm = con.prepareStatement(INSERT_EVENT_SQL);
 38  0
                     stm.setString(1, event.getCode());
 39  0
                         switch (event) {
 40  
                         case CREATION:
 41  0
                                 stm.setNull(2, Types.NUMERIC);
 42  0
                                 stm.setLong(3, afterId);
 43  0
                                 break;
 44  
                         case TRANSFER:
 45  0
                                 stm.setLong(2, beforeId);
 46  0
                                 stm.setLong(3, afterId);
 47  0
                                 break;
 48  
                         case DELETION:
 49  0
                                 stm.setLong(2, beforeId);
 50  0
                                 stm.setNull(3, Types.NUMERIC);
 51  0
                                 break;
 52  
                         default:
 53  0
                                 throw new IllegalArgumentException("Event not supported: " + event);
 54  
                         }
 55  0
                         if (note == null){
 56  0
                                 stm.setNull(4, Types.VARCHAR);
 57  
                         } else {
 58  0
                                 stm.setString(4, note);
 59  
                         }
 60  0
                         stm.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
 61  0
                         stm.executeUpdate();
 62  
             } finally {
 63  0
                     if (stm != null) stm.close();
 64  
             }
 65  0
         }
 66  
     
 67  
     /**
 68  
      * Updates the note for an event in the history.
 69  
      * @param eventId the ID of the event.
 70  
      * @param groupId the ID of the event group.
 71  
      * @param newNote the new note for the event.
 72  
      * @param con a database connection.
 73  
      * @throws SQLException
 74  
      */
 75  
     public void updateEventNote(int eventId, int groupId, String newNote,
 76  
                     Connection con) throws SQLException{
 77  0
             PreparedStatement ps = null;
 78  
             try {
 79  0
                     ps = con.prepareStatement(UPDATE_EVENT_NOTE_SQL);
 80  0
                     int n = 1;
 81  0
                     ps.setString(n++, newNote);
 82  0
                     ps.setInt(n++, eventId);
 83  0
                     ps.setInt(n++, groupId);
 84  0
                     ps.executeUpdate();
 85  
             } finally {
 86  0
                     if (ps != null) ps.close();
 87  
             }
 88  0
     }
 89  
     
 90  
     /**
 91  
      * Modifies an event in the history.
 92  
      * @param eventId the event ID.
 93  
      * @param groupId the event group ID.
 94  
      * @param newNote the new history note.
 95  
      * @param beforeId the ID of the enzyme before the event.
 96  
      * @param afterId the ID of the enzyme after the event.
 97  
      * @param con a database connection.
 98  
      * @throws SQLException
 99  
      */
 100  
     public void updateEvent(int eventId, int groupId, String newNote,
 101  
                     Integer beforeId, Integer afterId, Connection con)
 102  
         throws SQLException{
 103  0
             PreparedStatement ps = null;
 104  
             try {
 105  0
                     ps = con.prepareStatement(UPDATE_EVENT_SQL);
 106  0
                     int n = 1;
 107  0
                     if (beforeId == null){
 108  0
                             ps.setNull(n++, Types.INTEGER);
 109  
                     } else {
 110  0
                             ps.setInt(n++, beforeId);
 111  
                     }
 112  0
                     if (afterId == null){
 113  0
                             ps.setNull(n++, Types.INTEGER);
 114  
                     } else {
 115  0
                             ps.setInt(n++, afterId);
 116  
                     }
 117  0
                     ps.setString(n++, newNote);
 118  0
                     ps.setInt(n++, eventId);
 119  0
                     ps.setInt(n++, groupId);
 120  0
                     ps.executeUpdate();
 121  
             } finally {
 122  0
                     if (ps != null) ps.close();
 123  
             }
 124  0
     }
 125  
 }