/[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 690 - (show annotations)
Thu Jul 10 15:27:40 2008 UTC (12 years, 4 months ago) by bourges.laurent
File MIME type: application/xml
File size: 13306 byte(s)
added rank support for collections
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 This XSLT is used by intermediate2java.xsl to generate JPA annotations and JPA specific java code.
15
16 Java 1.5+ is required by JPA 1.0.
17 -->
18
19 <!-- common DDL templates used -->
20 <xsl:import href="common-ddl.xsl"/>
21
22 <xsl:output name="persistenceInfo" method="xml" encoding="UTF-8" indent="yes" />
23
24
25 <xsl:key name="element" match="*//*" use="@xmiid"/>
26
27
28
29 <xsl:template match="objectType" mode="JPAAnnotation">
30 <xsl:variable name="className" select="name" />
31 <xsl:variable name="xmiid" select="@xmiid" />
32 <xsl:variable name="hasChild">
33 <xsl:choose>
34 <xsl:when test="count(//extends[@xmiidref = $xmiid]) > 0">1</xsl:when>
35 <xsl:otherwise>0</xsl:otherwise>
36 </xsl:choose>
37 </xsl:variable>
38 <xsl:variable name="extMod">
39 <xsl:choose>
40 <xsl:when test="count(extends) = 1">1</xsl:when>
41 <xsl:otherwise>0</xsl:otherwise>
42 </xsl:choose>
43 </xsl:variable>
44 <xsl:variable name="hasName">
45 <xsl:choose>
46 <xsl:when test="count(attribute[name = 'name']) > 0">1</xsl:when>
47 <xsl:otherwise>0</xsl:otherwise>
48 </xsl:choose>
49 </xsl:variable>
50
51 @Entity
52 @Table( name = "<xsl:apply-templates select="." mode="tableName"/>" )
53 <xsl:if test="$hasChild = 1">
54 &cr;
55 <!-- JOINED strategy for inheritance -->
56 @Inheritance( strategy = InheritanceType.JOINED )
57 @DiscriminatorColumn( name = "<xsl:value-of select="$discriminatorColumnName"/>", discriminatorType = DiscriminatorType.STRING, length = <xsl:value-of select="$discriminatorColumnLength"/>)
58 &cr;
59 </xsl:if>
60 <xsl:if test="$extMod = 1">
61 @DiscriminatorValue( "<xsl:value-of select="$className"/>" ) <!-- TODO decide whether this should be a path -->
62 </xsl:if>
63
64 @NamedQueries( {
65 @NamedQuery( name = "<xsl:value-of select="$className"/>.findById", query = "SELECT o FROM <xsl:value-of select="$className"/> o WHERE o.id = :id"),
66 @NamedQuery( name = "<xsl:value-of select="$className"/>.findByXmlId", query = "SELECT o FROM <xsl:value-of select="$className"/> o WHERE o.identity.xmlId = :xmlId"),
67 @NamedQuery( name = "<xsl:value-of select="$className"/>.findByIvoId", query = "SELECT o FROM <xsl:value-of select="$className"/> o WHERE o.identity.ivoId = :ivoId")
68
69 <xsl:if test="$hasName = 1">
70 , @NamedQuery( name = "<xsl:value-of select="$className"/>.findByName", query = "SELECT o FROM <xsl:value-of select="$className"/> o WHERE o.name = :name")
71 </xsl:if>
72 } )
73 </xsl:template>
74
75
76
77
78
79 <xsl:template match="objectType|dataType" mode="JPASpecials">
80 <xsl:param name="hasChild"/>
81 <xsl:param name="hasExtends"/>
82
83 <xsl:if test="name() = 'objectType' and $hasExtends = 0 and $hasChild = 1">
84 /** classType gives the discriminator value stored in the database for an inheritance hierarchy */
85 @Column( name = "<xsl:value-of select="$discriminatorColumnName"/>", insertable = false, updatable = false, nullable = false )
86 protected String classType;
87 </xsl:if>
88
89 <xsl:if test="name() = 'objectType' and $hasExtends = 0">
90 /** jpaVersion gives the current version number for that entity (used by pessimistic / optimistic locking in JPA) */
91 @Version()
92 @Column( name = "OPTLOCK" )
93 protected int jpaVersion;
94 </xsl:if>
95
96 <xsl:if test="container">
97 <xsl:variable name="type"><xsl:call-template name="JavaType"><xsl:with-param name="xmiid" select="container/@xmiidref"/></xsl:call-template></xsl:variable>
98 /** container gives the parent entity which owns a collection containing instances of this class */
99 @ManyToOne( cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH } )
100 @JoinColumn( name = "containerId", referencedColumnName = "id", nullable = false )
101 protected <xsl:value-of select="$type"/> container;
102
103 /** rank : position in the container collection */
104 @Basic( optional = false )
105 @Column( name = "rank", nullable = false )
106 protected int rank = 0;
107 </xsl:if>
108 </xsl:template>
109
110
111
112
113 <xsl:template match="dataType" mode="JPAAnnotation">
114 <xsl:text>@Embeddable</xsl:text>&cr;
115 </xsl:template>
116
117
118
119
120 <!-- temlate attribute : adds JPA annotations for primitive types, data types & enumerations -->
121 <xsl:template match="attribute" mode="JPAAnnotation">
122 <xsl:variable name="type" select="key('element', datatype/@xmiidref)"/>
123
124 <xsl:choose>
125 <xsl:when test="name($type) = 'primitiveType'">
126 <xsl:choose>
127 <xsl:when test="number(constraints/maxLength) = -1">
128 @Basic( fetch = FetchType.LAZY, optional = <xsl:apply-templates select="." mode="nullable"/> )
129 @Lob
130 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/> )
131 </xsl:when>
132 <xsl:when test="number(constraints/maxLength) > 0">
133 @Basic( optional = <xsl:apply-templates select="." mode="nullable"/> )
134 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/>, length = <xsl:value-of select="constraints/maxLength"/> )
135 </xsl:when>
136 <xsl:when test="$type/name = 'datetime'">
137 @Basic( optional = <xsl:apply-templates select="." mode="nullable"/> )
138 @Temporal( TemporalType.TIMESTAMP )
139 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/> )
140 </xsl:when>
141 <xsl:otherwise>
142 @Basic( optional = <xsl:apply-templates select="." mode="nullable"/> )
143 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/> )
144 </xsl:otherwise>
145 </xsl:choose>
146 </xsl:when>
147 <xsl:when test="name($type) = 'enumeration'">
148 <xsl:call-template name="enumPattern">
149 <xsl:with-param name="columnName"><xsl:apply-templates select="." mode="columnName"/></xsl:with-param>
150 </xsl:call-template>
151 </xsl:when>
152 <xsl:when test="name($type) = 'dataType'">
153 <xsl:variable name="columns">
154 <xsl:apply-templates select="." mode="columns"/>
155 </xsl:variable>
156 @Embedded
157 @AttributeOverrides ( {
158 <xsl:for-each select="exsl:node-set($columns)/column">
159 @AttributeOverride( name = "<xsl:value-of select="attrname"/>", column = @Column( name = "<xsl:value-of select="name"/>" ) )
160 <xsl:if test="position() != last()"><xsl:text>,</xsl:text></xsl:if>
161 </xsl:for-each>
162 } )
163 </xsl:when>
164 <xsl:otherwise>
165 [NOT_SUPPORTED_ATTRIBUTE]
166 </xsl:otherwise>
167 </xsl:choose>
168 </xsl:template>
169
170
171
172
173 <xsl:template match="attribute|reference|collection" mode="nullable">
174 <xsl:choose>
175 <xsl:when test="starts-with(multiplicity, '0')">true</xsl:when>
176 <xsl:otherwise>false</xsl:otherwise>
177 </xsl:choose>
178 </xsl:template>
179
180
181
182
183 <xsl:template match="reference" mode="JPAAnnotation">
184 <xsl:variable name="type" select="key('element', datatype/@xmiidref)"/>
185
186 <xsl:choose>
187 <xsl:when test="name($type) = 'primitiveType' or name($type) = 'enumeration'">
188 [NOT_SUPPORTED_REFERENCE]
189 </xsl:when>
190 <xsl:otherwise>
191 <!-- do not remove referenced entity : do not cascade delete -->
192 @ManyToOne( cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH } )
193 @JoinColumn( name = "<xsl:apply-templates select="." mode="columnName"/>", referencedColumnName = "id", nullable = <xsl:apply-templates select="." mode="nullable"/> )
194 </xsl:otherwise>
195 </xsl:choose>
196 </xsl:template>
197
198
199
200
201 <xsl:template match="reference" mode="JPAAnnotation_reference">
202 @Transient
203 </xsl:template>
204
205
206
207
208 <xsl:template match="collection" mode="JPAAnnotation">
209 <xsl:variable name="type" select="key('element', datatype/@xmiidref)"/>
210
211 <xsl:choose>
212 <xsl:when test="name($type) = 'primitiveType' or name($type) = 'enumeration' or name($type) = 'dataType'">
213 [NOT_SUPPORTED_COLLECTION]
214 </xsl:when>
215 <xsl:otherwise>
216 @OrderBy( value = "rank" )
217 @OneToMany( cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="container" )
218 </xsl:otherwise>
219 </xsl:choose>
220 </xsl:template>
221
222
223
224
225 <xsl:template name="enumPattern">
226 <xsl:param name="columnName"/>
227
228 @Basic( optional=<xsl:apply-templates select="." mode="nullable"/> )
229 @Enumerated( EnumType.STRING )
230 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/> )
231 </xsl:template>
232
233
234
235
236 <xsl:template match="objectType|dataType" mode="hashcode_equals">
237 <xsl:variable name="name" select="name"/>
238
239 /**
240 * Returns equals from id attribute here. Child classes can override this method to allow deep equals with
241 * attributes / references / collections
242 *
243 * @param object the reference object with which to compare.
244 * @param isDeep true means to call hashCode(sb, true) for all attributes / references / collections which are
245 * MetadataElement implementations
246 *
247 * @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.
248 */
249 @Override
250 public boolean equals(final Object object, final boolean isDeep) {
251 /* identity, nullable, class and identifiers checks */
252 if( !(super.equals(object, isDeep))) {
253 return false;
254 }
255
256 /* do check values (attributes / references / collections) */
257 <xsl:choose>
258 <xsl:when test="name() = 'dataType'">
259 if (true) {
260 </xsl:when>
261 <xsl:otherwise>
262 if (isDeep) {
263 </xsl:otherwise>
264 </xsl:choose>
265
266 final <xsl:value-of select="$name"/> other = (<xsl:value-of select="$name"/>) object;
267 <xsl:for-each select="attribute">
268 if (! areEquals(this.<xsl:value-of select="name"/>, other.<xsl:value-of select="name"/>)) {
269 return false;
270 }
271 </xsl:for-each>
272 }
273
274 return true;
275 }
276 </xsl:template>
277
278
279
280 <!-- persistence.xml configuration file -->
281
282 <xsl:template match="model" mode="jpaConfig">
283 <xsl:variable name="file" select="'META-INF/persistence.xml'"/>
284
285 <!-- reading persistence-template.xml file : -->
286
287 <xsl:variable name="jpaConf" select="document('./persistence.xml')"/>
288
289 <!-- open file for global jpa configuration -->
290 <xsl:message >Opening file <xsl:value-of select="$file"/></xsl:message>
291 <xsl:result-document href="{$file}" format="persistenceInfo">
292
293 <xsl:apply-templates select="$jpaConf" mode="otherXml">
294 <xsl:with-param name="model" select="."/>
295 </xsl:apply-templates>
296
297 </xsl:result-document>
298 </xsl:template>
299
300
301
302
303 <xsl:template match="@*|node()" mode="otherXml">
304 <xsl:param name="model"/>
305
306 <xsl:choose>
307 <xsl:when test="name() = 'properties'">
308 &cr;
309 &cr;
310 <xsl:comment>generated JPA entities</xsl:comment>
311 &cr;
312 &cr;
313
314 <xsl:for-each select="$model/package">
315 <xsl:call-template name="packageJpaConfig">
316 <xsl:with-param name="package" select="."/>
317 <xsl:with-param name="path" select="$root_package"/>
318 </xsl:call-template>
319 </xsl:for-each>
320 &cr;
321 &cr;
322 <xsl:comment>JPA Properties</xsl:comment>
323 &cr;
324 &cr;
325 <xsl:element name="properties" namespace="http://java.sun.com/xml/ns/persistence">
326
327 <xsl:apply-templates select="child::*" mode="otherXml"/>
328
329 &cr;
330 &cr;
331 <xsl:comment>
332 &lt;property name="eclipseLink.cache.type.Cardinality" value="Full"/&gt;
333 &lt;property name="eclipseLink.cache.type.DataType" value="Full"/&gt;
334 </xsl:comment>
335
336 </xsl:element>
337
338 </xsl:when>
339 <xsl:otherwise>
340 <xsl:copy>
341 <xsl:apply-templates select="@*|node()" mode="otherXml">
342 <xsl:with-param name="model" select="$model"/>
343 </xsl:apply-templates>
344 </xsl:copy>
345 </xsl:otherwise>
346 </xsl:choose>
347
348 </xsl:template>
349
350
351
352
353 <xsl:template name="packageJpaConfig">
354 <xsl:param name="package"/>
355 <xsl:param name="path"/>
356
357 <xsl:variable name="newpath">
358 <xsl:choose>
359 <xsl:when test="$path">
360 <xsl:value-of select="concat($path,'.',$package/name)"/>
361 </xsl:when>
362 <xsl:otherwise>
363 <xsl:value-of select="$package/name"/>
364 </xsl:otherwise>
365 </xsl:choose>
366 </xsl:variable>
367
368 <xsl:message>package = <xsl:value-of select="$newpath"></xsl:value-of></xsl:message>
369
370 <xsl:for-each select="$package/objectType|$package/dataType">
371 <xsl:element name="class" namespace="http://java.sun.com/xml/ns/persistence">
372 <xsl:value-of select="$newpath"/><xsl:text>.</xsl:text><xsl:value-of select="name"/>
373 </xsl:element>
374 </xsl:for-each>
375
376 <xsl:for-each select="$package/package">
377 <xsl:call-template name="packageJpaConfig">
378 <xsl:with-param name="package" select="."/>
379 <xsl:with-param name="path" select="$newpath"/>
380 </xsl:call-template>
381 </xsl:for-each>
382 </xsl:template>
383
384
385 </xsl:stylesheet>

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