Index: lams_build/lib/lams/lams-monitoring.jar =================================================================== diff -u -rdddd4ed5ab9b4234f80c7e5a5186f75b3de008d8 -rc9c56707245e3ad1149bf1a5338eb251c96c75d2 Binary files differ Index: lams_build/lib/lams/lams.jar =================================================================== diff -u -r8e40b9fa63d28d09b78889d2c46eee7b24bf8c10 -rc9c56707245e3ad1149bf1a5338eb251c96c75d2 Binary files differ Index: lams_common/build.xml =================================================================== diff -u -r018ca38fe0d669447e24067a46d23b2b6ca940d2 -rc9c56707245e3ad1149bf1a5338eb251c96c75d2 --- lams_common/build.xml (.../build.xml) (revision 018ca38fe0d669447e24067a46d23b2b6ca940d2) +++ lams_common/build.xml (.../build.xml) (revision c9c56707245e3ad1149bf1a5338eb251c96c75d2) @@ -240,6 +240,13 @@ + + + + + + Index: lams_common/conf/hibernate/mappings/org/lamsfoundation/lams/lesson/Lesson.hbm.xml =================================================================== diff -u -r6b6cdd81e40d69950386be7442f4fc915d7f7ef5 -rc9c56707245e3ad1149bf1a5338eb251c96c75d2 --- lams_common/conf/hibernate/mappings/org/lamsfoundation/lams/lesson/Lesson.hbm.xml (.../Lesson.hbm.xml) (revision 6b6cdd81e40d69950386be7442f4fc915d7f7ef5) +++ lams_common/conf/hibernate/mappings/org/lamsfoundation/lams/lesson/Lesson.hbm.xml (.../Lesson.hbm.xml) (revision c9c56707245e3ad1149bf1a5338eb251c96c75d2) @@ -87,12 +87,15 @@ FROM lams_lesson {lesson}, lams_grouping, lams_group, - lams_user_group + lams_user_group, + lams_learning_design WHERE ({lesson}.class_grouping_id = lams_grouping.grouping_id) AND (lams_grouping.grouping_id = lams_group.grouping_id) AND (lams_group.group_id = lams_user_group.group_id) AND ({lesson}.lesson_state_id IN (3,5)) AND (lams_user_group.user_id = :userId) + AND ({lesson}.learning_design_id = lams_learning_design.learning_design_id) + AND (lams_learning_design.copy_type_id=2) Index: lams_common/src/java/org/lamsfoundation/lams/lesson/dao/ILessonDAO.java =================================================================== diff -u -r3edea683b9e1d87e68250b49fed5e46dfe0015bb -rc9c56707245e3ad1149bf1a5338eb251c96c75d2 --- lams_common/src/java/org/lamsfoundation/lams/lesson/dao/ILessonDAO.java (.../ILessonDAO.java) (revision 3edea683b9e1d87e68250b49fed5e46dfe0015bb) +++ lams_common/src/java/org/lamsfoundation/lams/lesson/dao/ILessonDAO.java (.../ILessonDAO.java) (revision c9c56707245e3ad1149bf1a5338eb251c96c75d2) @@ -6,6 +6,7 @@ package org.lamsfoundation.lams.lesson.dao; +import java.util.Date; import java.util.List; import org.lamsfoundation.lams.lesson.Lesson; @@ -55,13 +56,13 @@ public void updateLesson(Lesson lesson); /** - * Returns the list of available Lessons for - * a given user. Does not return disabled lessons. - * + * Returns the list of available Lessons created by + * a given user. Does not return disabled lessons or preview lessons. + * * @param userID The user_id of the user * @return List The list of Lessons for the given user */ - public List getLessonsForUser(Integer userID); + public List getLessonsCreatedByUser(Integer userID); /** * Returns the all the learners that have started the requested lesson. @@ -70,4 +71,12 @@ * @return the list of learners. */ public List getActiveLearnerByLesson(final long lessonId); + + /** + * Get all the preview lessons more with the creation date before the given date. + * + * @param startDate UTC date + * @return the list of Lessons + */ + public List getPreviewLessonsBeforeDate(final Date startDate); } Index: lams_common/src/java/org/lamsfoundation/lams/lesson/dao/hibernate/LessonDAO.java =================================================================== diff -u -r8bb3794080e8efa275c1bdf8fafa127569c345a2 -rc9c56707245e3ad1149bf1a5338eb251c96c75d2 --- lams_common/src/java/org/lamsfoundation/lams/lesson/dao/hibernate/LessonDAO.java (.../LessonDAO.java) (revision 8bb3794080e8efa275c1bdf8fafa127569c345a2) +++ lams_common/src/java/org/lamsfoundation/lams/lesson/dao/hibernate/LessonDAO.java (.../LessonDAO.java) (revision c9c56707245e3ad1149bf1a5338eb251c96c75d2) @@ -6,13 +6,15 @@ package org.lamsfoundation.lams.lesson.dao.hibernate; +import java.util.Date; import java.util.List; import org.hibernate.FetchMode; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.criterion.Expression; +import org.lamsfoundation.lams.learningdesign.LearningDesign; import org.lamsfoundation.lams.lesson.Lesson; import org.lamsfoundation.lams.lesson.dao.ILessonDAO; import org.lamsfoundation.lams.usermanagement.User; @@ -25,10 +27,17 @@ */ public class LessonDAO extends HibernateDaoSupport implements ILessonDAO { - private static final String TABLENAME ="lams_lesson"; - private final static String FIND_BY_USER="from " + TABLENAME + - " in class " + Lesson.class.getName() + - " where user_id=? and lesson_state_id <= 6"; + private final static String FIND_LESSON_BY_CREATOR="from " + + Lesson.class.getName() + + " lesson where lesson.user.userId=? and lesson.lessonStateId <= 6 and " + +" lesson.learningDesign.copyTypeID=" + + LearningDesign.COPY_TYPE_LESSON; + private final static String FIND_PREVIEW_BEFORE_START_DATE= "from " + + Lesson.class.getName() + + " lesson where lesson.learningDesign.copyTypeID=" + + LearningDesign.COPY_TYPE_PREVIEW + + "and lesson.start_date_time < ?"; + /** * Retrieves the Lesson * @param lessonId identifies the lesson to get @@ -137,15 +146,26 @@ } /** - * Returns the list of available Lessons for - * a given user. Does not return disabled lessons. + * Returns the list of available Lessons created by + * a given user. Does not return disabled lessons or preview lessons. * * @param userID The user_id of the user * @return List The list of Lessons for the given user */ - public List getLessonsForUser(Integer userID){ - List lessons = this.getHibernateTemplate().find(FIND_BY_USER,userID); + public List getLessonsCreatedByUser(Integer userID){ + List lessons = this.getHibernateTemplate().find(FIND_LESSON_BY_CREATOR,userID); return lessons; } + + /** + * Get all the preview lessons more with the creation date before the given date. + * + * @param startDate UTC date + * @return the list of Lessons + */ + public List getPreviewLessonsBeforeDate(final Date startDate){ + List lessons = this.getHibernateTemplate().find(FIND_PREVIEW_BEFORE_START_DATE,startDate); + return lessons; + } } Index: lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/monitoringApplicationContext.xml =================================================================== diff -u -rea7b29a6f1c1567f276bdd802ccffeb486e2f1e7 -rc9c56707245e3ad1149bf1a5338eb251c96c75d2 --- lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/monitoringApplicationContext.xml (.../monitoringApplicationContext.xml) (revision ea7b29a6f1c1567f276bdd802ccffeb486e2f1e7) +++ lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/monitoringApplicationContext.xml (.../monitoringApplicationContext.xml) (revision c9c56707245e3ad1149bf1a5338eb251c96c75d2) @@ -73,6 +73,9 @@ PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED + PROPAGATION_REQUIRED + PROPAGATION_REQUIRED + PROPAGATION_REQUIRED @@ -129,4 +132,5 @@ + Index: lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/service/IMonitoringService.java =================================================================== diff -u -r2b516bec2b4c42ad0c7f3ea68db139d31d831205 -rc9c56707245e3ad1149bf1a5338eb251c96c75d2 --- lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/service/IMonitoringService.java (.../IMonitoringService.java) (revision 2b516bec2b4c42ad0c7f3ea68db139d31d831205) +++ lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/service/IMonitoringService.java (.../IMonitoringService.java) (revision c9c56707245e3ad1149bf1a5338eb251c96c75d2) @@ -30,6 +30,7 @@ import org.lamsfoundation.lams.lesson.Lesson; import org.lamsfoundation.lams.tool.exception.LamsToolServiceException; import org.lamsfoundation.lams.usermanagement.Organisation; +import org.lamsfoundation.lams.usermanagement.exception.UserAccessDeniedException; /** @@ -57,6 +58,13 @@ */ public Lesson initializeLesson(String lessonName, String lessonDescription,long learningDesignId,Integer userID); /** + * Create new lesson according to the learning design specified by the + * user, but for a preview session rather than a normal learning session. + * The design is not assigned to any workspace folder. + */ + public Lesson initializeLessonForPreview(String lessonName,String lessonDescription,long learningDesignId,Integer userID); + + /** * Create a lession according to the input lession WDDX package. The sample package is following: * *
@@ -166,7 +174,13 @@
      * 		the specified the lesson id.
      */
 	public void removeLesson(long lessonId);
-    /**
+   /**
+    * 
+    * Permanently remove a lesson from the database. This can not be undone - once deleted the 
+    * data is gone forever.
+     */
+    public void deleteLesson(Lesson lesson); 
+     /**
      * Set the gate to open to let all the learners through. This learning service
      * is triggerred by the system scheduler. Will return true GateActivity (or subclass)
      * object, rather than a hibernate proxy. This is needed so that the class can 
@@ -389,10 +403,37 @@
      */
     public void performChosenGrouping(GroupingActivity groupingActivity, List groups);
     
+	//---------------------------------------------------------------------
+	// Preview Methods
+	//---------------------------------------------------------------------
+	/**
+	 * Create the lesson class and the staff class for a preview lesson.
+	 * 
+	 * @param userID User ID of the teacher running the preview. Mandatory.
+	 * @param lessonID ID of the lesson
+	 * @return Lesson
+	 */
+	public abstract Lesson createPreviewClassForLesson(int userID,
+			long lessonID) throws UserAccessDeniedException;
+
+	/**
+	 * Remove all the details for a particular preview lessons.
+	 * 
+	 * @param lessonID ID of the lesson which is the preview session. Mandatory.
+	 */
+	public abstract void deletePreviewLesson(long lessonID);
+
+	/**
+	 * Remove all the "old" preview lessons. Removes all preview lessons older than numDays old.  
+	 * 
+	 * @param numDays Delete any preview lessons older than numDays
+	 * @return number of lessons deleted.
+	 */
+	public abstract int deleteAllOldPreviewLessons(int numDays);
+
     /* TODO Dummy methods - to be removed */
     public List getOrganisationsUsers(Integer userId);
     public List getLearningDesigns(Long userId);
 	
 
-
 }
