LinkData API

LinkData Global  2.0.0

LinkData API > linkdata > script2.js (source view)
Search:
 
Filters
/**
 * The LinkData object is the single global object used by LinkData Library.
 * It contains utility function for read data from linkdata work.
 * @module linkdata
 * @title LinkData Global
 */
if(typeof LinkData === "undefined" || !LinkData) {
	/**
	 * The LinkData global namespace object.
	 * If LinkData is already defined, the existing LinkData object will
	 * not be overwritten so that defined namespaces are preserved.
	 * @class LinkData
	 * @static
	 */
	var LinkData = {};
}

/**
 * keeps the no of active ajax connections to the server
 */
LinkData.activeAjaxConnections = 0;

/**
 * Returns work ids with databases related to the application.
 * @method getWorks
 * @param {Function} callback the callback function ref
 * @return {Array} A array of work ids with related work files.
 * <dl>
 * 	<dd>Example: <code>[&#123;&lt;workId&gt;,[&lt;fileName&gt;,&lt;fileName&gt;...]&#125;,&#123;.....&#125;...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getWorks = function(callback) {
	return LinkData.app.api.getWorks(callback);
}

/**
 * Returns name by given rdf work id.
 * @method getWorkName
 * @param {String} workId Id of rdf work. (optional)
 * @param {Function} callback the callback function ref
 * @return {Array} A array of work name values. 
 * <dl>
 * 	<dd>Example:</dd>
 * 	<dd>If work id exist, <code>[&lt;workName&gt;]</code></dd>
 * 	<dd>If not work id exist, <code>[&#123;&lt;workId&gt;,[&lt;workName&gt;]&#125;,&#123;.....&#125;...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getWorkName = function(workId, callback) {
	return LinkData.app.api.getWorkName(workId, callback);
}

/**
 * Returns databases related to the application by given work id.
 * If work id is undefined or null, returns all databases related to the application grouped by work id.
 * @method getFiles
 * @param {String} workId Id of rdf work. (optional)
 * @param {Function} callback the callback function ref
 * @return {Array} A array of work files values. 
 * <dl>
 * 	<dd>Example:</dd>
 * 	<dd>If work id exist, <code>[&lt;fileName&gt;,&lt;fileName&gt;...]</code></dd>
 * 	<dd>If not work id exist, <code>[&#123;&lt;workId&gt;,[&lt;fileName&gt;,&ltfileName&gt;...]&#125;,&#123;.....&#125;...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getFiles = function(workId, callback) {
	return LinkData.app.api.getFiles(workId, callback);
}

/**
 * Returns databases related to the application by given work id and tag name.
 * If work id is undefined or null, returns all databases related to tag name grouped by work id.
 * @method getFilesByTag
 * @param {String} workId Id of rdf work. (optional)
 * @param {String} tagName name of the tag.
 * @param {Function} callback the callback function ref
 * @return {Array} A array of work files values. 
 * <dl>
 * 	<dd>Example:</dd>
 * 	<dd>If work id exist, <code>[&lt;fileName&gt;,&lt;fileName&gt;...]</code></dd>
 * 	<dd>If not work id exist, <code>[&#123;&lt;workId&gt;,[&lt;fileName&gt;,&ltfileName&gt;...]&#125;,&#123;.....&#125;...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getFilesByTag = function(workId, tagName, callback) {
	return LinkData.app.api.getFilesByTag(workId, tagName, callback);
}

/**
 * Returns contents of a rdf work file.
 * @method getFileContent
 * @param {String} workId Id of rdf work
 * @param {String} filename Name of rdf work file
 * @param {String} format Type of rdf work file. (JSON, TSV, TTL or TRUTLE, XML)
 * @param {String} callbackFn Name of the callback function
 * @return {String} Contents of rdf work file.
 * @static
 */
LinkData.getFileContent = function(workId, fileName, format, callbackFn) {
	return LinkData.app.api.getFileContent(workId, fileName, format, callbackFn);
}

