Coverage Report - uk.ac.ebi.intenz.mapper.EnzymeHistoryMapper
 
Classes in this File Line Coverage Branch Coverage Complexity
EnzymeHistoryMapper
84%
62/73
50%
17/34
4.333
 
 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  
  * Maps history event information to the corresponding database tables.
 24  
  *
 25  
  * @author Michael Darsow
 26  
  * @version $Revision: 1.3 $ $Date: 2009/05/26 14:59:09 $
 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  
   // ------------------- PRIVATE METHODS ------------------------
 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  
     // Keep history line loaded before.
 64  2
     historyNode.setHistoryLine(currentEntry.getHistory().getRootNode().getHistoryLine());
 65  
 
 66  2
     if (visitedNodes.containsKey(currentEntry.getId()))
 67  0
       return (HistoryNode) visitedNodes.get(currentEntry.getId()); // Stops recursion.
 68  
 
 69  2
     visitedNodes.put(currentEntry.getId(), historyNode);
 70  
 
 71  
     // Find event(s)
 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  
    * Creates the <code>EnzymeLink</code> object from the given result set.
 102  
    * Some modifications were made so that the timestamp could be included in the Date
 103  
    * object. In java.sql.Date the time is obsoleted to zero and only the date is
 104  
    * valid eg. 06-Jun-2005 23:34:00 in java.sql.Date becomes 06-Jun-2005 00:00:00
 105  
    *
 106  
    * @param rs The result set object.
 107  
    * @return an <code>EnzymeLink</code> instance.
 108  
    * @throws SQLException
 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  
     // Check for relative. Either beforeNode or afterNode is different from current node or one of them is null.
 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  
 }