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