From 7194ccb5b724a75fb129daa6a4661a3d9cc061d8 Mon Sep 17 00:00:00 2001
From: srosse <stephane.rosse@frentix.com>
Date: Tue, 26 Mar 2019 15:08:48 +0100
Subject: [PATCH] OO-3996: closed courses / inactive elements at the end

---
 .../ui/CurriculumElementWithViewsRow.java     |  12 ++
 .../CurriculumElementViewsRowComparator.java  |  41 ++--
 ...rriculumElementViewsRowComparatorTest.java | 198 ++++++++++++++++++
 3 files changed, 236 insertions(+), 15 deletions(-)
 create mode 100644 src/test/java/org/olat/modules/curriculum/ui/component/CurriculumElementViewsRowComparatorTest.java

diff --git a/src/main/java/org/olat/modules/curriculum/ui/CurriculumElementWithViewsRow.java b/src/main/java/org/olat/modules/curriculum/ui/CurriculumElementWithViewsRow.java
index a35d20f0a7c..082274ab59e 100644
--- a/src/main/java/org/olat/modules/curriculum/ui/CurriculumElementWithViewsRow.java
+++ b/src/main/java/org/olat/modules/curriculum/ui/CurriculumElementWithViewsRow.java
@@ -406,6 +406,18 @@ public class CurriculumElementWithViewsRow implements CurriculumElementWithView,
 	public void setAccessTypes(List<PriceMethod> accessTypes) {
 		this.accessTypes = accessTypes;
 	}
