From 066e74e011419136b83373ce1f282db0468696b9 Mon Sep 17 00:00:00 2001
From: srosse <stephane.rosse@frentix.com>
Date: Thu, 16 Jan 2020 08:42:13 +0100
Subject: [PATCH] OO-4470: allow to set empty classification and edu. context

---
 .../model/xml/ManifestMetadataBuilder.java    | 78 +++++++++++--------
 .../metadata/ManifestMetadataItemized.java    | 12 ++-
 2 files changed, 56 insertions(+), 34 deletions(-)

diff --git a/src/main/java/org/olat/ims/qti21/model/xml/ManifestMetadataBuilder.java b/src/main/java/org/olat/ims/qti21/model/xml/ManifestMetadataBuilder.java
index a4e2fc00588..a9f8fd61a5d 100644
--- a/src/main/java/org/olat/ims/qti21/model/xml/ManifestMetadataBuilder.java
+++ b/src/main/java/org/olat/ims/qti21/model/xml/ManifestMetadataBuilder.java
@@ -267,19 +267,26 @@ public class ManifestMetadataBuilder {
 	}
 	
 	public void setEducationalContext(String context, String lang) {
-		EducationalType educational = getEducational(true);
-		if(educational != null) {
-			ContextType type = getFromAny(ContextType.class, educational.getContent());
-			if(type == null) {
-				type = mdObjectFactory.createContextType();
-				educational.getContent().add(mdObjectFactory.createContext(type));
+		if(StringHelper.containsNonWhitespace(context)) {
+			EducationalType educational = getEducational(true);
+			if(educational != null) {
+				ContextType type = getFromAny(ContextType.class, educational.getContent());
+				if(type == null) {
+					type = mdObjectFactory.createContextType();
+					educational.getContent().add(mdObjectFactory.createContext(type));
+				}
+				SourceType sourceType = mdObjectFactory.createSourceType();
+				sourceType.setLangstring(createString("https://www.openolat.org", lang));
+				ValueType valueType = mdObjectFactory.createValueType();
+				valueType.setLangstring(createString(context, lang));
+				type.setSource(sourceType);
+				type.setValue(valueType);
+			}
+		} else {
+			EducationalType educational = getEducational(false);
+			if(educational != null) {
+				clearFromAny(ContextType.class, educational.getContent());
 			}
-			SourceType sourceType = mdObjectFactory.createSourceType();
-			sourceType.setLangstring(createString("https://www.openolat.org", lang));
-			ValueType valueType = mdObjectFactory.createValueType();
-			valueType.setLangstring(createString(context, lang));
-			type.setSource(sourceType);
-			type.setValue(valueType);
 		}
 	}
 	
@@ -341,7 +348,7 @@ public class ManifestMetadataBuilder {
 				List<TaxonType> taxons = taxonpath.getTaxon();
 				if(taxons != null) {
 					for(TaxonType taxon:taxons) {
-						if(taxon.getEntry() != null && taxon.getEntry().getLangstring().size() > 0) {
+						if(taxon.getEntry() != null && !taxon.getEntry().getLangstring().isEmpty()) {
 							LangstringType value = taxon.getEntry().getLangstring().get(0);
 							if(value != null && value.getValue() != null) {
 								sb.append("/").append(value.getValue());
@@ -360,25 +367,32 @@ public class ManifestMetadataBuilder {
 	 * @param lang
 	 */
 	public void setClassificationTaxonomy(String taxonomyPath, String lang) {
-		ClassificationType classification = getClassification("discipline", lang, true);
-		if(classification != null) {
-			TaxonpathType taxonpathType = mdObjectFactory.createTaxonpathType();
-			clearFromAny(TaxonpathType.class, classification.getContent());
-			classification.getContent().add(mdObjectFactory.createTaxonpath(taxonpathType));
-			taxonpathType.getTaxon().clear();
-			
-			SourceType sourceType = mdObjectFactory.createSourceType();
-			sourceType.setLangstring(createString("Unkown", "en"));
-			taxonpathType.setSource(sourceType);
-			
-			for(StringTokenizer tokenizer = new StringTokenizer(taxonomyPath, "/"); tokenizer.hasMoreTokens(); ) {
-				String level = tokenizer.nextToken();
+		if(StringHelper.containsNonWhitespace(taxonomyPath)) {
+			ClassificationType classification = getClassification("discipline", lang, true);
+			if(classification != null) {
+				TaxonpathType taxonpathType = mdObjectFactory.createTaxonpathType();
+				clearFromAny(TaxonpathType.class, classification.getContent());
+				classification.getContent().add(mdObjectFactory.createTaxonpath(taxonpathType));
+				taxonpathType.getTaxon().clear();
+				
+				SourceType sourceType = mdObjectFactory.createSourceType();
+				sourceType.setLangstring(createString("Unkown", "en"));
+				taxonpathType.setSource(sourceType);
 				
-				TaxonType taxonType = mdObjectFactory.createTaxonType();
-				EntryType entryType = mdObjectFactory.createEntryType();
-				createOrUpdateFirstLangstring(entryType.getLangstring(), level, lang);
-				taxonType.setEntry(entryType);
-				taxonpathType.getTaxon().add(taxonType);
+				for(StringTokenizer tokenizer = new StringTokenizer(taxonomyPath, "/"); tokenizer.hasMoreTokens(); ) {
+					String level = tokenizer.nextToken();
+					
+					TaxonType taxonType = mdObjectFactory.createTaxonType();
+					EntryType entryType = mdObjectFactory.createEntryType();
+					createOrUpdateFirstLangstring(entryType.getLangstring(), level, lang);
+					taxonType.setEntry(entryType);
+					taxonpathType.getTaxon().add(taxonType);
+				}
+			}
+		} else {
+			ClassificationType classification = getClassification("discipline", lang, false);
+			if(classification != null) {
+				clearFromAny(TaxonpathType.class, classification.getContent());
 			}
 		}
 	}
@@ -413,7 +427,7 @@ public class ManifestMetadataBuilder {
 	
 	public String getFirstString(List<LangstringType> langStrings) {
 		String firstString = null;
-		if(langStrings != null && langStrings.size() > 0) {
+		if(langStrings != null && !langStrings.isEmpty()) {
 			firstString = langStrings.get(0).getValue();
 		}
 		return firstString;
diff --git a/src/main/java/org/olat/ims/qti21/ui/editor/metadata/ManifestMetadataItemized.java b/src/main/java/org/olat/ims/qti21/ui/editor/metadata/ManifestMetadataItemized.java
index 1228f2f63b8..0c875a41522 100644
--- a/src/main/java/org/olat/ims/qti21/ui/editor/metadata/ManifestMetadataItemized.java
+++ b/src/main/java/org/olat/ims/qti21/ui/editor/metadata/ManifestMetadataItemized.java
@@ -307,7 +307,11 @@ class ManifestMetadataItemized implements QuestionItem, QuestionItemEditable {
 	@Override
 	public void setTaxonomyLevel(TaxonomyLevel taxonomyLevel) {
 		this.taxonomyLevel = taxonomyLevel;
-		metadataBuilder.setClassificationTaxonomy(getTaxonomicPath(), lang);
+		if(taxonomyLevel == null) {
+			metadataBuilder.setClassificationTaxonomy(null, lang);
+		} else {
+			metadataBuilder.setClassificationTaxonomy(getTaxonomicPath(), lang);
+		}
 	}
 
 	@Override
@@ -323,7 +327,11 @@ class ManifestMetadataItemized implements QuestionItem, QuestionItemEditable {
 	@Override
 	public void setEducationalContext(QEducationalContext context) {
 		this.context = context;
-		metadataBuilder.setEducationalContext(context.getLevel(), lang);
+		if(context == null) {
+			metadataBuilder.setEducationalContext(null, lang);
+		} else {
+			metadataBuilder.setEducationalContext(context.getLevel(), lang);
+		}
 	}
 
 	@Override
-- 
GitLab