From 9b683fae5dfc4b35cfa81de82227d2b9f675500a Mon Sep 17 00:00:00 2001
From: parkera <parkera>
Date: Wed, 22 Jul 2015 07:45:09 +0000
Subject: [PATCH] SSDM-2161 Change the way the sample codes are automatically
 generated

SVN: 34360
---
 .../eln-lims/html/js/server/ServerFacade.js   | 30 ++++++++++++++++---
 1 file changed, 26 insertions(+), 4 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 a8ad37b6fbd..4ec55bff574 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
@@ -258,16 +258,38 @@ function ServerFacade(openbisServer) {
 	
 
 	//
-	// Others
-	//
-	this.generateCode = function(sampleType, action) {
+	//OLD METHOD
+	/*this.generateCode = function(sampleType, action) {
 		this.openbisServer.countNumberOfSamplesForType(sampleType.code, function(response) {
 			if(response.result || response.result === 0) {
 				action(sampleType.codePrefix + (parseInt(response.result) + 1));
 			}
 		});
-	}
+	}*/
 	
+	this.generateCode = function(sampleType, action) {
+		var prefix = sampleType.codePrefix;
+		this.searchWithType(
+				sampleType.code,
+				"",
+				function(results) {
+					if(results.length == 0)
+						nextcode = prefix + "1";
+					else{
+						var codes = [];
+						for(var idx=0; idx<results.length; idx++){
+							numeric_code = results[idx].code.substring(3); //assuming all codes are 3 letters long!!!!
+							numeric_code = numeric_code.replace("_","");
+							codes[idx] = parseInt(numeric_code); 
+						}
+						codes.sort();
+						var nextid = codes[codes.length-1] + 1;
+						var nextcode = prefix + nextid;
+					}
+					action(nextcode);
+				});
+	}
+		
 	this.deleteDataSets = function(datasetIds, reason, callback) {
 		this.openbisServer.deleteDataSets(datasetIds, reason, "TRASH", callback);
 	}
-- 
GitLab