From fc3d2883a963fe4cb3855094bfa7ed3da52280d3 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Thu, 17 Feb 2022 12:29:28 +0300 Subject: [PATCH] release-0.0.2 --- pom.xml | 2 +- .../utils/fieldconstants/ClassCreator.java | 66 ----------- .../fieldconstants/CreatorClassTableMode.java | 107 +++++++++++++++++ .../fieldconstants/annotation/FieldNames.java | 4 +- .../field/ElementCollectionField.java | 16 +++ .../annotation/{ => field}/IgnoreField.java | 4 +- .../annotation/field/JoinField.java | 16 +++ .../utils/fieldconstants/domain/ClassDto.java | 44 ------- .../utils/fieldconstants/domain/Mode.java | 17 +++ .../domain/mode/table/ClassTableDto.java | 62 ++++++++++ .../mode/table/JoinElemCollectionDto.java | 34 ++++++ .../domain/mode/table/JoinFieldDto.java | 28 +++++ .../domain/mode/table/JoinTableContainer.java | 34 ++++++ .../table/SimpleFieldTableDto.java} | 10 +- .../processor/FieldNameProcessor.java | 112 ++++++++++++++---- src/main/java/module-info.java | 2 + 16 files changed, 416 insertions(+), 142 deletions(-) delete mode 100644 src/main/java/dev/struchkov/haiti/utils/fieldconstants/ClassCreator.java create mode 100644 src/main/java/dev/struchkov/haiti/utils/fieldconstants/CreatorClassTableMode.java create mode 100644 src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/field/ElementCollectionField.java rename src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/{ => field}/IgnoreField.java (71%) create mode 100644 src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/field/JoinField.java delete mode 100644 src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/ClassDto.java create mode 100644 src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/Mode.java create mode 100644 src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/ClassTableDto.java create mode 100644 src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/JoinElemCollectionDto.java create mode 100644 src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/JoinFieldDto.java create mode 100644 src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/JoinTableContainer.java rename src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/{FieldDto.java => mode/table/SimpleFieldTableDto.java} (56%) diff --git a/pom.xml b/pom.xml index 2db7537..202bdb5 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ dev.struchkov.haiti.utils haiti-utils-field-constants - 0.0.1 + 0.0.2 Haiti Field Name Constants Utils Generating class field names diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/ClassCreator.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/ClassCreator.java deleted file mode 100644 index cbf1669..0000000 --- a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/ClassCreator.java +++ /dev/null @@ -1,66 +0,0 @@ -package dev.struchkov.haiti.utils.fieldconstants; - -import dev.struchkov.haiti.utils.fieldconstants.domain.ClassDto; -import dev.struchkov.haiti.utils.fieldconstants.domain.FieldDto; - -import javax.annotation.processing.ProcessingEnvironment; -import javax.tools.JavaFileObject; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.Set; - -import static dev.struchkov.haiti.utils.Exceptions.utilityClass; -import static java.text.MessageFormat.format; - -/** - * // TODO: 20.06.2021 Добавить описание. - * - * @author upagge 20.06.2021 - */ -public final class ClassCreator { - - private static final String TABLE_NAME_DEFAULT = "TABLE_NAME"; - private static final String TABLE_NAME_DB = "DB_TABLE_NAME"; - - private ClassCreator() { - utilityClass(); - } - - public static void record(ClassDto classDto, ProcessingEnvironment environment) { - JavaFileObject builderFile = null; - try { - builderFile = environment.getFiler().createSourceFile(classDto.getClassName()); - } catch (IOException e) { - e.printStackTrace(); - } - - try (PrintWriter out = new PrintWriter(builderFile.openWriter())) { - out.println("package " + classDto.getClassPackage() + ";"); - out.println(); - out.print("public class " + classDto.getClassName() + " {"); - out.println(); - out.println(); - generateTableName(classDto.getTableName(), classDto.getFields(), out); - generateNames(classDto.getFields(), out); - out.println(); - out.println("}"); - out.println(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private static void generateTableName(String tableName, Set fields, PrintWriter out) { - if (tableName != null) { - final boolean isTableNameField = fields.stream().anyMatch(fieldDto -> fieldDto.getFieldStringName().equalsIgnoreCase(TABLE_NAME_DEFAULT)); - out.println(format(" public static final String {0} = \"{1}\";", isTableNameField ? TABLE_NAME_DB : TABLE_NAME_DEFAULT, tableName)); - } - } - - private static void generateNames(Set fields, PrintWriter out) { - for (FieldDto field : fields) { - out.println(format(" public static final String {0} = \"{1}\";", field.getFieldStringName(), field.getFieldName())); - } - } - -} diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/CreatorClassTableMode.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/CreatorClassTableMode.java new file mode 100644 index 0000000..d9f583e --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/CreatorClassTableMode.java @@ -0,0 +1,107 @@ +package dev.struchkov.haiti.utils.fieldconstants; + +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; +import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinTableContainer; +import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.SimpleFieldTableDto; + +import javax.annotation.processing.ProcessingEnvironment; +import javax.tools.JavaFileObject; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Collection; +import java.util.List; + +import static dev.struchkov.haiti.utils.Exceptions.utilityClass; +import static java.text.MessageFormat.format; + +/** + * // TODO: 20.06.2021 Добавить описание. + * + * @author upagge 20.06.2021 + */ +public final class CreatorClassTableMode { + + private static final String TABLE_NAME_DEFAULT = "TABLE_NAME"; + private static final String TABLE_NAME_DB = "DB_TABLE_NAME"; + + private CreatorClassTableMode() { + utilityClass(); + } + + public static void record(ClassTableDto classDto, ProcessingEnvironment environment) { + JavaFileObject builderFile = null; + try { + builderFile = environment.getFiler().createSourceFile(classDto.getClassName()); + } catch (IOException e) { + e.printStackTrace(); + } + + try (PrintWriter out = new PrintWriter(builderFile.openWriter())) { + out.println("package " + classDto.getClassPackage() + ";"); + out.println(); + generateImports(classDto.getJoinFields(), classDto.getJoinElemCollections(), out); + out.println(); + out.print(format("public class {0} '{'", classDto.getClassName())); + out.println(); + out.println(); + generateTableName(classDto.getTableName(), classDto.getSimpleFields(), out); + generateSimpleNames(classDto.getSimpleFields(), out); + generateJoinNames(classDto.getJoinFields(), out); + generateElementCollectionNames(classDto.getJoinElemCollections(), out); + out.println("}"); + out.println(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void generateImports(List joinFields, List joinElemCollections, PrintWriter out) { + if ((joinFields != null && !joinFields.isEmpty()) || (joinElemCollections != null && !joinElemCollections.isEmpty())) { + out.println("import dev.struchkov.haiti.filter.jooq.join.JoinTable;"); + } + } + + private static void generateTableName(String tableName, Collection fields, PrintWriter out) { + 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 generateSimpleNames(Collection fields, PrintWriter out) { + for (SimpleFieldTableDto field : fields) { + out.println(format(" public static final String {0} = \"{1}\";", field.getFieldStringName(), field.getFieldName())); + } + out.println(); + } + + private static void generateJoinNames(List joinFields, PrintWriter out) { + for (JoinFieldDto joinField : joinFields) { + final JoinTableContainer container = joinField.getContainer(); + out.println( + format( + " public static final JoinTable {0} = JoinTable.ofLeft(\"{1}\", \"{2}\", \"{3}\");", + joinField.getFieldName(), container.getTable(), container.getBaseId(), container.getReference() + ) + ); + } + out.println(); + } + + private static void generateElementCollectionNames(List joinElemCollections, PrintWriter out) { + for (JoinElemCollectionDto element : joinElemCollections) { + final String fieldName = element.getFieldName(); + final JoinTableContainer firstContainer = element.getFirstContainer(); + final JoinTableContainer secondContainer = element.getSecondContainer(); + out.println(format(" public static final JoinTable[] {0} = '{'", fieldName)); + out.println(format(" JoinTable.ofLeft(\"{0}\", \"{1}\", \"{2}\"),", firstContainer.getTable(), firstContainer.getBaseId(), firstContainer.getReference())); + out.println(format(" JoinTable.ofLeft(\"{0}\", \"{1}\", \"{2}\")", secondContainer.getTable(), secondContainer.getBaseId(), secondContainer.getReference())); + out.println(" };"); + } + out.println(); + } + +} diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/FieldNames.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/FieldNames.java index 32cf300..a574944 100644 --- a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/FieldNames.java +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/FieldNames.java @@ -1,5 +1,7 @@ package dev.struchkov.haiti.utils.fieldconstants.annotation; +import dev.struchkov.haiti.utils.fieldconstants.domain.Mode; + import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -9,6 +11,6 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.CLASS) public @interface FieldNames { - String postfix() default "Fields"; + Mode[] mode() default {Mode.SIMPLE}; } diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/field/ElementCollectionField.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/field/ElementCollectionField.java new file mode 100644 index 0000000..297af65 --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/field/ElementCollectionField.java @@ -0,0 +1,16 @@ +package dev.struchkov.haiti.utils.fieldconstants.annotation.field; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.CLASS) +public @interface ElementCollectionField { + + String parentId(); + String childTable(); + String childReference(); + +} diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/IgnoreField.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/field/IgnoreField.java similarity index 71% rename from src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/IgnoreField.java rename to src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/field/IgnoreField.java index 1a05f6a..e475c49 100644 --- a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/IgnoreField.java +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/field/IgnoreField.java @@ -1,11 +1,11 @@ -package dev.struchkov.haiti.utils.fieldconstants.annotation; +package dev.struchkov.haiti.utils.fieldconstants.annotation.field; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -@Target(ElementType.TYPE) +@Target(ElementType.FIELD) @Retention(RetentionPolicy.CLASS) public @interface IgnoreField { diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/field/JoinField.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/field/JoinField.java new file mode 100644 index 0000000..52877d9 --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/field/JoinField.java @@ -0,0 +1,16 @@ +package dev.struchkov.haiti.utils.fieldconstants.annotation.field; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.CLASS) +public @interface JoinField { + + String table(); + String baseId(); + String reference(); + +} diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/ClassDto.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/ClassDto.java deleted file mode 100644 index d60ec33..0000000 --- a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/ClassDto.java +++ /dev/null @@ -1,44 +0,0 @@ -package dev.struchkov.haiti.utils.fieldconstants.domain; - -import java.util.Set; - -public class ClassDto { - - private String classPackage; - private String className; - private String tableName; - private Set fields; - - public String getClassName() { - return className; - } - - public void setClassName(String className) { - this.className = className; - } - - public String getClassPackage() { - return classPackage; - } - - public void setClassPackage(String classPackage) { - this.classPackage = classPackage; - } - - public String getTableName() { - return tableName; - } - - public void setTableName(String tableName) { - this.tableName = tableName; - } - - public Set getFields() { - return fields; - } - - public void setFields(Set fields) { - this.fields = fields; - } - -} diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/Mode.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/Mode.java new file mode 100644 index 0000000..f01e326 --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/Mode.java @@ -0,0 +1,17 @@ +package dev.struchkov.haiti.utils.fieldconstants.domain; + +public enum Mode { + + SIMPLE("Fields"), TABLE("Columns"); + + private final String defaultPostfix; + + Mode(String defaultPostfix) { + this.defaultPostfix = defaultPostfix; + } + + public String getDefaultPostfix() { + return defaultPostfix; + } + +} 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 new file mode 100644 index 0000000..31ec372 --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/ClassTableDto.java @@ -0,0 +1,62 @@ +package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table; + +import java.util.List; + +public class ClassTableDto { + + private String classPackage; + private String className; + private String tableName; + private List simpleFields; + private List joinFields; + private List joinElemCollections; + + public String getClassPackage() { + return classPackage; + } + + public void setClassPackage(String classPackage) { + this.classPackage = classPackage; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public String getTableName() { + return tableName; + } + + public void setTableName(String tableName) { + this.tableName = tableName; + } + + public List getSimpleFields() { + return simpleFields; + } + + public void setSimpleFields(List simpleFields) { + this.simpleFields = simpleFields; + } + + public List getJoinFields() { + return joinFields; + } + + public void setJoinFields(List joinFields) { + this.joinFields = joinFields; + } + + public List getJoinElemCollections() { + return joinElemCollections; + } + + public void setJoinElemCollections(List joinElemCollections) { + this.joinElemCollections = joinElemCollections; + } + +} diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/JoinElemCollectionDto.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/JoinElemCollectionDto.java new file mode 100644 index 0000000..e7e702b --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/JoinElemCollectionDto.java @@ -0,0 +1,34 @@ +package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table; + +import dev.struchkov.haiti.utils.Assert; + +public class JoinElemCollectionDto { + + private String fieldName; + private JoinTableContainer firstContainer; + private JoinTableContainer secondContainer; + + private JoinElemCollectionDto(String fieldName, JoinTableContainer firstContainer, JoinTableContainer secondContainer) { + this.fieldName = fieldName; + this.firstContainer = firstContainer; + this.secondContainer = secondContainer; + } + + public static JoinElemCollectionDto of(String fieldName, JoinTableContainer firstContainer, JoinTableContainer secondContainer) { + Assert.isNotNull(fieldName, firstContainer, secondContainer); + return new JoinElemCollectionDto(fieldName, firstContainer, secondContainer); + } + + public String getFieldName() { + return fieldName; + } + + public JoinTableContainer getFirstContainer() { + return firstContainer; + } + + public JoinTableContainer getSecondContainer() { + return secondContainer; + } + +} diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/JoinFieldDto.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/JoinFieldDto.java new file mode 100644 index 0000000..9039029 --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/JoinFieldDto.java @@ -0,0 +1,28 @@ +package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table; + +import dev.struchkov.haiti.utils.Assert; + +public class JoinFieldDto { + + private String fieldName; + private JoinTableContainer container; + + private JoinFieldDto(String fieldName, JoinTableContainer container) { + this.fieldName = fieldName; + this.container = container; + } + + public static JoinFieldDto of(String fieldName, JoinTableContainer container) { + Assert.isNotNull(fieldName, container); + return new JoinFieldDto(fieldName, container); + } + + public String getFieldName() { + return fieldName; + } + + public JoinTableContainer getContainer() { + return container; + } + +} diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/JoinTableContainer.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/JoinTableContainer.java new file mode 100644 index 0000000..aa68ca8 --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/JoinTableContainer.java @@ -0,0 +1,34 @@ +package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table; + +import dev.struchkov.haiti.utils.Assert; + +public class JoinTableContainer { + + private String table; + private String baseId; + private String reference; + + private JoinTableContainer(String table, String baseId, String reference) { + this.table = table; + this.baseId = baseId; + this.reference = reference; + } + + public static JoinTableContainer of(String table, String baseId, String reference) { + Assert.isNotNull(table, baseId, reference); + return new JoinTableContainer(table, baseId, reference); + } + + public String getTable() { + return table; + } + + public String getBaseId() { + return baseId; + } + + public String getReference() { + return reference; + } + +} diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/FieldDto.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/SimpleFieldTableDto.java similarity index 56% rename from src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/FieldDto.java rename to src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/SimpleFieldTableDto.java index ed87228..be9f2e7 100644 --- a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/FieldDto.java +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/mode/table/SimpleFieldTableDto.java @@ -1,22 +1,22 @@ -package dev.struchkov.haiti.utils.fieldconstants.domain; +package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table; /** * // TODO: 07.06.2021 Добавить описание. * * @author upagge 07.06.2021 */ -public class FieldDto { +public class SimpleFieldTableDto { private final String fieldStringName; private final String fieldName; - private FieldDto(String fieldStringName, String fieldName) { + private SimpleFieldTableDto(String fieldStringName, String fieldName) { this.fieldStringName = fieldStringName; this.fieldName = fieldName; } - public static FieldDto of(String fieldStringName, String fieldName) { - return new FieldDto(fieldStringName, fieldName); + public static SimpleFieldTableDto of(String fieldStringName, String fieldName) { + return new SimpleFieldTableDto(fieldStringName, fieldName); } public String getFieldStringName() { diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/processor/FieldNameProcessor.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/processor/FieldNameProcessor.java index c746fe1..17119b8 100644 --- a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/processor/FieldNameProcessor.java +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/processor/FieldNameProcessor.java @@ -1,11 +1,17 @@ package dev.struchkov.haiti.utils.fieldconstants.processor; import com.google.auto.service.AutoService; -import dev.struchkov.haiti.utils.fieldconstants.ClassCreator; +import dev.struchkov.haiti.utils.fieldconstants.CreatorClassTableMode; import dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames; -import dev.struchkov.haiti.utils.fieldconstants.annotation.IgnoreField; -import dev.struchkov.haiti.utils.fieldconstants.domain.ClassDto; -import dev.struchkov.haiti.utils.fieldconstants.domain.FieldDto; +import dev.struchkov.haiti.utils.fieldconstants.annotation.field.ElementCollectionField; +import dev.struchkov.haiti.utils.fieldconstants.annotation.field.IgnoreField; +import dev.struchkov.haiti.utils.fieldconstants.annotation.field.JoinField; +import dev.struchkov.haiti.utils.fieldconstants.domain.Mode; +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; +import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinTableContainer; +import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.SimpleFieldTableDto; import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.Processor; @@ -16,9 +22,12 @@ import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import javax.lang.model.type.TypeMirror; +import javax.persistence.CollectionTable; import javax.persistence.Column; +import javax.persistence.ElementCollection; import javax.persistence.Table; import java.util.Arrays; +import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -35,31 +44,88 @@ public class FieldNameProcessor extends AbstractProcessor { final TypeMirror mirror = annotatedElement.asType(); final String annotatedElementName = annotatedElement.getSimpleName().toString(); final FieldNames settings = annotatedElement.getAnnotation(FieldNames.class); - final Table anTable = annotatedElement.getAnnotation(Table.class); - final String newClassName = annotatedElementName + settings.postfix(); - final Set fields = annotatedElement.getEnclosedElements().stream() - .filter(this::isField) - .filter(this::isNotIgnoreField) - .map( - element -> { - final String fieldName = element.getSimpleName().toString(); - final String columnName = element.getAnnotation(Column.class).name(); - return FieldDto.of(fieldName, columnName); - } - ).collect(Collectors.toSet()); - - final ClassDto newClass = new ClassDto(); - newClass.setClassName(newClassName); - newClass.setFields(fields); - newClass.setClassPackage(getPackage(mirror)); - newClass.setTableName(anTable.name()); - ClassCreator.record(newClass, processingEnv); + final Set modes = Arrays.stream(settings.mode()).collect(Collectors.toUnmodifiableSet()); + if (modes.contains(Mode.TABLE)) { + generateTableMode(annotatedElement, mirror, annotatedElementName); + } } } return true; } + private void generateTableMode(Element annotatedElement, TypeMirror mirror, String annotatedElementName) { + final Table anTable = annotatedElement.getAnnotation(Table.class); + final String newClassName = annotatedElementName + Mode.TABLE.getDefaultPostfix(); + + final List allFields = annotatedElement.getEnclosedElements().stream() + .filter(this::isField) + .filter(this::isNotIgnoreField) + .collect(Collectors.toList()); + + final List simpleFields = getSimpleFields(allFields); + final List joinFields = getJoinFields(allFields); + final List elementCollectionFields = getElementCollectionsFields(allFields); + + final ClassTableDto newClass = new ClassTableDto(); + newClass.setClassName(newClassName); + newClass.setSimpleFields(simpleFields); + newClass.setJoinFields(joinFields); + newClass.setJoinElemCollections(elementCollectionFields); + newClass.setClassPackage(getPackage(mirror)); + newClass.setTableName(anTable.name()); + CreatorClassTableMode.record(newClass, processingEnv); + } + + private List getElementCollectionsFields(List allFields) { + return allFields.stream() + .filter( + field -> field.getAnnotation(ElementCollectionField.class) != null && + field.getAnnotation(CollectionTable.class) != null && + field.getAnnotation(Column.class) != null + ) + .map(field -> { + final String fieldName = field.getSimpleName().toString(); + final ElementCollectionField elementCollectionField = field.getAnnotation(ElementCollectionField.class); + final CollectionTable collectionTable = field.getAnnotation(CollectionTable.class); + final Column column = field.getAnnotation(Column.class); + + final JoinTableContainer firstContainer = JoinTableContainer.of(collectionTable.name(), elementCollectionField.parentId(), collectionTable.joinColumns()[0].name()); + final JoinTableContainer secondContainer = JoinTableContainer.of(elementCollectionField.childTable(), column.name(), elementCollectionField.childReference()); + return JoinElemCollectionDto.of(fieldName, firstContainer, secondContainer); + }).collect(Collectors.toList()); + } + + private List getJoinFields(List allFields) { + return allFields.stream() + .filter( + field -> field.getAnnotation(JoinField.class) != null && + field.getAnnotation(ElementCollection.class) == null + ) + .map(field -> { + final String fieldName = field.getSimpleName().toString(); + final JoinField joinField = field.getAnnotation(JoinField.class); + final JoinTableContainer joinContainer = JoinTableContainer.of(joinField.table(), joinField.baseId(), joinField.reference()); + return JoinFieldDto.of(fieldName, joinContainer); + }) + .collect(Collectors.toList()); + } + + private List getSimpleFields(List allFields) { + return allFields.stream() + .filter( + field -> field.getAnnotation(Column.class) != null && + field.getAnnotation(ElementCollection.class) == null + ) + .map( + field -> { + final String fieldName = field.getSimpleName().toString(); + final String columnName = field.getAnnotation(Column.class).name(); + return SimpleFieldTableDto.of(fieldName, columnName); + } + ).collect(Collectors.toList()); + } + private boolean isNotIgnoreField(Element element) { return element.getAnnotation(IgnoreField.class) == null; } diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 86de4e3..dea9191 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -1,5 +1,7 @@ module haiti.utils.fieldconstants { exports dev.struchkov.haiti.utils.fieldconstants.annotation; + exports dev.struchkov.haiti.utils.fieldconstants.domain; + exports dev.struchkov.haiti.utils.fieldconstants.annotation.field; requires java.compiler; requires com.google.auto.service;