Index: lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/service/MonitoringService.java
===================================================================
diff -u -rdddd4ed5ab9b4234f80c7e5a5186f75b3de008d8 -rc9c56707245e3ad1149bf1a5338eb251c96c75d2
--- lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/service/MonitoringService.java	(.../MonitoringService.java)	(revision dddd4ed5ab9b4234f80c7e5a5186f75b3de008d8)
+++ lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/service/MonitoringService.java	(.../MonitoringService.java)	(revision c9c56707245e3ad1149bf1a5338eb251c96c75d2)
@@ -72,6 +72,7 @@
 import org.lamsfoundation.lams.usermanagement.dao.IOrganisationDAO;
 import org.lamsfoundation.lams.usermanagement.dao.IUserDAO;
 import org.lamsfoundation.lams.usermanagement.dao.IWorkspaceFolderDAO;
+import org.lamsfoundation.lams.usermanagement.exception.UserAccessDeniedException;
 import org.lamsfoundation.lams.usermanagement.service.IUserManagementService;
 import org.lamsfoundation.lams.util.WebUtil;
 import org.lamsfoundation.lams.util.wddx.FlashMessage;
@@ -108,7 +109,8 @@
     // Instance variables
     //---------------------------------------------------------------------
 	private static Logger log = Logger.getLogger(MonitoringService.class);
