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"> <component name="Encoding">
<file url="file://$PROJECT_DIR$" charset="UTF-8" /> <file url="file://$PROJECT_DIR$" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/MoviesClient" 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" 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" 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> </component>
</project> </project>

View File

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

View File

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