Implemented all or none principle for the save method

This commit is contained in:
Barbara 2019-11-28 19:43:07 +01:00
parent 7c0cb4150d
commit 5cbece42bb
3 changed files with 57 additions and 51 deletions

4
.idea/encodings.xml generated
View File

@ -3,7 +3,11 @@
<component name="Encoding">
<file url="file://$PROJECT_DIR$" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/MoviesClient" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/MoviesClient/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/MoviesCommon" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/MoviesCommon/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/MoviesWebApp" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/MoviesWebApp/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/MoviesWebApp/src/main/resources" charset="UTF-8" />
</component>
</project>

View File

@ -68,64 +68,68 @@ public class MoviesService {
// TODO maybe check if the movie already exists?
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void save(Movies movie) {
public void save(List<Movies> movies) {
// TODO maybe get just the id and improve checking in for loop?
// Get all actors and studios from the database
List<Actors> allActors = findAllActors();
List<Studios> allStudios = findAllStudios();
for (Movies movie : movies) {
try {
// Get studio for the movie from the database
Studios movieStudio = movie.getStudio();
// TODO maybe get just the id and improve checking in for loop?
// Get all actors and studios from the database
List<Actors> allActors = findAllActors();
List<Studios> allStudios = findAllStudios();
// 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 (movieStudio.getId() == null) {
LOGGER.info("Rollback! Movie studio: " + movieStudio);
context.setRollbackOnly();
return;
}
try {
// Get studio for the movie from the database
Studios movieStudio = movie.getStudio();
// TODO use also birthday
// 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)
.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())) {
// 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
movieActor.setId(actor.getId());
}
if (movieActor.getId() == null) {
LOGGER.info("Rollback! Movie actor: " + movieActor);
context.setRollbackOnly();
return;
movieStudio.setId(studio.getId());
}
}
if (movieStudio.getId() == null) {
LOGGER.info("Rollback! Movie studio: " + movieStudio);
context.setRollbackOnly();
return;
}
LOGGER.info("save() >> movies" + movie);
em.merge(movie);
// TODO use also birthday
// 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)
.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 (movieActor.getId() == null) {
LOGGER.info("Rollback! Movie actor: " + movieActor);
context.setRollbackOnly();
return;
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace();
LOGGER.info("save() >> movies" + movies);
for (Movies movie : movies) {
em.merge(movie);
}
}
}

View File

@ -28,8 +28,6 @@ public class MoviesWebServiceImpl implements MoviesWebService {
@Override
public void importMovies(List<Movies> movies) {
for (Movies movie : movies) {
moviesService.save(movie);
}
moviesService.save(movies);
}
}