diff --git a/src/main/java/org/olat/course/CourseFactory.java b/src/main/java/org/olat/course/CourseFactory.java index a9742457a63d8499c2c0976ae665c8136cb63078..285aae1944058bbbc3228544bd748749ab9f0d3c 100644 --- a/src/main/java/org/olat/course/CourseFactory.java +++ b/src/main/java/org/olat/course/CourseFactory.java @@ -213,8 +213,9 @@ public class CourseFactory extends BasicManager { * @param learningObjectives Learning objectives of root node * @return an empty course with a single root node. */ - public static ICourse createEmptyCourse(OLATResourceable ores, String shortTitle, String longTitle, String learningObjectives) { - PersistingCourseImpl newCourse = new PersistingCourseImpl(ores.getResourceableId()); + public static ICourse createEmptyCourse(RepositoryEntry courseEntry, String shortTitle, String longTitle, String learningObjectives) { + OLATResource courseResource = courseEntry.getOlatResource(); + PersistingCourseImpl newCourse = new PersistingCourseImpl(courseResource.getResourceableId()); // Put new course in course cache loadedCourses.put(newCourse.getResourceableId() ,newCourse); diff --git a/src/main/java/org/olat/course/PersistingCourseImpl.java b/src/main/java/org/olat/course/PersistingCourseImpl.java index f1c9af1526c8209441874d18659aaa8aa2341fb0..005c4e14a3ddb4be6e7fbbc485d667f9b4be96a7 100644 --- a/src/main/java/org/olat/course/PersistingCourseImpl.java +++ b/src/main/java/org/olat/course/PersistingCourseImpl.java @@ -134,8 +134,8 @@ public class PersistingCourseImpl implements ICourse, OLATResourceable, Serializ } PersistingCourseImpl(RepositoryEntry courseEntry) { - this.courseTitle = courseEntry.getDisplayname(); - this.resourceableId = courseEntry.getOlatResource().getResourceableId(); + courseTitle = courseEntry.getDisplayname(); + resourceableId = courseEntry.getOlatResource().getResourceableId(); // prepare filesystem and set course base path and course folder paths prepareFilesystem(); courseConfig = CourseConfigManagerImpl.getInstance().loadConfigFor(this); // load or init defaults diff --git a/src/main/java/org/olat/repository/handlers/CourseHandler.java b/src/main/java/org/olat/repository/handlers/CourseHandler.java index 4560823d42422c4669148412f7c971b5ade629a8..a7a70ee3084942800f1b3d01ac8c1cedb5fcede1 100644 --- a/src/main/java/org/olat/repository/handlers/CourseHandler.java +++ b/src/main/java/org/olat/repository/handlers/CourseHandler.java @@ -144,7 +144,7 @@ public class CourseHandler implements RepositoryHandler { RepositoryEntry re = repositoryService.create(initialAuthor, null, "", displayname, description, resource, RepositoryEntry.ACC_OWNERS); DBFactory.getInstance().commit(); - ICourse course = CourseFactory.createEmptyCourse(resource, "New Course", "New Course", ""); + ICourse course = CourseFactory.createEmptyCourse(re, "New Course", "New Course", ""); course = CourseFactory.openCourseEditSession(re.getOlatResource().getResourceableId()); String shortDisplayname = Formatter.truncateOnly(displayname, 25); diff --git a/src/main/java/org/olat/restapi/repository/course/CoursesWebService.java b/src/main/java/org/olat/restapi/repository/course/CoursesWebService.java index edb1e5b4c55dfb92d65510d06df4765ac4ebc5a8..331faae17498db8914ac0e001f672d8132f3ce3c 100644 --- a/src/main/java/org/olat/restapi/repository/course/CoursesWebService.java +++ b/src/main/java/org/olat/restapi/repository/course/CoursesWebService.java @@ -494,44 +494,38 @@ public class CoursesWebService { } try { - OLATResource resource = OLATResourceManager.getInstance().createOLATResourceInstance(CourseModule.class); // create a repository entry - RepositoryEntry addedEntry = createCourseRepositoryEntry(initialAuthor, reDisplayName, softKey, externalId, externalRef, managedFlags, resource); - // create an empty course - CourseFactory.createEmptyCourse(resource, shortTitle, longTitle, learningObjectives); + RepositoryService repositoryService = CoreSpringFactory.getImpl(RepositoryService.class); + OLATResource resource = OLATResourceManager.getInstance().createOLATResourceInstance(CourseModule.class); + RepositoryEntry addedEntry = repositoryService.create(initialAuthor, null, "-", shortTitle, null, resource, 0); + if(StringHelper.containsNonWhitespace(softKey) && softKey.length() <= 30) { + addedEntry.setSoftkey(softKey); + } + addedEntry.setExternalId(externalId); + addedEntry.setExternalRef(externalRef); + addedEntry.setManagedFlagsString(managedFlags); + if(RepositoryEntryManagedFlag.isManaged(addedEntry, RepositoryEntryManagedFlag.membersmanagement)) { + addedEntry.setAllowToLeaveOption(RepositoryEntryAllowToLeaveOptions.never); + } else { + addedEntry.setAllowToLeaveOption(RepositoryEntryAllowToLeaveOptions.atAnyTime);//default + } if(membersOnly) { addedEntry.setMembersOnly(true); addedEntry.setAccess(RepositoryEntry.ACC_OWNERS); } else { addedEntry.setAccess(access); } - CoreSpringFactory.getImpl(RepositoryService.class).update(addedEntry); + addedEntry = repositoryService.update(addedEntry); + + // create an empty course + CourseFactory.createEmptyCourse(addedEntry, shortTitle, longTitle, learningObjectives); + return prepareCourse(addedEntry, shortTitle, longTitle, courseConfigVO); } catch (Exception e) { throw new WebApplicationException(e); } } - private static RepositoryEntry createCourseRepositoryEntry(Identity initialAuthor, String shortTitle, - String softKey, String externalId, String externalRef, String managedFlags, OLATResourceable oresable) { - // create a repository entry - RepositoryService repositoryService = CoreSpringFactory.getImpl(RepositoryService.class); - OLATResource ores = OLATResourceManager.getInstance().findOrPersistResourceable(oresable); - RepositoryEntry addedEntry = repositoryService.create(initialAuthor, null, "-", shortTitle, null, ores, 0); - if(StringHelper.containsNonWhitespace(softKey) && softKey.length() <= 30) { - addedEntry.setSoftkey(softKey); - } - addedEntry.setExternalId(externalId); - addedEntry.setExternalRef(externalRef); - addedEntry.setManagedFlagsString(managedFlags); - if(RepositoryEntryManagedFlag.isManaged(addedEntry, RepositoryEntryManagedFlag.membersmanagement)) { - addedEntry.setAllowToLeaveOption(RepositoryEntryAllowToLeaveOptions.never); - } else { - addedEntry.setAllowToLeaveOption(RepositoryEntryAllowToLeaveOptions.atAnyTime);//default - } - return addedEntry;//!!!no update at this point - } - private static ICourse prepareCourse(RepositoryEntry addedEntry, String shortTitle, String longTitle, CourseConfigVO courseConfigVO) { // set root node title String courseShortTitle = addedEntry.getDisplayname(); @@ -576,7 +570,6 @@ public class CoursesWebService { CourseFactory.saveCourse(course.getResourceableId()); CourseFactory.closeCourseEditSession(course.getResourceableId(), true); - course = CourseFactory.loadCourse(course.getResourceableId()); - return course; + return CourseFactory.loadCourse(addedEntry); } } diff --git a/src/test/java/org/olat/course/config/CourseConfigManagerImplTest.java b/src/test/java/org/olat/course/config/CourseConfigManagerImplTest.java index 34d0d5c44195e8d1ed4b660dee05278d731727f3..52b8763ab46ff08dec365eb5a3e6999925574802 100644 --- a/src/test/java/org/olat/course/config/CourseConfigManagerImplTest.java +++ b/src/test/java/org/olat/course/config/CourseConfigManagerImplTest.java @@ -29,17 +29,18 @@ package org.olat.course.config; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import org.junit.Before; import org.junit.Test; -import org.olat.core.commons.persistence.DBFactory; -import org.olat.core.id.OLATResourceable; -import org.olat.core.logging.OLog; -import org.olat.core.logging.Tracing; -import org.olat.core.util.resource.OresHelper; +import org.olat.core.commons.persistence.DB; import org.olat.core.util.vfs.VFSItem; import org.olat.course.CourseFactory; +import org.olat.course.CourseModule; import org.olat.course.ICourse; +import org.olat.repository.RepositoryEntry; +import org.olat.repository.RepositoryService; +import org.olat.resource.OLATResource; +import org.olat.resource.OLATResourceManager; import org.olat.test.OlatTestCase; +import org.springframework.beans.factory.annotation.Autowired; /** * Description: <br> @@ -50,54 +51,43 @@ import org.olat.test.OlatTestCase; * @author patrick */ public class CourseConfigManagerImplTest extends OlatTestCase { - private static OLog log = Tracing.createLoggerFor(CourseConfigManagerImplTest.class); - private static ICourse course1; - - private static boolean isSetup = false; - - - /** - * SetUp is called before each test. - */ - @Before - public void setUp() { - if (isSetup) return; - try { - // create course and persist as OLATResourceImpl - OLATResourceable resourceable = OresHelper.createOLATResourceableInstance("junitConfigCourse", new Long(System.currentTimeMillis())); - course1 = CourseFactory.createEmptyCourse(resourceable, "JUnitCourseConfig", "JUnitCourseConfig Long Title", - "objective 1 objective 2 objective 3"); - DBFactory.getInstance().closeSession(); - isSetup = true; - } catch (Exception e) { - log.error("Exception in setUp(): " + e); - e.printStackTrace(); - } - } + @Autowired + private DB dbInstance; + @Autowired + private RepositoryService repositoryService; + @Autowired + private OLATResourceManager resourceManager; @Test public void testConfigFileCRUD() { + // create course and persist as OLATResourceImpl + OLATResource resource = resourceManager.createOLATResourceInstance(CourseModule.class); + RepositoryEntry addedEntry = repositoryService.create("Ayanami", "-", "JUnit course configuration course", "A JUnit course", resource); + ICourse course = CourseFactory.createEmptyCourse(addedEntry, "JUnitCourseConfig", "JUnitCourseConfig Long Title", + "objective 1 objective 2 objective 3"); + dbInstance.commitAndCloseSession(); + /* * a new created course gets its configuration upon the first load with * default values */ CourseConfigManager ccm = CourseConfigManagerImpl.getInstance(); - CourseConfig cc1 = ccm.loadConfigFor(course1); + CourseConfig cc1 = ccm.loadConfigFor(course); assertNotNull("CourseConfiguration is not null", cc1); /* * update values */ - ccm.saveConfigTo(course1, cc1); + ccm.saveConfigTo(course, cc1); cc1 = null; // check the saved values - cc1 = ccm.loadConfigFor(course1); + cc1 = ccm.loadConfigFor(course); assertNotNull("CourseConfiguration is not null", cc1); /* * delete configuration */ - ccm.deleteConfigOf(course1); - VFSItem cc1File = CourseConfigManagerImpl.getConfigFile(course1); + ccm.deleteConfigOf(course); + VFSItem cc1File = CourseConfigManagerImpl.getConfigFile(course); assertFalse("CourseConfig file no longer exists.", cc1File != null); } } \ No newline at end of file diff --git a/src/test/java/org/olat/course/nodes/en/EnrollmentManagerConcurrentTest.java b/src/test/java/org/olat/course/nodes/en/EnrollmentManagerConcurrentTest.java index f2d1c643e6ee16b1783f497333bbee8b222aecf1..bd6852a2eae0d6898bc9696fcc0b1cd0a3788126 100644 --- a/src/test/java/org/olat/course/nodes/en/EnrollmentManagerConcurrentTest.java +++ b/src/test/java/org/olat/course/nodes/en/EnrollmentManagerConcurrentTest.java @@ -52,15 +52,14 @@ import org.olat.core.gui.control.info.WindowControlInfo; import org.olat.core.gui.translator.Translator; import org.olat.core.id.Identity; import org.olat.core.id.IdentityEnvironment; -import org.olat.core.id.OLATResourceable; import org.olat.core.id.Roles; import org.olat.core.id.context.BusinessControl; import org.olat.core.id.context.ContextEntry; import org.olat.core.logging.OLog; import org.olat.core.logging.Tracing; import org.olat.core.util.Util; -import org.olat.core.util.resource.OresHelper; import org.olat.course.CourseFactory; +import org.olat.course.CourseModule; import org.olat.course.groupsandrights.CourseGroupManager; import org.olat.course.nodes.ENCourseNode; import org.olat.course.properties.CoursePropertyManager; @@ -70,6 +69,9 @@ import org.olat.course.run.userview.UserCourseEnvironmentImpl; import org.olat.group.BusinessGroup; import org.olat.group.BusinessGroupService; import org.olat.repository.RepositoryEntry; +import org.olat.repository.RepositoryService; +import org.olat.resource.OLATResource; +import org.olat.resource.OLATResourceManager; import org.olat.test.JunitTestHelper; import org.olat.test.OlatTestCase; import org.springframework.beans.factory.annotation.Autowired; @@ -98,6 +100,10 @@ public class EnrollmentManagerConcurrentTest extends OlatTestCase implements Win private static Translator testTranslator = null; private static BusinessGroup bgWithWaitingList = null; + @Autowired + private RepositoryService repositoryService; + @Autowired + private OLATResourceManager resourceManager; @Autowired private BusinessGroupService businessGroupService; @Autowired @@ -151,8 +157,10 @@ public class EnrollmentManagerConcurrentTest extends OlatTestCase implements Win public void testEnroll() throws Exception { log.info("testEnroll: start..."); ENCourseNode enNode = new ENCourseNode(); - OLATResourceable ores = OresHelper.createOLATResourceableTypeWithoutCheck("TestCourse"); - CourseEnvironment cenv = CourseFactory.createEmptyCourse(ores, "Test", "Test", "learningObjectives").getCourseEnvironment(); + + OLATResource resource = resourceManager.createOLATResourceInstance(CourseModule.class); + RepositoryEntry addedEntry = repositoryService.create("Ayanami", "-", "Enrollment test course 1", "A JUnit course", resource); + CourseEnvironment cenv = CourseFactory.createEmptyCourse(addedEntry, "Test", "Test", "learningObjectives").getCourseEnvironment(); // 1. enroll wg1 user IdentityEnvironment ienv = new IdentityEnvironment(); ienv.setIdentity(wg1); @@ -247,8 +255,9 @@ public class EnrollmentManagerConcurrentTest extends OlatTestCase implements Win } ENCourseNode enNode = new ENCourseNode(); - OLATResourceable ores = OresHelper.createOLATResourceableTypeWithoutCheck("TestEnrollmentCourse"); - CourseEnvironment cenv = CourseFactory.createEmptyCourse(ores, "Test-Enroll", "Test", "Test enrollment with concurrent users").getCourseEnvironment(); + OLATResource resource = resourceManager.createOLATResourceInstance(CourseModule.class); + RepositoryEntry addedEntry = repositoryService.create("Ayanami", "-", "Enrollment test course 2", "A JUnit course", resource); + CourseEnvironment cenv = CourseFactory.createEmptyCourse(addedEntry, "Test-Enroll", "Test", "Test enrollment with concurrent users").getCourseEnvironment(); BusinessGroup group = businessGroupService.createBusinessGroup(id1, "Enrollment", "Enroll", new Integer(1), new Integer(10), true, false, null); Assert.assertNotNull(group); dbInstance.commitAndCloseSession();