}
Fieldmapper.prototype.baseClass = Fieldmapper;
Fieldmapper.prototype.obj_cache = {};
+
+Fieldmapper.prototype.search function (type,field,value) {
+
+ var list = user_request(
+ 'open-ils.proxy',
+ 'open-ils.proxy.proxy',
+ [
+ mw.G.auth_ses[0],
+ 'open-ils.storage',
+ 'open-ils.storage.direct.' + type.db_type + '.search.' + field,
+ array
+ ]
+ )[0];
+ if (type.cacheable) {
+ if (type.baseClass.obj_cache[type.classname] == null)
+ type.baseClass.obj_cache[type.classname] = {};
+ for (var i in list) {
+ type.baseClass.obj_cache[type.classname][list[i].id()] = list[i];
+ }
+ }
+ return list;
+}
Fieldmapper.prototype.clone = function() {
var obj = new this.constructor();
<!-- sub-templates -->
<xsl:template match="opensrf:fieldmapper/opensrf:classes">
<xsl:for-each select="opensrf:class">
+ <xsl:sort select="@id"/>
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:template>
<xsl:template match="opensrf:class">
<xsl:apply-templates select="@javascript:class"/>
<xsl:apply-templates select="opensrf:fields"/>
- <xsl:apply-templates select="opensrf:links/opensrf:link[@cdbi:type='has_many']"/>
+ <xsl:apply-templates select="opensrf:links/opensrf:link[@type='has_many']"/>
</xsl:template>
- <xsl:template match="opensrf:links/opensrf:link[@cdbi:type='has_many']">
- <xsl:variable name="num"><xsl:number/></xsl:variable>
- <xsl:variable name="source"><xsl:value-of select="@source"/></xsl:variable>
- <xsl:variable name="classname"><xsl:value-of select="../../@javascript:class"/></xsl:variable>
-
-// accessor for <xsl:value-of select="$classname"/>:
-<xsl:value-of select="$classname"/>.prototype.<xsl:value-of select="@field"/> = function () {
-
- var _pos = <xsl:value-of select="$classname"/>.last_real_field + <xsl:value-of select="$num"/>;
-
- if (!instanceOf(this.array[_pos], Array)) {
- this.array[_pos] = [];
-
- if (this.array[_pos].length == 0) {
- /* get the real thing.
- * search where <xsl:value-of select="$source"/>.<xsl:value-of select="//*[@id=$source]/opensrf:links/opensrf:link[@cdbi:type='has_a' and @source=$classname]/@field"/>()
- * equals this.<xsl:value-of select="../../opensrf:fields/opensrf:field[@cdbi:primary='true']/@name"/>();
- */
- }
-
- return this.array[_pos];
-}
-
- </xsl:template>
-
-
-
-
<xsl:template match="@javascript:class">
this.uber = <xsl:value-of select="."/>.baseClass.prototype;
}
-<xsl:value-of select="."/>.prototype = new Fieldmapper();
+<xsl:value-of select="."/>.prototype = new <xsl:value-of select="../javascript:superclass"/>();
<xsl:value-of select="."/>.prototype.constructor = <xsl:value-of select="."/>;
-<xsl:value-of select="."/>.baseClass = Fieldmapper;
+<xsl:value-of select="."/>.baseClass = <xsl:value-of select="../javascript:superclass"/>;
<xsl:value-of select="."/>.prototype.cachable = true;
<xsl:value-of select="."/>.prototype.fields = [];
<xsl:value-of select="."/>.last_real_field = 2;
+ <!-- scalar valued fields and "has_a" relationships -->
<xsl:template match="opensrf:field">
<xsl:variable name="num"><xsl:number/></xsl:variable>
<xsl:value-of select="../../@javascript:class"/>.last_real_field++;
<xsl:value-of select="../../@javascript:class"/>.prototype.fields.push("<xsl:value-of select="$field_name"/>");
<xsl:value-of select="../../@javascript:class"/>.prototype.<xsl:value-of select="$field_name"/> = function (new_value) {
+ if(arguments.length == 1) { this.array[<xsl:value-of select="$field_pos"/>] = new_value; }
- <xsl:choose>
- <xsl:when test="../../opensrf:links/opensrf:link[@field=$field_name and @cdbi:type='has_a']">
- <xsl:variable
- name="source"
- select="../../opensrf:links/opensrf:link[@field=$field_name and @cdbi:type='has_a']/@source"/>
+ <xsl:if test="../../opensrf:links/opensrf:link[@field=$field_name and @type='has_a']">
+ <!-- We have a fkey on this field. Go fetch the referenced object. -->
+ <xsl:variable
+ name="source"
+ select="../../opensrf:links/opensrf:link[@field=$field_name and @type='has_a']/@source"/>
- if(arguments.length == 1) { this.array[<xsl:value-of select="$field_pos"/>] = new_value; }
var val = this.array[<xsl:value-of select="$field_pos"/>];
if (!instanceOf(this.array[<xsl:value-of select="$field_pos"/>], <xsl:value-of select="$source"/>)) {
this.array[<xsl:value-of select="$field_pos"/>] = new <xsl:value-of select="$source"/>(val);
}
}
+ </xsl:if>
+ return this.array[<xsl:value-of select="$field_pos"/>];
+}
+ </xsl:template>
- return this.array[<xsl:value-of select="$field_pos"/>];
- </xsl:when>
- <xsl:otherwise>
- if(arguments.length == 1) { this.array[<xsl:value-of select="$field_pos"/>] = new_value; }
- return this.array[<xsl:value-of select="$field_pos"/>];
- </xsl:otherwise>
- </xsl:choose>
+
+
+
+ <!-- "has_many" relationships -->
+ <xsl:template match="opensrf:links/opensrf:link[@type='has_many']">
+ <xsl:variable name="num"><xsl:number/></xsl:variable>
+ <xsl:variable name="source"><xsl:value-of select="@source"/></xsl:variable>
+ <xsl:variable name="classname"><xsl:value-of select="../../@javascript:class"/></xsl:variable>
+ <xsl:variable name="id"><xsl:value-of select="../../@id"/></xsl:variable>
+ <xsl:variable name="fkey" select="//*[@id=$source]/opensrf:links/opensrf:link[@type='has_a' and @source=$id]/@field"/>
+ <xsl:variable name="pkey" select="../../opensrf:fields/opensrf:field[@cdbi:primary='true']/@name"/>
+
+// accessor for <xsl:value-of select="$classname"/>:
+<xsl:value-of select="$classname"/>.prototype.<xsl:value-of select="@field"/> = function () {
+
+ var _pos = <xsl:value-of select="$classname"/>.last_real_field + <xsl:value-of select="$num"/>;
+
+ if (!instanceOf(this.array[_pos], Array)) {
+ this.array[_pos] = [];
+
+ if (this.array[_pos].length == 0) {
+ /* get the real thing.
+ * search where <xsl:value-of select="$source"/>.<xsl:value-of select="$fkey"/>()
+ * equals this.<xsl:value-of select="../../opensrf:fields/opensrf:field[@cdbi:primary='true']/@name"/>();
+ */
+ this.array[_pos] = this.uber.search(
+ <xsl:value-of select="$source"/>,
+ "<xsl:value-of select="$fkey"/>",
+ this.<xsl:value-of select="$pkey"/>()
+ );
+ }
+
+ return this.array[_pos];
}
+
</xsl:template>
+
+
+
</xsl:stylesheet>
--- /dev/null
+<xsl:stylesheet
+ version='1.0'
+ xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
+ xmlns:opensrf="http://opensrf.org/xmlns/opensrf"
+ xmlns:cdbi="http://opensrf.org/xmlns/opensrf/cdbi"
+ xmlns:perl="http://opensrf.org/xmlns/opensrf/perl"
+ xmlns:javascript="http://opensrf.org/xmlns/opensrf/javascript"
+ xmlns:c="http://opensrf.org/xmlns/opensrf/c">
+ <xsl:output method="text" />
+ <xsl:strip-space elements="xsl:*"/>
+ <xsl:variable name="last_field_pos"/>
+
+ <xsl:template match="/">
+package Fieldmapper;
+use JSON;
+use Data::Dumper;
+use base 'OpenSRF::Application';
+
+use OpenSRF::Utils::Logger;
+my $log = 'OpenSRF::Utils::Logger';
+
+sub new {
+ my $self = shift;
+ my $value = shift;
+ if (defined $value) {
+ if (!ref($value) or ref($value) ne 'ARRAY') {
+ # go fetch the object by id...
+ }
+ } else {
+ $value = [];
+ }
+ return bless $value => $self->class_name;
+}
+
+sub decast {
+ my $self = shift;
+ return [ @$self ];
+}
+
+sub DESTROY {}
+
+sub class_name {
+ my $class_name = shift;
+ return ref($class_name) || $class_name;
+}
+
+sub isnew { return $_[0][0]; }
+sub ischanged { return $_[0][1]; }
+sub isdeleted { return $_[0][2]; }
+
+
+ <xsl:apply-templates select="opensrf:fieldmapper/opensrf:classes"/>
+
+1;
+ </xsl:template>
+
+
+
+
+
+<!-- sub-templates -->
+ <xsl:template match="opensrf:fieldmapper/opensrf:classes">
+ <xsl:for-each select="opensrf:class">
+ <xsl:sort select="@id"/>
+ <xsl:apply-templates select="."/>
+ <xsl:apply-templates select="opensrf:links/opensrf:link[@type='has_many']"/>
+ </xsl:for-each>
+ </xsl:template>
+
+
+
+
+ <xsl:template match="opensrf:class">
+ <xsl:apply-templates select="@perl:class"/>
+ <xsl:apply-templates select="opensrf:fields"/>
+ </xsl:template>
+
+
+
+
+ <xsl:template match="opensrf:fields">
+ <xsl:apply-templates select="opensrf:field"/>
+ </xsl:template>
+
+
+
+
+
+ <xsl:template match="@perl:class">
+
+#-------------------------------------------------------------------------------
+# Class definition for "<xsl:value-of select="."/>"
+#-------------------------------------------------------------------------------
+
+package <xsl:value-of select="."/>;
+use base "<xsl:value-of select="../perl:superclass"/>";
+
+{ my @real;
+ sub real_fields {
+ push @real, @_ if (@_);
+ return @real;
+ }
+}
+
+{ my $last_real;
+ sub last_real_field : lvalue {
+ $last_real;
+ }
+}
+
+ <xsl:if test="../@cdbi:class">
+sub cdbi {
+ return "<xsl:value-of select="../@cdbi:class"/>";
+}
+ </xsl:if>
+
+sub json_hint {
+ return "<xsl:value-of select="../@id"/>";
+}
+
+
+sub is_virtual {
+ <xsl:choose>
+ <xsl:when test="../@virutal">
+ return 1;
+ </xsl:when>
+ <xsl:otherwise>
+ return 0;
+ </xsl:otherwise>
+ </xsl:choose>
+}
+
+ </xsl:template>
+
+
+
+
+
+ <!-- scalar valued fields and "has_a" relationships -->
+ <xsl:template match="opensrf:field">
+
+ <xsl:variable name="num"><xsl:number/></xsl:variable>
+ <xsl:variable name="field_pos" select="$num + 2"/>
+ <xsl:variable name="last_field_pos" select="$field_pos + 1"/>
+ <xsl:variable name="field_name" select="@name"/>
+ <xsl:variable name="classname" select="../../@perl:class"/>
+
+# Accessor/mutator for <xsl:value-of select="$classname"/>::<xsl:value-of select="$field_name"/>:
+__PACKAGE__->last_real_field()++;
+__PACKAGE__->real_fields("<xsl:value-of select="$field_name"/>");
+sub <xsl:value-of select="$field_name"/> {
+ my $self = shift;
+ my $new_val = shift;
+ $self->[<xsl:value-of select="$field_pos"/>] = $new_val if (defined $new_val);
+
+ <xsl:if test="../../opensrf:links/opensrf:link[@field=$field_name and @type='has_a']">
+ <!-- We have a fkey on this field. Go fetch the referenced object. -->
+ <xsl:variable
+ name="source"
+ select="../../opensrf:links/opensrf:link[@field=$field_name and @type='has_a']/@source"/>
+ <xsl:variable
+ name="sourceclass"
+ select="//*[@id=$source]/@perl:class"/>
+
+ my $val = $self->[<xsl:value-of select="$field_pos"/>];
+
+ if (defined $self->[<xsl:value-of select="$field_pos"/>]) {
+ if (!UNIVERSAL::isa($self->[<xsl:value-of select="$field_pos"/>], <xsl:value-of select="$sourceclass"/>)) {
+ $self->[<xsl:value-of select="$field_pos"/>] = <xsl:value-of select="$sourceclass"/>->new($val);
+ }
+ }
+ </xsl:if>
+
+ return $self->[<xsl:value-of select="$field_pos"/>];
+}
+
+
+sub clear_<xsl:value-of select="$field_name"/> {
+ my $self = shift;
+ $self->[<xsl:value-of select="$field_pos"/>] = undef;
+ return 1;
+}
+
+ </xsl:template>
+
+
+
+
+
+
+ <!-- "has_many" relationships -->
+ <xsl:template match="opensrf:links/opensrf:link[@type='has_many']">
+ <xsl:variable name="num"><xsl:number/></xsl:variable>
+ <xsl:variable name="source"><xsl:value-of select="@source"/></xsl:variable>
+ <xsl:variable name="sourceclass"><xsl:value-of select="//*[@id=$source]/@perl:class"/></xsl:variable>
+ <xsl:variable name="classname"><xsl:value-of select="../../@perl:class"/></xsl:variable>
+ <xsl:variable name="id"><xsl:value-of select="../../@id"/></xsl:variable>
+ <xsl:variable name="fkey" select="//*[@id=$source]/opensrf:links/opensrf:link[@type='has_a' and @source=$id]/@field"/>
+ <xsl:variable name="pkey" select="../../opensrf:fields/opensrf:field[@cdbi:primary='true']/@name"/>
+
+# accessor for <xsl:value-of select="$classname"/>::<xsl:value-of select="@field"/>:
+sub <xsl:value-of select="@field"/> {
+ my $self = shift;
+
+ my $_pos = <xsl:value-of select="$classname"/>->last_real_field + <xsl:value-of select="$num"/>;
+
+ if (!ref($self->[$_pos]) ne 'ARRAY') {
+ $self->[$_pos] = [];
+
+ if (@{$self->[$_pos]} == 0) {
+ # get the real thing.
+ # search where <xsl:value-of select="$sourceclass"/>-><xsl:value-of select="$fkey"/> == $self-><xsl:value-of select="$pkey"/>;
+ }
+
+ return $self->[$_pos];
+}
+
+ </xsl:template>
+
+
+
+
+</xsl:stylesheet>
+
-<? xml version="1.0" encoding="UTF-8" ?>
+<?xml version="1.0" encoding="UTF-8"?>
<opensrf:fieldmapper
xmlns:opensrf="http://opensrf.org/xmlns/opensrf"
xmlns:cdbi="http://opensrf.org/xmlns/opensrf/cdbi"
<cdbi:name></cdbi:name>
</cdbi:table>
- <perl:parent>Fieldmapper</perl:parent>
- <cdbi:parent>OpenILS::Storage::CDBI</cdbi:parent>
+ <javascript:superclass>Fieldmapper</javascript:superclass>
+ <perl:superclass>Fieldmapper</perl:superclass>
+ <cdbi:superclass>OpenILS::Storage::CDBI</cdbi:superclass>
<opensrf:fields>
<opensrf:field name="" datatype="" cdbi:primary="" cdbi:required="" default="" cdbi:default=""/>
</opensrf:fields>
<opensrf:links>
- <opensrf:link field="" source="" javascript:list="" cdbi:type=""/>
+ <opensrf:link field="" source="" javascript:list="" type=""/>
</opensrf:links>
</opensrf:class>
-->
- <opensrf:class id="asvr">
+ <opensrf:class
+ id="asvr"
+ virtual="0"
+ perl:class="Fieldmapper::action::survey_response"
+ cdbi:class="action::survey_response"
+ javascript:class="asvr"
+ c:class="asvr">
+ <cdbi:table rdbms="Pg">
+ <cdbi:name>action.survey_response</cdbi:name>
+ <cdbi:sequence>action.survey_response_id_seq</cdbi:sequence>
+ </cdbi:table>
+ <cdbi:table rdbms="MySQL">
+ <cdbi:name>action_survey_response</cdbi:name>
+ </cdbi:table>
+
+ <javascript:superclass>Fieldmapper</javascript:superclass>
+ <perl:superclass>Fieldmapper</perl:superclass>
+ <cdbi:superclass>OpenILS::Storage::CDBI</cdbi:superclass>
+
+ <opensrf:fields>
+ <opensrf:field
+ name="id"
+ datatype="int"
+ cdbi:primary="true" />
+
+ <opensrf:field
+ name="survey"
+ datatype="int"
+ cdbi:required="true" />
+
+ <opensrf:field
+ name="question"
+ datatype="int"
+ cdbi:required="true" />
+
+ <opensrf:field
+ name="answer"
+ datatype="int"
+ cdbi:required="true" />
+
+ <opensrf:field
+ name="usr"
+ datatype="int"
+ cdbi:required="true" />
+
+ <opensrf:field
+ name="response_group_id"
+ datatype="int"
+ cdbi:required="true" />
+
+ <opensrf:field
+ name="answser_date"
+ datatype="timestamp"
+ cdbi:required="true" />
+
+ <opensrf:field
+ name="effective_date"
+ datatype="timestamp"
+ cdbi:required="true" />
+
+ </opensrf:fields>
+
<opensrf:links>
- <opensrf:link field="survey" source="asv" cdbi:type="has_a"/>
+ <opensrf:link field="survey" source="asv" type="has_a"/>
+ <opensrf:link field="question" source="asvq" type="has_a"/>
+ <opensrf:link field="answer" source="asva" type="has_a"/>
+ <!-- <opensrf:link field="usr" source="au" type="has_a"/> -->
</opensrf:links>
+
</opensrf:class>
- <opensrf:class id="asvq">
+ <opensrf:class
+ id="asvq"
+ perl:class="Fieldmapper::action::survey_question"
+ cdbi:class="action::survey_question"
+ javascript:class="asvq"
+ c:class="asvq">
+ <cdbi:table rdbms="Pg">
+ <cdbi:name>action.survey_question</cdbi:name>
+ <cdbi:sequence>action.survey_question_id_seq</cdbi:sequence>
+ </cdbi:table>
+ <cdbi:table rdbms="MySQL">
+ <cdbi:name>action_survey_question</cdbi:name>
+ </cdbi:table>
+
+ <javascript:superclass>Fieldmapper</javascript:superclass>
+ <perl:superclass>Fieldmapper</perl:superclass>
+ <cdbi:superclass>OpenILS::Storage::CDBI</cdbi:superclass>
+
+ <opensrf:fields>
+ <opensrf:field
+ name="id"
+ datatype="int"
+ cdbi:primary="true" />
+
+ <opensrf:field
+ name="survey"
+ datatype="int"
+ cdbi:required="true" />
+
+ <opensrf:field
+ name="question"
+ datatype="text"
+ cdbi:required="true" />
+
+ </opensrf:fields>
+
<opensrf:links>
- <opensrf:link field="survey" source="asv" cdbi:type="has_a"/>
+ <opensrf:link field="survey" source="asv" type="has_a"/>
+ <opensrf:link field="answers" source="asva" type="has_many"/>
</opensrf:links>
</opensrf:class>
- <opensrf:class id="aou"/>
+ <opensrf:class
+ id="asva"
+ perl:class="Fieldmapper::action::survey_answer"
+ cdbi:class="action::survey_answer"
+ javascript:class="asva"
+ c:class="asva">
+ <cdbi:table rdbms="Pg">
+ <cdbi:name>action.survey_answer</cdbi:name>
+ <cdbi:sequence>action.survey_answer_id_seq</cdbi:sequence>
+ </cdbi:table>
+ <cdbi:table rdbms="MySQL">
+ <cdbi:name>action_survey_answer</cdbi:name>
+ </cdbi:table>
+
+ <javascript:superclass>Fieldmapper</javascript:superclass>
+ <perl:superclass>Fieldmapper</perl:superclass>
+ <cdbi:superclass>OpenILS::Storage::CDBI</cdbi:superclass>
+
+ <opensrf:fields>
+ <opensrf:field
+ name="id"
+ datatype="int"
+ cdbi:primary="true" />
+
+ <opensrf:field
+ name="question"
+ datatype="int"
+ cdbi:required="true" />
+
+ <opensrf:field
+ name="answer"
+ datatype="text"
+ cdbi:required="true" />
+
+ </opensrf:fields>
+ <opensrf:links>
+ <opensrf:link field="question" source="asvq" type="has_a"/>
+ <opensrf:link field="responses" source="asvr" type="has_many"/>
+ </opensrf:links>
+ </opensrf:class>
+
+ <opensrf:class
+ id="aou"
+ perl:class="Fieldmapper::actor::org_unit"
+ cdbi:class="actor::org_unit"
+ javascript:class="aou"
+ c:class="aou">
+ <cdbi:table rdbms="Pg">
+ <cdbi:name>actor.org_unit</cdbi:name>
+ <cdbi:sequence>actor.org_unit_id_seq</cdbi:sequence>
+ </cdbi:table>
+ <cdbi:table rdbms="MySQL">
+ <cdbi:name>actor_org_unit</cdbi:name>
+ </cdbi:table>
+
+ <javascript:superclass>Fieldmapper</javascript:superclass>
+ <perl:superclass>Fieldmapper</perl:superclass>
+ <cdbi:superclass>OpenILS::Storage::CDBI</cdbi:superclass>
+
+ <opensrf:fields>
+ <opensrf:field
+ name="id"
+ datatype="int"
+ cdbi:primary="true" />
+ </opensrf:fields>
+ </opensrf:class>
+
<!-- Survey class definition -->
<opensrf:class
id="asv"
<cdbi:name>action_survey</cdbi:name>
</cdbi:table>
- <perl:parent>Fieldmapper</perl:parent>
- <cdbi:parent>OpenILS::Storage::CDBI</cdbi:parent>
+ <javascript:superclass>Fieldmapper</javascript:superclass>
+ <perl:superclass>Fieldmapper</perl:superclass>
+ <cdbi:superclass>OpenILS::Storage::CDBI</cdbi:superclass>
<opensrf:fields>
<opensrf:field
</opensrf:fields>
<opensrf:links>
- <opensrf:link field="questions" source="asvq" cdbi:type="has_many"/>
- <opensrf:link field="responses" source="asvr" cdbi:type="has_many"/>
- <opensrf:link field="owner" source="aou" cdbi:type="has_a"/>
+ <opensrf:link field="questions" source="asvq" type="has_many"/>
+ <opensrf:link field="responses" source="asvr" type="has_many"/>
+ <opensrf:link field="owner" source="aou" type="has_a"/>
</opensrf:links>
</opensrf:class>