[FIX] correct the ID in uuid for uuid primary-key

This commit is contained in:
Edouard DUPIN 2024-06-12 20:08:26 +02:00
parent 15688f93e5
commit d4eb9c2a5f
4 changed files with 45 additions and 30 deletions

View File

@ -188,9 +188,21 @@ public class AddOnDataJson implements DataAccessAddOn {
DataAccess.update(data, data.id, List.of("covers"), new OverrideTableName(tableName)); DataAccess.update(data, data.id, List.of("covers"), new OverrideTableName(tableName));
} }
/**
* Adds a remoteKey to the covers list of a data entry identified by the given class type and ID.
* If the covers list is null, it initializes it. If the remoteKey already exists in the list,
* the method returns without making any changes.
*
* @param clazz The class type to retrieve the table name from.
* @param id The ID of the data object to fetch.
* @param column The name of the column (currently not used, but may be used for specifying a field name).
* @param remoteKey The UUID to add to the covers list.
* @throws Exception If an error occurs during data retrieval or update.
*/
public static void addLink(final Class<?> clazz, final Long id, final String column, final UUID remoteKey) public static void addLink(final Class<?> clazz, final Long id, final String column, final UUID remoteKey)
throws Exception { throws Exception {
final String tableName = AnnotationTools.getTableName(clazz); final String tableName = AnnotationTools.getTableName(clazz);
// TODO: Get primary key name
final TableCoversLongUUID data = DataAccess.get(TableCoversLongUUID.class, id, final TableCoversLongUUID data = DataAccess.get(TableCoversLongUUID.class, id,
new OverrideTableName(tableName)); new OverrideTableName(tableName));
if (data.covers == null) { if (data.covers == null) {
@ -205,10 +217,21 @@ public class AddOnDataJson implements DataAccessAddOn {
DataAccess.update(data, data.id, List.of("covers"), new OverrideTableName(tableName));// TODO: ,new OverrideFieldName("covers", column)); DataAccess.update(data, data.id, List.of("covers"), new OverrideTableName(tableName));// TODO: ,new OverrideFieldName("covers", column));
} }
public static void addLink(final Class<?> clazz, final UUID id, final String column, final UUID remoteKey) /**
* Adds a remoteKey to the covers list of a data entry identified by the given class type and ID.
* If the covers list is null, it initializes it. If the remoteKey already exists in the list,
* the method returns without making any changes.
*
* @param clazz The class type to retrieve the table name from.
* @param id The ID of the data object to fetch.
* @param column The name of the column (currently not used, but may be used for specifying a field name).
* @param remoteKey The UUID to add to the covers list.
* @throws Exception If an error occurs during data retrieval or update.
*/
public static void addLink(final Class<?> clazz, final UUID uuid, final String column, final UUID remoteKey)
throws Exception { throws Exception {
final String tableName = AnnotationTools.getTableName(clazz); final String tableName = AnnotationTools.getTableName(clazz);
final TableCoversUUIDUUID data = DataAccess.get(TableCoversUUIDUUID.class, id, final TableCoversUUIDUUID data = DataAccess.get(TableCoversUUIDUUID.class, uuid,
new OverrideTableName(tableName)); new OverrideTableName(tableName));
if (data.covers == null) { if (data.covers == null) {
data.covers = new ArrayList<>(); data.covers = new ArrayList<>();
@ -222,10 +245,10 @@ public class AddOnDataJson implements DataAccessAddOn {
DataAccess.update(data, data.id, List.of("covers"), new OverrideTableName(tableName)); DataAccess.update(data, data.id, List.of("covers"), new OverrideTableName(tableName));
} }
public static void addLink(final Class<?> clazz, final UUID id, final String column, final Long remoteKey) public static void addLink(final Class<?> clazz, final UUID uuid, final String column, final Long remoteKey)
throws Exception { throws Exception {
final String tableName = AnnotationTools.getTableName(clazz); final String tableName = AnnotationTools.getTableName(clazz);
final TableCoversUUIDLong data = DataAccess.get(TableCoversUUIDLong.class, id, final TableCoversUUIDLong data = DataAccess.get(TableCoversUUIDLong.class, uuid,
new OverrideTableName(tableName)); new OverrideTableName(tableName));
if (data.covers == null) { if (data.covers == null) {
data.covers = new ArrayList<>(); data.covers = new ArrayList<>();
@ -236,13 +259,13 @@ public class AddOnDataJson implements DataAccessAddOn {
} }
} }
data.covers.add(remoteKey); data.covers.add(remoteKey);
DataAccess.update(data, data.id, List.of("covers"), new OverrideTableName(tableName)); DataAccess.update(data, data.uuid, List.of("covers"), new OverrideTableName(tableName));
} }
public static void removeLink(final Class<?> clazz, final UUID id, final String column, final Long remoteKey) public static void removeLink(final Class<?> clazz, final UUID uuid, final String column, final Long remoteKey)
throws Exception { throws Exception {
final String tableName = AnnotationTools.getTableName(clazz); final String tableName = AnnotationTools.getTableName(clazz);
final TableCoversUUIDLong data = DataAccess.get(TableCoversUUIDLong.class, id, final TableCoversUUIDLong data = DataAccess.get(TableCoversUUIDLong.class, uuid,
new OverrideTableName(tableName)); new OverrideTableName(tableName));
if (data.covers == null) { if (data.covers == null) {
return; return;
@ -255,13 +278,13 @@ public class AddOnDataJson implements DataAccessAddOn {
newList.add(elem); newList.add(elem);
} }
data.covers = newList; data.covers = newList;
DataAccess.update(data, data.id, List.of("covers"), new OverrideTableName(tableName)); DataAccess.update(data, data.uuid, List.of("covers"), new OverrideTableName(tableName));
} }
public static void removeLink(final Class<?> clazz, final UUID id, final String column, final UUID remoteKey) public static void removeLink(final Class<?> clazz, final UUID uuid, final String column, final UUID remoteKey)
throws Exception { throws Exception {
final String tableName = AnnotationTools.getTableName(clazz); final String tableName = AnnotationTools.getTableName(clazz);
final TableCoversUUIDUUID data = DataAccess.get(TableCoversUUIDUUID.class, id, final TableCoversUUIDUUID data = DataAccess.get(TableCoversUUIDUUID.class, uuid,
new OverrideTableName(tableName)); new OverrideTableName(tableName));
if (data.covers == null) { if (data.covers == null) {
return; return;
@ -314,9 +337,4 @@ public class AddOnDataJson implements DataAccessAddOn {
data.covers = newList; data.covers = newList;
DataAccess.update(data, data.id, List.of("covers"), new OverrideTableName(tableName)); DataAccess.update(data, data.id, List.of("covers"), new OverrideTableName(tableName));
} }
/* public static <TYPE> void addLink(final Class<TYPE> clazz, final Object localKey, final String column, final Object remoteKey) throws Exception { final String tableName =
* AnnotationTools.getTableName(clazz); final TYPE data = DataAccess.get(clazz, localKey); // TODO: add filter of the "column" // find the field column: // add the remoteKey in the list: // post
* new data in the DB final String linkTableName = generateLinkTableName(this.tableName, this.column); final LinkTable insertElement = new LinkTable(this.localKey, this.remoteKey);
* DataAccess.insert(insertElement, new OverrideTableName(linkTableName)); } */
} }

View File

@ -4,11 +4,10 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
import org.kar.archidata.annotation.DataJson; import org.kar.archidata.annotation.DataJson;
import org.kar.archidata.model.GenericDataSoftDelete;
import jakarta.persistence.Column; import jakarta.persistence.Id;
public class TableCoversLongUUID extends GenericDataSoftDelete { public class TableCoversLongUUID {
public TableCoversLongUUID() { public TableCoversLongUUID() {
// nothing to do... // nothing to do...
} }
@ -18,8 +17,10 @@ public class TableCoversLongUUID extends GenericDataSoftDelete {
this.covers = covers; this.covers = covers;
} }
@Id
public Long id;
@DataJson() @DataJson()
@Column(nullable = false)
public List<UUID> covers; public List<UUID> covers;
} }

View File

@ -4,26 +4,22 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
import org.kar.archidata.annotation.DataJson; import org.kar.archidata.annotation.DataJson;
import org.kar.archidata.model.GenericDataSoftDelete;
import jakarta.persistence.Column;
import jakarta.persistence.Id; import jakarta.persistence.Id;
public class TableCoversUUIDLong extends GenericDataSoftDelete { public class TableCoversUUIDLong {
public TableCoversUUIDLong() { public TableCoversUUIDLong() {
// nothing to do... // nothing to do...
} }
public TableCoversUUIDLong(final UUID id, final List<Long> covers) { public TableCoversUUIDLong(final UUID uuid, final List<Long> covers) {
this.id = id; this.uuid = uuid;
this.covers = covers; this.covers = covers;
} }
@Column(nullable = false)
@Id @Id
public UUID id; public UUID uuid;
@DataJson() @DataJson()
@Column(nullable = false)
public List<Long> covers; public List<Long> covers;
} }

View File

@ -14,14 +14,14 @@ public class TableCoversUUIDUUID extends GenericDataSoftDelete {
// nothing to do... // nothing to do...
} }
public TableCoversUUIDUUID(final UUID id, final List<UUID> covers) { public TableCoversUUIDUUID(final UUID uuid, final List<UUID> covers) {
this.id = id; this.uuid = uuid;
this.covers = covers; this.covers = covers;
} }
@Column(nullable = false) @Column(nullable = false)
@Id @Id
public UUID id; public UUID uuid;
@DataJson() @DataJson()
@Column(nullable = false) @Column(nullable = false)
public List<UUID> covers; public List<UUID> covers;