Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/ScheduleGateActivity.java =================================================================== diff -u -r12d1f5a1272e856f37ca62fabd89a0a88ea82041 -r478b2795849c855847b5fe6d84f6a8fb9250b742 --- lams_common/src/java/org/lamsfoundation/lams/learningdesign/ScheduleGateActivity.java (.../ScheduleGateActivity.java) (revision 12d1f5a1272e856f37ca62fabd89a0a88ea82041) +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/ScheduleGateActivity.java (.../ScheduleGateActivity.java) (revision 478b2795849c855847b5fe6d84f6a8fb9250b742) @@ -10,7 +10,6 @@ import org.apache.commons.lang.builder.ToStringBuilder; import org.lamsfoundation.lams.learningdesign.exception.ActivityBehaviorException; import org.lamsfoundation.lams.learningdesign.strategy.ScheduleGateActivityStrategy; -import org.lamsfoundation.lams.util.DateUtil; /** @@ -40,19 +39,24 @@ *
The relative time that gate will be closed from the lesson start time. * For example, if the lesson starts at 3:00pm and offset is 2, the gate * will be closed at 5:00pm.
+ * * Note it must be larger thangateStartTimeOffset
.
*/
private Long gateEndTimeOffset;
/**
- * The absolute start time of the gate activity. If this is set, we are
- * expecting gateStartTimeOffset
is set to null.
+ * The absolute start time of the gate activity. If this is set, we are
+ * expecting gateStartTimeOffset
is set to null.
+ *
+ * All time value that used for persistent should be UTC time
*/ private Date gateStartDateTime; /** - * The absolute end time of the gate activity. If this is set, we are - * expectinggateEndTimeOffset
is set to null.
+ * The absolute end time of the gate activity. If this is set, we are
+ * expecting gateEndTimeOffset
is set to null.
All time value that used for persistent should be UTC time
*/ private Date gateEndDateTime; @@ -241,53 +245,70 @@ } /** - *Returns the real gate open time according to the settings done by the - * author.
- *If the gate is scheduled against time offset, the real gate open - * time will be the current system time plus the time offset. Otherwise, - * the real gate open time will be the same as start time.
+ *Returns the gate open time for a particular lesson according to the s + * ettings done by the author.
+ * + *If the gate is scheduled against time offset and the scheduler has + * never run this gate before, the lesson gate open time will be the lesson + * start time plus the time offset. Otherwise, the lesson gate open time + * will be the same as start time.
+ * * Note: the time will also be translated against server timezone. + * + * @param lessonStartTime the start time of the lesson. this should be + * the server local time. the UTC time is only used + * for persistent. * - * @return the date time that the gate will be opened. + * @return the server local date time that the gate will be opened. */ - public Date getRealGateOpenTime() + public Date getLessonGateOpenTime(Date lessonStartTime) { - Calendar openTime = new GregorianCalendar(TimeZone.getDefault()); - //compute the real opening time based on the + Calendar openTime = new GregorianCalendar(TimeZone.getDefault()); + openTime.setTime(lessonStartTime); + //compute the real opening time based on the lesson start time. if(isScheduledByTimeOffset()) + { openTime.add(Calendar.MINUTE,getGateStartTimeOffset().intValue()); + this.setGateStartDateTime(openTime.getTime()); + } + else if(isScheduledByDateTime()) - openTime.setTime(DateUtil.convertFromUTCToLocal(TimeZone.getDefault(), - getGateStartDateTime())); + openTime.setTime( getGateStartDateTime()); else throw new ActivityBehaviorException("No way of scheduling has " + "been setup - this usually should be done at authoring " + - "interface. Fail to calculate gate open time."); + "interface. Fail to calculate gate open time for lesson."); return openTime.getTime(); } /** - *Returns the real gate close time according to the settings done by the - * author.
- *If the gate is scheduled against time offset, the real gate close - * time will be the current system time plus the time offset. Otherwise, - * the real gate open time will be the same as close time.
+ *Returns the real gate close time for a particular lesson according to + * the settings done by the author.
+ * + *If the gate is scheduled against time offset, the lesson gate close + * time will be the lesson start time plus the time offset. Otherwise, + * the lesson gate open time will be the same as close time.
+ * * Note: the time will also be translated against proper timezone. + * + * @param lessonStartTime * * @return the date time that the gate will be closed. */ - public Date getRealGateCloseTime() + public Date getLessonGateCloseTime(Date lessonStartTime) { Calendar closeTime = new GregorianCalendar(TimeZone.getDefault()); - long offset = closeTime.get(Calendar.ZONE_OFFSET)+closeTime.get(Calendar.DST_OFFSET); - + closeTime.setTime(lessonStartTime); //compute the real opening time based on the if(isScheduledByTimeOffset()) + { closeTime.add(Calendar.MINUTE,getGateEndTimeOffset().intValue()); + this.setGateEndDateTime(closeTime.getTime()); + } + else if(isScheduledByDateTime()) - closeTime.setTime(DateUtil.convertFromUTCToLocal(TimeZone.getDefault(), - getGateEndDateTime())); + closeTime.setTime(getGateEndDateTime()); else throw new ActivityBehaviorException("No way of scheduling has " + "been setup - this usually should be done at authoring " + Index: lams_common/src/java/org/lamsfoundation/lams/util/DateUtil.java =================================================================== diff -u -r12d1f5a1272e856f37ca62fabd89a0a88ea82041 -r478b2795849c855847b5fe6d84f6a8fb9250b742 --- lams_common/src/java/org/lamsfoundation/lams/util/DateUtil.java (.../DateUtil.java) (revision 12d1f5a1272e856f37ca62fabd89a0a88ea82041) +++ lams_common/src/java/org/lamsfoundation/lams/util/DateUtil.java (.../DateUtil.java) (revision 478b2795849c855847b5fe6d84f6a8fb9250b742) @@ -54,9 +54,13 @@ String str = sdf.format(time); Timestamp gmtLocal = Timestamp.valueOf(str); - return new Date(gmtLocal.getTime()); + Calendar gmtCanlendar = new GregorianCalendar(gmt); + gmtCanlendar.setTimeInMillis(gmtLocal.getTime()); + + return gmtCanlendar.getTime(); } + /** * Convert from UTC time to your local time. Note: it is your * responsibility to pass in the correct UTC date. @@ -71,6 +75,5 @@ long offset = canlendar.get(Calendar.ZONE_OFFSET)+canlendar.get(Calendar.DST_OFFSET); return new Date(UTCDate.getTime()+offset); - } } Index: lams_common/test/java/org/lamsfoundation/lams/learningdesign/TestScheduleGateActivity.java =================================================================== diff -u -r12d1f5a1272e856f37ca62fabd89a0a88ea82041 -r478b2795849c855847b5fe6d84f6a8fb9250b742 --- lams_common/test/java/org/lamsfoundation/lams/learningdesign/TestScheduleGateActivity.java (.../TestScheduleGateActivity.java) (revision 12d1f5a1272e856f37ca62fabd89a0a88ea82041) +++ lams_common/test/java/org/lamsfoundation/lams/learningdesign/TestScheduleGateActivity.java (.../TestScheduleGateActivity.java) (revision 478b2795849c855847b5fe6d84f6a8fb9250b742) @@ -22,11 +22,13 @@ package org.lamsfoundation.lams.learningdesign; +import java.text.SimpleDateFormat; import java.util.Calendar; +import java.util.Date; import java.util.GregorianCalendar; +import java.util.TimeZone; import org.apache.log4j.Logger; -import org.lamsfoundation.lams.util.DateUtil; import junit.framework.TestCase; @@ -49,6 +51,7 @@ private ScheduleGateActivity gateByDateTime; private static final Long START_TIME_OFFSET = new Long(30); private static final Long END_TIME_OFFSET = new Long(60); + private Date lessonStartTime; /* * @see TestCase#setUp() */ @@ -65,11 +68,20 @@ //convert current system time to utc Calendar now = new GregorianCalendar(); - now.setTime(DateUtil.convertToUTC(now.getTime())); + now.setTime(now.getTime()); gateByDateTime.setGateStartDateTime(now.getTime()); now.add(Calendar.MINUTE,END_TIME_OFFSET.intValue()); gateByDateTime.setGateEndDateTime(now.getTime()); + + //setup a lesson start time. + Calendar lessonStart = new GregorianCalendar(2005, + Calendar.APRIL, + 20, + 9, + 50); + lessonStartTime = lessonStart.getTime(); + log.info("new lessonStartTime--"+lessonStartTime); } /* @@ -89,39 +101,52 @@ super(arg0); } - public void testGetRealGateOpenTime() + public void testGetLessonGateOpenTime() { - assertNotNull("verify real open time by offset",gateByTimeOffset.getRealGateOpenTime()); + assertNotNull("verify real open time by offset",gateByTimeOffset.getLessonGateOpenTime(lessonStartTime)); log.info("gate open time offset -- "+gateByTimeOffset.getGateStartTimeOffset()); - log.info("Real gate open time by offset --"+gateByTimeOffset.getRealGateOpenTime()); + log.info("Real gate open time by offset --"+gateByTimeOffset.getLessonGateOpenTime(lessonStartTime)); + log.info("gate open time by offset, calculated open time --"+gateByTimeOffset.getGateStartDateTime()); - assertNotNull("verify real open time by date time",gateByDateTime.getRealGateOpenTime()); - log.info("gate open date time(UTC) -- "+gateByDateTime.getGateStartDateTime()); - log.info("Real gate open time by date time -- "+gateByDateTime.getRealGateOpenTime()); + assertNotNull("verify real open time by date time",gateByDateTime.getLessonGateOpenTime(lessonStartTime)); + //log.info("gate open date time(UTC) -- "+gateByDateTime.getGateStartDateTime()); + //log.info("Just for testing:"+this.formatUTCTime(gateByDateTime.getGateStartDateTime())); + log.info("Real gate open time by date time -- "+gateByDateTime.getLessonGateOpenTime(lessonStartTime)); } - public void testGetRealGateCloseTime() + public void testGetLessonGateCloseTime() { - assertNotNull("verify real open time",gateByTimeOffset.getRealGateCloseTime()); + assertNotNull("verify real open time",gateByTimeOffset.getLessonGateCloseTime(lessonStartTime)); log.info("gate close time offset -- "+gateByTimeOffset.getGateEndTimeOffset()); - log.info("gate close time by offset --"+gateByTimeOffset.getRealGateCloseTime()); + log.info("gate close time by offset --"+gateByTimeOffset.getLessonGateCloseTime(lessonStartTime)); + log.info("gate close time by offset, calculated close time --"+gateByTimeOffset.getGateEndDateTime()); + - assertNotNull("verify real close time by date time",gateByDateTime.getRealGateCloseTime()); - log.info("gate close date time(UTC) -- "+gateByDateTime.getGateEndDateTime()); - log.info("Real gate close time by date time -- "+gateByDateTime.getRealGateCloseTime()); + assertNotNull("verify real close time by date time",gateByDateTime.getLessonGateCloseTime(lessonStartTime)); + //log.info("gate close date time(UTC) -- "+gateByDateTime.getGateEndDateTime()); + log.info("Real gate close time by date time -- "+gateByDateTime.getLessonGateCloseTime(lessonStartTime)); } - public void testGetRealCloseTimeWithLongOffset() + public void testGetLessonCloseTimeWithLongOffset() { Calendar cal = new GregorianCalendar(); cal.setTime(gateByDateTime.getGateEndDateTime()); cal.add(Calendar.MINUTE,END_TIME_OFFSET.intValue()*24); gateByDateTime.setGateEndDateTime(cal.getTime()); - assertNotNull("verify real close time by date time",gateByDateTime.getRealGateCloseTime()); + assertNotNull("verify real close time by date time",gateByDateTime.getLessonGateCloseTime(lessonStartTime)); log.info("gate close date time(UTC) -- "+gateByDateTime.getGateEndDateTime()); - log.info("Real gate close time by date time -- "+gateByDateTime.getRealGateCloseTime()); + log.info("Real gate close time by date time -- "+gateByDateTime.getLessonGateCloseTime(lessonStartTime)); } + + private String formatUTCTime(Date time) + { + TimeZone gmt = TimeZone.getTimeZone("Etc/GMT"); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss z"); + sdf.setTimeZone(gmt); + String str = sdf.format(time); + return str; + } }