Coverage Report - uk.ac.ebi.intenz.mapper.EnzymeFutureMapper
 
Classes in this File Line Coverage Branch Coverage Complexity
EnzymeFutureMapper
0%
0/99
0%
0/48
5
 
 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  0
     super();
 34  0
   }
 35  
 
 36  
   private String findStatement() {
 37  0
     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  0
     if (enzymeEntry == null) throw new NullPointerException();
 46  0
     HistoryNode currentNode = findNode(enzymeEntry, true, con);
 47  0
     if (currentNode == null)
 48  0
       return null;
 49  
 
 50  0
     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  0
     PreparedStatement findStatement = null;
 63  0
     ResultSet rs = null;
 64  
     try {
 65  0
       findStatement = con.prepareStatement(findStatement());
 66  0
       findStatement.setLong(1, enzymeId.longValue());
 67  0
       findStatement.setLong(2, enzymeId.longValue());
 68  0
       rs = findStatement.executeQuery();
 69  0
       if (rs.next()) {
 70  0
         return true;
 71  
       }
 72  
     } finally {
 73  0
             if (rs != null) rs.close();
 74  0
       findStatement.close();
 75  0
     }
 76  
 
 77  0
     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  0
     HistoryNode historyNode = new HistoryNode();
 87  0
     historyNode.setEnzymeEntry(currentEntry);
 88  0
     historyNode.setRoot(isRoot);
 89  
 
 90  
 // Keep history line loaded before. Ugly code but avoids another DB query.
 91  0
     historyNode.setHistoryLine(currentEntry.getHistory().getRootNode().getHistoryLine());
 92  
 
 93  0
     if (visitedNodes.containsKey(currentEntry.getId())) {
 94  0
       return (HistoryNode) visitedNodes.get(currentEntry.getId()); // Stops recursion.
 95  
     }
 96  
 
 97  0
     visitedNodes.put(currentEntry.getId(), historyNode);
 98  
 
 99  
     // Find event(s)
 100  0
     List futureEvents = findFutureEvents(historyNode, con);
 101  0
     historyNode.setEdges(futureEvents);
 102  
 
 103  0
     return historyNode;
 104  
   }
 105  
 
 106  
   private List findFutureEvents(HistoryNode currentNode, Connection con)
 107  
   throws SQLException, DomainException {
 108  0
     PreparedStatement findStatement = null;
 109  0
     ResultSet rs = null;
 110  0
     Vector result = new Vector();
 111  
 
 112  
     try {
 113  0
       findStatement = con.prepareStatement(findStatement());
 114  0
       findStatement.setLong(1, currentNode.getEnzymeEntry().getId().longValue());
 115  0
       findStatement.setLong(2, currentNode.getEnzymeEntry().getId().longValue());
 116  0
       rs = findStatement.executeQuery();
 117  0
       while (rs.next()) {
 118  0
         result.addElement(doLoad(rs, currentNode, con));
 119  
       }
 120  
     } finally {
 121  0
             if (rs != null) rs.close();
 122  0
             if (findStatement != null) findStatement.close();
 123  
     }
 124  
 
 125  0
     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  0
     long groupId = 0;
 139  0
     long eventId = 0;
 140  0
     int beforeId = 0;
 141  0
     int afterId = 0;
 142  0
     int enzymeId = 0;
 143  0
     Date eventYear = new Date();
 144  0
     Date startDate = null;
 145  0
     Date dueDate = null;
 146  0
     String eventNote = "";
 147  0
     String eventClass = "";
 148  0
     String status = "";
 149  0
     int timeoutId = 0;
 150  
 
 151  0
     if (rs.getInt(1) > 0) groupId = rs.getLong(1);
 152  0
     if (rs.getInt(2) > 0) eventId = rs.getLong(2);
 153  0
     if (rs.getInt(3) > 0) beforeId = rs.getInt(3);
 154  0
     if (rs.getInt(4) > 0) afterId = rs.getInt(4);
 155  0
     if (rs.getDate(5) != null) eventYear = rs.getDate(5);
 156  0
     if (rs.getString(6) != null) eventNote = rs.getString(6);
 157  0
     if (rs.getString(7) != null) eventClass = rs.getString(7);
 158  0
     if (rs.getString(8) != null) status = rs.getString(8);
 159  0
     if (rs.getInt(9) > 0) timeoutId = rs.getInt(9);
 160  0
     if (rs.getInt(10) > 0) enzymeId = rs.getInt(10);
 161  0
     if (rs.getDate(11) != null) startDate = rs.getDate(11);
 162  0
     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  0
     EnzymeEntryMapper enzymeEntryMapper = new EnzymeEntryMapper();
 166  0
     HistoryNode beforeNode = null, afterNode = null;
 167  0
     if (beforeId > 0) {
 168  0
       if (beforeId != currentNode.getEnzymeEntry().getId().longValue()) {
 169  0
         EnzymeEntry ghostEntry = enzymeEntryMapper.findGhostById(beforeId, con);
 170  0
         beforeNode = findNode(ghostEntry, false, con);
 171  0
         afterNode = currentNode;
 172  0
       } else {
 173  0
         beforeNode = currentNode;
 174  
       }
 175  
     }
 176  0
     if (afterId > 0) {
 177  0
       if (afterId != currentNode.getEnzymeEntry().getId().longValue()) {
 178  0
         EnzymeEntry ghostEntry = enzymeEntryMapper.findGhostById(afterId, con);
 179  0
         beforeNode = currentNode;
 180  0
         afterNode = findNode(ghostEntry, false, con);
 181  0
       } else {
 182  0
         afterNode = currentNode;
 183  
       }
 184  
     }
 185  
 
 186  0
     Timeout timeout = new Timeout();
 187  0
     timeout.setEnzymeId(enzymeId);
 188  0
     timeout.setStartDate(startDate);
 189  0
     timeout.setDueDate(dueDate);
 190  0
     timeout.setTimeoutId(timeoutId);
 191  
 
 192  0
     FutureEvent futureEvent = new FutureEvent();
 193  0
     futureEvent.setGroupId(new Long(groupId));
 194  0
     futureEvent.setEventId(new Long(eventId));
 195  0
     futureEvent.setBeforeNode(beforeNode);
 196  0
     futureEvent.setAfterNode(afterNode);
 197  0
     futureEvent.setDate(eventYear);
 198  0
     futureEvent.setNote(eventNote);
 199  0
     futureEvent.setEventClass(EventConstant.valueOf(eventClass));
 200  0
     futureEvent.setStatus(Status.fromCode(status));
 201  0
     futureEvent.setTimeout(timeout);
 202  
 
 203  0
     enzymeEntryMapper.close();
 204  
     
 205  0
     return futureEvent;
 206  
   }
 207  
 
 208  
 }