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 @TransactionManagement(value=TransactionManagementType.CONTAINER) 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 findByTitle(String title) { LOGGER.info("findByTitle() >> title=" + title); return em.createNamedQuery("Movies.selectByTitle", Movies.class) .setParameter("title", "%" + title + "%") .getResultList(); } public List 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 } public List findAllActors() { LOGGER.info("findAllActors)"); return em.createNamedQuery("Actors.selectAllActors", Actors.class) .getResultList(); } public List findAllStudios() { LOGGER.info("findAllStudios)"); return em.createNamedQuery("Studios.selectAllStudios", Studios.class) .getResultList(); } // TODO maybe check if the movie already exists? @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void save(List 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 allActors = findAllActors(); List allStudios = findAllStudios(); try { // Get studio for the movie from the database Studios movieStudio = movie.getStudio(); // 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; } // 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(); } } LOGGER.info("save() >> movies" + movies); for (Movies movie : movies) { em.merge(movie); } } }