-    
+   	private static final long numMilliSecondsInADay = 24 * 60 * 60 * 1000;
+
     private ILessonDAO lessonDAO;    
     private ILessonClassDAO lessonClassDAO;        
     private ITransitionDAO transitionDAO;
@@ -268,11 +270,41 @@
         	destinationFolder = originalLearningDesign.getWorkspaceFolder(); 
         }
         
+        return initializeLessonForPreview(lessonName, lessonDescription, originalLearningDesign, user, LearningDesign.COPY_TYPE_LESSON, destinationFolder);
+        
+    }
+    
+    /**
+     * Create new lesson according to the learning design specified by the 
+     * user, but for a preview session rather than a normal learning session.
+     * The design is not assigned to any workspace folder.
+     */
+    public Lesson initializeLessonForPreview(String lessonName,
+                               String lessonDescription,
+                               long learningDesignId,
+                               Integer userID) 
+    {
+        LearningDesign originalLearningDesign = authoringService.getLearningDesign(new Long(learningDesignId));
+        if ( originalLearningDesign == null) {
+        	throw new MonitoringServiceException("Learning design for id="+learningDesignId+" is missing. Unable to initialize lesson.");
+        }
+    	User user = (userID != null ? userManagementService.getUserById(userID) : null);
+
+        return initializeLessonForPreview(lessonName, lessonDescription, originalLearningDesign, user, LearningDesign.COPY_TYPE_PREVIEW, null);
+    }
+
+    public Lesson initializeLessonForPreview(String lessonName,
+            String lessonDescription,
+            LearningDesign originalLearningDesign,
+            User user,
+            int copyType,
+            WorkspaceFolder folder) { 
+    
         //copy the current learning design
         LearningDesign copiedLearningDesign = authoringService.copyLearningDesign(originalLearningDesign,
-                                                                                  new Integer(LearningDesign.COPY_TYPE_LESSON),
+                                                                                  new Integer(copyType),
                                                                                   user,
-                                                                                  destinationFolder, true);
+                                                                                  folder, true);
         // copy the tool content
         // unfortuanately, we have to reaccess the activities to make sure we get the
         // subclass, not a hibernate proxy.
