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.old.Item");
|
|
|
14 |
dojo.require("dojo.data.old.Observable");
|
|
|
15 |
dojo.require("dojo.data.old.Value");
|
|
|
16 |
dojo.require("dojo.lang.common");
|
|
|
17 |
dojo.require("dojo.lang.assert");
|
|
|
18 |
dojo.data.old.Item = function (dataProvider) {
|
|
|
19 |
dojo.lang.assertType(dataProvider, dojo.data.old.provider.Base, {optional:true});
|
|
|
20 |
dojo.data.old.Observable.call(this);
|
|
|
21 |
this._dataProvider = dataProvider;
|
|
|
22 |
this._dictionaryOfAttributeValues = {};
|
|
|
23 |
};
|
|
|
24 |
dojo.inherits(dojo.data.old.Item, dojo.data.old.Observable);
|
|
|
25 |
dojo.data.old.Item.compare = function (itemOne, itemTwo) {
|
|
|
26 |
dojo.lang.assertType(itemOne, dojo.data.old.Item);
|
|
|
27 |
if (!dojo.lang.isOfType(itemTwo, dojo.data.old.Item)) {
|
|
|
28 |
return -1;
|
|
|
29 |
}
|
|
|
30 |
var nameOne = itemOne.getName();
|
|
|
31 |
var nameTwo = itemTwo.getName();
|
|
|
32 |
if (nameOne == nameTwo) {
|
|
|
33 |
var attributeArrayOne = itemOne.getAttributes();
|
|
|
34 |
var attributeArrayTwo = itemTwo.getAttributes();
|
|
|
35 |
if (attributeArrayOne.length != attributeArrayTwo.length) {
|
|
|
36 |
if (attributeArrayOne.length > attributeArrayTwo.length) {
|
|
|
37 |
return 1;
|
|
|
38 |
} else {
|
|
|
39 |
return -1;
|
|
|
40 |
}
|
|
|
41 |
}
|
|
|
42 |
for (var i in attributeArrayOne) {
|
|
|
43 |
var attribute = attributeArrayOne[i];
|
|
|
44 |
var arrayOfValuesOne = itemOne.getValues(attribute);
|
|
|
45 |
var arrayOfValuesTwo = itemTwo.getValues(attribute);
|
|
|
46 |
dojo.lang.assert(arrayOfValuesOne && (arrayOfValuesOne.length > 0));
|
|
|
47 |
if (!arrayOfValuesTwo) {
|
|
|
48 |
return 1;
|
|
|
49 |
}
|
|
|
50 |
if (arrayOfValuesOne.length != arrayOfValuesTwo.length) {
|
|
|
51 |
if (arrayOfValuesOne.length > arrayOfValuesTwo.length) {
|
|
|
52 |
return 1;
|
|
|
53 |
} else {
|
|
|
54 |
return -1;
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
for (var j in arrayOfValuesOne) {
|
|
|
58 |
var value = arrayOfValuesOne[j];
|
|
|
59 |
if (!itemTwo.hasAttributeValue(value)) {
|
|
|
60 |
return 1;
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
return 0;
|
|
|
64 |
}
|
|
|
65 |
} else {
|
|
|
66 |
if (nameOne > nameTwo) {
|
|
|
67 |
return 1;
|
|
|
68 |
} else {
|
|
|
69 |
return -1;
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
};
|
|
|
73 |
dojo.data.old.Item.prototype.toString = function () {
|
|
|
74 |
var arrayOfStrings = [];
|
|
|
75 |
var attributes = this.getAttributes();
|
|
|
76 |
for (var i in attributes) {
|
|
|
77 |
var attribute = attributes[i];
|
|
|
78 |
var arrayOfValues = this.getValues(attribute);
|
|
|
79 |
var valueString;
|
|
|
80 |
if (arrayOfValues.length == 1) {
|
|
|
81 |
valueString = arrayOfValues[0];
|
|
|
82 |
} else {
|
|
|
83 |
valueString = "[";
|
|
|
84 |
valueString += arrayOfValues.join(", ");
|
|
|
85 |
valueString += "]";
|
|
|
86 |
}
|
|
|
87 |
arrayOfStrings.push(" " + attribute + ": " + valueString);
|
|
|
88 |
}
|
|
|
89 |
var returnString = "{ ";
|
|
|
90 |
returnString += arrayOfStrings.join(",\n");
|
|
|
91 |
returnString += " }";
|
|
|
92 |
return returnString;
|
|
|
93 |
};
|
|
|
94 |
dojo.data.old.Item.prototype.compare = function (otherItem) {
|
|
|
95 |
return dojo.data.old.Item.compare(this, otherItem);
|
|
|
96 |
};
|
|
|
97 |
dojo.data.old.Item.prototype.isEqual = function (otherItem) {
|
|
|
98 |
return (this.compare(otherItem) == 0);
|
|
|
99 |
};
|
|
|
100 |
dojo.data.old.Item.prototype.getName = function () {
|
|
|
101 |
return this.get("name");
|
|
|
102 |
};
|
|
|
103 |
dojo.data.old.Item.prototype.get = function (attributeId) {
|
|
|
104 |
var literalOrValueOrArray = this._dictionaryOfAttributeValues[attributeId];
|
|
|
105 |
if (dojo.lang.isUndefined(literalOrValueOrArray)) {
|
|
|
106 |
return null;
|
|
|
107 |
}
|
|
|
108 |
if (literalOrValueOrArray instanceof dojo.data.old.Value) {
|
|
|
109 |
return literalOrValueOrArray.getValue();
|
|
|
110 |
}
|
|
|
111 |
if (dojo.lang.isArray(literalOrValueOrArray)) {
|
|
|
112 |
var dojoDataValue = literalOrValueOrArray[0];
|
|
|
113 |
return dojoDataValue.getValue();
|
|
|
114 |
}
|
|
|
115 |
return literalOrValueOrArray;
|
|
|
116 |
};
|
|
|
117 |
dojo.data.old.Item.prototype.getValue = function (attributeId) {
|
|
|
118 |
var literalOrValueOrArray = this._dictionaryOfAttributeValues[attributeId];
|
|
|
119 |
if (dojo.lang.isUndefined(literalOrValueOrArray)) {
|
|
|
120 |
return null;
|
|
|
121 |
}
|
|
|
122 |
if (literalOrValueOrArray instanceof dojo.data.old.Value) {
|
|
|
123 |
return literalOrValueOrArray;
|
|
|
124 |
}
|
|
|
125 |
if (dojo.lang.isArray(literalOrValueOrArray)) {
|
|
|
126 |
var dojoDataValue = literalOrValueOrArray[0];
|
|
|
127 |
return dojoDataValue;
|
|
|
128 |
}
|
|
|
129 |
var literal = literalOrValueOrArray;
|
|
|
130 |
dojoDataValue = new dojo.data.old.Value(literal);
|
|
|
131 |
this._dictionaryOfAttributeValues[attributeId] = dojoDataValue;
|
|
|
132 |
return dojoDataValue;
|
|
|
133 |
};
|
|
|
134 |
dojo.data.old.Item.prototype.getValues = function (attributeId) {
|
|
|
135 |
var literalOrValueOrArray = this._dictionaryOfAttributeValues[attributeId];
|
|
|
136 |
if (dojo.lang.isUndefined(literalOrValueOrArray)) {
|
|
|
137 |
return null;
|
|
|
138 |
}
|
|
|
139 |
if (literalOrValueOrArray instanceof dojo.data.old.Value) {
|
|
|
140 |
var array = [literalOrValueOrArray];
|
|
|
141 |
this._dictionaryOfAttributeValues[attributeId] = array;
|
|
|
142 |
return array;
|
|
|
143 |
}
|
|
|
144 |
if (dojo.lang.isArray(literalOrValueOrArray)) {
|
|
|
145 |
return literalOrValueOrArray;
|
|
|
146 |
}
|
|
|
147 |
var literal = literalOrValueOrArray;
|
|
|
148 |
var dojoDataValue = new dojo.data.old.Value(literal);
|
|
|
149 |
array = [dojoDataValue];
|
|
|
150 |
this._dictionaryOfAttributeValues[attributeId] = array;
|
|
|
151 |
return array;
|
|
|
152 |
};
|
|
|
153 |
dojo.data.old.Item.prototype.load = function (attributeId, value) {
|
|
|
154 |
this._dataProvider.registerAttribute(attributeId);
|
|
|
155 |
var literalOrValueOrArray = this._dictionaryOfAttributeValues[attributeId];
|
|
|
156 |
if (dojo.lang.isUndefined(literalOrValueOrArray)) {
|
|
|
157 |
this._dictionaryOfAttributeValues[attributeId] = value;
|
|
|
158 |
return;
|
|
|
159 |
}
|
|
|
160 |
if (!(value instanceof dojo.data.old.Value)) {
|
|
|
161 |
value = new dojo.data.old.Value(value);
|
|
|
162 |
}
|
|
|
163 |
if (literalOrValueOrArray instanceof dojo.data.old.Value) {
|
|
|
164 |
var array = [literalOrValueOrArray, value];
|
|
|
165 |
this._dictionaryOfAttributeValues[attributeId] = array;
|
|
|
166 |
return;
|
|
|
167 |
}
|
|
|
168 |
if (dojo.lang.isArray(literalOrValueOrArray)) {
|
|
|
169 |
literalOrValueOrArray.push(value);
|
|
|
170 |
return;
|
|
|
171 |
}
|
|
|
172 |
var literal = literalOrValueOrArray;
|
|
|
173 |
var dojoDataValue = new dojo.data.old.Value(literal);
|
|
|
174 |
array = [dojoDataValue, value];
|
|
|
175 |
this._dictionaryOfAttributeValues[attributeId] = array;
|
|
|
176 |
};
|
|
|
177 |
dojo.data.old.Item.prototype.set = function (attributeId, value) {
|
|
|
178 |
this._dataProvider.registerAttribute(attributeId);
|
|
|
179 |
this._dictionaryOfAttributeValues[attributeId] = value;
|
|
|
180 |
this._dataProvider.noteChange(this, attributeId, value);
|
|
|
181 |
};
|
|
|
182 |
dojo.data.old.Item.prototype.setValue = function (attributeId, value) {
|
|
|
183 |
this.set(attributeId, value);
|
|
|
184 |
};
|
|
|
185 |
dojo.data.old.Item.prototype.addValue = function (attributeId, value) {
|
|
|
186 |
this.load(attributeId, value);
|
|
|
187 |
this._dataProvider.noteChange(this, attributeId, value);
|
|
|
188 |
};
|
|
|
189 |
dojo.data.old.Item.prototype.setValues = function (attributeId, arrayOfValues) {
|
|
|
190 |
dojo.lang.assertType(arrayOfValues, Array);
|
|
|
191 |
this._dataProvider.registerAttribute(attributeId);
|
|
|
192 |
var finalArray = [];
|
|
|
193 |
this._dictionaryOfAttributeValues[attributeId] = finalArray;
|
|
|
194 |
for (var i in arrayOfValues) {
|
|
|
195 |
var value = arrayOfValues[i];
|
|
|
196 |
if (!(value instanceof dojo.data.old.Value)) {
|
|
|
197 |
value = new dojo.data.old.Value(value);
|
|
|
198 |
}
|
|
|
199 |
finalArray.push(value);
|
|
|
200 |
this._dataProvider.noteChange(this, attributeId, value);
|
|
|
201 |
}
|
|
|
202 |
};
|
|
|
203 |
dojo.data.old.Item.prototype.getAttributes = function () {
|
|
|
204 |
var arrayOfAttributes = [];
|
|
|
205 |
for (var key in this._dictionaryOfAttributeValues) {
|
|
|
206 |
arrayOfAttributes.push(this._dataProvider.getAttribute(key));
|
|
|
207 |
}
|
|
|
208 |
return arrayOfAttributes;
|
|
|
209 |
};
|
|
|
210 |
dojo.data.old.Item.prototype.hasAttribute = function (attributeId) {
|
|
|
211 |
return (attributeId in this._dictionaryOfAttributeValues);
|
|
|
212 |
};
|
|
|
213 |
dojo.data.old.Item.prototype.hasAttributeValue = function (attributeId, value) {
|
|
|
214 |
var arrayOfValues = this.getValues(attributeId);
|
|
|
215 |
for (var i in arrayOfValues) {
|
|
|
216 |
var candidateValue = arrayOfValues[i];
|
|
|
217 |
if (candidateValue.isEqual(value)) {
|
|
|
218 |
return true;
|
|
|
219 |
}
|
|
|
220 |
}
|
|
|
221 |
return false;
|
|
|
222 |
};
|
|
|
223 |
|