LP#1292705 trim unneeded fields from IDL2js
[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}) x.fields[p] = {name:n,virtual:true};
23     for (var f in x.fields) x.field_map[x.fields[f].name] = x.fields[f];
24 }
25     </xsl:template>
26  
27     <xsl:template match="idl:IDL">
28         <xsl:choose>
29             <xsl:when test="$class_list = ''">
30                 <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>
31             </xsl:when>
32             <xsl:otherwise>
33                 <xsl:variable name="doc" select="."/>
34                 <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>
35             </xsl:otherwise>
36         </xsl:choose>
37     </xsl:template>
38  
39     <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:virtual = 'true'">virtual:true,</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>
40  
41     <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>
42
43     <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>
44  
45 <!-- to simplify the logic, the first and last field are assumed to
46      have values (and practically always will) -->
47 <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>
48  
49 <xsl:template name="pcrudPerms">
50     <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>
51 </xsl:template>
52
53 <xsl:template name="fieldOrLink">
54     <xsl:param name="f"/>
55     <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>
56 </xsl:template>
57
58 <xsl:template match="idl:link">key:"<xsl:value-of select="@key"/>","class":"<xsl:value-of select="@class"/>",reltype:"<xsl:value-of select="@reltype"/>"</xsl:template>
59
60 <xsl:template name="trueFalse">
61     <xsl:param name="tf"/>
62     <xsl:choose>
63         <xsl:when test="$tf='true'">true</xsl:when>
64         <xsl:otherwise>false</xsl:otherwise>
65     </xsl:choose>
66 </xsl:template>
67
68 <xsl:template name="defaultValue">
69     <xsl:param name="v"/>
70     <xsl:param name="d"/>
71     <xsl:choose>
72         <xsl:when test="string-length($v)=0"><xsl:value-of select="$d"/></xsl:when>
73         <xsl:otherwise><xsl:value-of select="$v"/></xsl:otherwise>
74     </xsl:choose>
75 </xsl:template>
76
77 </xsl:stylesheet>