/**
 * Returns subject list of the database.
 * @method getSubjects
 * @param {String} workId Id of rdf work
 * @param {String} filename Name of rdf work file
 * @param {Function} callback the callback function ref
 * @param {Object} index start(default 1) and end(default -1) position values. (Ex: &#123;start:1, end:10&#125;)
 * @return {Array} A array of subject url values.
 * <dl>
 * 	<dd>Example: <code>[&lt;subject&gt;,&lt;subject&gt;,...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getSubjects = function(workId, filename, callback, index) {
	return LinkData.app.api.getSubjects(workId, filename, callback, index);
}

/**
 * Returns property list of the database.
 * @method getProperties
 * @param {String} workId Id of rdf work
 * @param {String} filename Name of rdf work file
 * @param {Function} callback the callback function ref
 * @param {Object} index start(default 1) and end(default -1) position values. (Ex: &#123;start:1, end:10&#125;)
 * @return {Array} The array of property url values.
 * <dl>
 * 	<dd>Example: <code>[&lt;property&gt;,&lt;property&gt;,...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getProperties = function(workId, filename, callback, index) {
	return LinkData.app.api.getProperties(workId, filename, callback, index);
}

/**
 * Returns object list of the database by subject and property.
 * @method getObjects
 * @param {String} workId Id of rdf work
 * @param {String} filename Name of rdf work file
 * @param {String} subject Url of subject
 * @param {String} property Url of property
 * @param {Function} callback the callback function ref
 * @return {Array} The array of object values. 
 * <dl>
 * 	<dd>Example: <code>[&lt;obejctValue&gt;,&lt;obejctValue&gt;,...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getObjects = function(workId, filename, subject, property, callback) {
	return LinkData.app.api.getObjects(workId, filename, subject, property, callback);
}

/**
 * Returns all triple list of the database.
 * @method getTriples
 * @param {String} workId Id of rdf work
 * @param {String} filename Name of rdf work file
 * @param {Function} callback the callback function ref
 * @param {Object} index start(default 1) and end(default -1) position values. (Ex: &#123;start:1, end:10&#125;)
 * @return {Array} A array of all triples.
 * <dl>
 * 	<dd>Example: <code>[&#123;"subject":&lt;subject&gt;,"property":&lt;property&gt;, "object":&lt;object&gt;&#125;,...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getTriples = function(workId, filename, callback, index) {
	return LinkData.app.api.getTriples(workId, filename, callback, index);
}

/**
 * Returns triple list of the database by subject. If subject is undefined or null, then returns all triple list of the database.
 * @method getTriplesBySubject
 * @param {String} workId Id of rdf work
 * @param {String} filename Name of rdf work file
 * @param {String} subject Url of subject. (optional)
 * @param {Function} callback the callback function ref
 * @param {Object} index start(default 1) and end(default -1) position values. (Ex: &#123;start:1, end:10&#125;)
 * @return {Array} A array of triples with subject.
 * <dl>
 * 	<dd>Example: <code>[&#123;"subject":&lt;subject&gt;,"property":&lt;property&gt;, "object":&lt;object&gt;&#125;,...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getTriplesBySubject = function(workId, filename, subject, callback, index) {
	return LinkData.app.api.getTriplesBySubject(workId, filename, subject, callback, index);
}

/**
 * Returns triple list of the database by property. If property is undefined or null, then returns all triple list of the database.
 * @method getTriplesByProperty
 * @param {String} workId Id of rdf work.
 * @param {String} filename Name of rdf work file.
 * @param {String} property Url of property. (optional)
 * @param {Function} callback the callback function ref
 * @param {Object} index start(default 1) and end(default -1) position values. (Ex: &#123;start:1, end:10&#125;)
 * @return {Array} A array of triples by property.
 * <dl>
 * 	<dd>Example: <code>[&#123;"subject":&lt;subject&gt;,"property":&lt;property&gt;, "object":&lt;object&gt;&#125;,...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getTriplesByProperty = function(workId, filename, property, callback, index) {
	return LinkData.app.api.getTriplesByProperty(workId, filename, property, callback, index);
}

/**
 * Returns the no of subjects in the passed work id's file name. If either the workId or filename not defined, this will return 0.
 * @param {String} workId Id of rdf work.
 * @param {String} filename Name of rdf work file. 
 * @return {Int} no of subjects in the file.
 * <dl>
 * 	<dd>Example: <code>200</code></dd>
 * </dl>
 * @static
 */
