/[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 523 - (show annotations)
Mon May 19 16:41:03 2008 UTC (12 years, 6 months ago) by bourges.laurent
File MIME type: application/xml
File size: 12640 byte(s)
- added JAXBFactory to have a simple API to deal with JAXB
- JPA log level in jpa-config.properties
- less debug logs
- fixed equals methods in data types / Object types (+ isDeep)
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:apply-templates select="$className"/>" ) <!-- TODO decide whether this should be a path -->
62 </xsl:if>
63
64 @NamedQueries( {
65 @NamedQuery( name = "<xsl:apply-templates select="$className"/>.findById", query = "SELECT o FROM <xsl:apply-templates select="$className"/> o WHERE o.id = :id")
66 <xsl:if test="$hasName = 1">
67 , @NamedQuery( name = "<xsl:apply-templates select="$className"/>.findByName", query = "SELECT o FROM <xsl:apply-templates select="$className"/> o WHERE o.name = :name")
68 </xsl:if>
69 } )
70 </xsl:template>
71
72
73
74
75
76 <xsl:template match="objectType|dataType" mode="JPASpecials">
77 <xsl:param name="hasChild"/>
78 <xsl:param name="hasExtends"/>
79
80 <xsl:if test="name() = 'objectType' and $hasExtends = 0 and $hasChild = 1">
81 @Column(name = "<xsl:value-of select="$discriminatorColumnName"/>", insertable = false, updatable = false, nullable = false)
82 protected String classType;
83 </xsl:if>
84
85 <xsl:if test="name() = 'objectType' and $hasExtends = 0">
86 @Version
87 @Column( name = "OPTLOCK" )
88 protected Integer jpaVersion;
89 </xsl:if>
90
91 <xsl:if test="container">
92 <xsl:variable name="type"><xsl:call-template name="JavaType"><xsl:with-param name="xmiid" select="container/@xmiidref"/></xsl:call-template></xsl:variable>
93 @ManyToOne( cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH } )
94 @JoinColumn(name = "containerId", referencedColumnName = "id", nullable = false )
95 protected <xsl:value-of select="$type"/> container;
96 </xsl:if>
97 </xsl:template>
98
99
100
101
102 <xsl:template match="dataType" mode="JPAAnnotation">
103 <xsl:text>@Embeddable</xsl:text>&cr;
104 </xsl:template>
105
106
107
108
109 <!-- temlate attribute : adds JPA annotations for primitive types, data types & enumerations -->
110 <xsl:template match="attribute" mode="JPAAnnotation">
111 <xsl:variable name="type" select="key('element', datatype/@xmiidref)"/>
112
113 <xsl:choose>
114 <xsl:when test="name($type) = 'primitiveType'">
115 <xsl:choose>
116 <xsl:when test="number(constraints/maxLength) = -1">
117 @Basic(fetch = FetchType.LAZY, optional = <xsl:apply-templates select="." mode="nullable"/>)
118 @Lob
119 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/> )
120 </xsl:when>
121 <xsl:when test="number(constraints/maxLength) > 0">
122 @Basic( optional = <xsl:apply-templates select="." mode="nullable"/> )
123 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/>, length = <xsl:value-of select="constraints/maxLength"/> )
124 </xsl:when>
125 <xsl:when test="$type/name = 'datetime'">
126 @Basic( optional = <xsl:apply-templates select="." mode="nullable"/> )
127 @Temporal( TemporalType.TIMESTAMP )
128 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/> )
129 </xsl:when>
130 <xsl:otherwise>
131 @Basic( optional = <xsl:apply-templates select="." mode="nullable"/> )
132 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/> )
133 </xsl:otherwise>
134 </xsl:choose>
135 </xsl:when>
136 <xsl:when test="name($type) = 'enumeration'">
137 <xsl:call-template name="enumPattern">
138 <xsl:with-param name="columnName"><xsl:apply-templates select="." mode="columnName"/></xsl:with-param>
139 </xsl:call-template>
140 </xsl:when>
141 <xsl:when test="name($type) = 'dataType'">
142 <xsl:variable name="columns">
143 <xsl:apply-templates select="." mode="columns"/>
144 </xsl:variable>
145 @Embedded
146 @AttributeOverrides ( {
147 <xsl:for-each select="exsl:node-set($columns)/column">
148 @AttributeOverride( name = "<xsl:value-of select="attrname"/>", column = @Column( name = "<xsl:value-of select="name"/>" ) )
149 <xsl:if test="position() != last()"><xsl:text>,</xsl:text></xsl:if>
150 </xsl:for-each>
151 } )
152 </xsl:when>
153 <xsl:otherwise>
154 [NOT_SUPPORTED_ATTRIBUTE]
155 </xsl:otherwise>
156 </xsl:choose>
157 </xsl:template>
158
159
160
161
162 <xsl:template match="attribute|reference|collection" mode="nullable">
163 <xsl:choose>
164 <xsl:when test="starts-with(multiplicity, '0')">true</xsl:when>
165 <xsl:otherwise>false</xsl:otherwise>
166 </xsl:choose>
167 </xsl:template>
168
169
170
171
172 <xsl:template match="reference" mode="JPAAnnotation">
173 <xsl:variable name="type" select="key('element', datatype/@xmiidref)"/>
174
175 <xsl:choose>
176 <xsl:when test="name($type) = 'primitiveType' or name($type) = 'enumeration'">
177 [NOT_SUPPORTED_REFERENCE]
178 </xsl:when>
179 <xsl:otherwise>
180 <!-- do not remove referenced entity : do not cascade delete -->
181 @ManyToOne( cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH } )
182 @JoinColumn(name = "<xsl:apply-templates select="." mode="columnName"/>", referencedColumnName = "id", nullable = <xsl:apply-templates select="." mode="nullable"/> )
183 </xsl:otherwise>
184 </xsl:choose>
185 </xsl:template>
186
187 <xsl:template match="reference" mode="JPAAnnotation_identity">
188 @Transient
189 </xsl:template>
190
191
192
193
194 <xsl:template match="collection" mode="JPAAnnotation">
195 <xsl:variable name="type" select="key('element', datatype/@xmiidref)"/>
196
197 <xsl:choose>
198 <xsl:when test="name($type) = 'primitiveType' or name($type) = 'enumeration' or name($type) = 'dataType'">
199 [NOT_SUPPORTED_COLLECTION]
200 </xsl:when>
201 <xsl:otherwise>
202 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="container")
203 </xsl:otherwise>
204 </xsl:choose>
205
206 <xsl:comment>TODO COLLECTION URGENT</xsl:comment>
207 </xsl:template>
208
209
210
211
212
213
214
215 <xsl:template name="enumPattern">
216 <xsl:param name="columnName"/>
217
218 @Basic(optional=<xsl:apply-templates select="." mode="nullable"/>)
219 @Enumerated(EnumType.STRING)
220 @Column( name = "<xsl:apply-templates select="." mode="columnName"/>", nullable = <xsl:apply-templates select="." mode="nullable"/> )
221 </xsl:template>
222
223
224
225
226 <xsl:template match="objectType|dataType" mode="hashcode_equals">
227 <xsl:variable name="name" select="name"/>
228
229 /**
230 * Returns equals from id attribute here. Child classes can override this method to allow deep equals with
231 * attributes / references / collections
232 *
233 * @param object the reference object with which to compare.
234 * @param isDeep true means to call hashCode(sb, true) for all attributes / references / collections which are
235 * MetadataElement implementations
236 *
237 * @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.
238 */
239 @Override
240 public boolean equals(final Object object, final boolean isDeep) {
241 /* identity, nullable, class and identifiers checks */
242 if( !(super.equals(object, isDeep)))
243 return false;
244
245 /* do check values (attributes / references / collections) */
246 <xsl:choose>
247 <xsl:when test="name() = 'dataType'">
248 if (true) {
249 </xsl:when>
250 <xsl:otherwise>
251 if (isDeep) {
252 </xsl:otherwise>
253 </xsl:choose>
254
255 final <xsl:value-of select="$name"/> other = (<xsl:value-of select="$name"/>) object;
256 <xsl:for-each select="attribute">
257 if (this.<xsl:value-of select="name"/> != other.<xsl:value-of select="name"/>
258 &amp;&amp; (this.<xsl:value-of select="name"/> == null || !this.<xsl:value-of select="name"/>.equals(other.<xsl:value-of select="name"/>))) {
259 return false;
260 }
261 </xsl:for-each>
262 }
263
264 return true;
265 }
266 </xsl:template>
267
268
269
270 <!-- persistence.xml configuration file -->
271
272 <xsl:template match="model" mode="jpaConfig">
273 <xsl:variable name="file" select="'META-INF/persistence.xml'"/>
274
275 <!-- reading persistence-template.xml file : -->
276
277 <xsl:variable name="jpaConf" select="document('./persistence.xml')"/>
278
279 <!-- open file for global jpa configuration -->
280 <xsl:message >Opening file <xsl:value-of select="$file"/></xsl:message>
281 <xsl:result-document href="{$file}" format="persistenceInfo">
282
283 <xsl:apply-templates select="$jpaConf" mode="otherXml">
284 <xsl:with-param name="model" select="."/>
285 </xsl:apply-templates>
286
287 </xsl:result-document>
288 </xsl:template>
289
290
291
292
293 <xsl:template match="@*|node()" mode="otherXml">
294 <xsl:param name="model"/>
295
296 <xsl:choose>
297 <xsl:when test="name() = 'properties'">
298 &cr;
299 &cr;
300 <xsl:comment>generated JPA entities</xsl:comment>
301 &cr;
302 &cr;
303 <xsl:for-each select="$model/package">
304 <xsl:call-template name="packageJpaConfig">
305 <xsl:with-param name="package" select="."/>
306 <xsl:with-param name="path" select="$root_package"/>
307 </xsl:call-template>
308 </xsl:for-each>
309 &cr;
310 &cr;
311 <xsl:comment>JPA Properties</xsl:comment>
312 &cr;
313 &cr;
314 <xsl:element name="properties" namespace="http://java.sun.com/xml/ns/persistence">
315
316 <xsl:apply-templates select="child::*" mode="otherXml"/>
317
318 &cr;
319 &cr;
320 <xsl:comment>
321 &lt;property name="eclipseLink.cache.type.Cardinality" value="Full"/&gt;
322 &lt;property name="eclipseLink.cache.type.DataType" value="Full"/&gt;
323 </xsl:comment>
324
325 </xsl:element>
326
327 </xsl:when>
328 <xsl:otherwise>
329 <xsl:copy>
330 <xsl:apply-templates select="@*|node()" mode="otherXml">
331 <xsl:with-param name="model" select="$model"/>
332 </xsl:apply-templates>
333 </xsl:copy>
334 </xsl:otherwise>
335 </xsl:choose>
336
337 </xsl:template>
338
339
340
341
342 <xsl:template name="packageJpaConfig">
343 <xsl:param name="package"/>
344 <xsl:param name="path"/>
345
346 <xsl:variable name="newpath">
347 <xsl:choose>
348 <xsl:when test="$path">
349 <xsl:value-of select="concat($path,'.',$package/name)"/>
350 </xsl:when>
351 <xsl:otherwise>
352 <xsl:value-of select="$package/name"/>
353 </xsl:otherwise>
354 </xsl:choose>
355 </xsl:variable>
356
357 <xsl:message>package = <xsl:value-of select="$newpath"></xsl:value-of></xsl:message>
358
359 <xsl:for-each select="$package/objectType|$package/dataType">
360 <xsl:element name="class" namespace="http://java.sun.com/xml/ns/persistence">
361 <xsl:value-of select="$newpath"/><xsl:text>.</xsl:text><xsl:value-of select="name"/>
362 </xsl:element>
363 </xsl:for-each>
364
365 <xsl:for-each select="$package/package">
366 <xsl:call-template name="packageJpaConfig">
367 <xsl:with-param name="package" select="."/>
368 <xsl:with-param name="path" select="$newpath"/>
369 </xsl:call-template>
370 </xsl:for-each>
371 </xsl:template>
372
373
374 </xsl:stylesheet>

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