3 xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
4 xmlns:opensrf="http://opensrf.org/xmlns/opensrf"
5 xmlns:cdbi="http://opensrf.org/xmlns/opensrf/cdbi"
6 xmlns:database="http://opensrf.org/xmlns/opensrf/database"
7 xmlns:perl="http://opensrf.org/xmlns/opensrf/perl"
8 xmlns:javascript="http://opensrf.org/xmlns/opensrf/javascript"
9 xmlns:c="http://opensrf.org/xmlns/opensrf/c">
10 <xsl:output method="text" />
11 <xsl:strip-space elements="xsl:*"/>
12 <xsl:variable name="last_field_pos"/>
14 <xsl:template match="/">
18 var unit_test = false;
20 function instanceOf(object, constructorFunction) {
22 while (object != null) {
23 if (object == constructorFunction.prototype)
25 object = object.__proto__;
28 while(object != null) {
29 if( object instanceof constructorFunction )
31 object = object.__proto__;
37 // Top level superclass
38 function Fieldmapper(array) {
41 if (array.constructor == Array) {
43 } else if ( instanceOf( array, String ) || instanceOf( array, Number ) ) {
48 obj = this.baseClass.obj_cache[this.classname][array];
55 'open-ils.proxy.proxy',
59 'open-ils.storage.direct.' + this.db_type + '.retrieve',
65 if (this.baseClass.obj_cache[this.classname] == null)
66 this.baseClass.obj_cache[this.classname] = {};
68 this.baseClass.obj_cache[this.classname][obj.id()] = obj;
71 this.array = obj.array;
74 throw new FieldmapperException( "Attempt to build fieldmapper object with something wierd");
78 Fieldmapper.prototype.baseClass = Fieldmapper;
79 Fieldmapper.prototype.obj_cache = {};
81 Fieldmapper.prototype.search function (type,field,value) {
83 var list = user_request(
85 'open-ils.proxy.proxy',
89 'open-ils.storage.direct.' + type.db_type + '.search.' + field,
94 if (type.baseClass.obj_cache[type.classname] == null)
95 type.baseClass.obj_cache[type.classname] = {};
97 type.baseClass.obj_cache[type.classname][list[i].id()] = list[i];
103 Fieldmapper.prototype.clone = function() {
104 var obj = new this.constructor();
106 for( var i in this.array ) {
107 var thing = this.array[i];
108 if(thing == null) continue;
110 if( thing._isfieldmapper ) {
111 obj.array[i] = thing.clone();
114 if(instanceOf(thing, Array)) {
115 obj.array[i] = new Array();
117 for( var j in thing ) {
119 if( thing[j]._isfieldmapper )
120 obj.array[i][j] = thing[j].clone();
122 obj.array[i][j] = thing[j];
125 obj.array[i] = thing;
132 function FieldmapperException(message) {
133 this.message = message;
136 FieldmapperException.toString = function() {
137 return "FieldmapperException: " + this.message + "\n";
142 <xsl:apply-templates select="opensrf:fieldmapper/opensrf:classes"/>
149 <!-- sub-templates -->
150 <xsl:template match="opensrf:fieldmapper/opensrf:classes">
151 <xsl:for-each select="opensrf:class">
152 <xsl:sort select="@id"/>
153 <xsl:apply-templates select="."/>
160 <xsl:template match="opensrf:class">
161 <xsl:apply-templates select="@javascript:class"/>
162 <xsl:apply-templates select="opensrf:fields"/>
163 <xsl:apply-templates select="opensrf:links/opensrf:link[@type='has_many']"/>
169 <xsl:template match="opensrf:fields">
170 <xsl:apply-templates select="opensrf:field"/>
177 <xsl:template match="@javascript:class">
179 // Class definition for "<xsl:value-of select="."/>"
181 function <xsl:value-of select="."/> (array) {
183 if (!instanceOf(this, <xsl:value-of select="."/>))
184 return new <xsl:value-of select="."/>(array);
186 this.baseClass.call(this,array);
187 this.classname = "<xsl:value-of select="."/>";
188 this._isfieldmapper = true;
189 this.uber = <xsl:value-of select="."/>.baseClass.prototype;
192 <xsl:value-of select="."/>.prototype = new <xsl:value-of select="../javascript:superclass"/>();
193 <xsl:value-of select="."/>.prototype.constructor = <xsl:value-of select="."/>;
194 <xsl:value-of select="."/>.baseClass = <xsl:value-of select="../javascript:superclass"/>;
195 <xsl:value-of select="."/>.prototype.cachable = true;
196 <xsl:value-of select="."/>.prototype.fields = [];
197 <xsl:value-of select="."/>.last_real_field = 2;
199 <!-- XXX This needs to come from somewhere else!!!! -->
200 <xsl:value-of select="."/>.prototype.db_type = "<xsl:value-of select="../database:table[@rdbms='Pg']/database:name"/>";
202 <xsl:value-of select="."/>.prototype.isnew = function(new_value) {
203 if(arguments.length == 1) { this.array[0] = new_value; }
204 return this.array[0];
207 <xsl:value-of select="."/>.prototype.ischanged = function(new_value) {
208 if(arguments.length == 1) { this.array[1] = new_value; }
209 return this.array[1];
212 <xsl:value-of select="."/>.prototype.isdeleted = function(new_value) {
213 if(arguments.length == 1) { this.array[2] = new_value; }
214 return this.array[2];
223 <!-- scalar valued fields and "has_a" relationships -->
224 <xsl:template match="opensrf:field">
226 <xsl:variable name="num"><xsl:number/></xsl:variable>
227 <xsl:variable name="field_pos" select="$num + 2"/>
228 <xsl:variable name="last_field_pos" select="$field_pos + 1"/>
229 <xsl:variable name="field_name" select="@name"/>
231 // Accessor/mutator for <xsl:value-of select="../../@javascript:class"/>.<xsl:value-of select="$field_name"/>:
232 <xsl:value-of select="../../@javascript:class"/>.last_real_field++;
233 <xsl:value-of select="../../@javascript:class"/>.prototype.fields.push("<xsl:value-of select="$field_name"/>");
234 <xsl:value-of select="../../@javascript:class"/>.prototype.<xsl:value-of select="$field_name"/> = function (new_value) {
235 if(arguments.length == 1) { this.array[<xsl:value-of select="$field_pos"/>] = new_value; }
237 <xsl:if test="../../opensrf:links/opensrf:link[@field=$field_name and @type='has_a']">
238 <!-- We have a fkey on this field. Go fetch the referenced object. -->
241 select="../../opensrf:links/opensrf:link[@field=$field_name and @type='has_a']/@source"/>
243 var val = this.array[<xsl:value-of select="$field_pos"/>];
245 if (!instanceOf(this.array[<xsl:value-of select="$field_pos"/>], <xsl:value-of select="$source"/>)) {
246 if (this.array[<xsl:value-of select="$field_pos"/>] != null) {
247 this.array[<xsl:value-of select="$field_pos"/>] = new <xsl:value-of select="$source"/>(val);
251 return this.array[<xsl:value-of select="$field_pos"/>];
259 <!-- "has_many" relationships -->
260 <xsl:template match="opensrf:links/opensrf:link[@type='has_many']">
261 <xsl:variable name="num"><xsl:number/></xsl:variable>
262 <xsl:variable name="source"><xsl:value-of select="@source"/></xsl:variable>
263 <xsl:variable name="classname"><xsl:value-of select="../../@javascript:class"/></xsl:variable>
264 <xsl:variable name="id"><xsl:value-of select="../../@id"/></xsl:variable>
265 <xsl:variable name="fkey" select="//*[@id=$source]/opensrf:links/opensrf:link[@type='has_a' and @source=$id]/@field"/>
266 <xsl:variable name="pkey" select="../../opensrf:fields/opensrf:field[@database:primary='true']/@name"/>
268 // accessor for <xsl:value-of select="$classname"/>:
269 <xsl:value-of select="$classname"/>.prototype.<xsl:value-of select="@field"/> = function () {
271 var _pos = <xsl:value-of select="$classname"/>.last_real_field + <xsl:value-of select="$num"/>;
273 if (!instanceOf(this.array[_pos], Array)) {
274 this.array[_pos] = [];
276 if (this.array[_pos].length == 0) {
277 /* get the real thing.
278 * search where <xsl:value-of select="$source"/>.<xsl:value-of select="$fkey"/>()
279 * equals this.<xsl:value-of select="../../opensrf:fields/opensrf:field[@database:primary='true']/@name"/>();
281 this.array[_pos] = this.uber.search(
282 <xsl:value-of select="$source"/>,
283 "<xsl:value-of select="$fkey"/>",
284 this.<xsl:value-of select="$pkey"/>()
288 return this.array[_pos];