View Javadoc

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  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      	PreparedStatement stm = null;
36      	try {
37  	    	stm = con.prepareStatement(INSERT_EVENT_SQL);
38  	    	stm.setString(1, event.getCode());
39  			switch (event) {
40  			case CREATION:
41  				stm.setNull(2, Types.NUMERIC);
42  				stm.setLong(3, afterId);
43  				break;
44  			case TRANSFER:
45  				stm.setLong(2, beforeId);
46  				stm.setLong(3, afterId);
47  				break;
48  			case DELETION:
49  				stm.setLong(2, beforeId);
50  				stm.setNull(3, Types.NUMERIC);
51  				break;
52  			default:
53  				throw new IllegalArgumentException("Event not supported: " + event);
54  			}
55  			if (note == null){
56  				stm.setNull(4, Types.VARCHAR);
57  			} else {
58  				stm.setString(4, note);
59  			}
60  			stm.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
61  			stm.executeUpdate();
62      	} finally {
63      		if (stm != null) stm.close();
64      	}
65  	}
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      	PreparedStatement ps = null;
78      	try {
79      		ps = con.prepareStatement(UPDATE_EVENT_NOTE_SQL);
80      		int n = 1;
81      		ps.setString(n++, newNote);
82      		ps.setInt(n++, eventId);
83      		ps.setInt(n++, groupId);
84      		ps.executeUpdate();
85      	} finally {
86      		if (ps != null) ps.close();
87      	}
88      }
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     	PreparedStatement ps = null;
104     	try {
105     		ps = con.prepareStatement(UPDATE_EVENT_SQL);
106     		int n = 1;
107     		if (beforeId == null){
108     			ps.setNull(n++, Types.INTEGER);
109     		} else {
110     			ps.setInt(n++, beforeId);
111     		}
112     		if (afterId == null){
113     			ps.setNull(n++, Types.INTEGER);
114     		} else {
115     			ps.setInt(n++, afterId);
116     		}
117     		ps.setString(n++, newNote);
118     		ps.setInt(n++, eventId);
119     		ps.setInt(n++, groupId);
120     		ps.executeUpdate();
121     	} finally {
122     		if (ps != null) ps.close();
123     	}
124     }
125 }