@@ -287,15 +319,15 @@
                     toolActivity.setToolContentId(newContentId);
                 } catch (DataMissingException e) {
                     String error = "Unable to initialise the lesson. Data is missing for activity "+currentActivity.getActivityUIID()
-                            +" in learning design "+learningDesignId
+                            +" in learning design "+originalLearningDesign.getLearningDesignId()
                             +" default content may be missing for the tool. Error was "
                             +e.getMessage();
                     log.error(error,e);
                     throw new MonitoringServiceException(error,e);
                 } catch (ToolException e) {
                     String error = "Unable to initialise the lesson. Tool encountered an error copying the data is missing for activity "
                             +currentActivity.getActivityUIID()
-                            +" in learning design "+learningDesignId
+                            +" in learning design "+originalLearningDesign.getLearningDesignId()
                             +" default content may be missing for the tool. Error was "
                             +e.getMessage();
                     log.error(error,e);
@@ -309,6 +341,7 @@
         return createNewLesson(lessonName,lessonDescription,user,copiedLearningDesign);
 
     }
+    
     /**
      *  @see org.lamsfoundation.lams.monitoring.service.IMonitoringService#createLessonClassForLessonWDDX(Integer, String)
      */
@@ -616,7 +649,32 @@
         lessonDAO.updateLesson(requestedLesson);
     	
     }
+
     /**
+     * Delete a lesson and all its contents. Warning: at the moment, this should only be done to preview lessons.
+     * Can't guarentee data integrity if it is done to any other type of lesson. See removeLesson() for hiding
+     * lessons from a teacher's view without removing them from the database.
+     * 
+     * This code actually checks that the lesson is a preview lesson - writes out a warning message if it is not
+     * a preview lesson.
+     * 
+     * @see org.lamsfoundation.lams.monitoring.service.IMonitoringService#deleteLesson(org.lamsfoundation.lams.lesson.Lesson)
+     * TODO remove the related tool data.
+     */
+    public void deleteLesson(Lesson lesson) {
+    	
+		if ( lesson != null && lesson.getLearningDesign()!= null && lesson.getLearningDesign().getCopyTypeID() != null && 
+				LearningDesign.COPY_TYPE_PREVIEW == lesson.getLearningDesign().getCopyTypeID().intValue() ) { 
+	        lessonDAO.deleteLesson(lesson);
+		} else {
+			log.warn("Unable to delete lesson as lesson is not a preview lesson. Learning design copy type was "
+					+(lesson != null && lesson.getLearningDesign()!= null ? lesson.getLearningDesign().getCopyTypeID() : null)
+					+" Lesson is "+lesson);
+		}
+
+    }
+    
+    /**
      * @see org.lamsfoundation.lams.monitoring.service.IMonitoringService#openGate(org.lamsfoundation.lams.learningdesign.GateActivity)
      */
     public GateActivity openGate(Long gateId)
