Use distutils to install Python into the standard location
[OpenSRF.git] / examples / fieldmapper2perl.xsl
1 <xsl:stylesheet
2         version='1.0'
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"/>
13
14         <xsl:template match="/">
15 package Fieldmapper;
16 use OpenSRF::Utils::JSON;
17 use Data::Dumper;
18 use base 'OpenSRF::Application';
19
20 use OpenSRF::Utils::Logger;
21 my $log = 'OpenSRF::Utils::Logger';
22
23 sub new {                                                            
24         my $self = shift;                                            
25         my $value = shift;                                              
26         if (defined $value) {
27                 if (!ref($value) or ref($value) ne 'ARRAY') {
28                         # go fetch the object by id...
29                 }
30         } else {
31                 $value = [];
32         }
33         return bless $value => $self->class_name;                    
34 }
35
36 sub decast {
37         my $self = shift;
38         return [ @$self ];
39 }       
40
41 sub DESTROY {} 
42
43 sub class_name {
44         my $class_name = shift;
45         return ref($class_name) || $class_name;
46 }
47
48 sub isnew { return $_[0][0]; }
49 sub ischanged { return $_[0][1]; }
50 sub isdeleted { return $_[0][2]; }
51
52
53                 <xsl:apply-templates select="opensrf:fieldmapper/opensrf:classes"/>
54
55 1;
56         </xsl:template>
57
58
59
60
61
62 <!-- sub-templates -->
63         <xsl:template match="opensrf:fieldmapper/opensrf:classes">
64                 <xsl:for-each select="opensrf:class">
65                         <xsl:sort select="@id"/>
66                         <xsl:apply-templates select="."/>
67                         <xsl:apply-templates select="opensrf:links/opensrf:link[@type='has_many']"/>
68                 </xsl:for-each>
69         </xsl:template>
70
71
72
73
74         <xsl:template match="opensrf:class">
75                 <xsl:apply-templates select="@perl:class"/>
76                 <xsl:apply-templates select="opensrf:fields"/>
77         </xsl:template>
78
79
80
81
82         <xsl:template match="opensrf:fields">
83                 <xsl:apply-templates select="opensrf:field"/>
84         </xsl:template>
85
86
87
88
89
90         <xsl:template match="@perl:class">
91  
92 #-------------------------------------------------------------------------------
93 # Class definition for "<xsl:value-of select="."/>"
94 #-------------------------------------------------------------------------------
95  
96 package <xsl:value-of select="."/>;
97 use base "<xsl:value-of select="../perl:superclass"/>";
98
99 {       my @real;
100         sub real_fields {
101                 push @real, @_ if (@_);
102                 return @real;
103         }
104 }
105
106 {       my $last_real;
107         sub last_real_field : lvalue {
108                 $last_real;
109         }
110 }
111
112         <xsl:if test="../@cdbi:class">
113 sub cdbi {
114         return "<xsl:value-of select="../@cdbi:class"/>";
115 }
116         </xsl:if>
117
118 sub json_hint {
119         return "<xsl:value-of select="../@id"/>";
120 }
121
122
123 sub is_virtual {
124         <xsl:choose>
125                 <xsl:when test="../@virutal">
126         return 1;
127                 </xsl:when>
128                 <xsl:otherwise>
129         return 0;
130                 </xsl:otherwise>
131         </xsl:choose>
132 }
133
134         </xsl:template>
135
136
137
138
139
140         <!-- scalar valued fields and "has_a" relationships -->
141         <xsl:template match="opensrf:field">
142
143                 <xsl:variable name="num"><xsl:number/></xsl:variable>
144                 <xsl:variable name="field_pos" select="$num + 2"/>
145                 <xsl:variable name="last_field_pos" select="$field_pos + 1"/>
146                 <xsl:variable name="field_name" select="@name"/>
147                 <xsl:variable name="classname" select="../../@perl:class"/>
148
149 # Accessor/mutator for <xsl:value-of select="$classname"/>::<xsl:value-of select="$field_name"/>:
150 __PACKAGE__->last_real_field()++;
151 __PACKAGE__->real_fields("<xsl:value-of select="$field_name"/>");
152 sub <xsl:value-of select="$field_name"/> {
153         my $self = shift;
154         my $new_val = shift;
155         $self->[<xsl:value-of select="$field_pos"/>] = $new_val if (defined $new_val);
156
157                 <xsl:if test="../../opensrf:links/opensrf:link[@field=$field_name and @type='has_a']">
158                         <!-- We have a fkey on this field.  Go fetch the referenced object. -->
159                         <xsl:variable
160                                 name="source"
161                                 select="../../opensrf:links/opensrf:link[@field=$field_name and @type='has_a']/@source"/>
162                         <xsl:variable
163                                 name="sourceclass"
164                                 select="//*[@id=$source]/@perl:class"/>
165
166         my $val = $self->[<xsl:value-of select="$field_pos"/>];
167
168         if (defined $self->[<xsl:value-of select="$field_pos"/>]) {
169                 if (!UNIVERSAL::isa($self->[<xsl:value-of select="$field_pos"/>], <xsl:value-of select="$sourceclass"/>)) {
170                         $self->[<xsl:value-of select="$field_pos"/>] = <xsl:value-of select="$sourceclass"/>->new($val);
171                 }
172         }
173                 </xsl:if>
174
175         return $self->[<xsl:value-of select="$field_pos"/>];
176 }
177
178
179 sub clear_<xsl:value-of select="$field_name"/> {
180         my $self = shift;
181         $self->[<xsl:value-of select="$field_pos"/>] = undef;
182         return 1;
183 }
184
185         </xsl:template>
186
187
188
189
190
191
192         <!-- "has_many" relationships -->
193         <xsl:template match="opensrf:links/opensrf:link[@type='has_many']">
194                 <xsl:variable name="num"><xsl:number/></xsl:variable>
195                 <xsl:variable name="source"><xsl:value-of select="@source"/></xsl:variable>
196                 <xsl:variable name="sourceclass"><xsl:value-of select="//*[@id=$source]/@perl:class"/></xsl:variable>
197                 <xsl:variable name="classname"><xsl:value-of select="../../@perl:class"/></xsl:variable>
198                 <xsl:variable name="id"><xsl:value-of select="../../@id"/></xsl:variable>
199                 <xsl:variable name="fkey" select="//*[@id=$source]/opensrf:links/opensrf:link[@type='has_a' and @source=$id]/@field"/>
200                 <xsl:variable name="pkey" select="../../opensrf:fields/opensrf:field[@database:primary='true']/@name"/>
201
202 # accessor for <xsl:value-of select="$classname"/>::<xsl:value-of select="@field"/>:
203 sub <xsl:value-of select="@field"/> {
204         my $self = shift;
205  
206         my $_pos = <xsl:value-of select="$classname"/>->last_real_field + <xsl:value-of select="$num"/>;
207  
208         if (!ref($self->[$_pos]) ne 'ARRAY') {
209                 $self->[$_pos] = [];
210  
211         if (@{$self->[$_pos]} == 0) {
212                 # get the real thing.
213                 # search where <xsl:value-of select="$sourceclass"/>-><xsl:value-of select="$fkey"/> == $self-><xsl:value-of select="$pkey"/>;
214         }
215  
216         return $self->[$_pos];
217 }
218
219         </xsl:template>
220
221
222
223
224 </xsl:stylesheet>
225