View Javadoc

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.Date;
8   import java.util.List;
9   import java.util.Vector;
10  
11  import uk.ac.ebi.intenz.domain.constants.EventConstant;
12  import uk.ac.ebi.intenz.domain.constants.Status;
13  import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry;
14  import uk.ac.ebi.intenz.domain.exceptions.DomainException;
15  import uk.ac.ebi.intenz.domain.history.FutureEvent;
16  import uk.ac.ebi.intenz.domain.history.HistoryGraph;
17  import uk.ac.ebi.intenz.domain.history.HistoryNode;
18  import uk.ac.ebi.intenz.domain.history.Timeout;
19  
20  /**
21   * Maps future event information to the corresponding database tables.
22   *
23   * @author Michael Darsow
24   * @version $Revision: 1.3 $ $Date: 2009/05/26 14:59:09 $
25   */
26  public class EnzymeFutureMapper extends EnzymeHistoryMapper {
27  
28    private static final String COLUMNS = "f.group_id, f.event_id, f.before_id, f.after_id, f.event_year, " +
29            "f.event_note, f.event_class, f.status, f.timeout_id, " +
30            "t.enzyme_id, t.start_date, t.due_date";
31  
32    public EnzymeFutureMapper() {
33      super();
34    }
35  
36    private String findStatement() {
37      return "SELECT " + COLUMNS +
38              " FROM future_events f, timeouts t" +
39              " WHERE (before_id = ? OR after_id = ?) AND f.timeout_id = t.timeout_id";
40    }
41  
42      @Override
43    public HistoryGraph find(EnzymeEntry enzymeEntry, Connection con)
44    throws SQLException, DomainException {
45      if (enzymeEntry == null) throw new NullPointerException();
46      HistoryNode currentNode = findNode(enzymeEntry, true, con);
47      if (currentNode == null)
48        return null;
49  
50      return new HistoryGraph(currentNode);
51    }
52  
53    /**
54     * Checks whether an entry in this table exists containing the given enzyme_id.
55     *
56     * @param enzymeId Enzyme ID of the entry.
57     * @param con      The logical connection.
58     * @return a <code>HistoryEvent</code>instance or <code>null</code> if nothing has been found.
59     * @throws SQLException
60     */
61    public boolean futureEventExists(Long enzymeId, Connection con) throws SQLException {
62      PreparedStatement findStatement = null;
63      ResultSet rs = null;
64      try {
65        findStatement = con.prepareStatement(findStatement());
66        findStatement.setLong(1, enzymeId.longValue());
67        findStatement.setLong(2, enzymeId.longValue());
68        rs = findStatement.executeQuery();
69        if (rs.next()) {
70          return true;
71        }
72      } finally {
73      	if (rs != null) rs.close();
74        findStatement.close();
75      }
76  
77      return false;
78    }
79  
80  
81    // ---------------- PRIVATE METHODS ----------------------
82  
83    private HistoryNode findNode(EnzymeEntry currentEntry, boolean isRoot,
84  		  Connection con)
85    throws SQLException, DomainException {
86      HistoryNode historyNode = new HistoryNode();
87      historyNode.setEnzymeEntry(currentEntry);
88      historyNode.setRoot(isRoot);
89  
90  // Keep history line loaded before. Ugly code but avoids another DB query.
91      historyNode.setHistoryLine(currentEntry.getHistory().getRootNode().getHistoryLine());
92  
93      if (visitedNodes.containsKey(currentEntry.getId())) {
94        return (HistoryNode) visitedNodes.get(currentEntry.getId()); // Stops recursion.
95      }
96  
97      visitedNodes.put(currentEntry.getId(), historyNode);
98  
99      // Find event(s)
100     List futureEvents = findFutureEvents(historyNode, con);
101     historyNode.setEdges(futureEvents);
102 
103     return historyNode;
104   }
105 
106   private List findFutureEvents(HistoryNode currentNode, Connection con)
107   throws SQLException, DomainException {
108     PreparedStatement findStatement = null;
109     ResultSet rs = null;
110     Vector result = new Vector();
111 
112     try {
113       findStatement = con.prepareStatement(findStatement());
114       findStatement.setLong(1, currentNode.getEnzymeEntry().getId().longValue());
115       findStatement.setLong(2, currentNode.getEnzymeEntry().getId().longValue());
116       rs = findStatement.executeQuery();
117       while (rs.next()) {
118         result.addElement(doLoad(rs, currentNode, con));
119       }
120     } finally {
121     	if (rs != null) rs.close();
122     	if (findStatement != null) findStatement.close();
123     }
124 
125     return result;
126   }
127 
128   /**
129    * Creates the <code>EnzymeLink</code> object from the given result set.
130    *
131    * @param rs The result set object.
132    * @return an <code>EnzymeLink</code> instance.
133    * @throws SQLException
134    */
135   private FutureEvent doLoad(ResultSet rs, HistoryNode currentNode,
136 		  Connection con)
137   throws SQLException, DomainException {
138     long groupId = 0;
139     long eventId = 0;
140     int beforeId = 0;
141     int afterId = 0;
142     int enzymeId = 0;
143     Date eventYear = new Date();
144     Date startDate = null;
145     Date dueDate = null;
146     String eventNote = "";
147     String eventClass = "";
148     String status = "";
149     int timeoutId = 0;
150 
151     if (rs.getInt(1) > 0) groupId = rs.getLong(1);
152     if (rs.getInt(2) > 0) eventId = rs.getLong(2);
153     if (rs.getInt(3) > 0) beforeId = rs.getInt(3);
154     if (rs.getInt(4) > 0) afterId = rs.getInt(4);
155     if (rs.getDate(5) != null) eventYear = rs.getDate(5);
156     if (rs.getString(6) != null) eventNote = rs.getString(6);
157     if (rs.getString(7) != null) eventClass = rs.getString(7);
158     if (rs.getString(8) != null) status = rs.getString(8);
159     if (rs.getInt(9) > 0) timeoutId = rs.getInt(9);
160     if (rs.getInt(10) > 0) enzymeId = rs.getInt(10);
161     if (rs.getDate(11) != null) startDate = rs.getDate(11);
162     if (rs.getDate(12) != null) dueDate = rs.getDate(12);
163 
164     // Check for relative. Either beforeNode or afterNode is different from current node or one of them is null.
165     EnzymeEntryMapper enzymeEntryMapper = new EnzymeEntryMapper();
166     HistoryNode beforeNode = null, afterNode = null;
167     if (beforeId > 0) {
168       if (beforeId != currentNode.getEnzymeEntry().getId().longValue()) {
169         EnzymeEntry ghostEntry = enzymeEntryMapper.findGhostById(beforeId, con);
170         beforeNode = findNode(ghostEntry, false, con);
171         afterNode = currentNode;
172       } else {
173         beforeNode = currentNode;
174       }
175     }
176     if (afterId > 0) {
177       if (afterId != currentNode.getEnzymeEntry().getId().longValue()) {
178         EnzymeEntry ghostEntry = enzymeEntryMapper.findGhostById(afterId, con);
179         beforeNode = currentNode;
180         afterNode = findNode(ghostEntry, false, con);
181       } else {
182         afterNode = currentNode;
183       }
184     }
185 
186     Timeout timeout = new Timeout();
187     timeout.setEnzymeId(enzymeId);
188     timeout.setStartDate(startDate);
189     timeout.setDueDate(dueDate);
190     timeout.setTimeoutId(timeoutId);
191 
192     FutureEvent futureEvent = new FutureEvent();
193     futureEvent.setGroupId(new Long(groupId));
194     futureEvent.setEventId(new Long(eventId));
195     futureEvent.setBeforeNode(beforeNode);
196     futureEvent.setAfterNode(afterNode);
197     futureEvent.setDate(eventYear);
198     futureEvent.setNote(eventNote);
199     futureEvent.setEventClass(EventConstant.valueOf(eventClass));
200     futureEvent.setStatus(Status.fromCode(status));
201     futureEvent.setTimeout(timeout);
202 
203     enzymeEntryMapper.close();
204     
205     return futureEvent;
206   }
207 
208 }