@@ -757,7 +815,7 @@
      * @see org.lamsfoundation.lams.monitoring.service.IMonitoringService#getAllLessons(java.lang.Integer)
      */
     public List getAllLessons(Integer userID)throws IOException{
-    	return lessonDAO.getLessonsForUser(userID);
+    	return lessonDAO.getLessonsCreatedByUser(userID);
     }
 
     /**
@@ -1429,5 +1487,82 @@
     	
     	return learningDesignDAO.getLearningDesignByUserId(userId);
     }
+    
+   //---------------------------------------------------------------------
+   // Preview related methods
+   //---------------------------------------------------------------------
+   /* (non-Javadoc)
+   	 * @see org.lamsfoundation.lams.preview.service.IPreviewService#createPreviewClassForLesson(long, long)
+   	 */
+       public Lesson createPreviewClassForLesson(int userID, long lessonID) throws UserAccessDeniedException {
+
+           User user = userManagementService.getUserById(new Integer(userID));
+           if ( user == null ) {
+           	throw new UserAccessDeniedException("User "+userID+" not found");
+           }
+           Organisation organisation = user.getBaseOrganisation();
+           
+           // create the lesson class - add the teacher as the learner and as staff
+           LinkedList learners = new LinkedList();
+           learners.add(user);
+
+           LinkedList staffs = new LinkedList();
+           staffs.add(user);
+           
+           return createLessonClassForLesson(lessonID,
+           		organisation,
+           		"Learner Group",
+   				learners,
+   				"Staff Group",
+                   staffs);
+
+       }
+    
+       /* (non-Javadoc)
+   	 * @see org.lamsfoundation.lams.preview.service.IPreviewService#deletePreviewSession(long)
+   	 */
+       public void deletePreviewLesson(long lessonID) {
+       	Lesson lesson = lessonDAO.getLesson(new Long(lessonID));
+       	if ( lesson != null ) {
+       		if ( lesson.getLearningDesign().getCopyTypeID() != null && 
+       				LearningDesign.COPY_TYPE_PREVIEW == lesson.getLearningDesign().getCopyTypeID().intValue() ) { 
+       	    	deleteLesson(lesson);
+       		} else {
+       			log.warn("Unable to delete lesson as lesson is not a preview lesson. Learning design copy type was "+lesson.getLearningDesign().getCopyTypeID());
+       		}
+       	}
+       }
+
+       /* (non-Javadoc)
+   	 * @see org.lamsfoundation.lams.preview.service.IPreviewService#deleteAllOldPreviewLessons(int)
+   	 */
+       public int deleteAllOldPreviewLessons(int numDays) {
+
+   	    // Contract checking 
+   	    if ( numDays <= 0 ) {
+   	    	log.error("deleteAllOldPreviewSessions: number of days invalid ("+numDays+"). Unable to delete any preview lessons");
+   	    	return 0;
+   	    }
+   	   
+     		int numDeleted = 0;
+
+     		// calculate comparison date
+   	    long newestDateToKeep = System.currentTimeMillis() - ( numDays * numMilliSecondsInADay);
+   	    Date date = new Date(newestDateToKeep);
+   	    // convert data to UTC
+   	    log.info("Deleting all preview lessons before "+date.toString()+" (server time) ("+newestDateToKeep+")");
+     	  
+     		// get all the preview sessions older than a particular date.
+   	    List sessions = lessonDAO.getPreviewLessonsBeforeDate(date);
+   	    Iterator iter = sessions.iterator();
+   	    while (iter.hasNext()) {
+   	    	Lesson lesson = (Lesson) iter.next();
+   	    	deleteLesson(lesson);
+   	        log.info("Preview lesson deleted. Lesson was "+lesson);
+       	    numDeleted++;
+   		}
+   	    
+   		return numDeleted;
+       }
   
 }
\ No newline at end of file
Index: lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/web/MonitoringAction.java
===================================================================
diff -u -rd80d94d46bf4079a73eec34044f434c377a023f9 -rc9c56707245e3ad1149bf1a5338eb251c96c75d2
--- lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/web/MonitoringAction.java	(.../MonitoringAction.java)	(revision d80d94d46bf4079a73eec34044f434c377a023f9)
+++ lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/web/MonitoringAction.java	(.../MonitoringAction.java)	(revision c9c56707245e3ad1149bf1a5338eb251c96c75d2)
@@ -31,9 +31,7 @@
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
 
-import org.apache.log4j.Logger;
 import org.apache.struts.action.ActionForm;
 import org.apache.struts.action.ActionForward;
 import org.apache.struts.action.ActionMapping;
@@ -42,11 +40,9 @@
 import org.lamsfoundation.lams.monitoring.service.IMonitoringService;
 import org.lamsfoundation.lams.monitoring.service.MonitoringServiceProxy;
 import org.lamsfoundation.lams.tool.exception.LamsToolServiceException;
-import org.lamsfoundation.lams.usermanagement.dto.UserDTO;
 import org.lamsfoundation.lams.util.WebUtil;
 import org.lamsfoundation.lams.util.wddx.FlashMessage;
 import org.lamsfoundation.lams.web.action.LamsDispatchAction;