+	
+	public boolean isClosedOrInactive() {
+		if(isCurriculumElementOnly()) {
+			return element.getElementStatus() == CurriculumElementStatus.inactive || element.getElementStatus() == CurriculumElementStatus.deleted;
+		}
+		if(isRepositoryEntryOnly()) {
+			return status != null && status.decommissioned();
+		}
+		return (status != null && status.decommissioned())
+				|| (element != null &&  element.getElementStatus() != null
+				&& (element.getElementStatus() == CurriculumElementStatus.inactive || element.getElementStatus() == CurriculumElementStatus.deleted));
+	}
 
 	@Override
 	public CurriculumElementWithViewsRow getParent() {
diff --git a/src/main/java/org/olat/modules/curriculum/ui/component/CurriculumElementViewsRowComparator.java b/src/main/java/org/olat/modules/curriculum/ui/component/CurriculumElementViewsRowComparator.java
index 28d0469cca3..f609d7dccc6 100644
--- a/src/main/java/org/olat/modules/curriculum/ui/component/CurriculumElementViewsRowComparator.java
+++ b/src/main/java/org/olat/modules/curriculum/ui/component/CurriculumElementViewsRowComparator.java
@@ -78,13 +78,7 @@ public class CurriculumElementViewsRowComparator extends FlexiTreeNodeComparator
 		int c = 0;
 		if((c1.isCurriculumElementOnly() || c1.isCurriculumElementWithEntry()) && (c2.isCurriculumElementOnly() || c2.isCurriculumElementWithEntry())) {
 			// compare by position
-			Long pos1 = c1.getCurriculumElementPos();
-			Long pos2 = c2.getCurriculumElementPos();
-			if(pos1 == null || pos2 == null) {
-				c = compareNullObjects(pos1, pos2);
-			} else {
-				c = Long.compare(pos1.longValue(), pos2.longValue());
-			}	
+			c = compareCurriculumElements(c1, c2);
 		} else if(c1.isCurriculumElementOnly() || c1.isCurriculumElementWithEntry()) {
 			c = 1;
 		} else if(c2.isCurriculumElementOnly() || c2.isCurriculumElementWithEntry()) {
@@ -97,10 +91,18 @@ public class CurriculumElementViewsRowComparator extends FlexiTreeNodeComparator
 	
 	private int compareCurriculumElements(CurriculumElementWithViewsRow c1, CurriculumElementWithViewsRow c2) {
 		int c = 0;
-		if(c1.getCurriculumElementBeginDate() == null || c2.getCurriculumElementBeginDate() == null) {
-			c = compareNullObjects(c1.getCurriculumElementBeginDate(), c2.getCurriculumElementBeginDate());
-		} else {
-			c = c1.getCurriculumElementBeginDate().compareTo(c2.getCurriculumElementBeginDate());
+		if(c1.isClosedOrInactive() && !c2.isClosedOrInactive()) {
+			c = 1;
+		} else if(!c1.isClosedOrInactive() && c2.isClosedOrInactive()) {
+			c = -1;
+		}
+		
+		if(c == 0) {
+			if(c1.getCurriculumElementBeginDate() == null || c2.getCurriculumElementBeginDate() == null) {
+				c = compareNullObjects(c1.getCurriculumElementBeginDate(), c2.getCurriculumElementBeginDate());
+			} else {
+				c = c1.getCurriculumElementBeginDate().compareTo(c2.getCurriculumElementBeginDate());
+			}
 		}
 		
 		if(c == 0) {
@@ -127,10 +129,19 @@ public class CurriculumElementViewsRowComparator extends FlexiTreeNodeComparator
 	
 	private int compareRepositoryEntry(CurriculumElementWithViewsRow c1, CurriculumElementWithViewsRow c2) {
 		int c = 0;
-		if(c1.getRepositoryEntryDisplayName() == null || c2.getRepositoryEntryDisplayName() == null) {
-			c = compareNullObjects(c1.getRepositoryEntryDisplayName(), c2.getRepositoryEntryDisplayName());
-		} else {
-			c = collator.compare(c1.getRepositoryEntryDisplayName(), c2.getRepositoryEntryDisplayName());
+
+		if(c1.isClosedOrInactive() && !c2.isClosedOrInactive()) {
+			c = 1;
+		} else if(!c1.isClosedOrInactive() && c2.isClosedOrInactive()) {
+			c = -1;
+		}
+		
+		if(c == 0) {
+			if(c1.getRepositoryEntryDisplayName() == null || c2.getRepositoryEntryDisplayName() == null) {
+				c = compareNullObjects(c1.getRepositoryEntryDisplayName(), c2.getRepositoryEntryDisplayName());
+			} else {
+				c = collator.compare(c1.getRepositoryEntryDisplayName(), c2.getRepositoryEntryDisplayName());
+			}
 		}
 		
 		if(c == 0) {
diff --git a/src/test/java/org/olat/modules/curriculum/ui/component/CurriculumElementViewsRowComparatorTest.java b/src/test/java/org/olat/modules/curriculum/ui/component/CurriculumElementViewsRowComparatorTest.java
new file mode 100644
index 00000000000..e9783362759
--- /dev/null
+++ b/src/test/java/org/olat/modules/curriculum/ui/component/CurriculumElementViewsRowComparatorTest.java
@@ -0,0 +1,198 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+package org.olat.modules.curriculum.ui.component;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.olat.core.commons.persistence.DB;
+import org.olat.core.id.Identity;
+import org.olat.modules.curriculum.Curriculum;
+import org.olat.modules.curriculum.CurriculumCalendars;
+import org.olat.modules.curriculum.CurriculumElement;
+import org.olat.modules.curriculum.CurriculumElementStatus;
+import org.olat.modules.curriculum.CurriculumLectures;
+import org.olat.modules.curriculum.CurriculumService;
+import org.olat.modules.curriculum.manager.CurriculumDAO;
+import org.olat.modules.curriculum.manager.CurriculumElementDAO;
+import org.olat.modules.curriculum.ui.CurriculumElementWithViewsRow;
+import org.olat.repository.RepositoryEntry;
+import org.olat.repository.RepositoryEntryStatusEnum;
+import org.olat.repository.model.RepositoryEntryMyCourseImpl;
+import org.olat.test.JunitTestHelper;
+import org.olat.test.OlatTestCase;
+import org.springframework.beans.factory.annotation.Autowired;
+
+
+
+/**
+ * 
+ * Initial date: 26 mars 2019<br>
+ * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
+ *
+ */
+public class CurriculumElementViewsRowComparatorTest extends OlatTestCase {
+	
+	@Autowired
+	private DB dbInstance;
+	@Autowired
+	private CurriculumDAO curriculumDao;
+	@Autowired
+	private CurriculumService curriculumService;
+	@Autowired
+	private CurriculumElementDAO curriculumElementDao;
+	
+	@Test
+	public void testCurriculumElementActiveInactive() {
+		Curriculum curriculum = curriculumDao.createAndPersist("Cur-for-el-1", "Curriculum for element", "Curriculum", null);
+		CurriculumElement element1 = curriculumElementDao.createCurriculumElement("Element-1", "1. Element", CurriculumElementStatus.inactive,
+				new Date(), new Date(), null, null, CurriculumCalendars.disabled, CurriculumLectures.disabled, curriculum);
+		CurriculumElement element2 = curriculumElementDao.createCurriculumElement("Element-1", "2. Element", CurriculumElementStatus.inactive,
+				new Date(), new Date(), null, null, CurriculumCalendars.disabled, CurriculumLectures.disabled, curriculum);
+		CurriculumElement element3 = curriculumElementDao.createCurriculumElement("Element-1", "3. Element", CurriculumElementStatus.active,
+				new Date(), new Date(), null, null, CurriculumCalendars.disabled, CurriculumLectures.disabled, curriculum);
+		CurriculumElement element4 = curriculumElementDao.createCurriculumElement("Element-1", "4. Element", CurriculumElementStatus.active,
+				new Date(), new Date(), null, null, CurriculumCalendars.disabled, CurriculumLectures.disabled, curriculum);
+		dbInstance.commitAndCloseSession();
+		
+		CurriculumElementWithViewsRow row1 = new CurriculumElementWithViewsRow(element1, null, 0);
+		CurriculumElementWithViewsRow row2 = new CurriculumElementWithViewsRow(element2, null, 0);
+		CurriculumElementWithViewsRow row3 = new CurriculumElementWithViewsRow(element3, null, 0);
+		CurriculumElementWithViewsRow row4 = new CurriculumElementWithViewsRow(element4, null, 0);
+
+		List<CurriculumElementWithViewsRow> rows = new ArrayList<>();
+		rows.add(row1);
+		rows.add(row2);
+		rows.add(row3);
+		rows.add(row4);
+		
+		Collections.sort(rows, new CurriculumElementViewsRowComparator(Locale.ENGLISH));
+		
+		Assert.assertEquals(element3.getKey(), rows.get(0).getCurriculumElementKey());
+		Assert.assertEquals(element4.getKey(), rows.get(1).getCurriculumElementKey());
+		Assert.assertEquals(element1.getKey(), rows.get(2).getCurriculumElementKey());
+		Assert.assertEquals(element2.getKey(), rows.get(3).getCurriculumElementKey());
+	}
+	
+	/**
+	 * Simulate a list of repository entries under the same curriculum element.
+	 * 
+	 */
+	@Test
+	public void testRepositoryEntryActiveInactive() {
+		Identity author = JunitTestHelper.createAndPersistIdentityAsRndAuthor("sort-cur-el");
+
+		Curriculum curriculum = curriculumDao.createAndPersist("Cur-for-el-1", "Curriculum for element", "Curriculum", null);
+		CurriculumElement element = curriculumElementDao.createCurriculumElement("Element-1", "1. Element", CurriculumElementStatus.inactive,
+				new Date(), new Date(), null, null, CurriculumCalendars.disabled, CurriculumLectures.disabled, curriculum);
+		dbInstance.commitAndCloseSession();
+
+		RepositoryEntry entry1 = JunitTestHelper.deployBasicCourse(author, "1 course", RepositoryEntryStatusEnum.closed, false, false);
+		RepositoryEntry entry2 = JunitTestHelper.deployBasicCourse(author, "2 course", RepositoryEntryStatusEnum.trash, false, false);
+		RepositoryEntry entry3 = JunitTestHelper.deployBasicCourse(author, "3 course", RepositoryEntryStatusEnum.published, false, false);
+		RepositoryEntry entry4 = JunitTestHelper.deployBasicCourse(author, "4 course", RepositoryEntryStatusEnum.published, false, false);
+		// add the course and a participant to the curriculum
+		curriculumService.addRepositoryEntry(element, entry1, false);
+		curriculumService.addRepositoryEntry(element, entry2, false);
+		curriculumService.addRepositoryEntry(element, entry3, false);
+		curriculumService.addRepositoryEntry(element, entry4, false);
+		dbInstance.commitAndCloseSession();
+		
+		CurriculumElementWithViewsRow row1 = new CurriculumElementWithViewsRow(element, null,
+				new RepositoryEntryMyCourseImpl(entry1, null, false, 0, 0), false);
+		CurriculumElementWithViewsRow row2 = new CurriculumElementWithViewsRow(element, null,
+				new RepositoryEntryMyCourseImpl(entry2, null, false, 0, 0), false);
+		CurriculumElementWithViewsRow row3 = new CurriculumElementWithViewsRow(element, null,
+				new RepositoryEntryMyCourseImpl(entry3, null, false, 0, 0), false);
+		CurriculumElementWithViewsRow row4 = new CurriculumElementWithViewsRow(element, null,
+				new RepositoryEntryMyCourseImpl(entry4, null, false, 0, 0), false);
+
+		List<CurriculumElementWithViewsRow> rows = new ArrayList<>();
+		rows.add(row1);
+		rows.add(row2);
+		rows.add(row3);
+		rows.add(row4);
+		
+		Collections.sort(rows, new CurriculumElementViewsRowComparator(Locale.ENGLISH));
+
+		Assert.assertEquals(entry3.getKey(), rows.get(0).getRepositoryEntryKey());
+		Assert.assertEquals(entry4.getKey(), rows.get(1).getRepositoryEntryKey());
+		Assert.assertEquals(entry1.getKey(), rows.get(2).getRepositoryEntryKey());
+		Assert.assertEquals(entry2.getKey(), rows.get(3).getRepositoryEntryKey());
+	}
+	
+	/**
+	 * Simulate a list of repository entries under their own curriculum element.
+	 * 
+	 */
+	@Test
+	public void testActiveInactiveClosedOrNot() {
+		Identity author = JunitTestHelper.createAndPersistIdentityAsRndAuthor("sort-cur-el");
+
+		Curriculum curriculum = curriculumDao.createAndPersist("Cur-for-el-1", "Curriculum for element", "Curriculum", null);
+		CurriculumElement element1 = curriculumElementDao.createCurriculumElement("Element-1", "1. Element", CurriculumElementStatus.inactive,
+				new Date(), new Date(), null, null, CurriculumCalendars.disabled, CurriculumLectures.disabled, curriculum);
+		CurriculumElement element2 = curriculumElementDao.createCurriculumElement("Element-2", "2. Element", CurriculumElementStatus.inactive,
+				new Date(), new Date(), null, null, CurriculumCalendars.disabled, CurriculumLectures.disabled, curriculum);
+		CurriculumElement element3 = curriculumElementDao.createCurriculumElement("Element-3", "3. Element", CurriculumElementStatus.active,
+				new Date(), new Date(), null, null, CurriculumCalendars.disabled, CurriculumLectures.enabled, curriculum);
+		CurriculumElement element4 = curriculumElementDao.createCurriculumElement("Element-4", "4. Element", CurriculumElementStatus.active,
+				new Date(), new Date(), null, null, CurriculumCalendars.disabled, CurriculumLectures.enabled, curriculum);
+		dbInstance.commitAndCloseSession();
+
+		RepositoryEntry entry1 = JunitTestHelper.deployBasicCourse(author, "1 course", RepositoryEntryStatusEnum.closed, false, false);
+		RepositoryEntry entry2 = JunitTestHelper.deployBasicCourse(author, "2 course", RepositoryEntryStatusEnum.published, false, false);
+		RepositoryEntry entry3 = JunitTestHelper.deployBasicCourse(author, "3 course", RepositoryEntryStatusEnum.closed, false, false);
+		RepositoryEntry entry4 = JunitTestHelper.deployBasicCourse(author, "4 course", RepositoryEntryStatusEnum.published, false, false);
+		// add the course and a participant to the curriculum
+		curriculumService.addRepositoryEntry(element1, entry1, false);
+		curriculumService.addRepositoryEntry(element2, entry2, false);
+		curriculumService.addRepositoryEntry(element3, entry3, false);
+		curriculumService.addRepositoryEntry(element4, entry4, false);
+		dbInstance.commitAndCloseSession();
+		
+		CurriculumElementWithViewsRow row1 = new CurriculumElementWithViewsRow(element1, null,
+				new RepositoryEntryMyCourseImpl(entry1, null, false, 0, 0), true);
+		CurriculumElementWithViewsRow row2 = new CurriculumElementWithViewsRow(element2, null,
+				new RepositoryEntryMyCourseImpl(entry2, null, false, 0, 0), true);
+		CurriculumElementWithViewsRow row3 = new CurriculumElementWithViewsRow(element3, null,
+				new RepositoryEntryMyCourseImpl(entry3, null, false, 0, 0), true);
+		CurriculumElementWithViewsRow row4 = new CurriculumElementWithViewsRow(element4, null,
+				new RepositoryEntryMyCourseImpl(entry4, null, false, 0, 0), true);
+
+		List<CurriculumElementWithViewsRow> rows = new ArrayList<>();
+		rows.add(row1);
+		rows.add(row2);
+		rows.add(row3);
+		rows.add(row4);
+		
+		Collections.sort(rows, new CurriculumElementViewsRowComparator(Locale.ENGLISH));
+
+		Assert.assertEquals(entry4.getKey(), rows.get(0).getRepositoryEntryKey());
+		Assert.assertEquals(entry1.getKey(), rows.get(1).getRepositoryEntryKey());
+		Assert.assertEquals(entry2.getKey(), rows.get(2).getRepositoryEntryKey());
+		Assert.assertEquals(entry3.getKey(), rows.get(3).getRepositoryEntryKey());
+	}
+}
-- 
GitLab