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.old.provider.FlatFile");
12
dojo.require("dojo.data.old.provider.Base");
13
dojo.require("dojo.data.old.Item");
14
dojo.require("dojo.data.old.Attribute");
15
dojo.require("dojo.data.old.ResultSet");
16
dojo.require("dojo.data.old.format.Json");
17
dojo.require("dojo.data.old.format.Csv");
18
dojo.require("dojo.lang.assert");
19
dojo.data.old.provider.FlatFile = function (keywordParameters) {
20
	dojo.lang.assertType(keywordParameters, "pureobject", {optional:true});
21
	dojo.data.old.provider.Base.call(this);
22
	this._arrayOfItems = [];
23
	this._resultSet = null;
24
	this._dictionaryOfAttributes = {};
25
	if (keywordParameters) {
26
		var jsonObjects = keywordParameters["jsonObjects"];
27
		var jsonString = keywordParameters["jsonString"];
28
		var fileUrl = keywordParameters["url"];
29
		if (jsonObjects) {
30
			dojo.data.old.format.Json.loadDataProviderFromArrayOfJsonData(this, jsonObjects);
31
		}
32
		if (jsonString) {
33
			dojo.data.old.format.Json.loadDataProviderFromFileContents(this, jsonString);
34
		}
35
		if (fileUrl) {
36
			var arrayOfParts = fileUrl.split(".");
37
			var lastPart = arrayOfParts[(arrayOfParts.length - 1)];
38
			var formatParser = null;
39
			if (lastPart == "json") {
40
				formatParser = dojo.data.old.format.Json;
41
			}
42
			if (lastPart == "csv") {
43
				formatParser = dojo.data.old.format.Csv;
44
			}
45
			if (formatParser) {
46
				var fileContents = dojo.hostenv.getText(fileUrl);
47
				formatParser.loadDataProviderFromFileContents(this, fileContents);
48
			} else {
49
				dojo.lang.assert(false, "new dojo.data.old.provider.FlatFile({url: }) was passed a file without a .csv or .json suffix");
50
			}
51
		}
52
	}
53
};
54
dojo.inherits(dojo.data.old.provider.FlatFile, dojo.data.old.provider.Base);
55
dojo.data.old.provider.FlatFile.prototype.getProviderCapabilities = function (keyword) {
56
	dojo.lang.assertType(keyword, String, {optional:true});
57
	if (!this._ourCapabilities) {
58
		this._ourCapabilities = {transactions:false, undo:false, login:false, versioning:false, anonymousRead:true, anonymousWrite:false, permissions:false, queries:false, strongTyping:false, datatypes:[String, Date, Number]};
59
	}
60
	if (keyword) {
61
		return this._ourCapabilities[keyword];
62
	} else {
63
		return this._ourCapabilities;
64
	}
65
};
66
dojo.data.old.provider.FlatFile.prototype.registerAttribute = function (attributeId) {
67
	var registeredAttribute = this.getAttribute(attributeId);
68
	if (!registeredAttribute) {
69
		var newAttribute = new dojo.data.old.Attribute(this, attributeId);
70
		this._dictionaryOfAttributes[attributeId] = newAttribute;
71
		registeredAttribute = newAttribute;
72
	}
73
	return registeredAttribute;
74
};
75
dojo.data.old.provider.FlatFile.prototype.getAttribute = function (attributeId) {
76
	var attribute = (this._dictionaryOfAttributes[attributeId] || null);
77
	return attribute;
78
};
79
dojo.data.old.provider.FlatFile.prototype.getAttributes = function () {
80
	var arrayOfAttributes = [];
81
	for (var key in this._dictionaryOfAttributes) {
82
		var attribute = this._dictionaryOfAttributes[key];
83
		arrayOfAttributes.push(attribute);
84
	}
85
	return arrayOfAttributes;
86
};
87
dojo.data.old.provider.FlatFile.prototype.fetchArray = function (query) {
88
	return this._arrayOfItems;
89
};
90
dojo.data.old.provider.FlatFile.prototype.fetchResultSet = function (query) {
91
	if (!this._resultSet) {
92
		this._resultSet = new dojo.data.old.ResultSet(this, this.fetchArray(query));
93
	}
94
	return this._resultSet;
95
};
96
dojo.data.old.provider.FlatFile.prototype._newItem = function () {
97
	var item = new dojo.data.old.Item(this);
98
	this._arrayOfItems.push(item);
99
	return item;
100
};
101
dojo.data.old.provider.FlatFile.prototype._newAttribute = function (attributeId) {
102
	dojo.lang.assertType(attributeId, String);
103
	dojo.lang.assert(this.getAttribute(attributeId) === null);
104
	var attribute = new dojo.data.old.Attribute(this, attributeId);
105
	this._dictionaryOfAttributes[attributeId] = attribute;
106
	return attribute;
107
};
108
dojo.data.old.provider.Base.prototype._getResultSets = function () {
109
	return [this._resultSet];
110
};
111