[DEV] continue migration of the Zood and UUID for Data

This commit is contained in:
Edouard DUPIN 2024-03-03 08:49:20 +01:00
parent 6584022861
commit 389e4138c3
4 changed files with 79 additions and 40 deletions

View File

@ -25,20 +25,11 @@
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>

View File

@ -16,6 +16,7 @@ import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;
import javax.imageio.ImageIO;
@ -92,6 +93,21 @@ public class DataResource {
}
return filePath;
}
public static String getFileData(final UUID uuid) {
final String stringUUID = uuid.toString();
final String part1 = stringUUID.substring(0, 2);
final String part2 = stringUUID.substring(2, 4);
final String part3 = stringUUID.substring(4);
final String finalPath = part1 + File.separator + part2+ File.separator + part3;
String filePath = ConfigBaseVariable.getMediaDataFolder() + "_uuid" + File.separator + finalPath + File.separator;
try {
createFolder(filePath);
} catch (final IOException e) {
e.printStackTrace();
}
filePath += "data";
return filePath;
}
public static Data getWithSha512(final String sha512) {
LOGGER.info("find sha512 = {}", sha512);
@ -148,6 +164,24 @@ public class DataResource {
LOGGER.info("Move done");
return injectedData;
}
public static void modeFileOldModelToNewModel(final long id, final UUID uuid) throws IOException {
String mediaCurentPath = getFileData(id);
String mediaDestPath = getFileData(uuid);
LOGGER.info("src = {}", mediaCurentPath);
LOGGER.info("dst = {}", mediaDestPath);
if (Files.exists(Paths.get(mediaCurentPath))) {
LOGGER.info("move: {} ==> {}", mediaCurentPath, mediaDestPath);
Files.move(Paths.get(mediaCurentPath), Paths.get(mediaDestPath), StandardCopyOption.ATOMIC_MOVE);
}
// Move old meta-data...
mediaCurentPath = mediaCurentPath.substring(mediaCurentPath.length()-4) + "meta.json";
mediaDestPath = mediaCurentPath.substring(mediaDestPath.length()-4) + "meta.json";
if (Files.exists(Paths.get(mediaCurentPath))) {
LOGGER.info("moveM: {} ==> {}", mediaCurentPath, mediaDestPath);
Files.move(Paths.get(mediaCurentPath), Paths.get(mediaDestPath), StandardCopyOption.ATOMIC_MOVE);
}
LOGGER.info("Move done");
}
public static String saveTemporaryFile(final InputStream uploadedInputStream, final long idData) {
return saveFile(uploadedInputStream, DataResource.getTmpFileInData(idData));

View File

@ -1,7 +1,7 @@
package org.kar.archidata.dataAccess;
import java.io.FileWriter;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
@ -21,48 +21,48 @@ import org.slf4j.LoggerFactory;
public class DataFactoryZod {
static final Logger LOGGER = LoggerFactory.getLogger(DataFactoryZod.class);
public static String convertTypeZod(final Class<?> type) throws Exception {
public static String convertTypeZodSimpleType(final Class<?> type, final Map<String, String> previousClassesGenerated, final List<String> order) throws Exception {
if (type == UUID.class) {
return "string()";
return "zod.string().uuid()";
}
if (type == Long.class) {
return "bigint()";
return "zod.bigint()";
}
if (type == long.class) {
return "bigint()";
return "zod.bigint()";
}
if (type == Integer.class || type == int.class) {
return "number().safe()";
return "zod.number().safe()";
}
if (type == Boolean.class || type == boolean.class) {
return "boolean()";
return "zod.boolean()";
}
if (type == double.class || type == float.class || type == Double.class || type == Float.class) {
return "number()";
return "zod.number()";
}
if (type == Instant.class) {
return "string().utc()";
return "zod.string().utc()";
}
if (type == Date.class || type == Timestamp.class) {
return "date()";
return "zod.date()";
}
if (type == LocalDate.class) {
return "date()";
return "zod.date()";
}
if (type == LocalTime.class) {
return "date()";
return "zod.date()";
}
if (type == String.class) {
return "string()";
return "zod.string()";
}
if (type.isEnum()) {
final Object[] arr = type.getEnumConstants();
final StringBuilder out = new StringBuilder();
boolean first = true;
out.append("enum([");
out.append("zod.enum([");
for (final Object elem : arr) {
if (!first) {
out.append(",");
out.append(", ");
}
first = false;
out.append("\"");
@ -72,18 +72,35 @@ public class DataFactoryZod {
out.append("])");
return out.toString();
}
if (type == List.class) {
return null;
}
createTable(type, previousClassesGenerated, order);
return type.getSimpleName();
}
public static String convertTypeZod(final Field field, final Map<String, String> previousClassesGenerated, final List<String> order) throws Exception {
final Class<?> type = field.getType();
final String simpleType = convertTypeZodSimpleType(type, previousClassesGenerated, order);
if (simpleType != null) {
return simpleType;
}
if (type == List.class) {
final ParameterizedType listType = (ParameterizedType) field.getGenericType();
final Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0];
final String simpleSubType = convertTypeZodSimpleType(listClass, previousClassesGenerated, order);
return "zod.array(" + simpleSubType + ")";
}
throw new DataAccessException("Imcompatible type of element in object for: " + type.getCanonicalName());
}
public static String optionalTypeZod(final Class<?> type) throws Exception {
if (type.isEnum() || type == UUID.class || type == Long.class || type == Integer.class || type == Boolean.class | type == Double.class || type == Float.class || type == Instant.class
|| type == Date.class || type == Timestamp.class || type == LocalDate.class || type == LocalTime.class || type == String.class) {
return ".optional()";
}
if (type.isPrimitive()) {
return "";
}
return ".optional()";
}
public static void createTablesSpecificType(final Field elem, final int fieldId, final StringBuilder builder) throws Exception {
public static void createTablesSpecificType(final Field elem, final int fieldId, final StringBuilder builder, final Map<String, String> previousClassesGenerated, final List<String> order) throws Exception {
final String name = elem.getName();
final Class<?> classModel = elem.getType();
final int limitSize = AnnotationTools.getLimitSize(elem);
@ -99,8 +116,8 @@ public class DataFactoryZod {
}
builder.append("\n\t");
builder.append(name);
builder.append(": zod.");
builder.append(convertTypeZod(classModel));
builder.append(": ");
builder.append(convertTypeZod(elem, previousClassesGenerated, order));
if (limitSize > 0 && classModel == String.class) {
builder.append(".max(");
builder.append(limitSize);
@ -156,9 +173,6 @@ public class DataFactoryZod {
generatedData.append("\n\n");
}
LOGGER.info("generated: {}", generatedData.toString());
final FileWriter myWriter = new FileWriter("api.ts");
myWriter.write(generatedData.toString());
myWriter.close();
return generatedData.toString();
}
@ -189,7 +203,7 @@ public class DataFactoryZod {
}
alreadyAdded.add(dataName);
LOGGER.trace(" + '{}'", elem.getName());
if (DataAccess.isAddOnField(elem)) {
if (false && DataAccess.isAddOnField(elem)) {
final DataAccessAddOn addOn = DataAccess.findAddOnforField(elem);
LOGGER.error("Create type for: {} ==> {} (ADD-ON) ==> Not managed now ....", AnnotationTools.getFieldName(elem), elem.getType());
/* LOGGER.trace("Create type for: {} ==> {} (ADD-ON)", AnnotationTools.getFieldName(elem), elem.getType()); if (addOn != null) { addOn.createTables(tableName, elem, tmpOut,
@ -197,7 +211,7 @@ public class DataFactoryZod {
* tableName + " field name=" + AnnotationTools.getFieldName(elem) + " type=" + elem.getType()); } fieldId++; */
} else {
LOGGER.trace("Create type for: {} ==> {}", AnnotationTools.getFieldName(elem), elem.getType());
DataFactoryZod.createTablesSpecificType(elem, fieldId, internalBuilder);
DataFactoryZod.createTablesSpecificType(elem, fieldId, internalBuilder, previousClassesGenerated, order);
fieldId++;
}

View File

@ -11,7 +11,7 @@ import jakarta.persistence.Table;
@Table(name = "data")
@DataIfNotExists
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Data extends GenericDataSoftDelete {
public class Data extends UUIDGenericDataSoftDelete {
@Column(length = 128, nullable = false)
@Schema(description = "Sha512 of the data")
public String sha512;