Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/ScheduleGateActivity.java =================================================================== RCS file: /usr/local/cvsroot/lams_common/src/java/org/lamsfoundation/lams/learningdesign/ScheduleGateActivity.java,v diff -u -r1.12 -r1.13 --- lams_common/src/java/org/lamsfoundation/lams/learningdesign/ScheduleGateActivity.java 6 Apr 2005 07:34:45 -0000 1.12 +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/ScheduleGateActivity.java 14 Apr 2005 04:43:59 -0000 1.13 @@ -1,10 +1,14 @@ package org.lamsfoundation.lams.learningdesign; import java.io.Serializable; +import java.util.Calendar; import java.util.Date; +import java.util.GregorianCalendar; import java.util.Set; +import java.util.TimeZone; import org.apache.commons.lang.builder.ToStringBuilder; +import org.lamsfoundation.lams.learningdesign.exception.ActivityBehaviorException; import org.lamsfoundation.lams.learningdesign.strategy.ScheduleGateActivityStrategy; @@ -51,19 +55,6 @@ */ private Date gateEndDateTime; - - public Date getGateEndDateTime() { - return gateEndDateTime; - } - public void setGateEndDateTime(Date gateEndDateTime) { - this.gateEndDateTime = gateEndDateTime; - } - public Date getGateStartDateTime() { - return gateStartDateTime; - } - public void setGateStartDateTime(Date gateStartDateTime) { - this.gateStartDateTime = gateStartDateTime; - } /** full constructor */ public ScheduleGateActivity(Long activityId, Integer id, @@ -109,6 +100,11 @@ transitionFrom, gateActivityLevelId, waitingLearners); + //validate pre-condition. + if(gateStartTimeOffset.intValue()>gateEndTimeOffset.intValue()) + throw new IllegalStateException("End time offset must be larger" + + " than start time offset"); + this.gateStartTimeOffset = gateStartTimeOffset; this.gateEndTimeOffset = gateEndTimeOffset; this.simpleActivityStrategy = new ScheduleGateActivityStrategy(); @@ -146,6 +142,9 @@ transitionFrom, gateActivityLevelId, waitingLearners); + if(gateStartTimeOffset.intValue()>gateEndTimeOffset.intValue()) + throw new IllegalStateException("End time offset must be larger" + + " than start time offset"); this.gateStartTimeOffset = gateStartTimeOffset; this.gateEndTimeOffset = gateEndTimeOffset; this.simpleActivityStrategy = new ScheduleGateActivityStrategy(); @@ -195,29 +194,109 @@ return newScheduleGateActivity; } + public Date getGateEndDateTime() + { + return gateEndDateTime; + } + + public void setGateEndDateTime(Date gateEndDateTime) + { + this.gateEndDateTime = gateEndDateTime; + } + + public Date getGateStartDateTime() + { + return gateStartDateTime; + } + + public void setGateStartDateTime(Date gateStartDateTime) + { + this.gateStartDateTime = gateStartDateTime; + } /** * @hibernate.property column="gate_start_date_time" length="20" */ - public Long getGateStartTimeOffset() { + public Long getGateStartTimeOffset() + { return this.gateStartTimeOffset; } - public void setGateStartTimeOffset(Long gateStartTimeOffset) { + public void setGateStartTimeOffset(Long gateStartTimeOffset) + { this.gateStartTimeOffset = gateStartTimeOffset; } /** * @hibernate.property column="gate_end_date_time" length="20" */ - public Long getGateEndTimeOffset() { + public Long getGateEndTimeOffset() + { return this.gateEndTimeOffset; } - public void setGateEndTimeOffset(Long gateEndTimeOffset) { + public void setGateEndTimeOffset(Long gateEndTimeOffset) + { this.gateEndTimeOffset = gateEndTimeOffset; } - public String toString() { + /** + *
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.
+ * Note: the time will also be translated against server timezone. + * + * @return the date time that the gate will be opened. + */ + public Date getRealGateOpenTime() + { + + Calendar openTime = new GregorianCalendar(TimeZone.getDefault()); + long offset = openTime.get(Calendar.ZONE_OFFSET)+openTime.get(Calendar.DST_OFFSET); + //compute the real opening time based on the + if(isScheduledByTimeOffset()) + openTime.add(Calendar.MINUTE,getGateStartTimeOffset().intValue()); + else if(isScheduledByDateTime()) + openTime.setTime(new Date(getGateStartDateTime().getTime()+offset)); + 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."); + + 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.
+ * Note: the time will also be translated against proper timezone. + * + * @return the date time that the gate will be closed. + */ + public Date getRealGateCloseTime() + { + Calendar closeTime = new GregorianCalendar(TimeZone.getDefault()); + long offset = closeTime.get(Calendar.ZONE_OFFSET)+closeTime.get(Calendar.DST_OFFSET); + + //compute the real opening time based on the + if(isScheduledByTimeOffset()) + closeTime.add(Calendar.MINUTE,getGateEndTimeOffset().intValue()); + else if(isScheduledByDateTime()) + closeTime.setTime(new Date(getGateEndDateTime().getTime()+offset)); + else + throw new ActivityBehaviorException("No way of scheduling has " + + "been setup - this usually should be done at authoring " + + "interface. Fail to calculate gate close time."); + + return closeTime.getTime(); + } + + public String toString() + { return new ToStringBuilder(this) .append("activityId", getActivityId()) .toString(); @@ -230,4 +309,21 @@ return false; } + /** + * Helper method that determines the way of sheduling gate. + * @return is the gate scheduled by time offset + */ + private boolean isScheduledByTimeOffset() + { + return getGateStartTimeOffset()!=null&&getGateEndTimeOffset()!=null; + } + + /** + * Helper method that determines the way of sheduling gate. + * @return is the gate scheduled by the exact date time. + */ + private boolean isScheduledByDateTime() + { + return getGateStartDateTime()!=null&&getGateEndDateTime()!=null; + } } Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/exception/ActivityBehaviorException.java =================================================================== RCS file: /usr/local/cvsroot/lams_common/src/java/org/lamsfoundation/lams/learningdesign/exception/ActivityBehaviorException.java,v diff -u --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/exception/ActivityBehaviorException.java 14 Apr 2005 04:43:59 -0000 1.1 @@ -0,0 +1,71 @@ +/*************************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * ***********************************************************************/ + +package org.lamsfoundation.lams.learningdesign.exception; + + +/** + * The exception that indicates error behavior occurred when the activity + * is providing its service based on its data. + * + * @author Jacky Fang + * @since 2005-4-14 + * @version 1.1 + * + */ +public class ActivityBehaviorException extends RuntimeException +{ + + /** + * + */ + public ActivityBehaviorException() + { + super(); + } + + /** + * @param message + */ + public ActivityBehaviorException(String message) + { + super(message); + } + + /** + * @param cause + */ + public ActivityBehaviorException(Throwable cause) + { + super(cause); + } + + /** + * @param message + * @param cause + */ + public ActivityBehaviorException(String message, Throwable cause) + { + super(message, cause); + } + +} Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/strategy/ScheduleGateActivityStrategy.java =================================================================== RCS file: /usr/local/cvsroot/lams_common/src/java/org/lamsfoundation/lams/learningdesign/strategy/ScheduleGateActivityStrategy.java,v diff -u -r1.1 -r1.2 --- lams_common/src/java/org/lamsfoundation/lams/learningdesign/strategy/ScheduleGateActivityStrategy.java 6 Apr 2005 07:34:45 -0000 1.1 +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/strategy/ScheduleGateActivityStrategy.java 14 Apr 2005 04:43:59 -0000 1.2 @@ -57,8 +57,8 @@ /** * Regarding schedule gate, we don't validate the open condition for the * learner because the decision of opening the gate or not comes from the - * system scheduler. Lams open the gate when the start time is reached and - * closed the gate when the end time is reached. + * system scheduler. Lams opens the gate when the start time is reached and + * closes the gate when the end time is reached. * * @see org.lamsfoundation.lams.learningdesign.strategy.GateActivityStrategy#isOpenConditionMet() */ @@ -67,6 +67,4 @@ { return activity.getGateOpen().booleanValue(); } - - } Index: lams_common/test/java/org/lamsfoundation/lams/learningdesign/TestScheduleGateActivity.java =================================================================== RCS file: /usr/local/cvsroot/lams_common/test/java/org/lamsfoundation/lams/learningdesign/Attic/TestScheduleGateActivity.java,v diff -u --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lams_common/test/java/org/lamsfoundation/lams/learningdesign/TestScheduleGateActivity.java 14 Apr 2005 04:43:59 -0000 1.1 @@ -0,0 +1,121 @@ +/*************************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * ***********************************************************************/ + +package org.lamsfoundation.lams.learningdesign; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.sql.Timestamp; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +import org.apache.log4j.Logger; + +import junit.framework.TestCase; + + +/** + * + * @author Jacky Fang + * @since 2005-4-14 + * @version + * + */ +public class TestScheduleGateActivity extends TestCase +{ + //--------------------------------------------------------------------- + // Instance variables + //--------------------------------------------------------------------- + private static Logger log = Logger.getLogger(TestScheduleGateActivity.class); + + private ScheduleGateActivity gateByTimeOffset; + private ScheduleGateActivity gateByDateTime; + private static final Long START_TIME_OFFSET = new Long(30); + private static final Long END_TIME_OFFSET = new Long(60); + /* + * @see TestCase#setUp() + */ + protected void setUp() throws Exception + { + super.setUp(); + gateByTimeOffset = new ScheduleGateActivity(); + gateByTimeOffset.setGateStartTimeOffset(START_TIME_OFFSET); + gateByTimeOffset.setGateEndTimeOffset(END_TIME_OFFSET); + + //String tzid[] = TimeZone.getAvailableIDs(); + gateByDateTime = new ScheduleGateActivity(); + + //convert current system time to utc + Calendar now = new GregorianCalendar(); + TimeZone gmt = TimeZone.getTimeZone("Etc/GMT"); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); + sdf.setTimeZone(gmt); + String str = sdf.format(now.getTime()); + Timestamp gmtNow = Timestamp.valueOf(str); + now.setTime(new Date(gmtNow.getTime())); + gateByDateTime.setGateStartDateTime(now.getTime()); + + now.add(Calendar.MINUTE,END_TIME_OFFSET.intValue()); + gateByDateTime.setGateEndDateTime(now.getTime()); + } + + /* + * @see TestCase#tearDown() + */ + protected void tearDown() throws Exception + { + super.tearDown(); + } + + /** + * Constructor for TestScheduleGateActivity. + * @param arg0 + */ + public TestScheduleGateActivity(String arg0) + { + super(arg0); + } + + public void testGetRealGateOpenTime() + { + assertNotNull("verify real open time by offset",gateByTimeOffset.getRealGateOpenTime()); + log.info("gate open time offset -- "+gateByTimeOffset.getGateStartTimeOffset()); + log.info("Real gate open time by offset --"+gateByTimeOffset.getRealGateOpenTime()); + + 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()); + } + + public void testGetRealGateCloseTime() + { + assertNotNull("verify real open time",gateByTimeOffset.getRealGateCloseTime()); + log.info("gate open time offset -- "+gateByTimeOffset.getGateEndTimeOffset()); + log.info("Gate Close time by offset --"+gateByTimeOffset.getRealGateCloseTime()); + + 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()); + } + +}