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;
|
2019-11-26 15:27:02 +01:00
|
|
|
import java.sql.ResultSet;
|
2019-11-15 16:52:21 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
@Stateless
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
public void save(Movies movies) {
|
2019-11-26 15:27:02 +01:00
|
|
|
// // TODO
|
|
|
|
// for (Actors actor : movies.getActors()) {
|
|
|
|
// ResultSet rs = em.createNamedQuery("Actors.getIdByParameters", Actors.class)
|
|
|
|
// .setParameter() // TODO
|
|
|
|
// .getResultList();
|
|
|
|
//
|
|
|
|
// // Check if we have a result, if yes -> take first, actor.id = resultID
|
|
|
|
// // If no result: context.setRollbackOnly();
|
|
|
|
// }
|
2019-11-15 16:52:21 +01:00
|
|
|
LOGGER.info("save() >> movies" + movies);
|
|
|
|
|
2019-11-26 15:27:02 +01:00
|
|
|
em.merge(movies);
|
2019-11-15 16:52:21 +01:00
|
|
|
}
|
|
|
|
}
|