/[volute]/trunk/projects/theory/snapdm/res/jpa.xsl
ViewVC logotype

Contents of /trunk/projects/theory/snapdm/res/jpa.xsl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 422 - (show annotations)
Thu May 8 16:52:53 2008 UTC (12 years, 6 months ago) by gerard.lemson
File MIME type: application/xml
File size: 10489 byte(s)
Started work on properly (?) mapping embedded datatypes.
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!DOCTYPE stylesheet [
4 <!ENTITY cr "<xsl:text>
5 </xsl:text>">
6 <!ENTITY bl "<xsl:text> </xsl:text>">
7 ]>
8
9 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
10 xmlns:exsl="http://exslt.org/common"
11 extension-element-prefixes="exsl">
12
13 <!--
14 Common templates used by XSLT scripts
15 -->
16 <xsl:import href="common-ddl.xsl"/>
17
18 <xsl:output name="persistenceInfo" method="xml" encoding="UTF-8" indent="yes" />
19
20
21 <xsl:key name="element" match="*//*" use="@xmiid"/>
22
23
24
25 <xsl:template match="objectType" mode="JPAAnnotation">
26 <xsl:variable name="className" select="name" />
27 <xsl:variable name="xmiid" select="@xmiid" />
28 <xsl:variable name="childMod">
29 <xsl:choose>
30 <xsl:when test="count(//extends[@xmiidref = $xmiid]) > 0">1</xsl:when>
31 <xsl:otherwise>0</xsl:otherwise>
32 </xsl:choose>
33 </xsl:variable>
34 <xsl:variable name="extMod">
35 <xsl:choose>
36 <xsl:when test="count(extends) = 1">1</xsl:when>
37 <xsl:otherwise>0</xsl:otherwise>
38 </xsl:choose>
39 </xsl:variable>
40 <xsl:variable name="hasName">
41 <xsl:choose>
42 <xsl:when test="count(attribute[name = 'name']) > 0">1</xsl:when>
43 <xsl:otherwise>0</xsl:otherwise>
44 </xsl:choose>
45 </xsl:variable>
46
47 @Entity
48 @Table( name = "<xsl:apply-templates select="." mode="tableName"/>" )
49 <xsl:if test="$childMod = 1">
50 &cr;
51 <!-- JOINED strategy for inheritance -->
52 @Inheritance( strategy = InheritanceType.JOINED )
53 @DiscriminatorColumn( name = "<xsl:value-of select="$discriminatorColumnName"/>", discriminatorType = DiscriminatorType.STRING, length = <xsl:value-of select="$discriminatorColumnLength"/>)
54 &cr;
55 </xsl:if>
56 <xsl:if test="$extMod = 1">
57 @DiscriminatorValue( "<xsl:apply-templates select="$className"/>" ) <!-- TODO decide whether this should be a path -->
58 </xsl:if>
59
60 @NamedQueries( {
61 @NamedQuery( name = "<xsl:apply-templates select="$className"/>.findById", query = "SELECT o FROM <xsl:apply-templates select="$className"/> o WHERE o.id = :id")
62 <xsl:if test="$hasName = 1">
63 , @NamedQuery( name = "<xsl:apply-templates select="$className"/>.findByName", query = "SELECT o FROM <xsl:apply-templates select="$className"/> o WHERE o.name = :name")
64 </xsl:if>
65 } )
66 </xsl:template>
67
68
69
70
71 <!-- work in progress -->
72
73 <xsl:template match="dataType" mode="JPAAnnotation">
74 <xsl:text>@Embeddable</xsl:text>&cr;
75 </xsl:template>
76
77
78
79
80
81
82
83 <!-- TODO check whether type is complex or simple -->
84 <xsl:template match="attribute" mode="JPAAnnotation">
85 <xsl:variable name="type" select="key('element', datatype/@xmiidref)"/>
86
87 <xsl:choose>
88 <xsl:when test="name($type) = 'primitiveType'">
89 <xsl:choose>
90 <xsl:when test="number(constraints/maxLength) = -1">
91 @Basic(fetch=FetchType.LAZY, optional=<xsl:apply-templates select="." mode="nullable"/>)
92 @Lob
93 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/> )
94 </xsl:when>
95 <xsl:when test="number(constraints/maxLength) > 0">
96 @Basic(optional=<xsl:apply-templates select="." mode="nullable"/>)
97 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/>, length = <xsl:value-of select="constraints/maxLength"/> )
98 </xsl:when>
99 <xsl:when test="$type/name = 'datetime'">
100 @Basic(optional=<xsl:apply-templates select="." mode="nullable"/>)
101 @Temporal( TemporalType.TIMESTAMP )
102 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/> )
103 </xsl:when>
104 <xsl:otherwise>
105 @Basic(optional=<xsl:apply-templates select="." mode="nullable"/>)
106 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/> )
107 </xsl:otherwise>
108 </xsl:choose>
109 </xsl:when>
110 <xsl:when test="name($type) = 'enumeration'">
111 <xsl:call-template name="enumPattern">
112 <xsl:with-param name="columnName"><xsl:apply-templates select="." mode="columnName"/></xsl:with-param>
113 </xsl:call-template>
114 </xsl:when>
115 <xsl:when test="name($type) = 'dataType'">
116 <xsl:variable name="columns">
117 <xsl:apply-templates select="." mode="columns"/>
118 </xsl:variable>
119 @Embedded
120 @AttributeOverrides ({
121 <xsl:for-each select="exsl:node-set($columns)/column">
122 <xsl:if test="position() > 1">,&cr;</xsl:if>
123 @AttributeOverride(name="<xsl:value-of select="attrname"/>",column=@Column(name="<xsl:value-of select="name"/>"))
124 </xsl:for-each>
125 })
126 <xsl:comment>TODO ATTRIBUTE : dataType </xsl:comment>
127 </xsl:when>
128 <xsl:otherwise>
129 [NOT_SUPPORTED_ATTRIBUTE]
130 </xsl:otherwise>
131 </xsl:choose>
132 </xsl:template>
133
134
135
136
137 <xsl:template match="attribute|reference|collection" mode="nullable">
138 <xsl:choose>
139 <xsl:when test="starts-with(multiplicity, '0')">true</xsl:when>
140 <xsl:otherwise>false</xsl:otherwise>
141 </xsl:choose>
142 </xsl:template>
143
144
145
146
147 <xsl:template match="reference" mode="JPAAnnotation">
148 <xsl:variable name="type" select="key('element', datatype/@xmiidref)"/>
149
150 <xsl:choose>
151 <xsl:when test="name($type) = 'primitiveType' or name($type) = 'enumeration'">
152 [NOT_SUPPORTED_REFERENCE]
153 </xsl:when>
154 <xsl:otherwise>
155 @ManyToOne(cascade = CascadeType.ALL)
156 @JoinColumn(name = "<xsl:apply-templates select="." mode="columnName"/>", referencedColumnName = "id", nullable = <xsl:apply-templates select="." mode="nullable"/> )
157 </xsl:otherwise>
158 </xsl:choose>
159 </xsl:template>
160
161
162
163
164 <xsl:template match="collection" mode="JPAAnnotation">
165 @Transient
166 <xsl:comment>TODO COLLECTION</xsl:comment>
167 <!-- @Column(name="<xsl:apply-templates select="." mode="columnName"/>") -->
168 </xsl:template>
169
170
171
172
173
174
175
176 <xsl:template name="enumPattern">
177 <xsl:param name="columnName"/>
178
179 @Basic(optional=<xsl:apply-templates select="." mode="nullable"/>)
180 @Enumerated(EnumType.STRING)
181 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/> )
182 </xsl:template>
183
184
185
186
187 <xsl:template name="hashcode_equals">
188 <xsl:param name="name"/>
189
190 /**
191 * Returns equals from id attribute here. Child classes can override this method to allow deep equals with
192 * attributes / references / collections
193 *
194 * @param object the reference object with which to compare.
195 * @param isDeep true means to call hashCode(sb, true) for all attributes / references / collections which are
196 * MetadataElement implementations
197 *
198 * @return &lt;code&gt;true&lt;/code&gt; if this object is the same as the obj argument; &lt;code&gt;false&lt;/code&gt; otherwise.
199 */
200 @Override
201 public boolean equals(final Object object, final boolean isDeep) {
202 // first : identity comparison :
203 if (this == object) {
204 return true;
205 }
206
207 if (!(object instanceof <xsl:value-of select="$name"/>)) {
208 return false;
209 }
210 final <xsl:value-of select="$name"/> other = (<xsl:value-of select="$name"/>) object;
211 if (this.id != other.id
212 &amp;&amp; (this.id == null || !this.id.equals(other.id))) {
213 return false;
214 }
215
216 return true;
217 }
218 </xsl:template>
219
220
221
222
223 <!-- persistence.xml configuration file -->
224
225 <xsl:template match="model" mode="jpaConfig">
226 <xsl:variable name="file" select="'META-INF/persistence.xml'"/>
227
228 <!-- reading persistence-template.xml file : -->
229
230 <xsl:variable name="jpaConf" select="document('./persistence.xml')"/>
231
232 <!-- open file for global jpa configuration -->
233 <xsl:message >Opening file <xsl:value-of select="$file"/></xsl:message>
234 <xsl:result-document href="{$file}" format="persistenceInfo">
235
236 <xsl:apply-templates select="$jpaConf" mode="otherXml">
237 <xsl:with-param name="model" select="."/>
238 </xsl:apply-templates>
239
240 </xsl:result-document>
241 </xsl:template>
242
243
244
245
246 <xsl:template match="@*|node()" mode="otherXml">
247 <xsl:param name="model"/>
248
249 <xsl:choose>
250 <xsl:when test="name() = 'properties'">
251 &cr;
252 &cr;
253 <xsl:comment>generated JPA entities</xsl:comment>
254 &cr;
255 &cr;
256 <xsl:for-each select="$model/package">
257 <xsl:call-template name="packageJpaConfig">
258 <xsl:with-param name="package" select="."/>
259 <xsl:with-param name="path" select="$root_package"/>
260 </xsl:call-template>
261 </xsl:for-each>
262 &cr;
263 &cr;
264 <xsl:comment>JPA Properties</xsl:comment>
265 &cr;
266 &cr;
267 <xsl:element name="properties" namespace="http://java.sun.com/xml/ns/persistence">
268
269 <xsl:apply-templates select="child::*" mode="otherXml"/>
270
271 &cr;
272 &cr;
273 <xsl:comment>
274 &lt;property name="eclipseLink.cache.type.Cardinality" value="Full"/&gt;
275 &lt;property name="eclipseLink.cache.type.DataType" value="Full"/&gt;
276 </xsl:comment>
277
278 </xsl:element>
279
280 </xsl:when>
281 <xsl:otherwise>
282 <xsl:copy>
283 <xsl:apply-templates select="@*|node()" mode="otherXml">
284 <xsl:with-param name="model" select="$model"/>
285 </xsl:apply-templates>
286 </xsl:copy>
287 </xsl:otherwise>
288 </xsl:choose>
289
290 </xsl:template>
291
292
293
294
295 <xsl:template name="packageJpaConfig">
296 <xsl:param name="package"/>
297 <xsl:param name="path"/>
298
299 <xsl:variable name="newpath">
300 <xsl:choose>
301 <xsl:when test="$path">
302 <xsl:value-of select="concat($path,'.',$package/name)"/>
303 </xsl:when>
304 <xsl:otherwise>
305 <xsl:value-of select="$package/name"/>
306 </xsl:otherwise>
307 </xsl:choose>
308 </xsl:variable>
309
310 <xsl:message>package = <xsl:value-of select="$newpath"></xsl:value-of></xsl:message>
311
312 <xsl:for-each select="$package/objectType|$package/dataType">
313 <xsl:element name="class" namespace="http://java.sun.com/xml/ns/persistence">
314 <xsl:value-of select="$newpath"/><xsl:text>.</xsl:text><xsl:value-of select="name"/>
315 </xsl:element>
316 </xsl:for-each>
317
318 <xsl:for-each select="$package/package">
319 <xsl:call-template name="packageJpaConfig">
320 <xsl:with-param name="package" select="."/>
321 <xsl:with-param name="path" select="$newpath"/>
322 </xsl:call-template>
323 </xsl:for-each>
324 </xsl:template>
325
326
327 </xsl:stylesheet>

msdemlei@ari.uni-heidelberg.de
ViewVC Help
Powered by ViewVC 1.1.26