From b95b5d911056dd875443c17ff79be3f94f84b75d Mon Sep 17 00:00:00 2001
From: juanf <juanf>
Date: Wed, 13 Aug 2014 14:44:40 +0000
Subject: [PATCH] ELN Refactoring - Refactored Sample Form (Ongoing work)

SVN: 32184
---
 .../newbrowser/html/js/config/Profile.js      |   8 +
 .../views/SampleForm/SampleFormController.js  | 216 +++++++++++++++++-
 .../js/views/SampleForm/SampleFormView.js     |   6 +-
 3 files changed, 226 insertions(+), 4 deletions(-)

diff --git a/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/config/Profile.js b/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/config/Profile.js
index db44631bd0f..fd9fc24cdaa 100644
--- a/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/config/Profile.js
+++ b/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/config/Profile.js
@@ -69,6 +69,14 @@ $.extend(DefaultProfile.prototype, {
 			"isEnabled" : false
 		};
 		
+		this.getDefaultDataStoreCode = function() {
+			var dataStoreCode = null;
+			if(this.allDataStores.length > 0) {
+				 var dataStoreCode = this.allDataStores[0].code
+			}
+			return dataStoreCode;
+		}
+		
 		this.getDefaultDataStoreURL = function() {
 			var dataStoreURL = null;
 			if(this.allDataStores.length > 0) {
diff --git a/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/views/SampleForm/SampleFormController.js b/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/views/SampleForm/SampleFormController.js
index ee3e7c9534a..988cef9ff7b 100644
--- a/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/views/SampleForm/SampleFormController.js
+++ b/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/views/SampleForm/SampleFormController.js
@@ -31,7 +31,221 @@ function SampleFormController(mainController, mode, sample) {
 		return this._sampleFormModel.isFormLoaded;
 	}
 	
-	this.createUpdateSample = function() {
+	this.createUpdateCopySample = function(isCopyWithNewCode, linkParentsOnCopy, copyChildrenOnCopy) {
+		Util.blockUI();
+		var _this = this;
 		
+		//
+		// Parents/Children Links
+		//
+		if(!this._sampleFormModel.sampleLinksParents.isValid()) {
+			Util.showError("Missing Parents.");
+			return;
+		}
+		var sampleParentsFinal = this._sampleFormModel.sampleLinksParents.getSamplesIdentifiers();
+		
+		if(!this._sampleFormModel.sampleLinksChildren.isValid()) {
+			Util.showError("Missing Children.");
+			return;
+		}
+		var sampleChildrenFinal = this._sampleFormModel.sampleLinksChildren.getSamplesIdentifiers();
+		var sampleChildrenRemovedFinal = this._sampleFormModel.sampleLinksChildren.getSamplesRemovedIdentifiers();
+		
+		//
+		// Check that the same sample is not a parent and a child at the same time
+		//
+		var intersect_safe = function(a, b) {
+		  var ai=0, bi=0;
+		  var result = new Array();
+		  
+		  while( ai < a.length && bi < b.length )
+		  {
+		     if      (a[ai] < b[bi] ){ ai++; }
+		     else if (a[ai] > b[bi] ){ bi++; }
+		     else /* they're equal */
+		     {
+		       result.push(a[ai]);
+		       ai++;
+		       bi++;
+		     }
+		  }
+
+		  return result;
+		}
+		
+		sampleParentsFinal.sort();
+		sampleChildrenFinal.sort();
+		var intersection = intersect_safe(sampleParentsFinal, sampleChildrenFinal);
+		if(intersection.length > 0) {
+			Util.showError("The same entity can't be a parent and a child, please check: " + intersection);
+			return;
+		}
+		
+		//
+		//Identification Info
+		//
+		var sampleCode = this._sampleFormModel.sample.code;
+		var properties = this._sampleFormModel.sample.properties;
+		
+		var sampleSpace = this._sampleFormModel.sample.spaceCode; //ELN Sub Experiments will have a space by default but not common Samples
+		if(!sampleSpace) { //Common Samples have maybe a default space to be attached to
+			sampleSpace = profile.getSpaceForSampleType(this.sampleTypeCode);
+		}
+		
+		var experimentIdentifier = this._sampleFormModel.sample.experimentIdentifierOrNull; //ELN Sub Experiments will have a experiment by default but not common Samples
+		if(!experimentIdentifier) { //Common Samples have maybe a default experiment to be attached to
+			experimentIdentifier = profile.getExperimentIdentifierForSample(
+					this._sampleFormModel.sample.sampleTypeCode,
+					this._sampleFormModel.sample.code,
+					properties);
+		}
+		
+		var sampleProject = null;
+		var sampleExperiment = null;
+		
+		if(experimentIdentifier) { //If there is a experiment detected, the sample should be attached to the experiment completely.
+			sampleSpace = experimentIdentifier.split("/")[1];
+			sampleProject = experimentIdentifier.split("/")[2];
+			sampleExperiment = experimentIdentifier.split("/")[3];
+		}
+		
+		//Children to create
+		var samplesToCreate = [];
+		this._sampleFormModel.sampleLinksChildren.getSamples().forEach(function(child) {
+			if(child.newSample) {
+			  child.properties = {};
+				if(profile.storagesConfiguration["isEnabled"]) {
+					child.properties[_this.profile.storagesConfiguration["STORAGE_PROPERTIES"][0]["NAME_PROPERTY"]] = $("#childrenStorageSelector").val();
+					child.properties[_this.profile.storagesConfiguration["STORAGE_PROPERTIES"][0]["ROW_PROPERTY"]] = 1;
+					child.properties[_this.profile.storagesConfiguration["STORAGE_PROPERTIES"][0]["COLUMN_PROPERTY"]] = 1;
+					child.properties[_this.profile.storagesConfiguration["STORAGE_PROPERTIES"][0]["BOX_PROPERTY"]] = $("#sampleSpaceProject").val().replace(/\//g,'\/') + "_" + $("#sampleCode").val() + "_EXP_RESULTS";
+					child.properties[_this.profile.storagesConfiguration["STORAGE_PROPERTIES"][0]["USER_PROPERTY"]] = _this.serverFacade.openbisServer.getSession().split("-")[0];
+				}
+				samplesToCreate.push(child);
+			}
+		});
+		
+		//Method
+		var method = "";
+		if(this._sampleFormModel.mode === FormMode.CREATE) {
+			method = "insertSample";
+		} else if(this._sampleFormModel.mode === FormMode.EDIT) {
+			method = "updateSample";
+		}
+		
+		var parameters = {
+				//API Method
+				"method" : method,
+				//Identification Info
+				"sampleSpace" : sampleSpace,
+				"sampleProject" : sampleProject,
+				"sampleExperiment" : sampleExperiment,
+				"sampleCode" : sampleCode,
+				"sampleType" : this._sampleFormModel.sample.sampleTypeCode,
+				//Other Properties
+				"sampleProperties" : properties,
+				//Parent links
+				"sampleParents": sampleParentsFinal,
+				//Children links
+				"sampleChildren": sampleChildrenFinal,
+				"sampleChildrenNew": samplesToCreate,
+				"sampleChildrenRemoved": sampleChildrenRemovedFinal
+		};
+		
+		//
+		// Copy override - This part modifies what is done for a create/update and adds a couple of extra parameters needed to copy to the bench correctly
+		//
+		if(isCopyWithNewCode) {
+			parameters["method"] = "copySample";
+			parameters["sampleCode"] = isCopyWithNewCode;
+			if(!linkParentsOnCopy) {
+				parameters["sampleParents"] = [];
+			}
+			if(!copyChildrenOnCopy) {
+				parameters["sampleChildren"] = [];
+			} else if(profile.storagesConfiguration["isEnabled"]) {
+				//1. All properties belonging to benches, to not to copy
+				parameters["notCopyProperties"] = [];
+				parameters["defaultBenchPropertyList"] = [];
+				for(var i = 0; i < profile.storagesConfiguration["STORAGE_PROPERTIES"].length; i++) {
+					var storagePropertyGroup = profile.storagesConfiguration["STORAGE_PROPERTIES"][i];
+					var listToUse = "notCopyProperties";
+					if(i === 0) {
+						listToUse = "defaultBenchPropertyList";
+					}
+					parameters[listToUse].push(storagePropertyGroup["NAME_PROPERTY"]);
+					parameters[listToUse].push(storagePropertyGroup["ROW_PROPERTY"]);
+					parameters[listToUse].push(storagePropertyGroup["COLUMN_PROPERTY"]);
+					parameters[listToUse].push(storagePropertyGroup["BOX_PROPERTY"]);
+					parameters[listToUse].push(storagePropertyGroup["USER_PROPERTY"]);
+				}
+				
+				//2. Default Bench properties
+				var defaultStoragePropertyGroup = profile.storagesConfiguration["STORAGE_PROPERTIES"][0];
+				parameters["defaultBenchProperties"] = {};
+				var $benchDropdown = FormUtil.getDefaultBenchDropDown();
+				var defaultBench = $benchDropdown.children()[1].value;
+				parameters["defaultBenchProperties"][defaultStoragePropertyGroup["NAME_PROPERTY"]] = defaultBench;
+				parameters["defaultBenchProperties"][defaultStoragePropertyGroup["ROW_PROPERTY"]] = 1;
+				parameters["defaultBenchProperties"][defaultStoragePropertyGroup["COLUMN_PROPERTY"]] = 1;
+				parameters["defaultBenchProperties"][defaultStoragePropertyGroup["BOX_PROPERTY"]] = $("#sampleSpaceProject").val().replace(/\//g,'\/') + "_" + isCopyWithNewCode + "_EXP_RESULTS";
+				parameters["defaultBenchProperties"][defaultStoragePropertyGroup["USER_PROPERTY"]] = mainController.serverFacade.openbisServer.getSession().split("-")[0];
+			}
+			parameters["sampleChildrenNew"] = [];
+			parameters["sampleChildrenRemoved"] = [];
+		}
+		
+		//
+		// Sending the request to the server
+		//
+		if(profile.getDefaultDataStoreCode()) {
+			window.mainController.serverFacade.createReportFromAggregationService(profile.getDefaultDataStoreCode(), parameters, function(response) {
+				_this._createUpdateCopySampleCallback(_this, isCopyWithNewCode, response);
+			});
+		} else {
+			Util.showError("No DSS available.", function() {Util.unblockUI();});
+		}
+		
+		return false;
+	}
+	
+	this._createUpdateCopySampleCallback = function(_this, isCopyWithNewCode, response) {
+		if(response.error) { //Error Case 1
+			Util.showError(response.error.message, function() {Util.unblockUI();});
+		} else if (response.result.columns[1].title === "Error") { //Error Case 2
+			var stacktrace = response.result.rows[0][1].value;
+			Util.showStacktraceAsError(stacktrace);
+		} else if (response.result.columns[0].title === "STATUS" && response.result.rows[0][0].value === "OK") { //Success Case
+			var sampleType = profile.getSampleTypeForSampleTypeCode(_this._sampleFormModel.sample.sampleTypeCode);
+			var sampleTypeDisplayName = sampleType.description;
+			if(!sampleTypeDisplayName) {
+				sampleTypeDisplayName = _this._sampleFormModel.sample.sampleTypeCode;
+			}
+			
+			var message = "";
+			if(isCopyWithNewCode) {
+				message = "Copied with new code: " + isCopyWithNewCode + ".";
+			} else if(_this._sampleFormModel.mode === FormMode.CREATE) {
+				message = "Created.";
+			} else if(_this._sampleFormModel.mode === FormMode.EDIT) {
+				message = "Updated.";
+			}
+			
+			var callbackOk = function() {
+				Util.unblockUI();
+				if((isCopyWithNewCode || _this._sampleFormModel.mode === FormMode.CREATE) && _this._sampleFormModel.sample.experimentIdentifierOrNull) {
+					window.mainController.sideMenu.refreshSubExperiment(_this._sampleFormModel.sample.experimentIdentifierOrNull);
+				}
+//				TO-DO: The Sample is not necessarily searchable after creation since the index runs asynchronously
+//				localReference.serverFacade.searchWithType(localReference.sampleTypeCode, $("#sampleCode")[0].value, function(data) {
+//					mainController.changeView('showViewSamplePageFromPermId',data[0].permId);
+//				});
+			}
+			
+			Util.showSuccess(sampleTypeDisplayName + " " + message, callbackOk);
+			_this._sampleFormModel.isFormDirty = false;
+		} else { //This should never happen
+			Util.showError("Unknown Error.", function() {Util.unblockUI();});
+		}
 	}
 }
\ No newline at end of file
diff --git a/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/views/SampleForm/SampleFormView.js b/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/views/SampleForm/SampleFormView.js
index 5dc2d7b5ead..466d56632c0 100644
--- a/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/views/SampleForm/SampleFormView.js
+++ b/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/views/SampleForm/SampleFormView.js
@@ -123,7 +123,7 @@ function SampleFormView(sampleFormController, sampleFormModel) {
 					$formColumn.append(FormUtil.getFieldForComponentWithLabel(spacesDropDown, "Space"));
 					spacesDropDown.change(function(event){
 						var spacesDropDown = $(this);
-						_this._sampleFormModel.sample.space = spacesDropDown.val();
+						_this._sampleFormModel.sample.spaceCode = spacesDropDown.val();
 						_this._sampleFormModel.isFormDirty = true;
 					});
 				} else {
@@ -297,7 +297,7 @@ function SampleFormView(sampleFormController, sampleFormModel) {
 		if(this._sampleFormModel.mode !== FormMode.VIEW) {
 			var $updateBtn = $("<a>", { "class" : "btn btn-primary"}).append(title);
 			$updateBtn.click(function() {
-				_this._sampleFormController.createUpdateSample();
+				_this._sampleFormController.createUpdateCopySample();
 			});
 			
 			$formColumn.append($updateBtn);
@@ -397,7 +397,7 @@ function SampleFormView(sampleFormController, sampleFormModel) {
 				var isValid = newSampleCodeForCopy[0].checkValidity();
 				if(isValid) {
 					var newSampleCodeForCopyValue = newSampleCodeForCopy.val();
-					_this._sampleFormController.createUpdateSample(newSampleCodeForCopyValue, linkParentsOnCopy, copyChildrenOnCopy);
+					_this._sampleFormController.createUpdateCopySample(newSampleCodeForCopyValue, linkParentsOnCopy, copyChildrenOnCopy);
 					Util.unblockUI();
 				} else {
 					Util.showError("Invalid code.", function() {}, true);
-- 
GitLab