[Tynstep-svn] r39 - in trunk/step-web-app/src/main/java/com/tyndalehouse/step/web: client/presenter server/handler shared/command shared/result

ChrisBurrell at crosswire.org ChrisBurrell at crosswire.org
Sat Dec 12 15:09:51 MST 2009


Author: ChrisBurrell
Date: 2009-12-12 15:09:51 -0700 (Sat, 12 Dec 2009)
New Revision: 39

Added:
   trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/handler/GetTimelineOriginForScriptureHandler.java
   trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/shared/command/GetTimelineOriginForScriptureCommand.java
   trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/shared/result/GetTimelineOriginForScriptureResult.java
Modified:
   trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/client/presenter/TimelinePresenter.java
   trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/handler/GetEventsForDateRangeHandler.java
Log:


Modified: trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/client/presenter/TimelinePresenter.java
===================================================================
--- trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/client/presenter/TimelinePresenter.java	2009-12-12 22:06:30 UTC (rev 38)
+++ trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/client/presenter/TimelinePresenter.java	2009-12-12 22:09:51 UTC (rev 39)
@@ -24,10 +24,14 @@
 import com.tyndalehouse.step.web.shared.beans.TimelineBean;
 import com.tyndalehouse.step.web.shared.beans.TimelineEventBean;
 import com.tyndalehouse.step.web.shared.command.GetEventsForDateRangeCommand;
+import com.tyndalehouse.step.web.shared.command.GetTimelineOriginForScriptureCommand;
 import com.tyndalehouse.step.web.shared.command.GetTimelineUISetupCommand;
+import com.tyndalehouse.step.web.shared.event.ScriptureChangeEvent;
 import com.tyndalehouse.step.web.shared.event.TimelineScrollEvent;
+import com.tyndalehouse.step.web.shared.eventhandler.ScriptureChangeEventHandler;
 import com.tyndalehouse.step.web.shared.eventhandler.TimelineScrollEventHandler;
 import com.tyndalehouse.step.web.shared.result.GetEventsForDateRangeResult;
+import com.tyndalehouse.step.web.shared.result.GetTimelineOriginForScriptureResult;
 import com.tyndalehouse.step.web.shared.result.GetTimelineUISetupResult;
 
 public class TimelinePresenter extends WidgetPresenter<TimelinePresenter.Display> {
@@ -147,6 +151,38 @@
 			}
 		});
 
+		eventBus.addHandler(ScriptureChangeEvent.TYPE, new ScriptureChangeEventHandler() {
+
+			@Override
+			public void onScriptureChange(ScriptureChangeEvent event) {
+				Log.debug("Scripture Change event captured");
+				
+				dispatcher.execute(new GetTimelineOriginForScriptureCommand(event.getNewReference()), 
+						new DisplayCallback<GetTimelineOriginForScriptureResult>(display) {
+
+							@Override
+							protected void handleFailure(Throwable e) {
+								Log.error("Failed to get timeline origin", e);
+							}
+
+							@Override
+							protected void handleSuccess(GetTimelineOriginForScriptureResult value) {
+								if(value.isEmpty()) {
+									//do nothing - the reference submitted was either invalid or had no events to 
+									//be shown.
+								} else {
+									Timeline tl = display.getTimeline();
+									tl.repaint(value.getOriginDate());
+								}
+							}
+					
+				});
+				
+				//so now we update the timeline, and we need to work out
+				//where to go. 
+				
+			}
+		});
 		
 		// eventBus.addHandler(NewArticleSelectedEvent.TYPE, new
 		// NewArticleSelectedEventHandler() {
