From 0c05d45ebcb6c6d8c894dddba21b94db2661cf46 Mon Sep 17 00:00:00 2001
From: jakubs <jakubs>
Date: Fri, 23 Jan 2015 14:29:07 +0000
Subject: [PATCH] SSDM-1439 SSDM-1383 Make jsts utils into a module. Add
 resolution from json to entity arrays (children) and reused objects (with id
 placeholders)

SVN: 33307
---
 .../openbis-v3-api-test/html/support/Utils.js | 129 +++++++++++-------
 1 file changed, 80 insertions(+), 49 deletions(-)

diff --git a/js-test/servers/common/core-plugins/tests/1/as/webapps/openbis-v3-api-test/html/support/Utils.js b/js-test/servers/common/core-plugins/tests/1/as/webapps/openbis-v3-api-test/html/support/Utils.js
index 36fdfb8671d..9288d99960f 100644
--- a/js-test/servers/common/core-plugins/tests/1/as/webapps/openbis-v3-api-test/html/support/Utils.js
+++ b/js-test/servers/common/core-plugins/tests/1/as/webapps/openbis-v3-api-test/html/support/Utils.js
@@ -1,65 +1,96 @@
 /**
  * Library
  */
-var reg = require("support/type_registry");
-var STJSUtil = function() {}
+define(["support/type_registry", "support/underscore-min"], function(reg, _) {
+	var STJSUtil = function() {}
 
-STJSUtil.prototype.executeFunctionByName = function(functionName, context /*, args */) {
-	  var args = [].slice.call(arguments).splice(2);
-	  var namespaces = functionName.split(".");
-	  var func = namespaces.pop();
-	  for(var i = 0; i < namespaces.length; i++) {
-	    context = context[namespaces[i]];
-	  }
-	  return context[func].apply(this, args);
-}
+	STJSUtil.prototype.executeFunctionByName = function(functionName, context /*, args */) {
+		  var args = [].slice.call(arguments).splice(2);
+		  var namespaces = functionName.split(".");
+		  var func = namespaces.pop();
+		  for(var i = 0; i < namespaces.length; i++) {
+		    context = context[namespaces[i]];
+		  }
+		  return context[func].apply(this, args);
+	}
+
+	STJSUtil.prototype.fromJson = function(jsonObject) {
+		return fromJsonLocal(jsonObject, {})
+	}
 
-STJSUtil.prototype.fromJson = function(jsonObject) {
-	//console.log(jsonObject["@type"]);
-	
-	console.log("Creating " + jsonObject["@type"]);
-	var prototype = reg.get(jsonObject["@type"]);
-	var object = new prototype;
-	
-	for(var key in jsonObject) {
-		//if (!prototype.hasOwnProperty(key))
-		{
-			var property = jsonObject[key];
-			if(property instanceof Object && property["@type"] !== undefined) {
-				property = this.fromJson(property);
+	var objectPropertyFromJson = function (key, property, hashedObjects) {
+		if (_.isNumber(property) && key.indexOf("Date") == -1 && key != "@id") {
+					// I don't know what is the better way to distinguish between id numbers, and real numbers
+				    // As here we only analyse the fields and not property values the check if a field is not a date
+				    // should be enough
+         	if (property in hashedObjects) {
+				return hashedObjects[property]
+			} else {
+				throw "Expected that integer fields are id's of objects, and they should be present in cache"
+			}
+		} else if (property instanceof Object && property["@type"] !== undefined) {
+			return fromJsonLocal(property, hashedObjects);
+		} else if (property instanceof Array) {
+			for (idx in property) {
+				property[idx] = objectPropertyFromJson(key, property[idx], hashedObjects)
 			}
-			object[key] = property;
+			return property
+		}
+		else {
+			return property
 		}
 	}
-	return object;
-};
+    
+	var fromJsonLocal = function (jsonObject, hashedObjects) {
+		var jsonType = jsonObject["@type"]
+		var jsonId = jsonObject["@id"]
+		
+		var prototype = reg.get(jsonType);
+		var object = new prototype;
+
+        
+		if (jsonId) {
+			if (jsonId in hashedObjects) {
+				throw "This object has already been cached!"
+			}
+			hashedObjects[jsonId] = object
+		}
+
+		for(var key in jsonObject) {
+			object[key] = objectPropertyFromJson(key, jsonObject[key], hashedObjects)
+		}
+		return object;
+	};
 
-//var stjs = new STJS();
-var stjsUtil = new STJSUtil();
+	//var stjs = new STJS();
+	var stjsUtil = new STJSUtil();
 
-var Serializable = function() {};
+	var Serializable = function() {};
 
-function RuntimeException(message) {
-    this.name = "RuntimeException";
-    this.message = (message || "");
-}
-RuntimeException.prototype = Error.prototype;
+	function RuntimeException(message) {
+	    this.name = "RuntimeException";
+	    this.message = (message || "");
+	}
+	RuntimeException.prototype = Error.prototype;
 
-function IllegalArgumentException(message) {
-    this.name = "IllegalArgumentException";
-    this.message = (message || "");
-}
-IllegalArgumentException.prototype = Error.prototype;
+	function IllegalArgumentException(message) {
+	    this.name = "IllegalArgumentException";
+	    this.message = (message || "");
+	}
+	IllegalArgumentException.prototype = Error.prototype;
+
+	function NotFetchedException(message) {
+	    this.name = "NotFetchedException";
+	    this.message = (message || "");
+	}
+	NotFetchedException.prototype = Error.prototype;
 
-function NotFetchedException(message) {
-    this.name = "NotFetchedException";
-    this.message = (message || "");
-}
-NotFetchedException.prototype = Error.prototype;
 
 
+	var HashMap = function() {}
+	HashMap.prototype.put = function(key, val) {
+		this[key] = val;
+	};
 
-var HashMap = function() {}
-HashMap.prototype.put = function(key, val) {
-	this[key] = val;
-};
\ No newline at end of file
+	return stjsUtil;
+});
\ No newline at end of file
-- 
GitLab