LinkData.getSubjectsCount = function(workId, filename, callback) {
	return LinkData.app.api.getSubjectsCount(workId, filename, callback);
}

/**
 * Returns the subject name from subject url which passed by browser URL.
 * <dl>
 * 	<dd>Example:</dd>
 *  <dd>if Subject URI: <code>http://linkdata.org/resource/rdf1s1243i#ABC</code></dd>
 *  <dd>then Browser URL: <code>http://...?id=ABC&subject=http://linkdata.org/resource/rdf1s1243i#ABC  </code></dd>
 *  <dd>returns Subject Name: <code>ABC</code></dd>
 * </dl>
 * @method getSubjectName
 * @return {String} Subject Name
 * @static
 */
LinkData.getSubjectName = function() {
	return LinkDataUtil.app.api.getParameterByName("id");
}

/**
 * Returns the subject uri which passed by browser URL.
 * <dl>
 * 	<dd>Example:</dd>
 *  <dd>if Subject URI: <code>http://linkdata.org/resource/rdf1s1243i#ABC</code></dd>
 *  <dd>then Browser URL: <code>http://...?id=ABC&subject=http://linkdata.org/resource/rdf1s1243i#ABC  </code></dd>
 *  <dd>returns Subject URI: <code>http://linkdata.org/resource/rdf1s1243i#ABC</code></dd>
 * </dl>
 * @method getSubjectUri
 * @return {String} Subject URI
 * @static
 */
LinkData.getSubjectUri = function() {
	return LinkDataUtil.app.api.getParameterByName("subject");
}

/**
 * 
 * This class contains LinkData application internal API functions
 * Note: {{},{}...} case for (var obj in data) and [{},{}...] for (var i = 0, i < data.length, i++) 
 * @private
 * 
 */

if(typeof LinkData.app === "undefined" || !LinkData.app) {
	LinkData.app = {};
} 

