From 68022a09d76a8d1f8d6ff23904c0407f177e5e21 Mon Sep 17 00:00:00 2001
From: felmer <franz-josef.elmer@id.ethz.ch>
Date: Mon, 24 Apr 2023 09:04:40 +0200
Subject: [PATCH] SSDM-13120: Bug fixed: Semantic Annotations did shown up on
 sample creation view. Also remove doubled property annotations when not
 overload by property assignments.

---
 .../eln-lims/html/js/server/ServerFacade.js   | 24 +++--------
 .../js/views/SampleForm/SampleFormView.js     | 42 +++++++++++++++----
 2 files changed, 40 insertions(+), 26 deletions(-)

diff --git a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/server/ServerFacade.js b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/server/ServerFacade.js
index 44e7799baaa..5861601681c 100644
--- a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/server/ServerFacade.js
+++ b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/server/ServerFacade.js
@@ -643,6 +643,11 @@ function ServerFacade(openbisServer) {
 				var entityTypePermId = new EntityTypePermId(sampleType, EntityKind.SAMPLE);
 				var sampleTypeFetchOptions = new SampleTypeFetchOptions();
                 sampleTypeFetchOptions.withPropertyAssignments().withPropertyType();
+                if (profile.showSemanticAnnotations != false) {
+                    sampleTypeFetchOptions.withSemanticAnnotations();
+                    sampleTypeFetchOptions.withPropertyAssignments().withSemanticAnnotations();
+                    sampleTypeFetchOptions.withPropertyAssignments().withPropertyType().withSemanticAnnotations();
+                }
 				mainController.openbisV3.getSampleTypes(entityTypePermId, sampleTypeFetchOptions).done(function(sampleTypesByIds) {
 					callback(sampleTypesByIds[entityTypePermId]);
 				}).fail(function(error) {
@@ -2409,18 +2414,6 @@ function ServerFacade(openbisServer) {
 		v1Sample["experimentIdentifierOrNull"] = (v3Sample.experiment)?v3Sample.experiment.identifier.identifier:null;
         v1Sample["experimentTypeCode"] = v3Sample.experiment && v3Sample.experiment.type ? v3Sample.experiment.type.code : null;
 		v1Sample["sampleTypeCode"] = (v3Sample.type)?v3Sample.type.code:null;
-        v1Sample["semanticAnnotations"] = (v3Sample.type)?v3Sample.type.semanticAnnotations:null;
-        if (v3Sample.type && v3Sample.type.propertyAssignments) {
-            v1Sample["propertyTypesSemanticAnnotations"] = {}
-            v3Sample.type.propertyAssignments.forEach(function(assignment) {
-                var propertyType = assignment.getPropertyType();
-                var annotations = [].concat(assignment.getSemanticAnnotations());
-                if (propertyType && propertyType.getSemanticAnnotations()) {
-                    annotations = annotations.concat(propertyType.getSemanticAnnotations());
-                }
-                v1Sample["propertyTypesSemanticAnnotations"][propertyType.getCode()] = annotations;
-            });
-        }
 		v1Sample["properties"] = v3Sample.properties;
 
 		v1Sample["registrationDetails"] = {};
@@ -2754,11 +2747,6 @@ function ServerFacade(openbisServer) {
             var fetchOptions = new SampleFetchOptions();
             fetchOptions.withSpace();
             fetchOptions.withType();
-            if (fechOptions["withSemanticAnnotations"] && profile.showSemanticAnnotations != false) {
-                fetchOptions.withType().withSemanticAnnotations();
-                fetchOptions.withType().withPropertyAssignments().withSemanticAnnotations();
-                fetchOptions.withType().withPropertyAssignments().withPropertyType().withSemanticAnnotations();
-            }
             fetchOptions.withRegistrator();
             fetchOptions.withModifier();
             fetchOptions.withProject();
@@ -2819,7 +2807,6 @@ function ServerFacade(openbisServer) {
 			"samplePermId" : samplePermId,
 			"withProperties" : true,
 			"withParents" : true,
-            "withSemanticAnnotations" : true,
 			"withChildren" : true
 		}, callbackFunction);
 	}
@@ -2916,7 +2903,6 @@ function ServerFacade(openbisServer) {
 				"withProperties" : true,
 				"withParents" : true,
 				"withChildren" : true,
-                "withSemanticAnnotations" : true,
 				"sampleIdentifier" : sampleIdentifier
 			}, function(samples) {
 				samples.forEach(function(sample) {
diff --git a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SampleForm/SampleFormView.js b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SampleForm/SampleFormView.js
index e7a9d22e87d..1218fe7c8f0 100644
--- a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SampleForm/SampleFormView.js
+++ b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SampleForm/SampleFormView.js
@@ -675,14 +675,11 @@ function SampleFormView(sampleFormController, sampleFormModel) {
 			$legend.remove();
 		}
 
-		var showSemanticAnnotations = profile.showSemanticAnnotations;
 		var propertyGroupPropertiesOnForm = 0;
 		for(var j = 0; j < propertyTypeGroup.propertyTypes.length; j++) {
 		    var propertyType = propertyTypeGroup.propertyTypes[j];
 			var propertyTypeV3 = profile.getPropertyTypeFromSampleTypeV3(this._sampleFormModel.sampleType, propertyType.code);
-            if (this._sampleFormModel.sample.propertyTypesSemanticAnnotations) {
-                var semanticAnnotations = this._renderPropertyTypeSemanticAnnotations(this._sampleFormModel.sample.propertyTypesSemanticAnnotations[propertyTypeV3.code]);
-            }
+            var semanticAnnotations = this._renderPropertyTypeSemanticAnnotations(propertyType.code);
 			FormUtil.fixStringPropertiesForForm(propertyTypeV3, this._sampleFormModel.sample);
 			if(!propertyType.showInEditViews && (this._sampleFormModel.mode === FormMode.EDIT || this._sampleFormModel.mode === FormMode.CREATE) && propertyType.code !== "$XMLCOMMENTS") { //Skip
 				continue;
@@ -835,7 +832,8 @@ function SampleFormView(sampleFormController, sampleFormModel) {
 		return false;
 	}
 	
-    this._renderPropertyTypeSemanticAnnotations = function(annotations) {
+    this._renderPropertyTypeSemanticAnnotations = function(propertyTypeCode) {
+        var annotations = this._getAllSemanticAnnotations(propertyTypeCode);
         if (annotations && annotations.length > 0) {
             var $group = $("<div>", {class : "form-group"});
             $group.append($("<label>", {class : "control-label"}).text("Semantic Annotations:"));
@@ -852,6 +850,36 @@ function SampleFormView(sampleFormController, sampleFormModel) {
         return null;
     }
 
+    this._getAllSemanticAnnotations = function(propertyTypeCode) {
+        // Using a dict because the same property type annotations appear for the assignments if not
+        // overloaded
+        var semanticAnnotations = {};
+        var propertyAssignment = this._getPropertyAssignment(propertyTypeCode);
+        if (propertyAssignment) {
+            [propertyAssignment.semanticAnnotations, propertyAssignment.propertyType.semanticAnnotations].forEach(function(annotations) {
+                if (annotations) {
+                    annotations.forEach(function(annotation) {
+                        semanticAnnotations[annotation.permId.permId] = annotation; 
+                    });
+                }
+            });
+        }
+        return Object.values(semanticAnnotations);
+    }
+
+    this._getPropertyAssignment = function(propertyTypeCode) {
+        if (this._sampleFormModel.sampleType && this._sampleFormModel.sampleType.propertyAssignments) {
+            var propertyAssignments = this._sampleFormModel.sampleType.propertyAssignments;
+            for (var i = 0; i < propertyAssignments.length; i++) {
+                var propertyAssignment = propertyAssignments[i];
+                if (propertyAssignment.propertyType.code === propertyTypeCode) {
+                    return propertyAssignment;
+                }
+            }
+        }
+        return null;
+    }
+
 	this._createIdentificationInfoSection = function(hideShowOptionsModel, sampleType, entityPath) {
 		hideShowOptionsModel.push({
 			label : "Identification Info",
@@ -942,12 +970,12 @@ function SampleFormView(sampleFormController, sampleFormModel) {
 	}
 
     this._appendSemanticAnnotions = function($fieldset) {
-        if (this._sampleFormModel.sample.semanticAnnotations && this._sampleFormModel.sample.semanticAnnotations.length > 0) {
+        if (this._sampleFormModel.sampleType.semanticAnnotations && this._sampleFormModel.sampleType.semanticAnnotations.length > 0) {
             var $group = $("<div>", {class : "form-group"});
             $group.append($("<label>", {class : "control-label"}).text("Semantic Annotations:"));
             var $controls = $("<div>", {class : "controls" });
             var _this = this;
-            this._sampleFormModel.sample.semanticAnnotations.forEach(function(annotation) {
+            this._sampleFormModel.sampleType.semanticAnnotations.forEach(function(annotation) {
                 $controls.append(_this._renderSemanticAnnotation(annotation.getDescriptorAccessionId(),
                         annotation.getDescriptorOntologyId(),
                         annotation.getDescriptorOntologyVersion()));
-- 
GitLab