Subversion Repositories Applications.papyrus

Rev

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