From 7283aa7ccfda8bc3d8355ef2aa5845582b980162 Mon Sep 17 00:00:00 2001
From: parkera <parkera>
Date: Fri, 11 Mar 2016 15:00:47 +0000
Subject: [PATCH] SSDM-3024 : Some facade refactoring

SVN: 35887
---
 .../eln-lims/html/js/server/ServerFacade.js   | 218 ++++++++++--------
 1 file changed, 117 insertions(+), 101 deletions(-)

diff --git a/plasmid/source/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/server/ServerFacade.js b/plasmid/source/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/server/ServerFacade.js
index 77d59c1c99e..83311261ebb 100644
--- a/plasmid/source/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/server/ServerFacade.js
+++ b/plasmid/source/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/server/ServerFacade.js
@@ -599,7 +599,118 @@ function ServerFacade(openbisServer) {
 	//
 	// New Advanced Search
 	//
+	this.searchForEntitiesAdvanced = function(advancedSearchCriteria, searchCriteria, fetchOptions, v3Api, searchFunction, callback) {
+		//Setting the searchCriteria given the advancedSearchCriteria model
+		var searchCriteria = searchCriteria;
+		
+		//Setting the fetchOptions given standard settings
+		var fetchOptions = fetchOptions;
+		fetchOptions.withTags();
+		fetchOptions.withType();
+		fetchOptions.withSpace();
+		fetchOptions.withExperiment();
+		fetchOptions.withRegistrator();
+		fetchOptions.withModifier();
+		fetchOptions.withParents();
+		fetchOptions.withProperties();
+		
+		//Operator
+		var operator = advancedSearchCriteria.logicalOperator;
+		if (!operator) {
+			operator = "AND";
+		}
+		searchCriteria.withOperator(operator);
+		
+		//Rules
+		var ruleKeys = Object.keys(advancedSearchCriteria.rules);
+		for (var idx = 0; idx < ruleKeys.length; idx++)
+		{
+			var fieldType = advancedSearchCriteria.rules[ruleKeys[idx]].type;
+			var fieldName = advancedSearchCriteria.rules[ruleKeys[idx]].name;
+			var fieldNameType = null;
+			var fieldValue = advancedSearchCriteria.rules[ruleKeys[idx]].value;
+			
+			if(fieldName) {
+				var firstDotIndex = fieldName.indexOf(".");
+				fieldNameType = fieldName.substring(0, firstDotIndex);
+				fieldName = fieldName.substring(firstDotIndex + 1, fieldName.length);
+			}
+			
+			var setPropertyCriteria = function(criteria, propertyName, propertyValue) {
+				criteria.withProperty(propertyName).thatEquals(propertyValue);
+			}
+			
+			var setAttributeCriteria = function(criteria, attributeName, attributeValue) {
+				switch(attributeName) {
+					case "CODE":
+						criteria.withCode().thatEquals(attributeValue);
+						break;	
+					case "SAMPLE_TYPE":
+						criteria.withType().withCode().thatEquals(attributeValue);
+						break;
+					case "PERM_ID":
+						criteria.withPermId().thatEquals(attributeValue);
+						break;
+					case "SPACE":
+						criteria.withSpace().withCode().thatEquals(attributeValue);
+						break;
+					case "METAPROJECT":
+						criteria.withTag().withCode().thatEquals(attributeValue);
+						break;
+					case "REGISTRATION_DATE": //must be format 2009-08-18 ?
+						criteria.withRegistrationDate().thatEquals(new DateObjectEqualToValue(attributeValue));
+						break;
+					case "MODIFICATION_DATE": //must be format 2009-08-18 ?
+						criteria.withModificationDate().thatEquals(new DateObjectEqualToValue(attributeValue));
+						break;
+				}
+			}
+			
+			switch(fieldType) {
+				case "All":
+					searchCriteria.withAnyField().thatEquals(fieldValue);
+					break;
+				case "Property":
+					setPropertyCriteria(searchCriteria, fieldName, fieldValue);
+					break;
+				case "Attribute":
+					setAttributeCriteria(searchCriteria, fieldName, fieldValue);
+					break;
+				case "Parent":
+					switch(fieldNameType) {
+						case "PROP":
+							setPropertyCriteria(searchCriteria.withParents(), fieldName, fieldValue);
+							break;
+						case "ATTR":
+							setAttributeCriteria(searchCriteria.withParents(), fieldName, fieldValue);
+							break;
+					}
+					break;
+				case "Children":
+					switch(fieldNameType) {
+						case "PROP":
+							setPropertyCriteria(searchCriteria.withChildren(), fieldName, fieldValue);
+							break;
+						case "ATTR":
+							setAttributeCriteria(searchCriteria.withChildren(), fieldName, fieldValue);
+							break;
+					}
+					break;
+			}
+		}
+		
+		v3Api[searchFunction](searchCriteria, fetchOptions)
+		.done(function(result) {
+			callback(result);  //this will call the method defined in the AdvancedSearchController which will display the table
+		})
+		.fail(function(result) {
+			Util.showError("Call failed to server: " + JSON.stringify(result));
+			Util.unblockUI();
+		});
+	}
+	
 	this.searchForSamplesAdvanced = function(advancedSearchCriteria, callback) {
+		var _this = this;
 		require(['openbis', 
 		         'as/dto/sample/search/SampleSearchCriteria',
 		         'as/dto/sample/fetchoptions/SampleFetchOptions',
@@ -621,108 +732,13 @@ function ServerFacade(openbisServer) {
 			
 			//Setting the fetchOptions given standard settings
 			var fetchOptions = new SampleFetchOptions();
-			fetchOptions.withTags();
-			fetchOptions.withType();
-			fetchOptions.withSpace();
-			fetchOptions.withExperiment();
-			fetchOptions.withRegistrator();
-			fetchOptions.withModifier();
-			fetchOptions.withParents();
-			fetchOptions.withProperties();
-			
-			//Operator
-			var operator = advancedSearchCriteria.logicalOperator;
-			if (!operator) {
-				operator = "AND";
-			}
-			searchCriteria.withOperator(operator);
-			
-			//Rules
-			var ruleKeys = Object.keys(advancedSearchCriteria.rules);
-			for (var idx = 0; idx < ruleKeys.length; idx++)
-			{
-				var fieldType = advancedSearchCriteria.rules[ruleKeys[idx]].type;
-				var fieldName = advancedSearchCriteria.rules[ruleKeys[idx]].name;
-				var fieldNameType = null;
-				var fieldValue = advancedSearchCriteria.rules[ruleKeys[idx]].value;
-				
-				if(fieldName) {
-					var firstDotIndex = fieldName.indexOf(".");
-					fieldNameType = fieldName.substring(0, firstDotIndex);
-					fieldName = fieldName.substring(firstDotIndex + 1, fieldName.length);
-				}
-				
-				var setPropertyCriteria = function(criteria, propertyName, propertyValue) {
-					criteria.withProperty(propertyName).thatEquals(propertyValue);
-				}
-				
-				var setAttributeCriteria = function(criteria, attributeName, attributeValue) {
-					switch(attributeName) {
-						case "CODE":
-							criteria.withCode().thatEquals(attributeValue);
-							break;	
-						case "SAMPLE_TYPE":
-							criteria.withType().withCode().thatEquals(attributeValue);
-							break;
-						case "PERM_ID":
-							criteria.withPermId().thatEquals(attributeValue);
-							break;
-						case "SPACE":
-							criteria.withSpace().withCode().thatEquals(attributeValue);
-							break;
-						case "METAPROJECT":
-							criteria.withTag().withCode().thatEquals(attributeValue);
-							break;
-						case "REGISTRATION_DATE": //must be format 2009-08-18 ?
-							criteria.withRegistrationDate().thatEquals(new DateObjectEqualToValue(attributeValue));
-							break;
-						case "MODIFICATION_DATE": //must be format 2009-08-18 ?
-							criteria.withModificationDate().thatEquals(new DateObjectEqualToValue(attributeValue));
-							break;
-					}
-				}
-				
-				switch(fieldType) {
-					case "All":
-						searchCriteria.withAnyField().thatEquals(fieldValue);
-						break;
-					case "Property":
-						setPropertyCriteria(searchCriteria, fieldName, fieldValue);
-						break;
-					case "Attribute":
-						setAttributeCriteria(searchCriteria, fieldName, fieldValue);
-						break;
-					case "Parent":
-						switch(fieldNameType) {
-							case "PROP":
-								setPropertyCriteria(searchCriteria.withParents(), fieldName, fieldValue);
-								break;
-							case "ATTR":
-								setAttributeCriteria(searchCriteria.withParents(), fieldName, fieldValue);
-								break;
-						}
-						break;
-					case "Children":
-						switch(fieldNameType) {
-							case "PROP":
-								setPropertyCriteria(searchCriteria.withChildren(), fieldName, fieldValue);
-								break;
-							case "ATTR":
-								setAttributeCriteria(searchCriteria.withChildren(), fieldName, fieldValue);
-								break;
-						}
-						break;
-				}
-			}
 			
-			v3Api.searchSamples(searchCriteria, fetchOptions)
-			.done(function(result) {
-				callback(result);  //this will call the method defined in the AdvancedSearchController which will display the table
-			})
-			.fail(function(result) {
-				Util.showError("Call failed to server: " + JSON.stringify(result));
-				Util.unblockUI();
-			});
+			_this.searchForEntitiesAdvanced(advancedSearchCriteria,
+											searchCriteria, 
+											fetchOptions, 
+											v3Api,
+											"searchSamples",
+											callback);
 		});
 	}
 	
-- 
GitLab