From 82b4a4e1a7d69afa453ee9d425362c92ea693586 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Wed, 26 Mar 2014 14:20:13 +0100
Subject: [PATCH] OO-1042: check the boundaries of the hibernate bag before add
 a checkpoint, clean the velocity container and reload all data after a
 save...

---
 .../de/bps/olat/modules/cl/Checklist.java     |  6 +-
 .../ChecklistEditCheckpointsController.java   | 58 +++++++++++--------
 2 files changed, 40 insertions(+), 24 deletions(-)

diff --git a/src/main/java/de/bps/olat/modules/cl/Checklist.java b/src/main/java/de/bps/olat/modules/cl/Checklist.java
index a96205dea65..b9874080b52 100644
--- a/src/main/java/de/bps/olat/modules/cl/Checklist.java
+++ b/src/main/java/de/bps/olat/modules/cl/Checklist.java
@@ -138,7 +138,11 @@ public class Checklist extends PersistentObject implements ModifiedInfo, Seriali
 	 * @param checkpoint
 	 */
 	public void addCheckpoint(int index, Checkpoint checkpoint) {
-		checkpoints.add(index, checkpoint);
+		if(index >= 0 && index < checkpoints.size()) {
+			checkpoints.add(index, checkpoint);
+		} else {
+			checkpoints.add(checkpoint);
+		}
 	}
 	
 	/**
diff --git a/src/main/java/de/bps/olat/modules/cl/ChecklistEditCheckpointsController.java b/src/main/java/de/bps/olat/modules/cl/ChecklistEditCheckpointsController.java
index 9ebb83f7d43..029bb245dd6 100644
--- a/src/main/java/de/bps/olat/modules/cl/ChecklistEditCheckpointsController.java
+++ b/src/main/java/de/bps/olat/modules/cl/ChecklistEditCheckpointsController.java
@@ -127,9 +127,8 @@ public class ChecklistEditCheckpointsController extends FormBasicController {
 		}
 		checklist = checklistManager.updateChecklist(checklist);
 		DBFactory.getInstance().commit();
-		checkpointsInVc.clear();
-		checkpointsInVc.addAll(checklist.getCheckpointsSorted(checkpointComparator));
-		titleContainer.contextPut("checkpoints", checkpointsInVc);
+		loadCheckpointInVC();
+		titleContainer.setDirty(true);
 		// Inform all listeners about the changes
 		fireEvent(ureq, Event.CHANGED_EVENT);
 	}
@@ -223,27 +222,12 @@ public class ChecklistEditCheckpointsController extends FormBasicController {
 		titleContainer = FormLayoutContainer.createCustomFormLayout("titleLayout", getTranslator(), velocity_root + "/edit.html");
 		fic.add(titleContainer);
 		// create gui elements for all checkpoints
-		int numOfCheckpoints = checklist.getCheckpoints().size();
-		if(numOfCheckpoints == 0) {
-			Checkpoint newCheckpoint = new Checkpoint();
-			newCheckpoint.setChecklist(checklist);
-			newCheckpoint.setLastModified(new Date());
-			newCheckpoint.setTitle("");
-			newCheckpoint.setDescription("");
-			newCheckpoint.setMode(CheckpointMode.MODE_EDITABLE);
-			checklist.addCheckpoint(0, newCheckpoint);
-			addNewFormCheckpoint(0, newCheckpoint);
-		} else {
-			for (int i = 0; i<numOfCheckpoints; i++) {
-				Checkpoint checkpoint = checklist.getCheckpointsSorted(checkpointComparator).get(i);
-				addNewFormCheckpoint(i, checkpoint);
-			}
-		}
+		loadCheckpointInVC();
+		
 		addButton = new FormLinkImpl("add" + counter, "add" + counter, "cl.table.add", Link.BUTTON_SMALL);
 		addButton.setUserObject(checklist.getCheckpointsSorted(checkpointComparator).get(checklist.getCheckpoints().size() - 1));
 		titleContainer.add(addButton);
 		
-		checkpointsInVc.addAll(checklist.getCheckpointsSorted(checkpointComparator));
 		titleContainer.contextPut("checkpoints", checkpointsInVc);
 		titleContainer.contextPut("titleInputList", titleInputList);
 		titleContainer.contextPut("descriptionInputList", descriptionInputList);
@@ -258,6 +242,36 @@ public class ChecklistEditCheckpointsController extends FormBasicController {
 		uifactory.addFormSubmitButton("subm", submitKey, buttonContainer);
 	}
 	
+	private void loadCheckpointInVC() {
+		checkpointsInVc.clear();
+		titleInputList.clear();
+		descriptionInputList.clear();
+		modeInputList.clear();
+		delButtonList.clear();
+		
+		int numOfCheckpoints = checklist.getCheckpoints().size();
+		if(numOfCheckpoints == 0) {
+			Checkpoint newCheckpoint = new Checkpoint();
+			newCheckpoint.setChecklist(checklist);
+			newCheckpoint.setLastModified(new Date());
+			newCheckpoint.setTitle("");
+			newCheckpoint.setDescription("");
+			newCheckpoint.setMode(CheckpointMode.MODE_EDITABLE);
+			checklist.addCheckpoint(0, newCheckpoint);
+			addNewFormCheckpoint(0, newCheckpoint);
+			
+			List<Checkpoint> checkpoints = checklist.getCheckpointsSorted(checkpointComparator);
+			checkpointsInVc.addAll(checkpoints);
+		} else {
+			List<Checkpoint> checkpoints = checklist.getCheckpointsSorted(checkpointComparator);
+			for (int i = 0; i<numOfCheckpoints; i++) {
+				Checkpoint checkpoint = checkpoints.get(i);
+				addNewFormCheckpoint(i, checkpoint);
+			}
+			checkpointsInVc.addAll(checkpoints);
+		}
+	}
+	
 	@Override
 	protected void event(UserRequest ureq, Controller source, Event event) {
 		if(source == yesNoController) {
@@ -290,7 +304,6 @@ public class ChecklistEditCheckpointsController extends FormBasicController {
 		return isOk;
 	}
 	
-	
 	@Override	
 	protected void formCancelled(UserRequest ureq) {
 		// reset complete form
@@ -304,5 +317,4 @@ public class ChecklistEditCheckpointsController extends FormBasicController {
 		initForm(flc, this, ureq);
 		fireEvent(ureq, Event.CANCELLED_EVENT);
 	}
-
-}
+}
\ No newline at end of file
-- 
GitLab