-import org.lamsfoundation.lams.web.session.SessionManager;
 import org.lamsfoundation.lams.web.util.AttributeNames;
 
 
@@ -77,13 +73,10 @@
     //---------------------------------------------------------------------
     // Instance variables
     //---------------------------------------------------------------------
-	private static Logger log = Logger.getLogger(MonitoringAction.class);
 	
-	private IMonitoringService monitoringService;
     //---------------------------------------------------------------------
     // Class level constants - Struts forward
     //---------------------------------------------------------------------
-    private static final String SCHEDULER = "scheduler";
 
     /** If you want the output given as a jsp, set the request parameter "jspoutput" to 
      * some value other than an empty string (e.g. 1, true, 0, false, blah). 
@@ -116,16 +109,6 @@
 	    }
 	}
 	
-    private UserDTO getUser() throws IOException {
-    	HttpSession ss = SessionManager.getSession();
-    	UserDTO user = (UserDTO) ss.getAttribute(AttributeNames.USER);
-    	if ( user != null ) {
-    		return user;
-    	}
-    	
-    	throw new IOException("Unable to get user. User in session manager is "+user);
-    }
-
     /**
 	 * @param wddxPacket
 	 * @return
@@ -174,7 +157,7 @@
                                                                           ServletException
     {
 
-        this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
         FlashMessage flashMessage = null;
     	
     	try {
@@ -224,7 +207,7 @@
                                      HttpServletResponse response) throws IOException,
                                                                           ServletException
     {
-        this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+        IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
         long lessonId = WebUtil.readLongParam(request, AttributeNames.PARAM_LESSON_ID);
     	FlashMessage flashMessage = null;
     	
@@ -266,7 +249,7 @@
     		HttpServletResponse response) throws IOException,
     		ServletException 
     		{
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	long lessonId = WebUtil.readLongParam(request, AttributeNames.PARAM_LESSON_ID);
     	String dateStr = WebUtil.readStrParam(request, MonitoringConstants.PARAM_LESSON_START_DATE);
     	FlashMessage flashMessage = null;
@@ -315,7 +298,7 @@
     		HttpServletResponse response) throws IOException,
     		ServletException 
     		{
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	long lessonId = WebUtil.readLongParam(request, AttributeNames.PARAM_LESSON_ID);
     	String dateStr = WebUtil.readStrParam(request, MonitoringConstants.PARAM_LESSON_FINISH_DATE);
     	FlashMessage flashMessage = null;
@@ -363,7 +346,7 @@
                                                                           ServletException
     {
     	FlashMessage flashMessage = null;
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	long lessonId = WebUtil.readLongParam(request,AttributeNames.PARAM_LESSON_ID);
     	
     	try {
@@ -401,7 +384,7 @@
     		ServletException
     		{
     	FlashMessage flashMessage = null;
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	long lessonId = WebUtil.readLongParam(request,AttributeNames.PARAM_LESSON_ID);
     	
     	try {
@@ -436,7 +419,7 @@
     		ServletException
     		{
     	FlashMessage flashMessage = null;
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	long lessonId = WebUtil.readLongParam(request,AttributeNames.PARAM_LESSON_ID);
     	
     	try {
@@ -475,7 +458,7 @@
             HttpServletResponse response) throws IOException,
                                                  ServletException{
     	FlashMessage flashMessage = null;
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	long lessonId = WebUtil.readLongParam(request,AttributeNames.PARAM_LESSON_ID);
     	
     	try {
@@ -510,7 +493,7 @@
             HttpServletResponse response) throws IOException,
                                                  ServletException{
     	FlashMessage flashMessage = null;
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	//get parameters
     	Long activityId = null;
     	String actId = request.getParameter(AttributeNames.PARAM_ACTIVITY_ID);
@@ -540,15 +523,15 @@
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response)throws IOException{
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	String wddxPacket = monitoringService.getAllLessonsWDDX();
     	return outputPacket(mapping, request, response, wddxPacket, "details");
     }
     public ActionForward getLessonDetails(ActionMapping mapping,
             ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response)throws IOException{
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	Long lessonID = new Long(WebUtil.readLongParam(request,"lessonID"));
     	String wddxPacket = monitoringService.getLessonDetails(lessonID);
     	return outputPacket(mapping, request, response, wddxPacket, "details");
@@ -557,7 +540,7 @@
             ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response)throws IOException{
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	Long lessonID = new Long(WebUtil.readLongParam(request,"lessonID"));
     	String wddxPacket = monitoringService.getLessonLearners(lessonID);
     	return outputPacket(mapping, request, response, wddxPacket, "details");
@@ -566,7 +549,7 @@
             ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response)throws IOException{
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	Long lessonID = new Long(WebUtil.readLongParam(request,"lessonID"));
     	String wddxPacket = monitoringService.getLearningDesignDetails(lessonID);
     	return outputPacket(mapping, request, response, wddxPacket, "details");
@@ -575,7 +558,7 @@
             ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response)throws IOException{
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	Long lessonID = new Long(WebUtil.readLongParam(request,"lessonID"));
     	String wddxPacket = monitoringService.getAllLearnersProgress(lessonID);
     	return outputPacket(mapping, request, response, wddxPacket, "details");
@@ -584,7 +567,7 @@
             ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response)throws IOException{
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	Long lessonID = new Long(WebUtil.readLongParam(request,"lessonID"));
     	String wddxPacket = monitoringService.getAllContributeActivities(lessonID);
     	return outputPacket(mapping, request, response, wddxPacket, "details");
@@ -594,7 +577,7 @@
             ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response)throws IOException,LamsToolServiceException{
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	Integer userID = new Integer(WebUtil.readIntParam(request,"userID"));
     	Long activityID = new Long(WebUtil.readLongParam(request,"activityID"));
     	//Show learner in monitor in a single call: extract URL and redirect it rather than returning the WDDX packet
@@ -609,7 +592,7 @@
             ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response)throws IOException{
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());    	
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());    	
     	Long activityID = new Long(WebUtil.readLongParam(request,"activityID"));
     	String wddxPacket = monitoringService.getActivityContributionURL(activityID);
     	return outputPacket(mapping, request, response, wddxPacket, "details");
@@ -618,7 +601,7 @@
             ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response)throws IOException{
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	Long lessonID = new Long(WebUtil.readLongParam(request,"lessonID"));
     	Integer userID = new Integer(WebUtil.readIntParam(request,"userID"));
     	Integer targetWorkspaceFolderID = new Integer(WebUtil.readIntParam(request,"folderID"));
@@ -629,7 +612,7 @@
             ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response)throws IOException{
-    	this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+    	IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
     	Long lessonID = new Long(WebUtil.readLongParam(request,"lessonID"));
     	Integer userID = new Integer(WebUtil.readIntParam(request,"userID"));
     	String name = WebUtil.readStrParam(request,"name"); 
@@ -642,7 +625,7 @@
             HttpServletRequest request,
             HttpServletResponse response) throws IOException {
         
-        this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+        IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
         Long activityID = new Long(WebUtil.readLongParam(request, "activityID"));
         Long lessonID = new Long(WebUtil.readLongParam(request, "lessonID"));
         String wddxPacket = monitoringService.checkGateStatus(activityID, lessonID);
@@ -655,11 +638,56 @@
             ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response) throws IOException {
-        this.monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+        IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
         Long activityID = new Long(WebUtil.readLongParam(request, "activityID"));
         String wddxPacket = monitoringService.releaseGate(activityID);
        // request.setAttribute(USE_JSP_OUTPUT, "1");
         return outputPacket(mapping, request, response, wddxPacket, "details");
     }
 
+	public ActionForward startPreviewLesson(ActionMapping mapping,
+            ActionForm form,
+            HttpServletRequest request,
+            HttpServletResponse response) throws IOException {
+		IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet().getServletContext());
+		FlashMessage flashMessage = null;
+		
+		try {
+		
+			int userID = WebUtil.readIntParam(request,AttributeNames.PARAM_USER_ID); 
+			long learningDesignID = WebUtil.readLongParam(request,AttributeNames.PARAM_LEARNINGDESIGN_ID);
+			String title = WebUtil.readStrParam(request,"title");
+			String desc = WebUtil.readStrParam(request,"description"); 
+			
+	        // initialize the lesson
+	        Lesson previewLesson = monitoringService.initializeLessonForPreview(title,desc,learningDesignID,new Integer(userID));
+	        if ( previewLesson != null ) {
+
+	        	long lessonID = previewLesson.getLessonId().longValue();
+				
+	        	monitoringService.createPreviewClassForLesson(userID, lessonID);
+		        monitoringService.startLesson(lessonID);
+		
+				flashMessage = new FlashMessage("startPreviewSession",new Long(lessonID));
+				
+	        } else {
+	        	
+				flashMessage = new FlashMessage("startPreviewSession",
+						"Internal error - no lesson created.",
+						FlashMessage.CRITICAL_ERROR);
+	        }
+			
+		} catch (Exception e) {
+		
+			flashMessage = new FlashMessage("startPreviewSession",
+			e.getMessage(),
+			FlashMessage.ERROR);
+		
+		}
+		
+		PrintWriter writer = response.getWriter();
+		writer.println(flashMessage.serializeMessage());
+		return null;
+	}
+
 }
Index: lams_monitoring/test/java/org/lamsfoundation/lams/monitoring/service/TestMonitoringService.java
===================================================================
diff -u -r2b516bec2b4c42ad0c7f3ea68db139d31d831205 -rc9c56707245e3ad1149bf1a5338eb251c96c75d2
--- lams_monitoring/test/java/org/lamsfoundation/lams/monitoring/service/TestMonitoringService.java	(.../TestMonitoringService.java)	(revision 2b516bec2b4c42ad0c7f3ea68db139d31d831205)
+++ lams_monitoring/test/java/org/lamsfoundation/lams/monitoring/service/TestMonitoringService.java	(.../TestMonitoringService.java)	(revision c9c56707245e3ad1149bf1a5338eb251c96c75d2)
@@ -37,6 +37,7 @@
 import org.lamsfoundation.lams.tool.exception.LamsToolServiceException;
 import org.lamsfoundation.lams.usermanagement.Organisation;
 import org.lamsfoundation.lams.usermanagement.User;
+import org.lamsfoundation.lams.usermanagement.exception.UserAccessDeniedException;
 import org.lamsfoundation.lams.usermanagement.service.IUserManagementService;
 import org.springframework.jdbc.core.JdbcTemplate;
 
@@ -315,4 +316,61 @@
         return jt.queryForLong("SELECT max("+idname+") FROM "+tablename);
     }
 
+	/*
+	 * Test method for 'org.lamsfoundation.lams.preview.service.PreviewService.startPreviewLesson(int, long, String, String)'
+	 */
+	public void testStartPreviewLesson() {
+		String testName = "LESSON";
+		String testDesc = "DESC";
+		Lesson lesson = startLesson(testName, testDesc);
+		assertEquals("Lesson has correct title",testName, lesson.getLessonName());
+		assertEquals("Lesson has correct description",testDesc, lesson.getLessonDescription());
+
+		Lesson newLesson = lessonDao.getLesson(lesson.getLessonId());
+		assertNotNull("Lesson can be found in database",newLesson);
+	}
+
+	private Lesson startLesson(String testName, String testDesc) {
+		try {
+	        Lesson previewLesson = monitoringService.initializeLessonForPreview(testName,testDesc,TEST_LEARNING_DESIGN_ID,TEST_USER_ID);
+	        assertNotNull("Lesson created",previewLesson);
+			assertNotNull("Lesson has been saved - an id exists", previewLesson.getLessonId());
+
+	        Lesson newLesson = monitoringService.createPreviewClassForLesson(TEST_USER_ID.intValue(), previewLesson.getLessonId().longValue());
+	        assertNotNull("Lesson returned from create class",newLesson);
+	        assertSame("Lesson updated from create class", newLesson.getLessonId(),previewLesson.getLessonId());
+
+	        monitoringService.startLesson(previewLesson.getLessonId().longValue());
+
+			return previewLesson;
+		} catch (UserAccessDeniedException e) {
+			fail("Unable to start lesson as due to a user exception");
+		}
+		return null;
+		
+	}
+	
+	/*
+	 * Test method for 'org.lamsfoundation.lams.preview.service.PreviewService.deletePreviewLesson(long)'
+	 */
+	public void testDeletePreviewLesson() {
+		String testName = "LESSON TO DELETE";
+		String testDesc = "TO BE DELETED";
+		Lesson lesson = startLesson(testName, testDesc);
+		Long lessonId = lesson.getLessonId();
+		
+		monitoringService.deletePreviewLesson(lessonId.longValue());
+		Lesson deletedLesson = lessonDao.getLesson(lessonId);
+		assertNull("Deleted lesson cannot be found",deletedLesson);
+	}
+
+	/*
+	 * Test method for 'org.lamsfoundation.lams.preview.service.PreviewService.deleteAllOldPreviewLessons(int)'
+	 * Can't really test this properly - can't tell if deleted or not. Can only test that it doesn't fail
+	 */
+	public void testDeleteAllOldPreviewLessons() {
+		int lessonsDeleted = monitoringService.deleteAllOldPreviewLessons(1);
+		assertTrue("deleteAllOldPreviewLessons returns 0 or more", lessonsDeleted>=0);
+	}
+
 }