@@ -182,18 +218,18 @@
 		// we could avoid the copying here, since the timline bean
 		// is essentially what is needed, but nice to have it decoupled.
 		for (TimelineBean tb : setupData.getTimelines()) {
-			TimeBand band = new TimeBand(timeline, tb.getTimelineId());
+			TimeBand band = new TimeBand(timeline, tb.getTimelineId(), tb.getTimelineDescription());
 
 			// TODO: change the scale dynamically
 			band.setPixelsPerUnit(100);
-			band.setHeight(100);
+			band.setHeight(400);
 			band.setUnit(Unit.DECADE);		//TODO: what if i want 8 years, or 25 years, etc.
 
 			// set the date to the middle of the band: TODO: again something
 			// that needs changing.
 			//band.setCurrentDate((tb.getMaxDate() + tb.getMinDate()) / 2);
 			band.setCurrentDate(-61183987174853L); //one of the events in Jesus' life
-			band.setCurrentDateX(400);
+			band.setCurrentDateX(32000);
 			timeline.addBand(band);
 		}
 

Modified: trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/handler/GetEventsForDateRangeHandler.java
===================================================================
--- trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/handler/GetEventsForDateRangeHandler.java	2009-12-12 22:06:30 UTC (rev 38)
+++ trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/handler/GetEventsForDateRangeHandler.java	2009-12-12 22:09:51 UTC (rev 39)
@@ -67,6 +67,7 @@
 			GetEventsForDateRangeResult result = new GetEventsForDateRangeResult();
 			
 			for(TimeBandVisibleDate tbvd : timebands) {
+				logger.debug(tbvd.getTimebandId());
 				ps.setLong(1, tbvd.getMinDate());
 				ps.setLong(2, tbvd.getMaxDate());
 				ps.setLong(3, tbvd.getMinDate());

Added: trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/handler/GetTimelineOriginForScriptureHandler.java
===================================================================
--- trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/handler/GetTimelineOriginForScriptureHandler.java	                        (rev 0)
+++ trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/handler/GetTimelineOriginForScriptureHandler.java	2009-12-12 22:09:51 UTC (rev 39)
@@ -0,0 +1,134 @@
+package com.tyndalehouse.step.web.server.handler;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.text.SimpleDateFormat;
+
+import net.customware.gwt.dispatch.server.ActionHandler;
+import net.customware.gwt.dispatch.server.ExecutionContext;
+import net.customware.gwt.dispatch.shared.ActionException;
+
+import org.apache.commons.logging.Log;
+import org.crosswire.jsword.passage.Key;
+import org.crosswire.jsword.passage.KeyFactory;
+import org.crosswire.jsword.passage.NoSuchKeyException;
+import org.crosswire.jsword.passage.PassageKeyFactory;
+import org.crosswire.jsword.passage.RestrictionType;
+import org.crosswire.jsword.passage.RocketPassage;
+import org.crosswire.jsword.passage.VerseRange;
+
+import com.extjs.gxt.ui.client.util.DateWrapper.Unit;
+import com.google.inject.Inject;
+import com.tyndalehouse.step.web.server.db.DbProvider;
+import com.tyndalehouse.step.web.shared.beans.TimeBandVisibleDate;
+import com.tyndalehouse.step.web.shared.command.GetTimelineOriginForScriptureCommand;
+import com.tyndalehouse.step.web.shared.result.GetEventsForDateRangeResult;
+import com.tyndalehouse.step.web.shared.result.GetTimelineOriginForScriptureResult;
+
+public class GetTimelineOriginForScriptureHandler implements
+		ActionHandler<GetTimelineOriginForScriptureCommand, GetTimelineOriginForScriptureResult> {
+	private final Log logger;
+
+	@Inject
+	public GetTimelineOriginForScriptureHandler(Log logger) {
+		this.logger = logger;
+	}
+
+	@Override
+	//TODO: build a proper sql statement to query all time bands in one go
+	//TODO: this needs to change to take into account the three different types of date precision but for now,
+	//let's get something working
+	public GetTimelineOriginForScriptureResult execute(
+			GetTimelineOriginForScriptureCommand event, ExecutionContext arg1)
+			throws ActionException {
+
+		Connection conn = null;
+		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy G");
+
+		//TODO: do some cleaning up for DB code...
+		try {
+			conn = DbProvider.getConnection();
+			
+			//TODO: this is bad - look at JSword to work out how to properly get the verserange out
+			RocketPassage passage = (RocketPassage) resolveScriptureToVerseNumbers(event.getScriptureReference());
+			
+			//TODO: i guess this could come back with no range - check this here.
+			
+			//TODO: we're doing only the first range - ignoring others... What would we do with several ranges?
+			VerseRange range = passage.getRangeAt(0, RestrictionType.NONE);
+			int startVerseNo = range.getStart().getOrdinal();
+			int endVerseNo =  range.getEnd().getOrdinal();
+			
+			StringBuilder sql = new StringBuilder();
+			sql.append("select avg(ev.from_date) as ORIGIN from step.scripture_reference sr, step.event ev ");
+			sql.append("where target_type = 1 and ");
+			sql.append("((? between start_verse_id and end_verse_id) "); //-- the query start is between the event range
+			sql.append("or (? between start_verse_id and end_verse_id) "); //-- the query end is between the event range
+			sql.append("or (? < start_verse_id and ? > start_verse_id) "); //-- the query start is before the event start, but the end is after the event start
+			sql.append("or (? > end_verse_id and ? < end_verse_id)) "); //-- the end of the range is greater than event end, but the start is before the end
+			sql.append("and sr.TARGET_ID = ev.EVENT_ID");
+			  
+			//TODO: move all the prepared statements somewhere else so that
+			PreparedStatement ps = conn.prepareStatement(sql.toString());
+			int argPosition = 1;
+			ps.setInt(argPosition++, startVerseNo);
+			ps.setInt(argPosition++, endVerseNo);
+			ps.setInt(argPosition++, startVerseNo);
+			ps.setInt(argPosition++, endVerseNo);
+			ps.setInt(argPosition++, endVerseNo);
+			ps.setInt(argPosition++, startVerseNo);
+			
+			ResultSet rs = ps.executeQuery();
+						
+			//TODO: suggested scale needs to be looked up from timescale band table -
+			//and need to add that to the initial loading up
+			
+			if(rs.next()) {
+				return new GetTimelineOriginForScriptureResult(rs.getLong("ORIGIN"), 0L);
+			} else {
+				//no passages found, so return an empty result
+				return new GetTimelineOriginForScriptureResult(true);
+			}
+						
+		} catch (SQLException e) {
+			logger.error(e);
+			throw new ActionException(e);
+		} catch (NoSuchKeyException e) {
+			logger.warn("Could not resolve verse number", e);
+			return new GetTimelineOriginForScriptureResult(true);
+		} finally {
+			try {
+				DbProvider.finaliseConnection(conn);
+			} catch (SQLException e) {
+				logger.error("An error occured while trying to tear down the connection", e);
+			}
+		}
+	}
+
+
+	/**
+	 * resolves a string to a range of verses
+	 * @throws NoSuchKeyException 
+	 */
+	private Key resolveScriptureToVerseNumbers(String scriptureReference) throws NoSuchKeyException {
+		KeyFactory keyFactory = PassageKeyFactory.instance();
+		return keyFactory.getKey(scriptureReference);
+		
+		
+	}
+
+	//This method is used to determine which type of command this handler serves I believe...
+	@Override
+	public Class<GetTimelineOriginForScriptureCommand> getActionType() {
+			return GetTimelineOriginForScriptureCommand.class;
+	}
+
+	@Override
+	public void rollback(GetTimelineOriginForScriptureCommand arg0,
+			GetTimelineOriginForScriptureResult arg1, ExecutionContext arg2)
+			throws ActionException {
+		
+		
+	}}
\ No newline at end of file

Added: trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/shared/command/GetTimelineOriginForScriptureCommand.java
===================================================================
--- trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/shared/command/GetTimelineOriginForScriptureCommand.java	                        (rev 0)
+++ trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/shared/command/GetTimelineOriginForScriptureCommand.java	2009-12-12 22:09:51 UTC (rev 39)
@@ -0,0 +1,49 @@
+package com.tyndalehouse.step.web.shared.command;
+
+import net.customware.gwt.dispatch.shared.Action;
+
+import com.tyndalehouse.step.web.shared.result.GetTimelineOriginForScriptureResult;
+
+public class GetTimelineOriginForScriptureCommand  implements Action<GetTimelineOriginForScriptureResult> {
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = -6454957611866927920L;
+	/**
+	 * default uid
+	 */
+
+	private String scriptureReference;
+
+	/** 
+	 * default constructor that should be used
+	 */
+	public GetTimelineOriginForScriptureCommand(String scriptureReference) {
+		this.scriptureReference = scriptureReference;
+	}
+	
+	
+	/**
+	 * Used for serialization
+	 */	
+	@SuppressWarnings("unused")
+	private GetTimelineOriginForScriptureCommand() {
+		
+	}
+	
+	
+	/**
+	 * @return the scriptureReference
+	 */
+	public String getScriptureReference() {
+		return scriptureReference;
+	}
+	
+	
+	/**
+	 * @param scriptureReference the scriptureReference to set
+	 */
+	public void setScriptureReference(String scriptureReference) {
+		this.scriptureReference = scriptureReference;
+	}
+}

Added: trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/shared/result/GetTimelineOriginForScriptureResult.java
===================================================================
--- trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/shared/result/GetTimelineOriginForScriptureResult.java	                        (rev 0)
+++ trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/shared/result/GetTimelineOriginForScriptureResult.java	2009-12-12 22:09:51 UTC (rev 39)
@@ -0,0 +1,78 @@
+package com.tyndalehouse.step.web.shared.result;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import net.customware.gwt.dispatch.shared.Result;
+
+import com.tyndalehouse.step.web.shared.beans.TimelineEventBean;
+
+public class GetTimelineOriginForScriptureResult implements Result {
+
+
+	/**
+	 * generated serial version id 
+	 */
+	private static final long serialVersionUID = 4256230588330550554L;
+	private Long originDate;
+	private Long suggestedTimeScale;
+	private boolean isEmpty;
+	
+	/**
+	 * @return the isEmpty
+	 */
+	public boolean isEmpty() {
+		return isEmpty;
+	}
+
+	/**
+	 * @param isEmpty the isEmpty to set
+	 */
+	public void setEmpty(boolean isEmpty) {
+		this.isEmpty = isEmpty;
+	}
+
+	public GetTimelineOriginForScriptureResult(Long originDate, Long suggestedTimeScale) {
+		this.setOriginDate(originDate);
+		this.setSuggestedTimeScale(suggestedTimeScale);
+		isEmpty = false;
+	}
+	
+	public GetTimelineOriginForScriptureResult(boolean isEmpty) {
+		this.isEmpty = isEmpty;
+	}
+	
+	@SuppressWarnings("unused")
+	private GetTimelineOriginForScriptureResult() {
+		
+	}
+
+	/**
+	 * @param originDate the originDate to set
+	 */
+	public void setOriginDate(Long originDate) {
+		this.originDate = originDate;
+	}
+
+	/**
+	 * @return the originDate
+	 */
+	public Long getOriginDate() {
+		return originDate;
+	}
+
+	/**
+	 * @param suggestedTimeScale the suggestedTimeScale to set
+	 */
+	public void setSuggestedTimeScale(Long suggestedTimeScale) {
+		this.suggestedTimeScale = suggestedTimeScale;
+	}
+
+	/**
+	 * @return the suggestedTimeScale
+	 */
+	public Long getSuggestedTimeScale() {
+		return suggestedTimeScale;
+	}
+
+}




More information about the Tynstep-svn mailing list