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.Timestamp;
8 import java.util.Date;
9 import java.util.Hashtable;
10 import java.util.List;
11 import java.util.Vector;
12
13 import org.apache.log4j.Logger;
14
15 import uk.ac.ebi.intenz.domain.constants.EventConstant;
16 import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry;
17 import uk.ac.ebi.intenz.domain.exceptions.DomainException;
18 import uk.ac.ebi.intenz.domain.history.HistoryEvent;
19 import uk.ac.ebi.intenz.domain.history.HistoryGraph;
20 import uk.ac.ebi.intenz.domain.history.HistoryNode;
21
22
23
24
25
26
27
28 public class EnzymeHistoryMapper {
29
30 private Logger LOGGER = Logger.getLogger(EnzymeHistoryMapper.class.getName());
31
32 protected Hashtable visitedNodes;
33
34 private static final String COLUMNS = "group_id, event_id, before_id, after_id, event_year, event_note, event_class";
35
36 public EnzymeHistoryMapper() {
37 visitedNodes = new Hashtable();
38 }
39
40 private String findStatement() {
41 return "SELECT " + COLUMNS +
42 " FROM history_events WHERE before_id = ? OR after_id = ?";
43 }
44
45 public HistoryGraph find(EnzymeEntry enzymeEntry, Connection con)
46 throws SQLException, DomainException {
47 if (enzymeEntry == null) throw new NullPointerException();
48 HistoryNode currentNode = findNode(enzymeEntry, true, con);
49 if (currentNode == null) return null;
50 return new HistoryGraph(currentNode);
51 }
52
53
54
55
56 private HistoryNode findNode(EnzymeEntry currentEntry, boolean isRoot,
57 Connection con)
58 throws SQLException, DomainException {
59 HistoryNode historyNode = new HistoryNode();
60 historyNode.setEnzymeEntry(currentEntry);
61 historyNode.setRoot(isRoot);
62
63
64 historyNode.setHistoryLine(currentEntry.getHistory().getRootNode().getHistoryLine());
65
66 if (visitedNodes.containsKey(currentEntry.getId()))
67 return (HistoryNode) visitedNodes.get(currentEntry.getId());
68
69 visitedNodes.put(currentEntry.getId(), historyNode);
70
71
72 List historyEvents = findHistoryEvents(historyNode, con);
73 historyNode.setEdges(historyEvents);
74
75 return historyNode;
76 }
77
78 private List findHistoryEvents(HistoryNode currentNode, Connection con)
79 throws SQLException, DomainException {
80 PreparedStatement findStatement = null;
81 ResultSet rs = null;
82 Vector result = new Vector();
83
84 try {
85 findStatement = con.prepareStatement(findStatement());
86 findStatement.setLong(1, currentNode.getEnzymeEntry().getId().longValue());
87 findStatement.setLong(2, currentNode.getEnzymeEntry().getId().longValue());
88 rs = findStatement.executeQuery();
89 while (rs.next()) {
90 result.addElement(doLoad(rs, currentNode, con));
91 }
92 } finally {
93 if (rs != null) rs.close();
94 if (findStatement != null) findStatement.close();
95 }
96
97 return result;
98 }
99
100
101
102
103
104
105
106
107
108
109
110 private HistoryEvent doLoad(ResultSet rs, HistoryNode currentNode,
111 Connection con)
112 throws SQLException, DomainException {
113 long groupId = 0;
114 long eventId = 0;
115 int beforeId = 0;
116 int afterId = 0;
117 java.util.Date eventYear = new Date();
118 String eventNote = "";
119 String eventClass = "";
120
121 if (rs.getInt("group_id") > 0) groupId = rs.getLong("group_id");
122 if (rs.getInt("event_id") > 0) eventId = rs.getInt("event_id");
123 if (rs.getInt("before_id") > 0) beforeId = rs.getInt("before_id");
124 if (rs.getInt("after_id") > 0) afterId = rs.getInt("after_id");
125 if (rs.getDate("event_year") != null){
126 Timestamp ts = rs.getTimestamp("event_year");
127 eventYear = new Date(ts.getTime());
128 }
129 if (rs.getString("event_note") != null) eventNote = rs.getString("event_note");
130 if (rs.getString("event_class") != null) eventClass = rs.getString("event_class");
131
132
133 EnzymeEntryMapper enzymeEntryMapper = new EnzymeEntryMapper();
134 HistoryNode beforeNode = null, afterNode = null;
135 if (beforeId > 0) {
136 if (beforeId != currentNode.getEnzymeEntry().getId().longValue()) {
137 EnzymeEntry ghostEntry = enzymeEntryMapper.findGhostById(beforeId, con);
138 beforeNode = findNode(ghostEntry, false, con);
139 afterNode = currentNode;
140 } else {
141 beforeNode = currentNode;
142 }
143 }
144 if (afterId > 0) {
145 if (afterId != currentNode.getEnzymeEntry().getId().longValue()) {
146 EnzymeEntry ghostEntry = enzymeEntryMapper.findGhostById(afterId, con);
147 beforeNode = currentNode;
148 afterNode = findNode(ghostEntry, false, con);
149 } else {
150 afterNode = currentNode;
151 }
152 }
153
154 HistoryEvent historyEvent = new HistoryEvent();
155 historyEvent.setGroupId(new Long(groupId));
156 historyEvent.setEventId(new Long(eventId));
157 historyEvent.setBeforeNode(beforeNode);
158 historyEvent.setAfterNode(afterNode);
159 historyEvent.setDate(eventYear);
160 historyEvent.setNote(eventNote);
161 historyEvent.setEventClass(EventConstant.valueOf(eventClass));
162
163 enzymeEntryMapper.close();
164
165 return historyEvent;
166 }
167
168 }