From 8979371d858f62f5b80669abb7e3053383153a2e Mon Sep 17 00:00:00 2001
From: gnaegi <none@none>
Date: Tue, 20 Jan 2015 16:34:14 +0100
Subject: [PATCH] OO-1162 create sp nodes in directory also when added by
 course wizard

---
 .../helper/course/HTMLDocumentHelper.java     | 46 ++++++++++++++-----
 .../coursecreation/CourseCreationHelper.java  |  9 ++--
 2 files changed, 39 insertions(+), 16 deletions(-)

diff --git a/src/main/java/de/tuchemnitz/wizard/helper/course/HTMLDocumentHelper.java b/src/main/java/de/tuchemnitz/wizard/helper/course/HTMLDocumentHelper.java
index 26f2d4ae227..a61d564cf48 100644
--- a/src/main/java/de/tuchemnitz/wizard/helper/course/HTMLDocumentHelper.java
+++ b/src/main/java/de/tuchemnitz/wizard/helper/course/HTMLDocumentHelper.java
@@ -35,33 +35,55 @@ package de.tuchemnitz.wizard.helper.course;
 import java.io.BufferedOutputStream;
 import java.io.IOException;
 
+import org.olat.core.logging.OLog;
 import org.olat.core.logging.Tracing;
 import org.olat.core.util.FileUtils;
+import org.olat.core.util.StringHelper;
+import org.olat.core.util.vfs.VFSContainer;
 import org.olat.core.util.vfs.VFSLeaf;
 import org.olat.course.ICourse;
 
 public final class HTMLDocumentHelper {
-	
+	private static final OLog log = Tracing.createLoggerFor(HTMLDocumentHelper.class);
 	public static String ENCODING = "utf-8";
 
 	/**
 	 * Create a HTML file and put it into the course folder container.
 	 * 
 	 * @param course the corresponding course object
-	 * @param htmlFileName HTML file name
+	 * @param relFilePath HTML file name
 	 * @param htmlText the full html site content with head and body
 	 * @return the created folder leaf
 	 */
-	public static final VFSLeaf createHtmlDocument(final ICourse course, final String htmlFileName, final String htmlText) {
-		// create the HTML file inside the course base folder container
-		final VFSLeaf leaf = course.getCourseFolderContainer().createChildLeaf(htmlFileName);
-		final BufferedOutputStream bos = new BufferedOutputStream(leaf.getOutputStream(false));
-		FileUtils.save(bos, htmlText, ENCODING);
-		try {
-			bos.close();
-		} catch (IOException e) {
-			Tracing.createLoggerFor(HTMLDocumentHelper.class).error("Error writing the HTML file: " + e.getLocalizedMessage());
+	public static final VFSLeaf createHtmlDocument(final ICourse course, final String relFilePath, final String htmlText) {
+		// Create the HTML file inside the course base folder container
+		
+		VFSContainer parent = course.getCourseFolderContainer();
+		VFSLeaf file = (VFSLeaf) parent.resolve(relFilePath);
+		if (file == null) {
+			// Expected: file does not exist, create it now. 
+			String[] pathSegments = relFilePath.split("/");
+			for (int i = 0; i < pathSegments.length; i++) {
+				String segment = pathSegments[i];
+				if (StringHelper.containsNonWhitespace(segment)) {
+					if (i == pathSegments.length -1) {
+						// last one is leaf
+						file = parent.createChildLeaf(segment);											
+					} else {
+						parent = parent.createChildContainer(segment);
+					}						
+				}
+			}
+			final BufferedOutputStream bos = new BufferedOutputStream(file.getOutputStream(false));
+			FileUtils.save(bos, htmlText, ENCODING);
+			try {
+				bos.close();
+			} catch (IOException e) {
+				log.error("Error writing the HTML file::" + relFilePath, e);
+			}
+		} else {
+			log.error("Can not create file::" + relFilePath + ", does already exist");
 		}
-		return leaf;
+		return file;
 	}
 }
diff --git a/src/main/java/de/tuchemnitz/wizard/workflows/coursecreation/CourseCreationHelper.java b/src/main/java/de/tuchemnitz/wizard/workflows/coursecreation/CourseCreationHelper.java
index 04e74b7513e..5701e2d83bd 100644
--- a/src/main/java/de/tuchemnitz/wizard/workflows/coursecreation/CourseCreationHelper.java
+++ b/src/main/java/de/tuchemnitz/wizard/workflows/coursecreation/CourseCreationHelper.java
@@ -46,13 +46,13 @@ import org.olat.core.id.UserConstants;
 import org.olat.core.logging.OLog;
 import org.olat.core.logging.Tracing;
 import org.olat.core.util.Util;
-import org.olat.core.util.vfs.VFSLeaf;
 import org.olat.course.CourseFactory;
 import org.olat.course.ICourse;
 import org.olat.course.condition.Condition;
+import org.olat.course.editor.CourseEditorHelper;
 import org.olat.course.editor.PublishProcess;
-import org.olat.course.editor.StatusDescription;
 import org.olat.course.editor.PublishSetInformations;
+import org.olat.course.editor.StatusDescription;
 import org.olat.course.nodes.AbstractAccessableCourseNode;
 import org.olat.course.nodes.BCCourseNode;
 import org.olat.course.nodes.COCourseNode;
@@ -142,8 +142,9 @@ public class CourseCreationHelper {
 			singlePageNode = CourseExtensionHelper.createSinglePageNode(course, translator.translate("cce.informationpage"), translator
 					.translate("cce.informationpage.descr"));
 			if (singlePageNode instanceof SPCourseNode) {
-				final VFSLeaf htmlLeaf = HTMLDocumentHelper.createHtmlDocument(course, "start.html", courseConfig.getSinglePageText(translator));
-				((SPCourseNode) singlePageNode).getModuleConfiguration().set(SPEditController.CONFIG_KEY_FILE, "/" + htmlLeaf.getName());
+				final String relPath = CourseEditorHelper.createUniqueRelFilePathFromShortTitle(singlePageNode, course.getCourseFolderContainer());
+				HTMLDocumentHelper.createHtmlDocument(course, relPath, courseConfig.getSinglePageText(translator));
+				((SPCourseNode) singlePageNode).getModuleConfiguration().set(SPEditController.CONFIG_KEY_FILE, relPath);
 			}
 		}
 		// enrollment node
-- 
GitLab