| 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 | 2 | 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 | 2 | public EnzymeHistoryMapper() { |
| 37 | 2 | visitedNodes = new Hashtable(); |
| 38 | 2 | } |
| 39 | |
|
| 40 | |
private String findStatement() { |
| 41 | 2 | 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 | 2 | if (enzymeEntry == null) throw new NullPointerException(); |
| 48 | 2 | HistoryNode currentNode = findNode(enzymeEntry, true, con); |
| 49 | 2 | if (currentNode == null) return null; |
| 50 | 2 | 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 | 2 | HistoryNode historyNode = new HistoryNode(); |
| 60 | 2 | historyNode.setEnzymeEntry(currentEntry); |
| 61 | 2 | historyNode.setRoot(isRoot); |
| 62 | |
|
| 63 | |
|
| 64 | 2 | historyNode.setHistoryLine(currentEntry.getHistory().getRootNode().getHistoryLine()); |
| 65 | |
|
| 66 | 2 | if (visitedNodes.containsKey(currentEntry.getId())) |
| 67 | 0 | return (HistoryNode) visitedNodes.get(currentEntry.getId()); |
| 68 | |
|
| 69 | 2 | visitedNodes.put(currentEntry.getId(), historyNode); |
| 70 | |
|
| 71 | |
|
| 72 | 2 | List historyEvents = findHistoryEvents(historyNode, con); |
| 73 | 2 | historyNode.setEdges(historyEvents); |
| 74 | |
|
| 75 | 2 | return historyNode; |
| 76 | |
} |
| 77 | |
|
| 78 | |
private List findHistoryEvents(HistoryNode currentNode, Connection con) |
| 79 | |
throws SQLException, DomainException { |
| 80 | 2 | PreparedStatement findStatement = null; |
| 81 | 2 | ResultSet rs = null; |
| 82 | 2 | Vector result = new Vector(); |
| 83 | |
|
| 84 | |
try { |
| 85 | 2 | findStatement = con.prepareStatement(findStatement()); |
| 86 | 2 | findStatement.setLong(1, currentNode.getEnzymeEntry().getId().longValue()); |
| 87 | 2 | findStatement.setLong(2, currentNode.getEnzymeEntry().getId().longValue()); |
| 88 | 2 | rs = findStatement.executeQuery(); |
| 89 | 4 | while (rs.next()) { |
| 90 | 2 | result.addElement(doLoad(rs, currentNode, con)); |
| 91 | |
} |
| 92 | |
} finally { |
| 93 | 2 | if (rs != null) rs.close(); |
| 94 | 2 | if (findStatement != null) findStatement.close(); |
| 95 | |
} |
| 96 | |
|
| 97 | 2 | 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 | 2 | long groupId = 0; |
| 114 | 2 | long eventId = 0; |
| 115 | 2 | int beforeId = 0; |
| 116 | 2 | int afterId = 0; |
| 117 | 2 | java.util.Date eventYear = new Date(); |
| 118 | 2 | String eventNote = ""; |
| 119 | 2 | String eventClass = ""; |
| 120 | |
|
| 121 | 2 | if (rs.getInt("group_id") > 0) groupId = rs.getLong("group_id"); |
| 122 | 2 | if (rs.getInt("event_id") > 0) eventId = rs.getInt("event_id"); |
| 123 | 2 | if (rs.getInt("before_id") > 0) beforeId = rs.getInt("before_id"); |
| 124 | 2 | if (rs.getInt("after_id") > 0) afterId = rs.getInt("after_id"); |
| 125 | 2 | if (rs.getDate("event_year") != null){ |
| 126 | 2 | Timestamp ts = rs.getTimestamp("event_year"); |
| 127 | 2 | eventYear = new Date(ts.getTime()); |
| 128 | |
} |
| 129 | 2 | if (rs.getString("event_note") != null) eventNote = rs.getString("event_note"); |
| 130 | 2 | if (rs.getString("event_class") != null) eventClass = rs.getString("event_class"); |
| 131 | |
|
| 132 | |
|
| 133 | 2 | EnzymeEntryMapper enzymeEntryMapper = new EnzymeEntryMapper(); |
| 134 | 2 | HistoryNode beforeNode = null, afterNode = null; |
| 135 | 2 | if (beforeId > 0) { |
| 136 | 0 | if (beforeId != currentNode.getEnzymeEntry().getId().longValue()) { |
| 137 | 0 | EnzymeEntry ghostEntry = enzymeEntryMapper.findGhostById(beforeId, con); |
| 138 | 0 | beforeNode = findNode(ghostEntry, false, con); |
| 139 | 0 | afterNode = currentNode; |
| 140 | 0 | } else { |
| 141 | 0 | beforeNode = currentNode; |
| 142 | |
} |
| 143 | |
} |
| 144 | 2 | if (afterId > 0) { |
| 145 | 2 | if (afterId != currentNode.getEnzymeEntry().getId().longValue()) { |
| 146 | 0 | EnzymeEntry ghostEntry = enzymeEntryMapper.findGhostById(afterId, con); |
| 147 | 0 | beforeNode = currentNode; |
| 148 | 0 | afterNode = findNode(ghostEntry, false, con); |
| 149 | 0 | } else { |
| 150 | 2 | afterNode = currentNode; |
| 151 | |
} |
| 152 | |
} |
| 153 | |
|
| 154 | 2 | HistoryEvent historyEvent = new HistoryEvent(); |
| 155 | 2 | historyEvent.setGroupId(new Long(groupId)); |
| 156 | 2 | historyEvent.setEventId(new Long(eventId)); |
| 157 | 2 | historyEvent.setBeforeNode(beforeNode); |
| 158 | 2 | historyEvent.setAfterNode(afterNode); |
| 159 | 2 | historyEvent.setDate(eventYear); |
| 160 | 2 | historyEvent.setNote(eventNote); |
| 161 | 2 | historyEvent.setEventClass(EventConstant.valueOf(eventClass)); |
| 162 | |
|
| 163 | 2 | enzymeEntryMapper.close(); |
| 164 | |
|
| 165 | 2 | return historyEvent; |
| 166 | |
} |
| 167 | |
|
| 168 | |
} |