LinkData.app.api = {
	
	getWorks : function(callback) {
		var result = [];
		var files = this.getFiles();
		for (var key in files) {
			result.push(key);
		}
		
		if (callback && typeof callback === 'function') {
			callback(result);
		}
	},

	getWorkName : function(workId, callback) {
		var result = [];
		
		var files = this.getFiles();
		for (var key in files) {
			if ( !result[key] ) {
				result[key] = [];
			}
			var rootEl = document.getElementById("applicationWorkFileListContainer");
			var workFileElList = LinkData.getElementsByClassName("applicationWorkFile", "div", rootEl);
			for (var i = 0; i < workFileElList.length; i++) {
				var workFileEl = workFileElList[i];
				var id = LinkData.getElementsByClassName("applicationWorkFileWorkId", "input", workFileEl)[0].value;
				var name = LinkData.getElementsByClassName("applicationWorkFileWorkName", "input", workFileEl)[0].value;
				
				if ( !result[id] || result[id].length == 0) {
					result[id] = [];
					result[id].push(name);
				}
				
			}
		}
		
		if (callback && typeof callback === 'function') {
			if (typeof workId == "undefined" || !workId || workId.trim().length == 0) {
				callback(result);
			} else {
				callback(result[workId]);
			}
		} else {
			return result;
		}
	},
	
	getFiles : function(workId, callback) {
		var result = new Object();
		
		var rootEl = document.getElementById("applicationWorkFileListContainer");
		var workFileElList = LinkData.getElementsByClassName("applicationWorkFile", "div", rootEl);
		for (var i = 0; i < workFileElList.length; i++) {
			var workFileEl = workFileElList[i];
			var id = LinkData.getElementsByClassName("applicationWorkFileWorkId", "input", workFileEl)[0].value;
			var filename = LinkData.getElementsByClassName("applicationWorkFileName", "input", workFileEl)[0].value;
			
			if ( !result[id] ) {
				result[id] = [];
			}
			
			result[id].push(filename);
		}
	
		if (callback && typeof callback === 'function') {
			if (typeof workId == "undefined" || !workId || workId.trim().length == 0) {
				callback(result);
			} else {
				callback(result[workId]);
			}
		} else {
			return result;
		}
	},
	
	getFilesByTag : function(workId, tag, callback) {
		var result = new Object();
		var rootEl = document.getElementById("applicationWorkFileListContainer");
		var workFileElList = LinkData.getElementsByClassName("applicationWorkFile", "div", rootEl);
		for (var i = 0; i < workFileElList.length; i++) {
			var workFileEl = workFileElList[i];
			var id = LinkData.getElementsByClassName("applicationWorkFileWorkId", "input", workFileEl)[0].value;
			var filename = LinkData.getElementsByClassName("applicationWorkFileName", "input", workFileEl)[0].value;
			var tagName = LinkData.getElementsByClassName("applicationWorkTag", "input", workFileEl)[0].value;
			var tmpTag = tagName.trim();
			var array = tmpTag.split(","); // for comma separated tag
			for (var j = 0; j < array.length; j++) {
				var t = array[j];
				if (t.trim() == tag.trim()) {
					if ( !result[id] ) {
						result[id] = [];
					}
					result[id].push(filename);					
				}
			}
		}
		if (callback && typeof callback === 'function') {
			if (typeof workId == "undefined" || !workId || workId.trim().length == 0) {
				callback(result);
			} else {
				callback(result[workId]);
			}
		}
	},
	
	getFileContent : function(workId, fileName, format, callbackFn) {
		var ext = null;
		var postfix = null;
		
		if (format) {
			var _FORMAT = format.toUpperCase();
			if (_FORMAT === "TSV") {
				ext = "txt";
				postfix = "tsv";
			} else if (_FORMAT === "TTL" || _FORMAT === "TRUTLE") {
				ext = "txt";
				postfix = "ttl";
			} else if (_FORMAT === "JSON") {
				ext = "json";
				postfix = "rdf";
			} else if (_FORMAT === "XML") {
				ext = "xml";
				postfix = "rdf";
			}
		}
		
		if (ext && postfix) {
			var script = document.createElement('script');
			var url = LinkData.apiUrl + workId + "/" + fileName + "_" + postfix + "." + ext + "?callback=" + callbackFn;
			script.setAttribute('src', url);
			document.getElementsByTagName('head')[0].appendChild(script);
		}
	},
	
	getSubjects : function(workId, filename, callback, index) {
		
		var oCallback = function(data) {
			var result = [];
			if (data.status == "success") {
				var arr = data.json;
				for (var i = 0; i < arr.length; i++) {
					result.push(arr[i].subject);
				}
			}
			
			if (callback && typeof callback === 'function') {
				callback(result);
			}
		}
		
		this._ajaxJsApi("getSubjects", {"workId" : workId, "filename" : filename}, oCallback, index);
	},
	
	getProperties : function(workId, filename, callback, index) {
			
		var oCallback = function(data) {
			var result = [];
			if (data.status == "success") {
				var arr = data.json;
				for (var i = 0; i < arr.length; i++) {
					result.push(arr[i].property);
				}
			}
			
			if (callback && typeof callback === 'function') {
				callback(result);
			}
		}
		
		this._ajaxJsApi("getProperties", {"workId" : workId, "filename" : filename}, oCallback, index);
	},
	
	getObjects : function(workId, filename, subject, property, callback) {
		
		var oCallback = function(data) {
			var result = [];
			if (data.status == "success") {
				var arr = data.json;
				for (var i = 0; i < arr.length; i++) {
					result.push(arr[i].object);
				}
			}
			
			if (callback && typeof callback === 'function') {
				callback(result);
			}
		}
		
		this._ajaxJsApi("getObjects", {"workId" : workId, "filename" : filename, "subject" : subject, "property" : property}, oCallback, undefined);
	},
	
	getTriples : function(workId, filename, callback, index) {
		return this._getTriples(workId, filename, null, null, callback, index);
	},
	
	getTriplesBySubject : function(workId, filename, subject, callback, index) {
		return this._getTriples(workId, filename, subject, null, callback, index);
	},
	
	getTriplesByProperty : function(workId, filename, property, callback, index) {
		return this._getTriples(workId, filename, null, property, callback, index);
	},
	
	_getTriples : function(workId, filename, subject, property, callback, index) {

		var oCallback = function(data) {
			var result = [];
			if (data.status == "success") {
				var arr = data.json;
				for (var i = 0; i < arr.length; i++) {
					var obj = arr[i].triple;
					var triple = new Object();
					triple["subject"] = obj.subject;
					triple["property"] = obj.property;
					triple["object"] = obj.object;
					result.push(triple);
				}
			}
			
			if (callback && typeof callback === 'function') {
				callback(result);
			}
		}
		
		this._ajaxJsApi("getTriples", {"workId" : workId, "filename" : filename, "subject" : subject, "property" : property}, oCallback, index);
	},
	
	getSubjectsCount : function(workId, filename, callback) {
		
		var oCallback = function(data) {
			var count = 0;
			if (data.status == "success") {
				var obj = data.json;
				count = obj.count;
			}
			if (callback && typeof callback === 'function') {
				callback(count);
			}
		}
		this._ajaxJsApi("getSubjectsCount", {"workId" : workId, "filename" : filename}, oCallback, undefined);
	},
	
	_arrContains : function(arr, data) {
		for(var i = 0; i < arr.length; i++) {
            if(arr[i] === data) {
                return i;
            }
        }
        return -1;
	},
	
	_exist : function(data) {
		return (data && data.length > 0);
	},
	
	_valid : function(value, param) {
		if (param) {
			return (param == value);
		}
		return true;
	},
	
	_getValidIndex : function(index) {
		if (!index) {
			var index = {};
		}
		if (!index.start) {
			index.start = 1;
		}
		if (!index.end) {
			index.end = -1;
		}
		return index;
	},
	
	_getJson : function(workId, filename) {
		return LinkData.userdata[workId][filename];
	},
	
	_ajaxJsApi : function(url, params, callback, index) {
		index = this._getValidIndex(index);
		if (typeof url !== "string" && url.indexOf( " " ) >= 0) {
			return;
		} else {
			url = LinkData.systemUrl + "jsapi/" + url + ".action";
		}
		if (!params) {
			var params = {};
		}
		params.start = index.start;
		params.end = index.end;
		return this._ajax(url, params, callback);
	},
	
	_ajax : function(url, params, callback) {
		if (typeof url !== "string" && url.indexOf( " " ) >= 0) {
			return;
		}
		
		// Default to a GET request
		var type = "GET";

		// If the second parameter was provided
		if (params) {
			// If it's a function
			if (typeof params === 'function') {
				// We assume that it's the callback
				callback = params;
				params = undefined;

			// Otherwise, build a param string
			} else if (typeof params === "object") {
				params = this._serialize(params);
				type = "POST";
			}
		}
		
		var xmlhttp = false;
		
		if (window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			try {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){}
			}
		} else {
			return false;
		}
		
		if (callback) {
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					LinkData.activeAjaxConnections--;
					callback(JSON.parse(xmlhttp.responseText));
				}
			}
		}
		
		xmlhttp.open(type, url);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		xmlhttp.send(params);
		LinkData.activeAjaxConnections++;
		
		return this;
	},
	
	_serialize : function(obj) {
		var str = [];
		for(var p in obj) {
			if (obj[p]) {
				if (typeof obj[p] == "object") {
					for (var i = 0; i < obj[p].length; i++) {
						str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p][i]));
					}
				} else {
					str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
				}
			}
		}
		return str.join("&");
	}
};

/**
 * 
 * @private
 * linkdata traversing method by class name
 * 
 */
LinkData.getElementsByClassName = function(classname, tagname, root, fn) {
	if (!root) { 
		root = document.getElementsByTagName('body')[0]; 
	}
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b'); 
	var els = root.getElementsByTagName(tagname);
	if (fn) {
		for (var i = 0, j = els.length; i < j; i++) { 
			if ( re.test(els[i].className) ) { 
				fn(els[i]);
			} 
		}
	} else {
		for (var i = 0, j = els.length; i < j; i++) { 
			if ( re.test(els[i].className) ) { 
				a.push(els[i]); 
			} 
		}
	}
	return a;
};

Copyright © 2014 app.linkdata.org, All rights reserved.