Compare commits

...

2 Commits

Author SHA1 Message Date
b0d565b014 [FEAT] remove static from DataAPI 2024-12-29 16:22:20 +01:00
fe84af5d96 [FEAT] add some annoatation tools 2024-12-29 16:22:03 +01:00
2 changed files with 34 additions and 11 deletions

View File

@ -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<OverrideTableName> 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<OverrideTableName> 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) {

View File

@ -118,7 +118,7 @@ public class DataResource {
return getFileData(uuid) + ".json";
}
public static Data getWithSha512(final String sha512) {
public Data getWithSha512(final String sha512) {
LOGGER.info("find sha512 = {}", sha512);
try {
return DataAccess.getWhere(Data.class, new Condition(new QueryCondition("sha512", "=", sha512)));
@ -129,7 +129,7 @@ public class DataResource {
return null;
}
public static Data getWithId(final long id) {
public Data getWithId(final long id) {
LOGGER.info("find id = {}", id);
try {
return DataAccess.get(Data.class, id);
@ -140,7 +140,7 @@ public class DataResource {
return null;
}
public static Data createNewData(final long tmpUID, final String originalFileName, final String sha512)
public Data createNewData(final long tmpUID, final String originalFileName, final String sha512)
throws IOException {
// determine mime type:
Data injectedData = new Data();
@ -304,11 +304,9 @@ public class DataResource {
// logger.info("===================================================");
final Data value = getSmall(uuid);
if (value == null) {
LOGGER.warn("Request data that does not exist : {}", uuid);
return Response.status(404).entity("media NOT FOUND: " + uuid).type("text/plain").build();
}
try {
LOGGER.warn("Generate stream : {}", uuid);
return buildStream(getFileData(uuid), range,
value.mimeType == null ? "application/octet-stream" : value.mimeType);
} catch (final Exception ex) {
@ -502,7 +500,7 @@ public class DataResource {
}
}
public static void undelete(final Long id) throws Exception {
public void undelete(final Long id) throws Exception {
DataAccess.unsetDelete(Data.class, id);
}