From 73c0945c4acdcb00e28797a0a26d226dd24e57ae Mon Sep 17 00:00:00 2001 From: Barbara Date: Thu, 28 Nov 2019 20:14:04 +0100 Subject: [PATCH] Used NamedQueries in the save method --- .../technikumwien/movies/MoviesService.java | 47 ++++++++----------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/MoviesWebApp/src/main/java/at/technikumwien/movies/MoviesService.java b/MoviesWebApp/src/main/java/at/technikumwien/movies/MoviesService.java index f562a1f..a30cb06 100644 --- a/MoviesWebApp/src/main/java/at/technikumwien/movies/MoviesService.java +++ b/MoviesWebApp/src/main/java/at/technikumwien/movies/MoviesService.java @@ -72,24 +72,22 @@ public class MoviesService { for (Movies movie : movies) { - // TODO maybe get just the id and improve checking in for loop? - // Get all actors and studios from the database - List allActors = findAllActors(); - List allStudios = findAllStudios(); - try { // Get studio for the movie from the database Studios movieStudio = movie.getStudio(); + LOGGER.info("Get a studio" + movieStudio); + List studioListResult = em.createNamedQuery("Studios.getIdByProperties", Studios.class) + .setParameter("name", movieStudio.getName()) + .setParameter("countrycode", movieStudio.getCountrycode()) + .setParameter("postcode", movieStudio.getPostcode()) + .getResultList(); - // If the studio is missing in the database, roll back only - for (Studios studio : allStudios) { - if (studio.getName().equals(movieStudio.getName()) & studio.getCountrycode().equals(movieStudio.getCountrycode()) - & studio.getPostcode().equals(movieStudio.getPostcode())) { - // Set id - movieStudio.setId(studio.getId()); - } + if (!studioListResult.isEmpty() && studioListResult.size() > 0) { + Long actorId = studioListResult.get(0).getId(); + movieStudio.setId(actorId); } - if (movieStudio.getId() == null) { + else + { LOGGER.info("Rollback! Movie studio: " + movieStudio); context.setRollbackOnly(); return; @@ -99,27 +97,22 @@ public class MoviesService { // Get actor for the movie from the database for (Actors movieActor : movie.getActors()) { LOGGER.info("Get an actor " + movieActor); - List actorListResult = em.createNamedQuery("Actors.getIdByProperties", Actors.class) + List actorListResult = em.createNamedQuery("Actors.getIdByProperties", Actors.class) .setParameter("firstname", movieActor.getFirstname()) .setParameter("lastname", movieActor.getLastname()) .setParameter("sex", movieActor.getSex()) //.setParameter("birthdate", movieActor.getBirthdate()) .getResultList(); - // If any actor is missing in the database, roll back only - for (Actors actor : allActors) { - if (actor.getFirstname().equals(movieActor.getFirstname()) - & actor.getLastname().equals(movieActor.getLastname()) - & actor.getSex().equals(movieActor.getSex())) { - // & actor.getBirthdate().equals(movieActor.getBirthdate())) { - // Set id - movieActor.setId(actor.getId()); + if (!actorListResult.isEmpty() && actorListResult.size() > 0) { + Long actorId = actorListResult.get(0).getId(); + movieActor.setId(actorId); } - if (movieActor.getId() == null) { - LOGGER.info("Rollback! Movie actor: " + movieActor); - context.setRollbackOnly(); - return; - } + else + { + LOGGER.info("Rollback! Movie actor: " + movieActor); + context.setRollbackOnly(); + return; } } }