Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.data.RdfStore");
14
dojo.provide("dojo.data.RhizomeStore");
15
dojo.require("dojo.lang.declare");
16
dojo.require("dojo.data.core.RemoteStore");
17
dojo.require("dojo.experimental");
18
dojo.data.RdfDatatypeSerializer = function (type, convertFunc, uri) {
19
	this.type = type;
20
	this._converter = convertFunc;
21
	this.uri = uri;
22
	this.serialize = function (value) {
23
		return this._converter.call(value, value);
24
	};
25
};
26
dojo.declare("dojo.data.RdfStore", dojo.data.core.RemoteStore, {_datatypeMap:{literal:function (value) {
27
	var literal = value.value;
28
	if (value["xml:lang"]) {
29
		literal.lang = value["xml:lang"];
30
	}
31
	return literal;
32
}, uri:function (value) {
33
	return {id:value.value};
34
}, bnode:function (value) {
35
	return {id:"_:" + value.value};
36
}, "http://www.w3.org/2001/XMLSchema#int":function (value) {
37
	return parseInt(value.value);
38
}, "http://www.w3.org/2001/XMLSchema#integer":function (value) {
39
	return parseInt(value.value);
40
}, "http://www.w3.org/2001/XMLSchema#long":function (value) {
41
	return parseInt(value.value);
42
}, "http://www.w3.org/2001/XMLSchema#float":function (value) {
43
	return parseFloat(value.value);
44
}, "http://www.w3.org/2001/XMLSchema#double":function (value) {
45
	return parseFloat(value.value);
46
}, "http://www.w3.org/2001/XMLSchema#boolean":function (value) {
47
	return !value || value == "false" || value == "0" ? false : true;
48
}}, _datatypeSerializers:[new dojo.data.RdfDatatypeSerializer(Number, Number.toString, "http://www.w3.org/2001/XMLSchema#float"), new dojo.data.RdfDatatypeSerializer(Boolean, Boolean.toString, "http://www.w3.org/2001/XMLSchema#boolean")], _findDatatypeSerializer:function (value) {
49
	var length = this._datatypeSerializers.length;
50
	for (var i = 0; i < length; i++) {
51
		var datatype = this._datatypeSerializers[i];
52
		if (value instanceof datatype.type) {
53
			return datatype;
54
		}
55
	}
56
}, _toRDFValue:function (value) {
57
	var rdfvalue = {};
58
	if (value.id) {
59
		if (value.id.slice(0, 2) == "_:") {
60
			rdfvalue.type = "bnode";
61
			rdfvalue.value = value.id.substring(2);
62
		} else {
63
			rdfvalue.type = "uri";
64
			rdfvalue.value = value.id;
65
		}
66
	} else {
67
		if (typeof value == "string" || value instanceof String) {
68
			rdfvalue.type = "literal";
69
			rdfvalue.value = value;
70
			if (value.lang) {
71
				rdfvalue["xml:lang"] = value.lang;
72
			}
73
		} else {
74
			if (typeof value == "number") {
75
				value = new Number(value);
76
			} else {
77
				if (typeof value == "boolean") {
78
					value = new Boolean(value);
79
				}
80
			}
81
			var datatype = this._findDatatypeSerializer(value);
82
			if (datatype) {
83
				rdfvalue = {"type":"typed-literal", "datatype":datatype.uri, "value":value.toString()};
84
			} else {
85
				rdfvalue = {"type":"literal", "value":value.toString()};
86
			}
87
		}
88
	}
89
	return rdfvalue;
90
}, _setupSaveRequest:function (saveKeywordArgs, requestKw) {
91
	var rdfResult = {"head":{"vars":["s", "p", "o"]}, "results":{"bindings":[]}};
92
	var resources = [];
93
	for (var key in this._deleted) {
94
		resources.push(key);
95
	}
96
	rdfResult.results.deleted = resources;
97
	for (key in this._changed) {
98
		var subject = this._toRDFValue(this.getIdentity(key));
99
		var attributes = this._changed[key];
100
		for (var attr in attributes) {
101
			var predicate = {type:"uri", value:attr};
102
			var values = attributes[attr];
103
			if (!values.length) {
104
				continue;
105
			}
106
			var rdfvalues = [];
107
			for (var i = 0; i < values.length; i++) {
108
				var rdfvalue = this._toRDFValue(values[i]);
109
				rdfResult.results.bindings.push({s:subject, p:predicate, o:rdfvalue});
110
			}
111
		}
112
	}
113
	var oldRegistry = dojo.json.jsonRegistry;
114
	dojo.json.jsonRegistry = this._jsonRegistry;
115
	var jsonString = dojo.json.serialize(rdfResult);
116
	dojo.json.jsonRegistry = oldRegistry;
117
	requestKw.postContent = jsonString;
118
}, _resultToQueryMetadata:function (json) {
119
	return json.head;
120
}, _resultToQueryData:function (json) {
121
	var items = {};
122
	var stmts = json.results.bindings;
123
	for (var i = 0; i < stmts.length; i++) {
124
		var stmt = stmts[i];
125
		var subject = stmt.s.value;
126
		if (stmt.s.type == "bnode") {
127
			subject = "_:" + subject;
128
		}
129
		var attributes = data[subject];
130
		if (!attributes) {
131
			attributes = {};
132
			data[stmt.s] = attributes;
133
		}
134
		var attr = attributes[stmt.p.value];
135
		if (!attr) {
136
			attributes[stmt.p.value] = [stmt.o];
137
		} else {
138
			attr.push(stmt.o);
139
		}
140
	}
141
	return items;
142
}});
143
dojo.declare("dojo.data.RhizomeStore", dojo.data.RdfStore, {initializer:function (kwArgs) {
144
	this._serverQueryUrl = kwArgs.baseUrl + "search?view=json&searchType=RxPath&search=";
145
	this._serverSaveUrl = kwArgs.baseUrl + "save-metadata";
146
}, _resultToQueryMetadata:function (json) {
147
	return json;
148
}, _resultToQueryData:function (json) {
149
	return json;
150
}, _setupSaveRequest:function (saveKeywordArgs, requestKw) {
151
	requestKw.url = this._serverSaveUrl;
152
	requestKw.method = "post";
153
	requestKw.mimetype = "text/plain";
154
	var resources = [];
155
	for (var key in this._deleted) {
156
		resources.push(key);
157
	}
158
	var changes = {};
159
	for (key in this._changed) {
160
		if (!this._added[key]) {
161
			resources.push(key);
162
		}
163
		var attributes = this._changed[key];
164
		var rdfattributes = {};
165
		for (var attr in attributes) {
166
			var values = attributes[attr];
167
			if (!values.length) {
168
				continue;
169
			}
170
			var rdfvalues = [];
171
			for (var i = 0; i < values.length; i++) {
172
				var rdfvalue = this._toRDFValue(values[i]);
173
				rdfvalues.push(rdfvalue);
174
			}
175
			rdfattributes[attr] = rdfvalues;
176
		}
177
		changes[key] = rdfattributes;
178
	}
179
	var oldRegistry = dojo.json.jsonRegistry;
180
	dojo.json.jsonRegistry = this._jsonRegistry;
181
	var jsonString = dojo.json.serialize(changes);
182
	dojo.json.jsonRegistry = oldRegistry;
183
	requestKw.content = {rdfFormat:"json", resource:resources, metadata:jsonString};
184
}});
185