2019-11-15 16:52:21 +01:00
|
|
|
package at.technikumwien.movies;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import javax.ejb.*;
|
|
|
|
import javax.persistence.EntityManager;
|
|
|
|
import javax.persistence.EntityNotFoundException;
|
|
|
|
import javax.persistence.PersistenceContext;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
@Stateless
|
2019-11-28 19:24:14 +01:00
|
|
|
@TransactionManagement(value=TransactionManagementType.CONTAINER)
|
2019-11-15 16:52:21 +01:00
|
|
|
public class MoviesService {
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(MoviesService.class.getName());
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
private SessionContext context;
|
|
|
|
|
|
|
|
@PersistenceContext(unitName = "MoviesPU")
|
|
|
|
private EntityManager em;
|
|
|
|
|
|
|
|
public Movies findById(long id) {
|
|
|
|
LOGGER.info("findById() >> id=" + id);
|
|
|
|
|
|
|
|
Movies movies = em.find(Movies.class, id);
|
|
|
|
if (movies == null) {
|
|
|
|
throw new EntityNotFoundException("can't find movie with id=" + id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return movies;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Movies> findByTitle(String title) {
|
|
|
|
LOGGER.info("findByTitle() >> title=" + title);
|
|
|
|
|
|
|
|
return em.createNamedQuery("Movies.selectByTitle", Movies.class)
|
|
|
|
.setParameter("title", "%" + title + "%")
|
|
|
|
.getResultList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Movies> findAll() {
|
|
|
|
LOGGER.info("findAll()");
|
|
|
|
|
|
|
|
return em.createNamedQuery("Movies.selectAll", Movies.class) //JPQL -> java persistence query language
|
|
|
|
.getResultList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeById(long id) {
|
|
|
|
LOGGER.info("removeById() >> id=" + id);
|
|
|
|
|
|
|
|
Movies movies = findById(id);
|
|
|
|
em.remove(movies); //managed news required
|
|
|
|
}
|
|
|
|
|
2019-11-28 19:24:14 +01:00
|
|
|
public List<Actors> findAllActors() {
|
|
|
|
LOGGER.info("findAllActors)");
|
|
|
|
|
|
|
|
return em.createNamedQuery("Actors.selectAllActors", Actors.class)
|
|
|
|
.getResultList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Studios> findAllStudios() {
|
|
|
|
LOGGER.info("findAllStudios)");
|
|
|
|
|
|
|
|
return em.createNamedQuery("Studios.selectAllStudios", Studios.class)
|
|
|
|
.getResultList();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO maybe check if the movie already exists?
|
|
|
|
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
|
2019-11-28 19:43:07 +01:00
|
|
|
public void save(List<Movies> movies) {
|
|
|
|
|
|
|
|
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<Actors> allActors = findAllActors();
|
|
|
|
List<Studios> allStudios = findAllStudios();
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Get studio for the movie from the database
|
|
|
|
Studios movieStudio = movie.getStudio();
|
2019-11-28 19:24:14 +01:00
|
|
|
|
2019-11-28 19:43:07 +01:00
|
|
|
// 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())) {
|
2019-11-28 19:24:14 +01:00
|
|
|
// Set id
|
2019-11-28 19:43:07 +01:00
|
|
|
movieStudio.setId(studio.getId());
|
2019-11-28 19:24:14 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-28 19:43:07 +01:00
|
|
|
if (movieStudio.getId() == null) {
|
|
|
|
LOGGER.info("Rollback! Movie studio: " + movieStudio);
|
|
|
|
context.setRollbackOnly();
|
|
|
|
return;
|
|
|
|
}
|
2019-11-28 19:24:14 +01:00
|
|
|
|
2019-11-28 19:43:07 +01:00
|
|
|
// 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();
|
2019-11-28 19:24:14 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-28 19:43:07 +01:00
|
|
|
LOGGER.info("save() >> movies" + movies);
|
|
|
|
for (Movies movie : movies) {
|
|
|
|
em.merge(movie);
|
2019-11-28 19:24:14 +01:00
|
|
|
}
|
2019-11-15 16:52:21 +01:00
|
|
|
}
|
|
|
|
}
|