Skip to content
Snippets Groups Projects
Commit 0c05d45e authored by jakubs's avatar jakubs
Browse files

SSDM-1439 SSDM-1383 Make jsts utils into a module. Add resolution from json to...

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
parent 5485107a
No related branches found
No related tags found
No related merge requests found
/**
* 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment