Author: tchemit Date: 2013-05-30 11:49:50 +0200 (Thu, 30 May 2013) New Revision: 638 Url: http://nuiton.org/projects/sandbox/repository/revisions/638 Log: add id generator mecanism Added: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/DefaultJpaEntityIdGenerator.java nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityIdGenerator.java nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityIdGeneratorFactory.java nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/ nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/LICENSE.txt nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/README.txt nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/changelog.txt nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/goals.txt nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/pom.xml nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/postbuild.groovy nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/ nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/ nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/ nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.properties nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.zargo nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaModelValidator.java Modified: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/AbstractJpaPersistenceContext.java nuiton-jpa/nuiton-jpa-templates/src/it/magalie/pom.xml nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaEntityTransformer.java nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaMetaTransformer.java nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaPersistenceContextTransformer.java nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesGeneratorUtil.java nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesStereoTypes.java nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesTagValues.java Modified: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/AbstractJpaPersistenceContext.java =================================================================== --- nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/AbstractJpaPersistenceContext.java 2013-05-29 13:23:18 UTC (rev 637) +++ nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/AbstractJpaPersistenceContext.java 2013-05-30 09:49:50 UTC (rev 638) @@ -41,6 +41,14 @@ protected EntityTransaction entityTransaction; public AbstractJpaPersistenceContext(EntityManager entityManager) { + this(null, entityManager); + } + + public AbstractJpaPersistenceContext(JpaEntityIdGenerator idGenerator, + EntityManager entityManager) { + if (idGenerator != null) { + JpaEntityIdGeneratorFactory.setGenerator(idGenerator); + } this.entityManager = entityManager; entityTransaction = entityManager.getTransaction(); if (!entityTransaction.isActive()) { Added: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/DefaultJpaEntityIdGenerator.java =================================================================== --- nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/DefaultJpaEntityIdGenerator.java (rev 0) +++ nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/DefaultJpaEntityIdGenerator.java 2013-05-30 09:49:50 UTC (rev 638) @@ -0,0 +1,44 @@ +package org.nuiton.jpa.api; + +/* + * #%L + * Nuiton Jpa :: API + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +/** + * Default implementation of {@link JpaEntityIdGenerator}. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.1 + */ +public class DefaultJpaEntityIdGenerator implements JpaEntityIdGenerator { + + @Override + public String generate(JpaEntity entity) { + double random = Math.random(); + while (Double.toString(random).contains("E-")) { + random = Math.random(); + } + return entity.getClass().getName() + '#' + System.currentTimeMillis() + '#' + + random; + } +} Property changes on: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/DefaultJpaEntityIdGenerator.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityIdGenerator.java =================================================================== --- nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityIdGenerator.java (rev 0) +++ nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityIdGenerator.java 2013-05-30 09:49:50 UTC (rev 638) @@ -0,0 +1,36 @@ +package org.nuiton.jpa.api; + +/* + * #%L + * Nuiton Jpa :: API + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +/** + * To generate entity id. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.1 + */ +public interface JpaEntityIdGenerator { + + String generate(JpaEntity entity); +} Property changes on: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityIdGenerator.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityIdGeneratorFactory.java =================================================================== --- nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityIdGeneratorFactory.java (rev 0) +++ nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityIdGeneratorFactory.java 2013-05-30 09:49:50 UTC (rev 638) @@ -0,0 +1,48 @@ +package org.nuiton.jpa.api; + +/* + * #%L + * Nuiton Jpa :: API + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import com.google.common.base.Preconditions; + +/** + * {@link JpaEntityIdGenerator} used in generated code in entities. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.1 + */ +public class JpaEntityIdGeneratorFactory implements JpaEntityIdGenerator { + + protected static JpaEntityIdGenerator generator; + + public static void setGenerator(JpaEntityIdGenerator generator) { + JpaEntityIdGeneratorFactory.generator = generator; + } + + @Override + public String generate(JpaEntity entity) { + Preconditions.checkNotNull(generator, "No generator assigned."); + return generator.generate(entity); + } +} Property changes on: nuiton-jpa/nuiton-jpa-api/src/main/java/org/nuiton/jpa/api/JpaEntityIdGeneratorFactory.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: nuiton-jpa/nuiton-jpa-templates/src/it/magalie/pom.xml =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/it/magalie/pom.xml 2013-05-29 13:23:18 UTC (rev 637) +++ nuiton-jpa/nuiton-jpa-templates/src/it/magalie/pom.xml 2013-05-30 09:49:50 UTC (rev 638) @@ -90,7 +90,8 @@ com.franciaflex.magalie.persistence.entity </fullPackagePath> <!-- defaultPackage used for generation --> - <defaultPackage>com.franciaflex.magalie.persistence + <defaultPackage> + com.franciaflex.magalie.persistence </defaultPackage> <templates> org.nuiton.eugene.java.JavaEnumerationTransformer, Copied: nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/LICENSE.txt (from rev 636, nuiton-jpa/nuiton-jpa-templates/src/it/magalie/LICENSE.txt) =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/LICENSE.txt (rev 0) +++ nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/LICENSE.txt 2013-05-30 09:49:50 UTC (rev 638) @@ -0,0 +1,166 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + Copied: nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/README.txt (from rev 636, nuiton-jpa/nuiton-jpa-templates/src/it/magalie/README.txt) =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/README.txt (rev 0) +++ nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/README.txt 2013-05-30 09:49:50 UTC (rev 638) @@ -0,0 +1 @@ +Test jpa templates on project Magalie (see http://forge.codelutin.com/projects/franciaflex-magalie) \ No newline at end of file Copied: nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/changelog.txt (from rev 636, nuiton-jpa/nuiton-jpa-templates/src/it/magalie/changelog.txt) =================================================================== Copied: nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/goals.txt (from rev 636, nuiton-jpa/nuiton-jpa-templates/src/it/magalie/goals.txt) =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/goals.txt (rev 0) +++ nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/goals.txt 2013-05-30 09:49:50 UTC (rev 638) @@ -0,0 +1 @@ +clean compile \ No newline at end of file Copied: nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/pom.xml (from rev 636, nuiton-jpa/nuiton-jpa-templates/src/it/magalie/pom.xml) =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/pom.xml (rev 0) +++ nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/pom.xml 2013-05-30 09:49:50 UTC (rev 638) @@ -0,0 +1,119 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + #%L + EUGene :: Maven plugin + + $Id$ + $HeadURL$ + %% + Copyright (C) 2006 - 2010 CodeLutin + %% + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Lesser Public License for more details. + + You should have received a copy of the GNU General Lesser Public + License along with this program. If not, see + <http://www.gnu.org/licenses/lgpl-3.0.html>. + #L% + --> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <groupId>org.nuiton.jpa.templates.test</groupId> + <artifactId>magalie</artifactId> + <version>@pom.version@</version> + + <name>Nuiton Jpa Temlates :: Test magalie model</name> + + <properties> + + <eugeneVersion>@eugeneVersion@</eugeneVersion> + + <!-- default encoding --> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + + <!-- compiler configuration --> + <maven.compiler.source>1.6</maven.compiler.source> + <maven.compiler.target>1.6</maven.compiler.target> + + </properties> + + <dependencies> + + <dependency> + <groupId>org.nuiton.jpa</groupId> + <artifactId>nuiton-jpa-api</artifactId> + <version>@pom.version@</version> + </dependency> + + </dependencies> + + <build> + + <pluginManagement> + <plugins> + <plugin> + <groupId>org.nuiton.eugene</groupId> + <artifactId>eugene-maven-plugin</artifactId> + <version>${eugeneVersion}</version> + </plugin> + </plugins> + </pluginManagement> + + <plugins> + + <plugin> + <groupId>org.nuiton.eugene</groupId> + <artifactId>eugene-maven-plugin</artifactId> + <configuration> + <inputs>zargo</inputs> + <resolver>org.nuiton.util.FasterCachedResourceResolver</resolver> + </configuration> + <executions> + <execution> + <id>generate-entities</id> + <phase>generate-sources</phase> + <configuration> + <!-- Corresponding to extracted package from zargo file --> + <fullPackagePath> + com.franciaflex.magalie.persistence.entity + </fullPackagePath> + <!-- defaultPackage used for generation --> + <defaultPackage> + com.franciaflex.magalie.persistence + </defaultPackage> + <templates> + org.nuiton.eugene.java.JavaEnumerationTransformer, + org.nuiton.jpa.templates.JpaMetaTransformer + </templates> + </configuration> + <goals> + <goal>generate</goal> + </goals> + </execution> + </executions> + <dependencies> + <dependency> + <groupId>org.nuiton.jpa</groupId> + <artifactId>nuiton-jpa-templates</artifactId> + <version>@pom.version@</version> + </dependency> + </dependencies> + </plugin> + + </plugins> + </build> +</project> + + Copied: nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/postbuild.groovy (from rev 636, nuiton-jpa/nuiton-jpa-templates/src/it/magalie/postbuild.groovy) =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/postbuild.groovy (rev 0) +++ nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/postbuild.groovy 2013-05-30 09:49:50 UTC (rev 638) @@ -0,0 +1,39 @@ +/* + * #%L + * nuiton-jpa-templates + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +outputDir = new File(basedir, 'target/generated-sources/java'); + +file = new File(outputDir, 'com/franciaflex/magalie/persistence'); +assert file.exists(); +assert file.isDirectory(); + +file = new File(outputDir, 'com/franciaflex/magalie/persistence/entity'); +assert file.exists(); +assert file.isDirectory(); + +file = new File(outputDir, 'com/franciaflex/magalie/persistence/dao'); +assert file.exists(); +assert file.isDirectory(); + +return true; \ No newline at end of file Added: nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.properties =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.properties (rev 0) +++ nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.properties 2013-05-30 09:49:50 UTC (rev 638) @@ -0,0 +1,37 @@ +### +# #%L +# Nuiton Jpa :: Temlates +# $Id$ +# $HeadURL$ +# %% +# Copyright (C) 2013 CodeLutin +# %% +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Lesser Public License for more details. +# +# You should have received a copy of the GNU General Lesser Public +# License along with this program. If not, see +# <http://www.gnu.org/licenses/lgpl-3.0.html>. +# #L% +### +model.tagValue.idGenerator=org.nuiton.jpa.api.DefaultJpaEntityIdGenerator + +com.franciaflex.magalie.persistence.entity.Article.attribute.fixedLocations.stereotype=unique + +com.franciaflex.magalie.persistence.entity.Article.class.stereotype=notGeneratedValue +com.franciaflex.magalie.persistence.entity.Building.class.stereotype=notGeneratedValue +com.franciaflex.magalie.persistence.entity.Location.class.stereotype=notGeneratedValue +com.franciaflex.magalie.persistence.entity.MagalieUser.class.stereotype=notGeneratedValue +com.franciaflex.magalie.persistence.entity.PreparedArticleReception.class.stereotype=notGeneratedValue +com.franciaflex.magalie.persistence.entity.RequestedArticle.class.stereotype=notGeneratedValue +com.franciaflex.magalie.persistence.entity.RequestedList.class.stereotype=notGeneratedValue +com.franciaflex.magalie.persistence.entity.StoredArticle.class.stereotype=notGeneratedValue +com.franciaflex.magalie.persistence.entity.Supplier.class.stereotype=notGeneratedValue +com.franciaflex.magalie.persistence.entity.Warehouse.class.stereotype=notGeneratedValue Property changes on: nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.properties ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.zargo =================================================================== (Binary files differ) Property changes on: nuiton-jpa/nuiton-jpa-templates/src/it/magalie-with-generated-value/src/main/xmi/magalie.zargo ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: svn:keywords + Author Date Id Revision HeadURL Modified: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaEntityTransformer.java =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaEntityTransformer.java 2013-05-29 13:23:18 UTC (rev 637) +++ nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaEntityTransformer.java 2013-05-30 09:49:50 UTC (rev 638) @@ -38,6 +38,7 @@ import org.nuiton.eugene.models.object.ObjectModelOperation; import org.nuiton.eugene.models.object.xml.ObjectModelAttributeImpl; import org.nuiton.jpa.api.AbstractJpaEntity; +import org.nuiton.jpa.api.JpaEntityIdGeneratorFactory; import javax.persistence.CascadeType; import javax.persistence.Entity; @@ -51,6 +52,7 @@ import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.OrderColumn; +import javax.persistence.PrePersist; import java.util.Collection; import java.util.List; import java.util.Set; @@ -87,18 +89,25 @@ String entityConcreteName = JpaTemplatesGeneratorUtil.getEntityConcreteName(input); - boolean generateAbstract = !isInClassPath(packageName, entityAbstractName); + boolean generateAbstract = !isInClassPath(packageName, + entityAbstractName); - boolean generateConcrete = !isInClassPath(packageName, entityConcreteName); + boolean generateConcrete = !isInClassPath(packageName, + entityConcreteName); if (generateAbstract) { - generateAbstract(input, packageName, entityAbstractName); + generateAbstract(input, + packageName, + entityAbstractName); } if (generateConcrete) { - generateImpl(input, packageName, entityAbstractName, entityConcreteName); + generateImpl(input, + packageName, + entityAbstractName, + entityConcreteName); } } @@ -156,6 +165,28 @@ addSerializable(input, output, serializableFound); + String idGenerator = + JpaTemplatesGeneratorUtil.getIdGeneratorTagValue(getModel()); + + if (StringUtils.isNotEmpty(idGenerator)) { + + // use an idGenerator + + // add prepersist method + ObjectModelOperation operation = addOperation( + output, + "prepersist", + String.class, + ObjectModelJavaModifier.PUBLIC); + addAnnotation(output, operation, PrePersist.class); + addImport(output, JpaEntityIdGeneratorFactory.class); + setOperationBody(operation, "" + /*{ + return new JpaEntityIdGeneratorFactory().generate(this); + }*/ + ); + } + Set<String> constantNames = addConstantsFromDependency(input, output); // Get available properties @@ -241,7 +272,7 @@ if (PROPERTY_ID.equals(propertyName)) { addAnnotation(result, property, output, Id.class); - if (!JpaTemplatesGeneratorUtil.hasNotGeneratedValueStereotype(input)) { + if (JpaTemplatesGeneratorUtil.generateEntityGeneratedValueAnnotation(model, input)) { addAnnotation(result, property, output, GeneratedValue.class); } continue; Modified: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaMetaTransformer.java =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaMetaTransformer.java 2013-05-29 13:23:18 UTC (rev 637) +++ nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaMetaTransformer.java 2013-05-30 09:49:50 UTC (rev 638) @@ -84,8 +84,7 @@ classValidator.addNameAndReason("user", "Nom de classe incompatible avec certains SGBD"); validators.add(classValidator); -// validators.add(new TopiaJavaValidator(model)); -// validators.add(new TopiaRelationValidator(model)); + validators.add(new JpaModelValidator(model)); for (ObjectModelValidator validator : validators) { if (!validator.validate()) { Added: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaModelValidator.java =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaModelValidator.java (rev 0) +++ nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaModelValidator.java 2013-05-30 09:49:50 UTC (rev 638) @@ -0,0 +1,69 @@ +package org.nuiton.jpa.templates; + +/* + * #%L + * Nuiton Jpa :: Temlates + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2013 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.apache.commons.lang3.StringUtils; +import org.nuiton.eugene.models.object.ObjectModel; +import org.nuiton.eugene.models.object.ObjectModelClass; +import org.nuiton.eugene.models.object.ObjectModelClassifier; +import org.nuiton.eugene.models.object.validator.ObjectModelValidator; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since TODO + */ +public class JpaModelValidator extends ObjectModelValidator { + public JpaModelValidator(ObjectModel model) { + super(model); + } + + boolean useIdGenerator; + + @Override + protected boolean validateModel(ObjectModel model) { + + String idGeneratorTagValue = JpaTemplatesGeneratorUtil.getIdGeneratorTagValue(model); + useIdGenerator = StringUtils.isNotEmpty(idGeneratorTagValue); + return super.validateModel(model); + } + + @Override + protected boolean validateClass(ObjectModelClass clazz) { + if (JpaTemplatesGeneratorUtil.isEntity(clazz)) { + + if (useIdGenerator) { + + // can't have any stuff around GeneraturValue + if(JpaTemplatesGeneratorUtil.hasNotGeneratedValueStereotype(clazz)) { + + addError(clazz,"Can't use a NotGenerated tag value when using an id generator"); + } + } + } + return super.validateClass(clazz); + } +} Property changes on: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaModelValidator.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaPersistenceContextTransformer.java =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaPersistenceContextTransformer.java 2013-05-29 13:23:18 UTC (rev 637) +++ nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaPersistenceContextTransformer.java 2013-05-30 09:49:50 UTC (rev 638) @@ -24,11 +24,15 @@ * #L% */ +import org.apache.commons.lang3.StringUtils; import org.nuiton.eugene.models.object.ObjectModel; import org.nuiton.eugene.models.object.ObjectModelClass; +import org.nuiton.eugene.models.object.ObjectModelJavaModifier; import org.nuiton.eugene.models.object.ObjectModelOperation; import org.nuiton.jpa.api.AbstractJpaPersistenceContext; +import org.nuiton.jpa.api.JpaEntityIdGenerator; +import javax.persistence.EntityManager; import java.util.List; /*{generator option: parentheses = false}*/ @@ -91,8 +95,29 @@ setSuperClass(output, superClass); - addConstructorWithEntityManager(output); + String idGenerator = + JpaTemplatesGeneratorUtil.getIdGeneratorTagValue(getModel()); + if (StringUtils.isNotEmpty(idGenerator)) { + + // using an idGenerator + ObjectModelOperation constructor = addConstructor( + output, + ObjectModelJavaModifier.PUBLIC); + + addParameter(constructor, EntityManager.class, "entityManager"); + + setOperationBody(constructor, "" + /*{ + super(new <%=idGenerator%>(), entityManager); + }*/ + ); + } else { + + // not using an idGenerator + addConstructorWithEntityManager(output); + } + // add dao factories List<ObjectModelClass> entityClasses = JpaTemplatesGeneratorUtil.getEntityClasses(getModel(), true); Modified: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesGeneratorUtil.java =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesGeneratorUtil.java 2013-05-29 13:23:18 UTC (rev 637) +++ nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesGeneratorUtil.java 2013-05-30 09:49:50 UTC (rev 638) @@ -234,37 +234,36 @@ } /** - * Obtain the value of the {@link JpaTemplatesTagValues#TAG_ENTITY_SUPER_CLASS} + * Obtain the value of the {@link JpaTemplatesTagValues#TAG_ID_GENERATOR} * tag value on the given model or classifier. * <p/> * It will first look on the model, and then in the given classifier. * - * @param model model to seek - * @param classifier classifier to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see JpaTemplatesTagValues#TAG_ENTITY_SUPER_CLASS + * @param model model to seek + * @return the none empty value of the found tag value or {@code null} + * if not found nor empty. + * @see JpaTemplatesTagValues#TAG_ID_GENERATOR * @since 2.3 */ - public static String getEntitySuperClassTagValue(ObjectModel model, ObjectModelClassifier classifier) { - String value = findTagValue(JpaTemplatesTagValues.TAG_ENTITY_SUPER_CLASS, classifier, model); + public static String getIdGeneratorTagValue(ObjectModel model) { + String value = findTagValue(JpaTemplatesTagValues.TAG_ID_GENERATOR, null, model); return value; } /** - * Obtain the value of the {@link JpaTemplatesTagValues#TAG_ENTITY_PACKAGE} - * tag value on the given model. + * Obtain the value of the {@link JpaTemplatesTagValues#TAG_ENTITY_SUPER_CLASS} + * tag value on the given model or classifier. * <p/> * It will first look on the model, and then in the given classifier. * * @param model model to seek * @param classifier classifier to seek * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see JpaTemplatesTagValues#TAG_ENTITY_PACKAGE + * @see JpaTemplatesTagValues#TAG_ENTITY_SUPER_CLASS * @since 2.3 */ - public static String getEntityPackageTagValue(ObjectModel model, - ObjectModelClassifier classifier) { - String value = findTagValue(JpaTemplatesTagValues.TAG_ENTITY_PACKAGE, classifier, model); + public static String getEntitySuperClassTagValue(ObjectModel model, ObjectModelClassifier classifier) { + String value = findTagValue(JpaTemplatesTagValues.TAG_ENTITY_SUPER_CLASS, classifier, model); return value; } @@ -286,24 +285,6 @@ } /** - * Obtain the value of the {@link JpaTemplatesTagValues#TAG_DAO_PACKAGE} - * tag value on the given model. - * <p/> - * It will first look on the model, and then in the given classifier. - * - * @param model model to seek - * @param classifier classifier to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see JpaTemplatesTagValues#TAG_DAO_PACKAGE - * @since 2.3 - */ - public static String getDaoPackageTagValue(ObjectModel model, - ObjectModelClassifier classifier) { - String value = findTagValue(JpaTemplatesTagValues.TAG_DAO_PACKAGE, classifier, model); - return value; - } - - /** * Obtain the value of the {@link JpaTemplatesTagValues#TAG_PERSISTENCE_CONTEXT_SUPER_CLASS} * tag value on the given model. * @@ -318,21 +299,6 @@ } /** - * Obtain the value of the {@link JpaTemplatesTagValues#TAG_PERSISTENCE_CONTEXT_PACKAGE} - * tag value on the given model. - * <p/> - * - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see JpaTemplatesTagValues#TAG_PERSISTENCE_CONTEXT_PACKAGE - * @since 2.3 - */ - public static String getPersistenceContextPackageTagValue(ObjectModel model) { - String value = findTagValue(JpaTemplatesTagValues.TAG_PERSISTENCE_CONTEXT_PACKAGE, null, model); - return value; - } - - /** * Obtain the value of the {@link JpaTemplatesTagValues#TAG_INVERSE} * tag value on the given attribute. * <p/> @@ -360,6 +326,18 @@ return attribute.hasStereotype(JpaTemplatesStereoTypes.STEREOTYPE_UNIQUE); } + public static boolean generateEntityGeneratedValueAnnotation(ObjectModel model, + ObjectModelClass clazz) { + String idGeneratorTagValue = getIdGeneratorTagValue(model); + boolean result = idGeneratorTagValue == null; + if (result) { + + // check entity is not marked to not generate value + result = !hasNotGeneratedValueStereotype(clazz); + } + return result; + } + /** * Check if the given class has the * {@link JpaTemplatesStereoTypes#STEREOTYPE_NOT_GENERATED_VALUE} stereotype. @@ -369,7 +347,7 @@ * @see JpaTemplatesStereoTypes#STEREOTYPE_NOT_GENERATED_VALUE * @since 2.5 */ - public static boolean hasNotGeneratedValueStereotype(ObjectModelClass clazz) { + public static boolean hasNotGeneratedValueStereotype(ObjectModelClassifier clazz) { return clazz.hasStereotype(JpaTemplatesStereoTypes.STEREOTYPE_NOT_GENERATED_VALUE); } } Modified: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesStereoTypes.java =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesStereoTypes.java 2013-05-29 13:23:18 UTC (rev 637) +++ nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesStereoTypes.java 2013-05-30 09:49:50 UTC (rev 638) @@ -28,6 +28,7 @@ import org.nuiton.eugene.ModelPropertiesUtil; import org.nuiton.eugene.models.object.ObjectModelAttribute; import org.nuiton.eugene.models.object.ObjectModelClass; +import org.nuiton.eugene.models.object.ObjectModelClassifier; import javax.persistence.GeneratedValue; @@ -49,11 +50,11 @@ String STEREOTYPE_UNIQUE = "unique"; /** - * Stéréotype pour ne pas générer l' annotation {@link GeneratedValue} sur une entité. + * Stéréotype pour ne pas générer l'annotation {@link GeneratedValue} sur une entité. * - * @see JpaTemplatesGeneratorUtil#hasNotGeneratedValueStereotype(ObjectModelAttribute) + * @see JpaTemplatesGeneratorUtil#hasNotGeneratedValueStereotype(ObjectModelClassifier) */ - @ModelPropertiesUtil.StereotypeDefinition(target = ObjectModelClass.class, + @ModelPropertiesUtil.StereotypeDefinition(target = ObjectModelClassifier.class, documentation = "To specify that an entity deal by itself with his id, the GeneratedValue annotation will not be generated then(JPA mapping)") String STEREOTYPE_NOT_GENERATED_VALUE = "notGeneratedValue"; } Modified: nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesTagValues.java =================================================================== --- nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesTagValues.java 2013-05-29 13:23:18 UTC (rev 637) +++ nuiton-jpa/nuiton-jpa-templates/src/main/java/org/nuiton/jpa/templates/JpaTemplatesTagValues.java 2013-05-30 09:49:50 UTC (rev 638) @@ -51,17 +51,6 @@ String TAG_ENTITY_SUPER_CLASS = "entitySuperClass"; /** - * Tag value to specify where to generate entities. - * - * @see JpaTemplatesGeneratorUtil#getEntityPackageTagValue(ObjectModel, ObjectModelClassifier) - * @since 0.1 - */ - @ModelPropertiesUtil.TagValueDefinition( - target = {ObjectModel.class}, - documentation = "To specify the package where to generate entities") - String TAG_ENTITY_PACKAGE = "entityPackage"; - - /** * Tag value to use a super class for generated bean. * * @see JpaTemplatesGeneratorUtil#getDaoSuperClassTagValue(ObjectModel, ObjectModelClassifier) @@ -74,17 +63,6 @@ String TAG_DAO_SUPER_CLASS = "daoSuperClass"; /** - * Tag value to specify where to generate daos. - * - * @see JpaTemplatesGeneratorUtil#getDaoPackageTagValue(ObjectModel, ObjectModelClassifier) - * @since 0.1 - */ - @ModelPropertiesUtil.TagValueDefinition( - target = {ObjectModel.class}, - documentation = "To specify the package where to generate daos") - String TAG_DAO_PACKAGE = "daoPackage"; - - /** * Tag value to use a super class for generated bean. * * @see JpaTemplatesGeneratorUtil#getDaoSuperClassTagValue(ObjectModel, ObjectModelClassifier) @@ -96,18 +74,6 @@ String TAG_PERSISTENCE_CONTEXT_SUPER_CLASS = "persistenceContextSuperClass"; /** - * Tag value to specify where to generate persistenceContext. - * - * @see JpaTemplatesGeneratorUtil#getPersistenceContextPackageTagValue(ObjectModel) - * @since 0.1 - */ - @ModelPropertiesUtil.TagValueDefinition( - target = {ObjectModel.class}, - documentation = "To specify the package where to generate persistenceContexts") - String TAG_PERSISTENCE_CONTEXT_PACKAGE = "persistenceContextPackage"; - - - /** * Tag pour permettre de choisir qui contrôle la relation N-N * bidirectionnelle. On a inverse=true sur le père de la relation.. * <p/> @@ -120,4 +86,15 @@ @ModelPropertiesUtil.TagValueDefinition(target = {ObjectModelAttribute.class}, documentation = "Sets which part of a N-N relation is master (inverse=true) and slave (inverse=false) (must be put on each side on a such relation) (Hibernate mapping)") String TAG_INVERSE = "inverse"; + + /** + * Tag value to get user defined id generator. + * + * @see JpaTemplatesGeneratorUtil#getIdGeneratorTagValue(ObjectModel) + * @since 0.1 + */ + @ModelPropertiesUtil.TagValueDefinition( + target = {ObjectModel.class}, + documentation = "To specify a user defined id generator to use for all entities of the model") + String TAG_ID_GENERATOR = "idGenerator"; }