LP1819179 IDL2js includes 'map' attribute data
[working/Evergreen.git] / Open-ILS / xsl / fm_IDL2js.xsl
1 <xsl:stylesheet
2     version='1.0'
3     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
4     xmlns:idl="http://opensrf.org/spec/IDL/base/v1"
5     xmlns:oils_persist="http://open-ils.org/spec/opensrf/IDL/persistence/v1"
6     xmlns:oils_obj="http://open-ils.org/spec/opensrf/IDL/objects/v1"
7     xmlns:reporter="http://open-ils.org/spec/opensrf/IDL/reporter/v1"
8     xmlns:permacrud="http://open-ils.org/spec/opensrf/IDL/permacrud/v1"
9     xmlns:str="http://exslt.org/strings"
10     extension-element-prefixes="str"
11 >
12     <xsl:output method="text" />
13     <xsl:strip-space elements="xsl:*"/>
14     <xsl:param name="class_list"/>
15  
16  
17     <xsl:template match="/">
18 var _preload_fieldmapper_IDL = {<xsl:apply-templates select="idl:IDL"/>};
19 for (var c in _preload_fieldmapper_IDL) {
20     var x = _preload_fieldmapper_IDL[c]; x.field_map = {};
21     var p = x.fields.length;
22     for (var n in {isnew:1,ischanged:1,isdeleted:1}) {
23         x.fields[p] = {name:n,virtual:true};
24         p++;
25     }
26     for (var f in x.fields) x.field_map[x.fields[f].name] = x.fields[f];
27 }
28     </xsl:template>
29  
30     <xsl:template match="idl:IDL">
31         <xsl:choose>
32             <xsl:when test="$class_list = ''">
33                 <xsl:for-each select="idl:class"><xsl:sort select="@id"/><xsl:apply-templates select="."/><xsl:if test="not(position() = last())">,</xsl:if></xsl:for-each>
34             </xsl:when>
35             <xsl:otherwise>
36                 <xsl:variable name="doc" select="."/>
37                 <xsl:for-each select="str:split($class_list,',')"><xsl:sort select="./text()"/><xsl:variable name="current_class" select="./text()"/><xsl:apply-templates select="$doc/idl:class[@id=$current_class]"/><xsl:if test="not(position() = last())">,</xsl:if></xsl:for-each>
38             </xsl:otherwise>
39         </xsl:choose>
40     </xsl:template>
41  
42     <xsl:template match="idl:class"><xsl:value-of select="@id"/>:{name:"<xsl:value-of select="@id"/>",<xsl:if test="@reporter:label">label:"<xsl:value-of select="@reporter:label"/>",</xsl:if><xsl:if test="@oils_persist:restrict_primary">restrict_primary:"<xsl:value-of select="@oils_persist:restrict_primary"/>",</xsl:if><xsl:if test="@oils_persist:tablename">table:"<xsl:value-of select="@oils_persist:tablename"/>",</xsl:if><xsl:if test="@reporter:core = 'true'">core:true,</xsl:if><xsl:if test="@oils_persist:virtual = 'true'">virtual:true,</xsl:if><xsl:if test="oils_persist:source_definition">source:"(<xsl:value-of select="oils_persist:source_definition/text()"/>)",</xsl:if><xsl:if test="idl:fields/@oils_persist:primary">pkey:"<xsl:value-of select="idl:fields/@oils_persist:primary"/>",</xsl:if><xsl:if test="idl:fields/@oils_persist:sequence">pkey_sequence:"<xsl:value-of select="idl:fields/@oils_persist:sequence"/>",</xsl:if><xsl:apply-templates select="idl:fields"/><xsl:apply-templates select="permacrud:permacrud"/>}</xsl:template>
43  
44     <xsl:template match="idl:fields">fields:[<xsl:for-each select="idl:field"><xsl:call-template name="printField"><xsl:with-param name='pos' select="position()"/></xsl:call-template><xsl:if test="not(position() = last())">,</xsl:if></xsl:for-each>]</xsl:template>
45
46     <xsl:template match="permacrud:permacrud">,permacrud:{<xsl:for-each select="permacrud:actions/*"><xsl:if test="name() = 'delete'">"</xsl:if><xsl:value-of select="name()"/><xsl:if test="name() = 'delete'">"</xsl:if>:{<xsl:call-template name='pcrudPerms'/>}<xsl:if test="not(position() = last())">,</xsl:if></xsl:for-each>}</xsl:template>
47  
48 <!-- to simplify the logic, the first and last field are assumed to
49      have values (and practically always will) -->
50 <xsl:template name='printField'>{name:"<xsl:value-of select="@name"/>",<xsl:if test="@reporter:label != ''">label:"<xsl:value-of select="@reporter:label"/>",</xsl:if><xsl:if test="@oils_persist:primitive = 'true'">primitive:true,</xsl:if><xsl:if test="@reporter:selector != ''">selector:"<xsl:value-of select="@reporter:selector"/>",</xsl:if><xsl:if test="@oils_persist:virtual = 'true'">virtual:true,</xsl:if><xsl:if test="@oils_obj:required = 'true'">required:true,</xsl:if><xsl:if test="@oils_persist:i18n = 'true'">i18n:true,</xsl:if><xsl:call-template name='fieldOrLink'><xsl:with-param name='f' select="."/></xsl:call-template>datatype:"<xsl:call-template name='defaultValue'><xsl:with-param name='v' select="@reporter:datatype"/><xsl:with-param name='d' select="string('text')"/></xsl:call-template>"}</xsl:template>
51  
52 <xsl:template name="pcrudPerms">
53     <xsl:if test="@permission">perms:[<xsl:for-each select="str:split(@permission,' ')">'<xsl:value-of select="./text()"/>'<xsl:if test="not(position() = last())">,</xsl:if></xsl:for-each>]</xsl:if>
54 </xsl:template>
55
56 <xsl:template name="fieldOrLink">
57     <xsl:param name="f"/>
58     <xsl:if test="$f/../../idl:links/idl:link[@field=$f/@name]">type:"link",<xsl:apply-templates select="$f/../../idl:links/idl:link[@field=$f/@name]"></xsl:apply-templates>,</xsl:if>
59 </xsl:template>
60
61 <xsl:template match="idl:link"><xsl:if test="@map != ''">map:"<xsl:value-of select="@map"/>",</xsl:if>key:"<xsl:value-of select="@key"/>","class":"<xsl:value-of select="@class"/>",reltype:"<xsl:value-of select="@reltype"/>"</xsl:template>
62
63 <xsl:template name="trueFalse">
64     <xsl:param name="tf"/>
65     <xsl:choose>
66         <xsl:when test="$tf='true'">true</xsl:when>
67         <xsl:otherwise>false</xsl:otherwise>
68     </xsl:choose>
69 </xsl:template>
70
71 <xsl:template name="defaultValue">
72     <xsl:param name="v"/>
73     <xsl:param name="d"/>
74     <xsl:choose>
75         <xsl:when test="string-length($v)=0"><xsl:value-of select="$d"/></xsl:when>
76         <xsl:otherwise><xsl:value-of select="$v"/></xsl:otherwise>
77     </xsl:choose>
78 </xsl:template>
79
80 </xsl:stylesheet>