From 5dc69b034f7d02b5b8fac36a652890c147b29a09 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Mon, 23 Jan 2023 16:41:13 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B3=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D1=8E=20schema?= =?UTF-8?q?-name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../creator/CreatorClassTableMode.java | 33 +++++++++++++++++-- .../domain/mode/table/ClassTableDto.java | 9 +++++ .../generator/GeneratorTableMode.java | 9 ++--- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/creator/CreatorClassTableMode.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/creator/CreatorClassTableMode.java index 6a79582..5d188ed 100644 --- a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/creator/CreatorClassTableMode.java +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/creator/CreatorClassTableMode.java @@ -1,6 +1,5 @@ package dev.struchkov.haiti.utils.fieldconstants.creator; -import dev.struchkov.haiti.utils.Checker; import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.ClassTableDto; import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinElemCollectionDto; import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinFieldDto; @@ -28,6 +27,11 @@ public final class CreatorClassTableMode { private static final String TABLE_NAME_DEFAULT = "TABLE_NAME"; private static final String TABLE_NAME_DB = "DB_TABLE_NAME"; + private static final String SCHEMA_NAME_DEFAULT = "SCHEMA_NAME"; + private static final String SCHEMA_NAME_DB = "DB_SCHEMA_NAME"; + + private static final String SCHEMA_AND_TABLE = "SCHEMA_AND_TABLE"; + private CreatorClassTableMode() { utilityClass(); } @@ -35,7 +39,7 @@ public final class CreatorClassTableMode { public static void record(ClassTableDto classDto, ProcessingEnvironment environment) { JavaFileObject builderFile = null; try { - builderFile = environment.getFiler().createSourceFile(classDto.getClassPackage() +"." + classDto.getClassName()); + builderFile = environment.getFiler().createSourceFile(classDto.getClassPackage() + "." + classDto.getClassName()); } catch (IOException e) { e.printStackTrace(); } @@ -49,6 +53,8 @@ public final class CreatorClassTableMode { out.println(); out.println(); generateTableName(classDto.getTableName(), classDto.getSimpleFields(), out); + generateTableSchema(classDto.getTableSchema(), classDto.getSimpleFields(), out); + generateTableSchemaAndTable(classDto.getTableSchema(), classDto.getTableName(), out); generateSimpleNames(classDto.getSimpleFields(), out); generateJoinNames(classDto.getJoinFields(), out); generateElementCollectionNames(classDto.getJoinElemCollections(), out); @@ -69,11 +75,26 @@ public final class CreatorClassTableMode { if (tableName != null) { final boolean isTableNameField = fields.stream().anyMatch(simpleFieldTableDto -> simpleFieldTableDto.getFieldStringName().equalsIgnoreCase(TABLE_NAME_DEFAULT)); out.println(format(" public static final String {0} = \"{1}\";", isTableNameField ? TABLE_NAME_DB : TABLE_NAME_DEFAULT, tableName)); - out.println(); + } + } + + private static void generateTableSchema(String tableSchema, Collection fields, PrintWriter out) { + if (tableSchema != null && !"".equals(tableSchema)) { + final boolean isTableSchemaField = fields.stream().anyMatch(simpleFieldTableDto -> simpleFieldTableDto.getFieldStringName().equalsIgnoreCase(TABLE_NAME_DEFAULT)); + out.println(format(" public static final String {0} = \"{1}\";", isTableSchemaField ? SCHEMA_NAME_DB : SCHEMA_NAME_DEFAULT, tableSchema)); + } + } + + private static void generateTableSchemaAndTable(String tableSchema, String tableName, PrintWriter out) { + if (tableSchema != null && tableName != null && !"".equals(tableSchema)) { + out.println(format(" public static final String {0} = \"{1}\";", SCHEMA_AND_TABLE, tableSchema + "." + tableName)); } } private static void generateSimpleNames(Collection fields, PrintWriter out) { + if (checkNotEmpty(fields)) { + out.println(); + } for (SimpleTableFieldDto field : fields) { out.println(format(" public static final String {0} = \"{1}\";", field.getFieldStringName(), field.getFieldName())); } @@ -83,6 +104,9 @@ public final class CreatorClassTableMode { } private static void generateJoinNames(List joinFields, PrintWriter out) { + if (checkNotEmpty(joinFields)) { + out.println(); + } for (JoinFieldDto joinField : joinFields) { final JoinTableContainer container = joinField.getContainer(); out.println( @@ -98,6 +122,9 @@ public final class CreatorClassTableMode { } private static void generateElementCollectionNames(List joinElemCollections, PrintWriter out) { + if (checkNotEmpty(joinElemCollections)) { + out.println(); + } for (JoinElemCollectionDto element : joinElemCollections) { final String fieldName = element.getFieldName(); final JoinTableContainer firstContainer = element.getFirstContainer(); diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/ClassTableDto.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/ClassTableDto.java index 1d0e870..947a699 100644 --- a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/ClassTableDto.java +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/ClassTableDto.java @@ -7,6 +7,7 @@ public class ClassTableDto { private String classPackage; private String className; private String tableName; + private String tableSchema; private List simpleFields; private List joinFields; private List joinElemCollections; @@ -35,6 +36,14 @@ public class ClassTableDto { this.tableName = tableName; } + public String getTableSchema() { + return tableSchema; + } + + public void setTableSchema(String tableSchema) { + this.tableSchema = tableSchema; + } + public List getSimpleFields() { return simpleFields; } diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/generator/GeneratorTableMode.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/generator/GeneratorTableMode.java index 124bbd5..30c40ba 100644 --- a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/generator/GeneratorTableMode.java +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/generator/GeneratorTableMode.java @@ -49,6 +49,7 @@ public class GeneratorTableMode { newClass.setJoinElemCollections(elementCollectionFields); newClass.setClassPackage(Generator.getPackage(annotatedElement)); newClass.setTableName(checkNotNull(anTable) ? anTable.name() : null); + newClass.setTableSchema(checkNotNull(anTable) ? anTable.schema() : null); CreatorClassTableMode.record(newClass, processingEnv); } @@ -57,8 +58,8 @@ public class GeneratorTableMode { return allFields.stream() .filter( field -> field.getAnnotation(ElementCollectionField.class) != null && - field.getAnnotation(CollectionTable.class) != null && - field.getAnnotation(Column.class) != null + field.getAnnotation(CollectionTable.class) != null && + field.getAnnotation(Column.class) != null ) .map(field -> { final String fieldName = field.getSimpleName().toString(); @@ -78,7 +79,7 @@ public class GeneratorTableMode { return allFields.stream() .filter( field -> field.getAnnotation(JoinField.class) != null && - field.getAnnotation(ElementCollection.class) == null + field.getAnnotation(ElementCollection.class) == null ) .map(field -> { final String fieldName = field.getSimpleName().toString(); @@ -95,7 +96,7 @@ public class GeneratorTableMode { allFields.stream() .filter( field -> field.getAnnotation(Column.class) != null && - field.getAnnotation(ElementCollection.class) == null + field.getAnnotation(ElementCollection.class) == null ) .forEach( field -> {