From fe84af5d96eb0d4352b46104a5c7180efe7d14fa Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Sun, 29 Dec 2024 16:22:03 +0100 Subject: [PATCH] [FEAT] add some annoatation tools --- .../archidata/annotation/AnnotationTools.java | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/org/kar/archidata/annotation/AnnotationTools.java b/src/org/kar/archidata/annotation/AnnotationTools.java index ce738ad..77894dd 100644 --- a/src/org/kar/archidata/annotation/AnnotationTools.java +++ b/src/org/kar/archidata/annotation/AnnotationTools.java @@ -11,6 +11,8 @@ import org.kar.archidata.exception.DataAccessException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import dev.morphia.annotations.Entity; +import dev.morphia.mapping.Mapper; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.annotation.Nullable; import jakarta.persistence.Column; @@ -32,6 +34,7 @@ import jakarta.ws.rs.DefaultValue; public class AnnotationTools { static final Logger LOGGER = LoggerFactory.getLogger(AnnotationTools.class); + // For SQL declaration table Name public static String getTableName(final Class clazz, final QueryOptions options) throws DataAccessException { if (options != null) { final List data = options.get(OverrideTableName.class); @@ -42,16 +45,13 @@ public class AnnotationTools { return AnnotationTools.getTableName(clazz); } - public static String getTableName(final Class element) throws DataAccessException { + // For SQL declaration table Name + public static String getTableName(final Class element) { final Annotation[] annotation = element.getDeclaredAnnotationsByType(Table.class); if (annotation.length == 0) { // when no annotation is detected, then the table name is the class name return element.getSimpleName(); } - if (annotation.length > 1) { - throw new DataAccessException( - "Must not have more than 1 element @Table on " + element.getClass().getCanonicalName()); - } final String tmp = ((Table) annotation[0]).name(); if (tmp == null) { return element.getSimpleName(); @@ -59,6 +59,31 @@ public class AnnotationTools { return tmp; } + public static String getCollectionName(final Class clazz, final QueryOptions options) { + if (options != null) { + // TODO: maybe change OverrideTableName with OverrideCollectionName + final List data = options.get(OverrideTableName.class); + if (data.size() == 1) { + return data.get(0).getName(); + } + } + return AnnotationTools.getCollectionName(clazz); + } + + // For No-SQL Table/Collection Name + public static String getCollectionName(final Class clazz) { + final Annotation[] annotation = clazz.getDeclaredAnnotationsByType(Entity.class); + if (annotation.length == 0) { + // when no annotation is detected, then the table name is the class name + return clazz.getSimpleName(); + } + final String tmp = ((Entity) annotation[0]).value(); + if (tmp == null || tmp.length() == 0 || Mapper.IGNORED_FIELDNAME.equals(tmp)) { + return clazz.getSimpleName(); + } + return tmp; + } + public static boolean getSchemaReadOnly(final Field element) throws DataAccessException { final Annotation[] annotation = element.getDeclaredAnnotationsByType(Schema.class); if (annotation.length == 0) {