75 lines
2.1 KiB
Java
75 lines
2.1 KiB
Java
package at.technikumwien.movies;
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.security.RolesAllowed;
|
|
import javax.ejb.*;
|
|
import javax.inject.Inject;
|
|
import javax.persistence.EntityManager;
|
|
import javax.persistence.EntityNotFoundException;
|
|
import javax.persistence.PersistenceContext;
|
|
import javax.security.enterprise.SecurityContext;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.logging.Logger;
|
|
|
|
@Stateless
|
|
@TransactionManagement(value=TransactionManagementType.CONTAINER)
|
|
public class StudiosService {
|
|
private static final Logger LOGGER = Logger.getLogger(StudiosService.class.getName());
|
|
|
|
@Resource
|
|
private SessionContext context;
|
|
|
|
@PersistenceContext(unitName = "MoviesPU")
|
|
private EntityManager em;
|
|
|
|
@Inject
|
|
private SecurityContext securityContext;
|
|
|
|
public Studio findStudioById(long id) {
|
|
LOGGER.info("findStudioById() >> id=" + id);
|
|
|
|
Studio studio = em.find(Studio.class, id);
|
|
if (studio == null) {
|
|
throw new EntityNotFoundException("can't find studio with id=" + id);
|
|
}
|
|
|
|
return studio;
|
|
}
|
|
|
|
public List<Studio> findAllStudios() {
|
|
LOGGER.info("findAllStudios)");
|
|
|
|
return em.createNamedQuery("Studios.selectAllStudios", Studio.class)
|
|
.getResultList();
|
|
}
|
|
|
|
public void removeStudioById(long id) {
|
|
LOGGER.info("removeStudioById() >> id=" + id);
|
|
|
|
Studio studio = findStudioById(id);
|
|
em.remove(studio);
|
|
}
|
|
|
|
// TODO maybe check if the studio already exists?
|
|
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
|
|
public List<Studio> saveStudio(List<Studio> studios) {
|
|
|
|
try {
|
|
List<Studio> addedStudios = new ArrayList<>();
|
|
|
|
LOGGER.info("save() >> studios" + studios);
|
|
for (Studio studioToImport : studios) {
|
|
addedStudios.add(em.merge(studioToImport));
|
|
}
|
|
|
|
return addedStudios;
|
|
} catch (Exception e) {
|
|
LOGGER.info("Rollback when adding studio!");
|
|
context.setRollbackOnly();
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
} |