Added CRUD operations for Studio
This commit is contained in:
parent
3bd40b6280
commit
e54eaf6ee3
@ -70,24 +70,6 @@ public class MoviesService {
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
public List<Studio> findAllStudios() {
|
||||
LOGGER.info("findAllStudios)");
|
||||
|
||||
return em.createNamedQuery("Studios.selectAllStudios", Studio.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// TODO maybe check if the movie already exists?
|
||||
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
|
||||
public List<Movie> save(List<Movie> movies) {
|
||||
@ -141,6 +123,7 @@ public class MoviesService {
|
||||
|
||||
List<Movie> addedMovies = new ArrayList<>();
|
||||
|
||||
// TODO: Add Rollback if error while looping!
|
||||
LOGGER.info("save() >> movies" + movies);
|
||||
for (Movie movieToImport : movies) {
|
||||
addedMovies.add(em.merge(movieToImport));
|
||||
|
@ -14,7 +14,7 @@ import java.util.List;
|
||||
@RolesAllowed("MoviesUserRole")
|
||||
public class StudioResource {
|
||||
@Inject
|
||||
private MoviesService moviesService;
|
||||
private StudiosService studiosService;
|
||||
|
||||
@Context
|
||||
private UriInfo uriInfo;
|
||||
@ -25,7 +25,7 @@ public class StudioResource {
|
||||
MediaType.APPLICATION_XML
|
||||
})
|
||||
public List<Studio> retrieveAll() {
|
||||
return moviesService.findAllStudios();
|
||||
return studiosService.findAllStudios();
|
||||
}
|
||||
|
||||
@GET
|
||||
@ -35,13 +35,13 @@ public class StudioResource {
|
||||
})
|
||||
@Path("/{id}")
|
||||
public Studio retrieve(@PathParam("id") long id) {
|
||||
return moviesService.findStudioById(id);
|
||||
return studiosService.findStudioById(id);
|
||||
}
|
||||
/*
|
||||
|
||||
@DELETE
|
||||
@Path("/{id}")
|
||||
public void delete(@PathParam("id") long id) {
|
||||
moviesService.removeStudioById(id);
|
||||
studiosService.removeStudioById(id);
|
||||
}
|
||||
|
||||
@POST
|
||||
@ -49,11 +49,11 @@ public class StudioResource {
|
||||
MediaType.APPLICATION_JSON,
|
||||
MediaType.APPLICATION_XML
|
||||
})
|
||||
public Response create(Movie movie) {
|
||||
movie.setId(null); // Make sure that a new movie is added, not overwriting existing one
|
||||
List<Movie> newMovies = moviesService.save(List.of(movie));
|
||||
public Response create(Studio studio) {
|
||||
studio.setId(null); // Make sure that a new studio is added, not overwriting existing one
|
||||
List<Studio> newStudios = studiosService.saveStudio(List.of(studio));
|
||||
|
||||
URI location = uriInfo.getAbsolutePathBuilder().path(newMovies.get(0).getId().toString()).build();
|
||||
URI location = uriInfo.getAbsolutePathBuilder().path(newStudios.get(0).getId().toString()).build();
|
||||
|
||||
return Response.created(location).build();
|
||||
}
|
||||
@ -64,8 +64,8 @@ public class StudioResource {
|
||||
MediaType.APPLICATION_XML
|
||||
})
|
||||
@Path("/{id}")
|
||||
public void update(@PathParam("id") long id, Movie movie) {
|
||||
movie.setId(id); // Make sure that a new movie is added, not overwriting existing one
|
||||
moviesService.save(List.of(movie));
|
||||
}*/
|
||||
public void update(@PathParam("id") long id, Studio studio) {
|
||||
studio.setId(id); // Make sure that a new studio is added, not overwriting existing one
|
||||
studiosService.saveStudio(List.of(studio));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,76 @@
|
||||
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)
|
||||
@RolesAllowed("MoviesUserRole")
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user