From fa4948ef004f8a7d0bd2dcff90482d0c4c1beb97 Mon Sep 17 00:00:00 2001
From: alaskowski <alaskowski@ethz.ch>
Date: Fri, 19 May 2023 09:47:57 +0200
Subject: [PATCH] SSDM-13524: Fixed adding comments to dataset properties upon
 creation

---
 .../1/as/webapps/eln-lims/html/js/util/FormUtil.js   |  8 +++++---
 .../js/views/DataSetForm/DataSetFormController.js    | 10 +++++++---
 .../js/views/SampleForm/widgets/CommentsModel.js     | 12 ++++++++++--
 3 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/FormUtil.js b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/FormUtil.js
index 4266047457e..f21fd5ff39d 100644
--- a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/FormUtil.js
+++ b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/util/FormUtil.js
@@ -1280,9 +1280,11 @@ var FormUtil = new function() {
 	}
 	
 	this.fixStringPropertiesForForm = function(propertyType, entity) {
-		var originalValue = entity.properties[propertyType.code];
-		if(propertyType.metaData["custom_widget"] && propertyType.metaData["custom_widget"] === "Word Processor" && originalValue) { // Only filter properties rendered as HTML
-			entity.properties[propertyType.code] = this.sanitizeRichHTMLText(originalValue);
+	    if(entity) {
+            var originalValue = entity.properties[propertyType.code];
+            if(propertyType.metaData["custom_widget"] && propertyType.metaData["custom_widget"] === "Word Processor" && originalValue) { // Only filter properties rendered as HTML
+                entity.properties[propertyType.code] = this.sanitizeRichHTMLText(originalValue);
+            }
 		}
 	}
 	
diff --git a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/DataSetFormController.js b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/DataSetFormController.js
index a731947a24b..3c68d2b5bfe 100644
--- a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/DataSetFormController.js
+++ b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/DataSetForm/DataSetFormController.js
@@ -18,6 +18,7 @@ function DataSetFormController(parentController, mode, entity, dataSet, isMini,
 	this._parentController = parentController;
 	this._dataSetFormModel = new DataSetFormModel(mode, entity, isMini, dataSetV3);
 	this._dataSetFormView = new DataSetFormView(this, this._dataSetFormModel);
+	this._commentsController = null;
 	
 	this.init = function(views) {
 		var _this = this;
@@ -70,10 +71,10 @@ function DataSetFormController(parentController, mode, entity, dataSet, isMini,
 	}
 	
 	this._addCommentsWidget = function($container) {
-		var commentsController = new CommentsController(this._dataSetFormModel.dataSetV3, this._dataSetFormModel.mode, this._dataSetFormModel);
+		this._commentsController  = new CommentsController(this._dataSetFormModel.dataSetV3, this._dataSetFormModel.mode, this._dataSetFormModel);
 		if(this._dataSetFormModel.mode !== FormMode.VIEW || 
-			this._dataSetFormModel.mode === FormMode.VIEW && !commentsController.isEmpty()) {
-			commentsController.init($container);
+			this._dataSetFormModel.mode === FormMode.VIEW && !this._commentsController.isEmpty()) {
+			this._commentsController.init($container);
 			return true;
 		} else {
 			return false;
@@ -154,6 +155,9 @@ function DataSetFormController(parentController, mode, entity, dataSet, isMini,
 		// Metadata Submit and Creation (Step 2)
 		//
 		var metadata = this._dataSetFormModel.dataSetV3 ? this._dataSetFormModel.dataSetV3.properties : {};
+        if(this._commentsController) {
+			metadata = Object.assign({}, metadata, this._commentsController._commentsModel._getProperties());
+		}
 
 		var isZipDirectoryUpload = profile.isZipDirectoryUpload($('#DATASET_TYPE').val());
 		if(isZipDirectoryUpload === null) {
diff --git a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SampleForm/widgets/CommentsModel.js b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SampleForm/widgets/CommentsModel.js
index 1f9d7731aa0..0241db07c71 100644
--- a/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SampleForm/widgets/CommentsModel.js
+++ b/ui-eln-lims/src/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SampleForm/widgets/CommentsModel.js
@@ -20,11 +20,19 @@ function CommentsModel(entity, mode, form) {
 	this.form = form;
 	
 	this.getComments = function() {
-		return this.entity.properties["$XMLCOMMENTS"];
+	    return this._getProperties()["$XMLCOMMENTS"];
 	}
 	
 	this.setComments = function(commentsXML) {
-		this.entity.properties["$XMLCOMMENTS"] = commentsXML;
+	    this._getProperties()["$XMLCOMMENTS"] = commentsXML;
 		this.form.isFormDirty = true;
 	}
+
+	this._getProperties = function() {
+	    if(!this.entity) {
+            this.entity = { properties : {} };
+        }
+        return this.entity.properties;
+	}
+
 }
\ No newline at end of file
-- 
GitLab