[DEV] correct the backend can not insert trach artist ... fail t in archidata model

This commit is contained in:
Edouard DUPIN 2024-02-26 00:01:05 +01:00
parent 619973459a
commit 0af1bbfb52
10 changed files with 85 additions and 35 deletions

View File

@ -20,7 +20,7 @@
<dependency> <dependency>
<groupId>kangaroo-and-rabbit</groupId> <groupId>kangaroo-and-rabbit</groupId>
<artifactId>archidata</artifactId> <artifactId>archidata</artifactId>
<version>0.6.1</version> <version>0.6.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>

View File

@ -29,6 +29,7 @@ import org.kar.karusic.api.UserResource;
import org.kar.karusic.filter.KarusicAuthenticationFilter; import org.kar.karusic.filter.KarusicAuthenticationFilter;
import org.kar.karusic.migration.Initialization; import org.kar.karusic.migration.Initialization;
import org.kar.karusic.migration.Migration20231126; import org.kar.karusic.migration.Migration20231126;
import org.kar.karusic.migration.Migration20240225;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -54,6 +55,7 @@ public class WebLauncher {
migrationEngine.setInit(new Initialization()); migrationEngine.setInit(new Initialization());
WebLauncher.LOGGER.info("Add migration since last version"); WebLauncher.LOGGER.info("Add migration since last version");
migrationEngine.add(new Migration20231126()); migrationEngine.add(new Migration20231126());
migrationEngine.add(new Migration20240225());
WebLauncher.LOGGER.info("Migrate the DB [START]"); WebLauncher.LOGGER.info("Migrate the DB [START]");
migrationEngine.migrateWaitAdmin(GlobalConfiguration.dbConfig); migrationEngine.migrateWaitAdmin(GlobalConfiguration.dbConfig);
WebLauncher.LOGGER.info("Migrate the DB [STOP]"); WebLauncher.LOGGER.info("Migrate the DB [STOP]");

View File

@ -9,6 +9,8 @@ import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.addOn.AddOnManyToMany; import org.kar.archidata.dataAccess.addOn.AddOnManyToMany;
import org.kar.archidata.tools.DataTools; import org.kar.archidata.tools.DataTools;
import org.kar.karusic.model.Album; import org.kar.karusic.model.Album;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.annotation.security.RolesAllowed; import jakarta.annotation.security.RolesAllowed;
import jakarta.ws.rs.Consumes; import jakarta.ws.rs.Consumes;
@ -25,6 +27,7 @@ import jakarta.ws.rs.core.Response;
@Path("/album") @Path("/album")
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
public class AlbumResource { public class AlbumResource {
private static final Logger LOGGER = LoggerFactory.getLogger(AlbumResource.class);
@GET @GET
@Path("{id}") @Path("{id}")

View File

@ -9,6 +9,8 @@ import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.addOn.AddOnManyToMany; import org.kar.archidata.dataAccess.addOn.AddOnManyToMany;
import org.kar.archidata.tools.DataTools; import org.kar.archidata.tools.DataTools;
import org.kar.karusic.model.Artist; import org.kar.karusic.model.Artist;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.annotation.security.RolesAllowed; import jakarta.annotation.security.RolesAllowed;
import jakarta.ws.rs.Consumes; import jakarta.ws.rs.Consumes;
@ -25,6 +27,7 @@ import jakarta.ws.rs.core.Response;
@Path("/artist") @Path("/artist")
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
public class ArtistResource { public class ArtistResource {
private static final Logger LOGGER = LoggerFactory.getLogger(ArtistResource.class);
@GET @GET
@Path("{id}") @Path("{id}")

View File

@ -9,6 +9,8 @@ import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.addOn.AddOnManyToMany; import org.kar.archidata.dataAccess.addOn.AddOnManyToMany;
import org.kar.archidata.tools.DataTools; import org.kar.archidata.tools.DataTools;
import org.kar.karusic.model.Gender; import org.kar.karusic.model.Gender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.annotation.security.RolesAllowed; import jakarta.annotation.security.RolesAllowed;
import jakarta.ws.rs.Consumes; import jakarta.ws.rs.Consumes;
@ -25,6 +27,7 @@ import jakarta.ws.rs.core.Response;
@Path("/gender") @Path("/gender")
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
public class GenderResource { public class GenderResource {
private static final Logger LOGGER = LoggerFactory.getLogger(GenderResource.class);
@GET @GET
@Path("{id}") @Path("{id}")

View File

@ -9,6 +9,8 @@ import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.addOn.AddOnManyToMany; import org.kar.archidata.dataAccess.addOn.AddOnManyToMany;
import org.kar.archidata.tools.DataTools; import org.kar.archidata.tools.DataTools;
import org.kar.karusic.model.Playlist; import org.kar.karusic.model.Playlist;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.annotation.security.RolesAllowed; import jakarta.annotation.security.RolesAllowed;
import jakarta.ws.rs.Consumes; import jakarta.ws.rs.Consumes;
@ -25,6 +27,7 @@ import jakarta.ws.rs.core.Response;
@Path("/playlist") @Path("/playlist")
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
public class PlaylistResource { public class PlaylistResource {
private static final Logger LOGGER = LoggerFactory.getLogger(PlaylistResource.class);
@GET @GET
@Path("{id}") @Path("{id}")

View File

@ -18,6 +18,8 @@ import org.kar.karusic.model.Album;
import org.kar.karusic.model.Artist; import org.kar.karusic.model.Artist;
import org.kar.karusic.model.Gender; import org.kar.karusic.model.Gender;
import org.kar.karusic.model.Track; import org.kar.karusic.model.Track;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.annotation.security.RolesAllowed; import jakarta.annotation.security.RolesAllowed;
import jakarta.ws.rs.Consumes; import jakarta.ws.rs.Consumes;
@ -34,6 +36,7 @@ import jakarta.ws.rs.core.Response;
@Path("/track") @Path("/track")
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
public class TrackResource { public class TrackResource {
private static final Logger LOGGER = LoggerFactory.getLogger(TrackResource.class);
@GET @GET
@Path("{id}") @Path("{id}")
@ -110,10 +113,18 @@ public class TrackResource {
@Path("/upload/") @Path("/upload/")
@RolesAllowed("ADMIN") @RolesAllowed("ADMIN")
@Consumes({ MediaType.MULTIPART_FORM_DATA }) @Consumes({ MediaType.MULTIPART_FORM_DATA })
public Response uploadFile(@FormDataParam("fileName") String fileName, @FormDataParam("gender") String gender, @FormDataParam("artist") String artist, // Formatter:off
public Response uploadFile(
@FormDataParam("fileName") String fileName,
@FormDataParam("gender") String gender,
@FormDataParam("artist") String artist,
//@FormDataParam("seriesId") String seriesId, Not used ... //@FormDataParam("seriesId") String seriesId, Not used ...
@FormDataParam("album") String album, @FormDataParam("trackId") String trackId, @FormDataParam("title") String title, @FormDataParam("file") final InputStream fileInputStream, @FormDataParam("album") String album,
@FormDataParam("trackId") String trackId,
@FormDataParam("title") String title,
@FormDataParam("file") final InputStream fileInputStream,
@FormDataParam("file") final FormDataContentDisposition fileMetaData) { @FormDataParam("file") final FormDataContentDisposition fileMetaData) {
// Formatter:on
try { try {
// correct input string stream : // correct input string stream :
fileName = DataTools.multipartCorrection(fileName); fileName = DataTools.multipartCorrection(fileName);
@ -124,16 +135,15 @@ public class TrackResource {
title = DataTools.multipartCorrection(title); title = DataTools.multipartCorrection(title);
//public NodeSmall uploadFile(final FormDataMultiPart form) { //public NodeSmall uploadFile(final FormDataMultiPart form) {
System.out.println("Upload media file: " + fileMetaData); LOGGER.info("Upload media file: " + fileMetaData);
System.out.println(" - fileName: " + fileName); LOGGER.info(" > fileName: " + fileName);
System.out.println(" - gender: " + gender); LOGGER.info(" > gender: " + gender);
System.out.println(" - artist: " + artist); LOGGER.info(" > artist: " + artist);
System.out.println(" - album: " + album); LOGGER.info(" > album: " + album);
System.out.println(" - trackId: " + trackId); LOGGER.info(" > trackId: " + trackId);
System.out.println(" - title: " + title); LOGGER.info(" > title: " + title);
System.out.println(" - fileInputStream: " + fileInputStream); LOGGER.info(" > fileInputStream: " + fileInputStream);
System.out.println(" - fileMetaData: " + fileMetaData); LOGGER.info(" > fileMetaData: " + fileMetaData);
System.out.flush();
/* /*
if (typeId == null) { if (typeId == null) {
return Response.status(406). return Response.status(406).
@ -147,8 +157,8 @@ public class TrackResource {
final String sha512 = DataTools.saveTemporaryFile(fileInputStream, tmpUID); final String sha512 = DataTools.saveTemporaryFile(fileInputStream, tmpUID);
Data data = DataTools.getWithSha512(sha512); Data data = DataTools.getWithSha512(sha512);
if (data == null) { if (data == null) {
System.out.println("Need to add the data in the BDD ... "); LOGGER.info("Need to add the data in the BDD ... ");
System.out.flush();
try { try {
data = DataTools.createNewData(tmpUID, fileName, sha512); data = DataTools.createNewData(tmpUID, fileName, sha512);
} catch (final IOException ex) { } catch (final IOException ex) {
@ -161,16 +171,14 @@ public class TrackResource {
return Response.notModified("Error in SQL insertion ...").build(); return Response.notModified("Error in SQL insertion ...").build();
} }
} else if (data.deleted) { } else if (data.deleted) {
System.out.println("Data already exist but deleted"); LOGGER.info("Data already exist but deleted");
System.out.flush();
DataTools.undelete(data.id); DataTools.undelete(data.id);
data.deleted = false; data.deleted = false;
} else { } else {
System.out.println("Data already exist ... all good"); LOGGER.info("Data already exist ... all good");
System.out.flush();
} }
// Fist step: retrieve all the Id of each parents:... // Fist step: retrieve all the Id of each parents:...
System.out.println("Find typeNode"); LOGGER.info("Find typeNode");
Gender genderElem = null; Gender genderElem = null;
if (gender != null) { if (gender != null) {
genderElem = DataAccess.getWhere(Gender.class, new Condition(new QueryCondition("name", "=", gender))); genderElem = DataAccess.getWhere(Gender.class, new Condition(new QueryCondition("name", "=", gender)));
@ -185,18 +193,20 @@ public class TrackResource {
// DataTools.removeTemporaryFile(tmpUID); // DataTools.removeTemporaryFile(tmpUID);
// return Response.notModified("TypeId does not exist ...").build(); // return Response.notModified("TypeId does not exist ...").build();
// } // }
System.out.println(" ==> " + genderElem); LOGGER.info(" ==> genderElem={}", genderElem);
Artist artistElem = null; Artist artistElem = null;
if (artist != null) { if (artist != null) {
LOGGER.info(" Try to find Artist: '{}'", artist);
artistElem = DataAccess.getWhere(Artist.class, new Condition(new QueryCondition("name", "=", artist))); artistElem = DataAccess.getWhere(Artist.class, new Condition(new QueryCondition("name", "=", artist)));
if (artistElem == null) { if (artistElem == null) {
LOGGER.info(" ** Create a new one...");
artistElem = new Artist(); artistElem = new Artist();
artistElem.name = artist; artistElem.name = artist;
artistElem = DataAccess.insert(artistElem); artistElem = DataAccess.insert(artistElem);
} }
} }
System.out.println(" ==> " + artistElem); LOGGER.info(" ==> artistElem={}", artistElem);
Album albumElem = null; Album albumElem = null;
if (album != null) { if (album != null) {
@ -207,9 +217,9 @@ public class TrackResource {
albumElem = DataAccess.insert(albumElem); albumElem = DataAccess.insert(albumElem);
} }
} }
System.out.println(" ==> " + album); LOGGER.info(" ==> " + album);
System.out.println("add media"); LOGGER.info("add media");
Track trackElem = new Track(); Track trackElem = new Track();
trackElem.name = title; trackElem.name = title;
@ -231,7 +241,7 @@ public class TrackResource {
*/ */
return Response.ok(trackElem).build(); return Response.ok(trackElem).build();
} catch (final Exception ex) { } catch (final Exception ex) {
System.out.println("Catch an unexpected error ... " + ex.getMessage()); LOGGER.info("Catch an unexpected error ... {}", ex.getMessage());
ex.printStackTrace(); ex.printStackTrace();
return Response.status(417).entity("Back-end error : " + ex.getMessage()).type("text/plain").build(); return Response.status(417).entity("Back-end error : " + ex.getMessage()).type("text/plain").build();
} }

View File

@ -22,7 +22,7 @@ import jakarta.ws.rs.core.SecurityContext;
@Path("/users") @Path("/users")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public class UserResource { public class UserResource {
final Logger logger = LoggerFactory.getLogger(UserResource.class); private static final Logger LOGGER = LoggerFactory.getLogger(UserResource.class);
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(JsonInclude.Include.NON_NULL)
public class UserOut { public class UserOut {
@ -42,7 +42,7 @@ public class UserResource {
@GET @GET
@RolesAllowed("ADMIN") @RolesAllowed("ADMIN")
public List<UserKarusic> getUsers() { public List<UserKarusic> getUsers() {
System.out.println("getUsers"); LOGGER.info("getUsers");
try { try {
return DataAccess.gets(UserKarusic.class); return DataAccess.gets(UserKarusic.class);
} catch (final Exception e) { } catch (final Exception e) {
@ -57,11 +57,11 @@ public class UserResource {
@Path("{id}") @Path("{id}")
@RolesAllowed("ADMIN") @RolesAllowed("ADMIN")
public UserKarusic getUsers(@Context final SecurityContext sc, @PathParam("id") final long userId) { public UserKarusic getUsers(@Context final SecurityContext sc, @PathParam("id") final long userId) {
System.out.println("getUser " + userId); LOGGER.info("getUser {}", userId);
final GenericContext gc = (GenericContext) sc.getUserPrincipal(); final GenericContext gc = (GenericContext) sc.getUserPrincipal();
System.out.println("==================================================="); LOGGER.info("===================================================");
System.out.println("== USER ? " + gc.userByToken.name); LOGGER.info("== USER {} ", gc.userByToken.name);
System.out.println("==================================================="); LOGGER.info("===================================================");
try { try {
return DataAccess.get(UserKarusic.class, userId); return DataAccess.get(UserKarusic.class, userId);
} catch (final Exception e) { } catch (final Exception e) {
@ -75,9 +75,9 @@ public class UserResource {
@Path("me") @Path("me")
@RolesAllowed("USER") @RolesAllowed("USER")
public UserOut getMe(@Context final SecurityContext sc) { public UserOut getMe(@Context final SecurityContext sc) {
this.logger.debug("getMe()"); LOGGER.debug("getMe()");
final GenericContext gc = (GenericContext) sc.getUserPrincipal(); final GenericContext gc = (GenericContext) sc.getUserPrincipal();
this.logger.debug("== USER ? {}", gc.userByToken); LOGGER.debug("== USER ? {}", gc.userByToken);
return new UserOut(gc.userByToken.id, gc.userByToken.name); return new UserOut(gc.userByToken.id, gc.userByToken.name);
} }
} }

View File

@ -0,0 +1,26 @@
package org.kar.karusic.migration;
import org.kar.archidata.migration.MigrationSqlStep;
public class Migration20240225 extends MigrationSqlStep {
public static final int KARSO_INITIALISATION_ID = 1;
@Override
public String getName() {
return "migration-2024-02-25: change model of thrack to use real json";
}
public Migration20240225() {
}
@Override
public void generateStep() throws Exception {
// update migration update (last one)
addAction("""
UPDATE `track` SET artists = CONCAT('[', artists, ']') WHERE artists IS NOT NULL
""");
}
}

View File

@ -15,7 +15,7 @@ CREATE TABLE `node` (
import java.util.List; import java.util.List;
import org.kar.archidata.annotation.DataIfNotExists; import org.kar.archidata.annotation.DataIfNotExists;
import org.kar.archidata.annotation.addOn.SQLTableExternalForeinKeyAsList; import org.kar.archidata.annotation.DataJson;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
@ -31,7 +31,7 @@ public class Track extends NodeSmall {
public Long track = null; public Long track = null;
public Long dataId = null; public Long dataId = null;
//@ManyToMany(fetch = FetchType.LAZY, targetEntity = Artist.class) //@ManyToMany(fetch = FetchType.LAZY, targetEntity = Artist.class)
@SQLTableExternalForeinKeyAsList @DataJson
@Column(length = 0) @Column(length = 0)
public List<Long> artists = null; public List<Long> artists = null;