From c72ae45dddf82b28ac15eacc782d7adb98f11563 Mon Sep 17 00:00:00 2001 From: "Matteo Franci a.k.a. Fugerit" Date: Fri, 22 Nov 2024 19:33:55 +0100 Subject: [PATCH] want to avoid useless imports --- .../java/daogen/base/gen/HelperGenerator.java | 7 ++---- .../daogen/base/gen/util/HelperUtils.java | 23 +++++++++++++++++++ .../daogen/base/gen/util/WrapperUtils.java | 6 +---- .../daogen/base/config/TestDaogenRun.java | 12 ++++++++++ 4 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/util/HelperUtils.java diff --git a/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/HelperGenerator.java b/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/HelperGenerator.java index 88296ae9..ba7251f4 100644 --- a/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/HelperGenerator.java +++ b/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/HelperGenerator.java @@ -13,6 +13,7 @@ import org.fugerit.java.daogen.base.config.DaogenCatalogField; import org.fugerit.java.daogen.base.config.DaogenCatalogRelation; import org.fugerit.java.daogen.base.config.DaogenClassConfigHelper; +import org.fugerit.java.daogen.base.gen.util.HelperUtils; public class HelperGenerator extends DaogenBasicGenerator { @@ -41,12 +42,8 @@ public void init( DaogenCatalogConfig daogenConfig, DaogenCatalogEntity entity ) } else { throw new ConfigException( "Invalid "+DaogenCatalogConstants.GEN_PROP_DAO_HELPER_NG_MODE+" parameter : "+daoHelperNgMode ); } - this.getImportList().add( this.getDaogenConfig().getGeneralProp( DaogenCatalogConstants.GEN_PROP_PACKAGE_MODEL )+"."+this.getEntityModelName() ); this.setImplementsInterface( this.getEntityModelName() ); - for ( DaogenCatalogRelation relation : this.getCurrentEntity().getRelations() ) { - DaogenCatalogEntity entityTo = this.getDaogenConfig().getListMap( relation.getTo() ); - this.getImportList().add( this.getDaogenConfig().getGeneralProp( DaogenCatalogConstants.GEN_PROP_PACKAGE_MODEL )+"."+DaogenCatalogConstants.modelName( entityTo ) ); - } + HelperUtils.checkImportModel( this.getDaogenConfig(), this ); } private void generateRelations() { diff --git a/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/util/HelperUtils.java b/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/util/HelperUtils.java new file mode 100644 index 00000000..2b3e3e88 --- /dev/null +++ b/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/util/HelperUtils.java @@ -0,0 +1,23 @@ +package org.fugerit.java.daogen.base.gen.util; + +import org.fugerit.java.daogen.base.config.DaogenCatalogConfig; +import org.fugerit.java.daogen.base.config.DaogenCatalogConstants; +import org.fugerit.java.daogen.base.config.DaogenCatalogEntity; +import org.fugerit.java.daogen.base.config.DaogenCatalogRelation; +import org.fugerit.java.daogen.base.gen.DaogenBasicGenerator; + +public class HelperUtils { + + private HelperUtils() {} + + public static void checkImportModel(DaogenCatalogConfig daogenConfig, DaogenBasicGenerator gen ) { + if ( !daogenConfig.getGeneralProp( DaogenCatalogConstants.GEN_PROP_PACKAGE_MODEL ).equals( daogenConfig.getGeneralProp( DaogenCatalogConstants.GEN_PROP_PACKAGE_HELPER ) ) ) { + gen.getImportList().add( gen.getDaogenConfig().getGeneralProp( DaogenCatalogConstants.GEN_PROP_PACKAGE_MODEL )+"."+gen.getEntityModelName() ); + for ( DaogenCatalogRelation relation : gen.getCurrentEntity().getRelations() ) { + DaogenCatalogEntity entityTo = gen.getDaogenConfig().getListMap( relation.getTo() ); + gen.getImportList().add( gen.getDaogenConfig().getGeneralProp( DaogenCatalogConstants.GEN_PROP_PACKAGE_MODEL )+"."+DaogenCatalogConstants.modelName( entityTo ) ); + } + } + } + +} diff --git a/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/util/WrapperUtils.java b/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/util/WrapperUtils.java index 367a7931..57e122ca 100644 --- a/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/util/WrapperUtils.java +++ b/fj-daogen-base/src/main/java/org/fugerit/java/daogen/base/gen/util/WrapperUtils.java @@ -31,12 +31,8 @@ public static void init(DaogenCatalogConfig daogenConfig, DaogenCatalogEntity en } else { throw new ConfigException( "Invalid "+DaogenCatalogConstants.GEN_PROP_DAO_WRAPPER_NG_MODE+" parameter : "+daoWrapperNgMode ); } - gen.getImportList().add( gen.getDaogenConfig().getGeneralProp( DaogenCatalogConstants.GEN_PROP_PACKAGE_MODEL )+"."+gen.getEntityModelName() ); gen.setImplementsInterface( gen.getEntityModelName() ); - for ( DaogenCatalogRelation relation : gen.getCurrentEntity().getRelations() ) { - DaogenCatalogEntity entityTo = gen.getDaogenConfig().getListMap( relation.getTo() ); - gen.getImportList().add( gen.getDaogenConfig().getGeneralProp( DaogenCatalogConstants.GEN_PROP_PACKAGE_MODEL )+"."+DaogenCatalogConstants.modelName( entityTo ) ); - } + HelperUtils.checkImportModel( daogenConfig, gen ); } public static void generateRelations(DaogenBasicGenerator gen ) { diff --git a/fj-daogen-base/src/test/java/test/org/fugerit/java/daogen/base/config/TestDaogenRun.java b/fj-daogen-base/src/test/java/test/org/fugerit/java/daogen/base/config/TestDaogenRun.java index 80030dca..03d747a0 100644 --- a/fj-daogen-base/src/test/java/test/org/fugerit/java/daogen/base/config/TestDaogenRun.java +++ b/fj-daogen-base/src/test/java/test/org/fugerit/java/daogen/base/config/TestDaogenRun.java @@ -81,6 +81,18 @@ public void testDaoGenerationClassicalEntity() throws IOException, ConfigExcepti Assert.assertEquals( Result.RESULT_CODE_OK, result ); } + @Test + public void testDaoModelAndHelperInTheSameFolder() throws IOException, ConfigException { + File file = new File( "target/daogen-run-model-helper-coexists" ); + Properties overrideProperties = new Properties(); + overrideProperties.setProperty( + DaogenCatalogConstants.GEN_PROP_PACKAGE_HELPER , + "org.fugerit.java.daogen.sample.def.model"); + int result = this.testDaoGenerationWorker(file, overrideProperties); + Assert.assertTrue( file.exists() ); + Assert.assertEquals( Result.RESULT_CODE_OK, result ); + } + @Test public void testDaoGenerationFailHelperNg() throws IOException, ConfigException { File file = new File( "target/daogen-run-fail-helper-ng" );