diff --git a/TopModel.Generator.Jpa/JpaConfig.cs b/TopModel.Generator.Jpa/JpaConfig.cs index 1327b233..a08157d3 100644 --- a/TopModel.Generator.Jpa/JpaConfig.cs +++ b/TopModel.Generator.Jpa/JpaConfig.cs @@ -99,6 +99,16 @@ public class JpaConfig : GeneratorConfigBase /// public bool UseJdbc { get; set; } = false; + /// + /// Génération d'interface Abstract avec @NoRepositoryBean permettant de mettre à jour le code généré. + /// + public bool DaosAbstract { get; set; } = false; + + /// + /// Précise l'interface des Daos générés. + /// + public string? DaosInterface { get; set; } + /// /// Indique s'il faut ajouter les mappers en tant méthode ou constructeur dans les classes qui les déclarent. /// diff --git a/TopModel.Generator.Jpa/JpaDaoGenerator.cs b/TopModel.Generator.Jpa/JpaDaoGenerator.cs index 85a64892..337b631c 100644 --- a/TopModel.Generator.Jpa/JpaDaoGenerator.cs +++ b/TopModel.Generator.Jpa/JpaDaoGenerator.cs @@ -26,16 +26,17 @@ protected override bool FilterClass(Class classe) protected override string GetFileName(Class classe, string tag) { + string path = Config.DaosAbstract ? $"Abstract{classe.NamePascal}DAO.java" : $"{classe.NamePascal}DAO.java"; return Path.Combine( Config.OutputDirectory, Config.ResolveVariables(Config.DaosPath!, tag, module: classe.Namespace.Module).ToFilePath(), - $"{classe.NamePascal}DAO.java"); + path); } protected override void HandleClass(string fileName, Class classe, string tag) { // Ne génère le DAO qu'une seule fois - if (File.Exists(fileName)) + if (!Config.DaosAbstract && File.Exists(fileName)) { return; } @@ -64,7 +65,32 @@ protected override void HandleClass(string fileName, Class classe, string tag) pk = classe.PrimaryKey.Count() > 1 ? $"{classe.NamePascal}.{classe.NamePascal}Id" : Config.GetType(classe.PrimaryKey.Single()); } - fw.WriteLine($"public interface {classe.NamePascal}DAO extends {(classe.Reference || Config.UseJdbc ? "CrudRepository" : "JpaRepository")}<{classe.NamePascal}, {pk}> {{"); + string daosInterface; + if (Config.DaosInterface != null) + { + int lastIndexOf = Config.DaosInterface.LastIndexOf("."); + string daosInterfaceName = lastIndexOf > -1 ? Config.DaosInterface.Substring(lastIndexOf + 1) : Config.DaosInterface; + daosInterface = $"{daosInterfaceName}<{classe.NamePascal}, {pk}>"; + } + else if (classe.Reference || Config.UseJdbc) + { + daosInterface = $"CrudRepository<{classe.NamePascal}, {pk}>"; + } + else + { + daosInterface = $"JpaRepository<{classe.NamePascal}, {pk}>"; + } + + if (Config.DaosAbstract) + { + fw.WriteLine("@NoRepositoryBean"); + fw.WriteLine($"interface Abstract{classe.NamePascal}DAO extends {daosInterface} {{"); + } + else + { + fw.WriteLine($"interface {classe.NamePascal}DAO extends {daosInterface} {{"); + } + fw.WriteLine(); fw.WriteLine("}"); } @@ -75,11 +101,12 @@ private void WriteImports(JavaWriter fw, Class classe, string tag) { classe.GetImport(Config, tag) }; - if (Config.UseJdbc) + + if (Config.DaosInterface != null) { - imports.Add("org.springframework.data.repository.CrudRepository"); + imports.Add($"{Config.DaosInterface}"); } - else if (classe.Reference) + else if (classe.Reference || Config.UseJdbc) { imports.Add("org.springframework.data.repository.CrudRepository"); } @@ -88,6 +115,11 @@ private void WriteImports(JavaWriter fw, Class classe, string tag) imports.Add("org.springframework.data.jpa.repository.JpaRepository"); } + if (Config.DaosAbstract) + { + imports.Add("org.springframework.data.repository.NoRepositoryBean"); + } + fw.AddImports(imports); } } \ No newline at end of file diff --git a/TopModel.Generator.Jpa/jpa.config.json b/TopModel.Generator.Jpa/jpa.config.json index 8fe6f1cf..c290866a 100644 --- a/TopModel.Generator.Jpa/jpa.config.json +++ b/TopModel.Generator.Jpa/jpa.config.json @@ -133,6 +133,16 @@ "description": "Générer les entités en mode JDBC", "default": "false" }, + "daosAbstract": { + "type": "boolean", + "description": " Génération d'interface Abstract avec @NoRepositoryBean permettant de mettre à jour le code généré", + "default": "false" + }, + "daosInterface": { + "type": "string", + "description": "Précise l'interface des Daos générés.", + "default": "false" + }, "mappersInClass": { "type": "boolean", "description": "Indique s'il faut ajouter les mappers en tant méthode ou constructeur dans les classes qui les déclarent" diff --git a/docs/generator/jpa.md b/docs/generator/jpa.md index c73c2272..34c6d86c 100644 --- a/docs/generator/jpa.md +++ b/docs/generator/jpa.md @@ -515,6 +515,29 @@ Le générateur créé un fichier de configuration de job par module. Ce job ord _Variables par tag_: **oui** (plusieurs DAOs pourraient être générés si un fichier à plusieurs tags) + +- `daosAbstract` + + Génération des DAO sous forme 'Abtract' à hériter pour l'utiliser dans le projet dans le projet avec : + - le nom Abstract{classe.NamePascal}DAO + - le fichier java sera mise à jour (écrasé) à chaque génération de code + - l'annotation @NoRepositoryBean ajoutée (org.springframework.data.repository.NoRepositoryBean) permettant de ne pas considérer cette interface comme un DAO + - il faut donc créer une interface qui en hérite dans le projet + - le 'daosPath' peut être dans un répertoire de type 'javagen' + +- `daosInterface` + + Permet de surcharger les interfaces par default des DAOS: + - si UseJdbc, l'interface est org.springframework.data.repository.CrudRepository + - si Reference, l'interface est org.springframework.data.repository.CrudRepository + - si aucun des deux, l'interface est org.springframework.data.jpa.repository.JpaRepository + - si daosInterface est précisée, les autres cas ne sont pas utilisés. + + Seul le nom de la classe est configurable, elle doit respecter le même pattern générique que JpaRespository et CrudRepository soit : + - La classe de l'entité en premier + - La classe de l'identifiant en second + - {DaosInterface}<{classe.NamePascal}, {pk}> + - `dtosPath` Localisation des classes non persistées du modèle, relative au répertoire de génération. diff --git a/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/repository/CustomCrudRepository.java b/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/repository/CustomCrudRepository.java new file mode 100644 index 00000000..61431c43 --- /dev/null +++ b/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/repository/CustomCrudRepository.java @@ -0,0 +1,6 @@ +package topmodel.jpa.sample.demo.daos.repository; + +import org.springframework.data.repository.CrudRepository; + +public interface CustomCrudRepository extends CrudRepository { +} diff --git a/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/profil/DroitDAO.java b/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/profil/DroitDAO.java new file mode 100644 index 00000000..f68343f0 --- /dev/null +++ b/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/profil/DroitDAO.java @@ -0,0 +1,5 @@ +package topmodel.jpa.sample.demo.daos.securite.profil; + +public interface DroitDAO extends AbstractDroitDAO { + +} diff --git a/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/profil/ProfilDAO.java b/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/profil/ProfilDAO.java new file mode 100644 index 00000000..5641f9df --- /dev/null +++ b/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/profil/ProfilDAO.java @@ -0,0 +1,5 @@ +package topmodel.jpa.sample.demo.daos.securite.profil; + +public interface ProfilDAO extends AbstractProfilDAO { + +} diff --git a/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/profil/TypeDroitDAO.java b/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/profil/TypeDroitDAO.java new file mode 100644 index 00000000..0acdce98 --- /dev/null +++ b/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/profil/TypeDroitDAO.java @@ -0,0 +1,5 @@ +package topmodel.jpa.sample.demo.daos.securite.profil; + +public interface TypeDroitDAO extends AbstractTypeDroitDAO { + +} diff --git a/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/utilisateur/TypeUtilisateurDAO.java b/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/utilisateur/TypeUtilisateurDAO.java new file mode 100644 index 00000000..03c37ef8 --- /dev/null +++ b/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/utilisateur/TypeUtilisateurDAO.java @@ -0,0 +1,5 @@ +package topmodel.jpa.sample.demo.daos.securite.utilisateur; + +public interface TypeUtilisateurDAO extends AbstractTypeUtilisateurDAO { + +} diff --git a/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/utilisateur/UtilisateurDAO.java b/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/utilisateur/UtilisateurDAO.java new file mode 100644 index 00000000..85c11dd7 --- /dev/null +++ b/samples/generators/jpa/src/main/java/topmodel/jpa/sample/demo/daos/securite/utilisateur/UtilisateurDAO.java @@ -0,0 +1,5 @@ +package topmodel.jpa.sample.demo.daos.securite.utilisateur; + +public interface UtilisateurDAO extends AbstractUtilisateurDAO { + +} diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/DroitDAO.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/AbstractDroitDAO.java similarity index 51% rename from samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/DroitDAO.java rename to samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/AbstractDroitDAO.java index 890e98ea..2ab3f02d 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/DroitDAO.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/AbstractDroitDAO.java @@ -4,12 +4,14 @@ package topmodel.jpa.sample.demo.daos.securite.profil; -import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.NoRepositoryBean; +import topmodel.jpa.sample.demo.daos.repository.CustomCrudRepository; import topmodel.jpa.sample.demo.entities.securite.profil.Droit; import topmodel.jpa.sample.demo.enums.securite.profil.DroitCode; -public interface DroitDAO extends CrudRepository { +@NoRepositoryBean +interface AbstractDroitDAO extends CustomCrudRepository { } diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/AbstractProfilDAO.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/AbstractProfilDAO.java new file mode 100644 index 00000000..e857d7b0 --- /dev/null +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/AbstractProfilDAO.java @@ -0,0 +1,16 @@ +//// +//// ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! +//// + +package topmodel.jpa.sample.demo.daos.securite.profil; + +import org.springframework.data.repository.NoRepositoryBean; + +import topmodel.jpa.sample.demo.daos.repository.CustomCrudRepository; +import topmodel.jpa.sample.demo.entities.securite.profil.Profil; + + +@NoRepositoryBean +interface AbstractProfilDAO extends CustomCrudRepository { + +} diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/TypeDroitDAO.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/AbstractTypeDroitDAO.java similarity index 51% rename from samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/TypeDroitDAO.java rename to samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/AbstractTypeDroitDAO.java index 24824ac2..1ac10fe3 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/TypeDroitDAO.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/AbstractTypeDroitDAO.java @@ -4,12 +4,14 @@ package topmodel.jpa.sample.demo.daos.securite.profil; -import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.NoRepositoryBean; +import topmodel.jpa.sample.demo.daos.repository.CustomCrudRepository; import topmodel.jpa.sample.demo.entities.securite.profil.TypeDroit; import topmodel.jpa.sample.demo.enums.securite.profil.TypeDroitCode; -public interface TypeDroitDAO extends CrudRepository { +@NoRepositoryBean +interface AbstractTypeDroitDAO extends CustomCrudRepository { } diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/ProfilDAO.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/ProfilDAO.java deleted file mode 100644 index eccbe32b..00000000 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/ProfilDAO.java +++ /dev/null @@ -1,14 +0,0 @@ -//// -//// ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! -//// - -package topmodel.jpa.sample.demo.daos.securite.profil; - -import org.springframework.data.jpa.repository.JpaRepository; - -import topmodel.jpa.sample.demo.entities.securite.profil.Profil; - - -public interface ProfilDAO extends JpaRepository { - -} diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/TypeUtilisateurDAO.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/AbstractTypeUtilisateurDAO.java similarity index 51% rename from samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/TypeUtilisateurDAO.java rename to samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/AbstractTypeUtilisateurDAO.java index ab63aa5f..7ff105a1 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/TypeUtilisateurDAO.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/AbstractTypeUtilisateurDAO.java @@ -4,12 +4,14 @@ package topmodel.jpa.sample.demo.daos.securite.utilisateur; -import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.NoRepositoryBean; +import topmodel.jpa.sample.demo.daos.repository.CustomCrudRepository; import topmodel.jpa.sample.demo.entities.securite.utilisateur.TypeUtilisateur; import topmodel.jpa.sample.demo.enums.securite.utilisateur.TypeUtilisateurCode; -public interface TypeUtilisateurDAO extends CrudRepository { +@NoRepositoryBean +interface AbstractTypeUtilisateurDAO extends CustomCrudRepository { } diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/AbstractUtilisateurDAO.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/AbstractUtilisateurDAO.java new file mode 100644 index 00000000..0789d0c2 --- /dev/null +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/AbstractUtilisateurDAO.java @@ -0,0 +1,16 @@ +//// +//// ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! +//// + +package topmodel.jpa.sample.demo.daos.securite.utilisateur; + +import org.springframework.data.repository.NoRepositoryBean; + +import topmodel.jpa.sample.demo.daos.repository.CustomCrudRepository; +import topmodel.jpa.sample.demo.entities.securite.utilisateur.Utilisateur; + + +@NoRepositoryBean +interface AbstractUtilisateurDAO extends CustomCrudRepository { + +} diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/UtilisateurDAO.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/UtilisateurDAO.java deleted file mode 100644 index 2b0296cd..00000000 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/UtilisateurDAO.java +++ /dev/null @@ -1,14 +0,0 @@ -//// -//// ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! -//// - -package topmodel.jpa.sample.demo.daos.securite.utilisateur; - -import org.springframework.data.jpa.repository.JpaRepository; - -import topmodel.jpa.sample.demo.entities.securite.utilisateur.Utilisateur; - - -public interface UtilisateurDAO extends JpaRepository { - -} diff --git a/samples/generators/jpa/topmodel.config b/samples/generators/jpa/topmodel.config index cab41384..97773d17 100644 --- a/samples/generators/jpa/topmodel.config +++ b/samples/generators/jpa/topmodel.config @@ -1,4 +1,4 @@ -# yaml-language-server: $schema=./topmodel.config.schema.json +# yaml-language-server: $schema=./topmodel.config.schema.json --- app: topmodel.jpa.sample.demo modelRoot: ../../model @@ -22,3 +22,5 @@ jpa: fieldsEnum: Persisted_Dto enumShortcutMode: false persistenceMode: jakarta + daosInterface: topmodel.jpa.sample.demo.daos.repository.CustomCrudRepository + daosAbstract: true diff --git a/samples/model/jpa.topmodel.lock b/samples/model/jpa.topmodel.lock index 4048f214..a2d3afca 100644 --- a/samples/model/jpa.topmodel.lock +++ b/samples/model/jpa.topmodel.lock @@ -8,11 +8,11 @@ generatedFiles: - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/api/client/securite/utilisateur/UtilisateurClient.java - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/api/server/securite/profil/ProfilController.java - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/api/server/securite/utilisateur/UtilisateurController.java - - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/DroitDAO.java - - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/ProfilDAO.java - - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/TypeDroitDAO.java - - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/TypeUtilisateurDAO.java - - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/UtilisateurDAO.java + - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/AbstractDroitDAO.java + - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/AbstractProfilDAO.java + - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/profil/AbstractTypeDroitDAO.java + - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/AbstractTypeUtilisateurDAO.java + - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/daos/securite/utilisateur/AbstractUtilisateurDAO.java - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/profil/ProfilItem.java - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/profil/ProfilRead.java - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/profil/ProfilWrite.java