From 31f0869b5c191aaebbbf819b75a451c963266258 Mon Sep 17 00:00:00 2001
From: juanf <juanf@bsse-bs-dock-dhcp-015.ethz.ch>
Date: Wed, 2 May 2018 15:45:01 +0200
Subject: [PATCH] SSDM-6062 : Hiding sample types can be configured on the
 settings + loading multiple settings

---
 .../html/js/config/SettingsManager.js         | 49 +++++++++++++------
 .../html/js/config/StandardProfile.js         |  7 ++-
 .../SettingsForm/SettingsFormController.js    |  7 +--
 .../js/views/SettingsForm/SettingsFormView.js |  8 ++-
 4 files changed, 49 insertions(+), 22 deletions(-)

diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/SettingsManager.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/SettingsManager.js
index a2a853232a0..acf393c8087 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/SettingsManager.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/SettingsManager.js
@@ -19,10 +19,17 @@ function SettingsManager(serverFacade) {
 	}
 
 	this.loadSettings = function(callback) {
-		this._serverFacade.searchSamples({ "sampleIdentifier" : "/ELN_SETTINGS/GENERAL_ELN_SETTINGS", "withProperties" : true }, (function(data) {
-			if (data && data[0] && data[0].properties && data[0].properties.ELN_SETTINGS) {
-				var settings = JSON.parse(data[0].properties.ELN_SETTINGS);
-				callback(settings);
+		this._serverFacade.searchSamples({ 	"sampleTypeCode" : "GENERAL_ELN_SETTINGS",
+											"withProperties" : true }, (function(settingsObjects) {
+			if(settingsObjects && settingsObjects.length > 0) {
+				settingsObjects.sort(function(a, b) {
+				    if(a.identifier === "/ELN_SETTINGS/GENERAL_ELN_SETTINGS") { // Global settings are applied first to be overriden by others
+				    		return 1;
+				    } else {
+				    		return -1;
+				    }
+				});
+				callback(settingsObjects);
 			} else {
 				callback();
 			}
@@ -32,15 +39,23 @@ function SettingsManager(serverFacade) {
 
 	// Loads settings and logs validation errors to console.
 	// Applies the settings to the profile even if they are invalid.
-    this.loadSettingsAndApplyToProfile = function(doneCallback) {
-		this.loadSettings((function(settings) {
-			if (settings) {
-				var errors = this._validateSettings(settings);
-				if (errors.length > 0) {
-					console.log("The settings contain the following errors:");
-					console.log(errors);
+    this.loadSettingsAndApplyToProfile = function(doneCallback, profileToEditOrNull) {
+		this.loadSettings((function(settingsObjects) {
+			if(settingsObjects) {
+				for(var sIdx = 0; sIdx < settingsObjects.length; sIdx++) {
+					var settingsObject = settingsObjects[sIdx];
+					if (settingsObject && settingsObject.properties && settingsObject.properties.ELN_SETTINGS) {
+						var settings = JSON.parse(settingsObject.properties.ELN_SETTINGS);
+						if (settings) {
+							var errors = this._validateSettings(settings);
+							if (errors.length > 0) {
+								console.log("The settings contain the following errors:");
+								console.log(errors);
+							}
+							this.applySettingsToProfile(settings, (profileToEditOrNull)?profileToEditOrNull:profile);
+						}
+					}
 				}
-				this.applySettingsToProfile(settings, profile);
 			}
 			doneCallback();
 		}).bind(this));
@@ -71,7 +86,7 @@ function SettingsManager(serverFacade) {
 	}
 
 	this.applySettingsToProfile = function(settings, targetProfile) {
-		// fields to be overwritten
+		// fields that get overwritten with settings if found
 		var fields = [
 			"dataSetTypeForFileNameMap",
 			"forcedDisableRTF",
@@ -83,15 +98,19 @@ function SettingsManager(serverFacade) {
 				targetProfile[field] = settings[field];
 			}
 		}
-		// main menu
+		// main menu, checks menu items one by one to keep new ones
 		for (var menuItem of Object.keys(targetProfile.mainMenu)) {
 			if (settings.mainMenu[menuItem] != undefined) {
 				targetProfile.mainMenu[menuItem] = settings.mainMenu[menuItem];
 			}
 		}
-		// sampleTypeDefinitionsExtension
+		// sampleTypeDefinitionsExtension gets overwritten with settings if found
 		for (var sampleType of Object.keys(settings.sampleTypeDefinitionsExtension)) {
 			profile.sampleTypeDefinitionsExtension[sampleType] = settings.sampleTypeDefinitionsExtension[sampleType];
+			// Add the types to hide
+			if(settings.sampleTypeDefinitionsExtension[sampleType].HIDE) {
+				targetProfile.hideTypes["sampleTypeCodes"].push(sampleType);
+			}
 		}
 	}
 
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/StandardProfile.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/StandardProfile.js
index b2454b21db8..a7e7904184c 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/StandardProfile.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/StandardProfile.js
@@ -254,12 +254,17 @@ $.extend(StandardProfile.prototype, DefaultProfile.prototype, {
 													}																					
 												],
 				},
+				"GENERAL_ELN_SETTINGS" : {
+					"HIDE" : true
+				},
 				"STORAGE" : {
 					"SAMPLE_CHILDREN_DISABLED" : true,
 					"SAMPLE_PARENTS_DISABLED" : true,
+					"HIDE" : true
 				},
 				"STORAGE_POSITION" : {
-					"SAMPLE_CHILDREN_DISABLED" : true
+					"SAMPLE_CHILDREN_DISABLED" : true,
+					"HIDE" : true
 				},
 				"REQUEST" : {
 					"SAMPLE_PARENTS_TITLE" : "Products from Catalog",
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormController.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormController.js
index afe1675fd2c..3015e629d9c 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormController.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormController.js
@@ -28,12 +28,9 @@ function SettingsFormController(mainController, settingsSample, mode) {
 
 		// apply settings to copy of profile, even if invalid, for editing
 		var profileToEdit = jQuery.extend(true, {}, profile);
-		this._settingsManager.loadSettings((function(settings) {
-			if (settings) {
-				this._settingsManager.applySettingsToProfile(settings, profileToEdit);
-			}
+		this._settingsManager.loadSettingsAndApplyToProfile((function() {
 			this._settingsFormView.repaint(views, profileToEdit);
-		}).bind(this));
+		}).bind(this), profileToEdit);
 	}
 
 	this.save = function(settings) {
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormView.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormView.js
index 07cacc74623..2449dfef151 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormView.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormView.js
@@ -325,7 +325,7 @@ function SettingsFormView(settingsFormController, settingsFormModel) {
 	this._paintSampleTypesDefinition = function($container, text) {
 		var $fieldset = this._getFieldset($container, text.title, "settings-section-sampletype-definitions");
 		$fieldset.append(FormUtil.getInfoText(text.info));
-		for (var sampleType of this._profileToEdit.getAllSampleTypes(true)) {
+		for (var sampleType of this._profileToEdit.getAllSampleTypes(false)) {
 			// layout
 			var $div = $("<div>").css("padding-left", "15px");
 			var displayName = Util.getDisplayNameFromCode(sampleType.code);
@@ -389,6 +389,10 @@ function SettingsFormView(settingsFormController, settingsFormModel) {
 				name : "Enable Storage",
 				enabled : sampleTypeSettings["ENABLE_STORAGE"]
 			});
+			tableModel.addRow({
+				name : "Hide",
+				enabled : sampleTypeSettings["HIDE"]
+			});
 		} else { // default values
 			tableModel.addRow({
 				name : "Use as Protocol",
@@ -407,6 +411,8 @@ function SettingsFormView(settingsFormController, settingsFormModel) {
 					settings["USE_AS_PROTOCOL"] = rowValues["enabled"];
 				} else if (rowValues["Options"] === "Enable Storage") {
 					settings["ENABLE_STORAGE"] = rowValues["enabled"];
+				} else if (rowValues["Options"] === "Hide") {
+					settings["HIDE"] = rowValues["enabled"];
 				}
 			}
 			return settings;
-- 
GitLab