Merge branch 'master' of https://gitlab.hexaquo.at/sks/movies
This commit is contained in:
commit
76fc45760a
@ -0,0 +1,71 @@
|
||||
package at.technikumwien.movies;
|
||||
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
@Path("/actor")
|
||||
@RolesAllowed("MoviesUserRole")
|
||||
public class ActorResource {
|
||||
@Inject
|
||||
private ActorsService actorsService;
|
||||
|
||||
@Context
|
||||
private UriInfo uriInfo;
|
||||
|
||||
@GET
|
||||
@Produces({
|
||||
MediaType.APPLICATION_JSON,
|
||||
MediaType.APPLICATION_XML
|
||||
})
|
||||
public List<Actor> retrieveAll() {
|
||||
return actorsService.findAllActors();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Produces({
|
||||
MediaType.APPLICATION_JSON,
|
||||
MediaType.APPLICATION_XML
|
||||
})
|
||||
@Path("/{id}")
|
||||
public Actor retrieve(@PathParam("id") long id) {
|
||||
return actorsService.findActorById(id);
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/{id}")
|
||||
public void delete(@PathParam("id") long id) {
|
||||
actorsService.removeActorById(id);
|
||||
}
|
||||
|
||||
@POST
|
||||
@Consumes({
|
||||
MediaType.APPLICATION_JSON,
|
||||
MediaType.APPLICATION_XML
|
||||
})
|
||||
public Response create(Actor actor) {
|
||||
actor.setId(null); // Make sure that a new actor is added, not overwriting existing one
|
||||
List<Actor> newActors = actorsService.saveActor(List.of(actor));
|
||||
|
||||
URI location = uriInfo.getAbsolutePathBuilder().path(newActors.get(0).getId().toString()).build();
|
||||
|
||||
return Response.created(location).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Consumes({
|
||||
MediaType.APPLICATION_JSON,
|
||||
MediaType.APPLICATION_XML
|
||||
})
|
||||
@Path("/{id}")
|
||||
public void update(@PathParam("id") long id, Actor actor) {
|
||||
actor.setId(id);
|
||||
actorsService.saveActor(List.of(actor));
|
||||
}
|
||||
}
|
@ -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 ActorsService {
|
||||
private static final Logger LOGGER = Logger.getLogger(ActorsService.class.getName());
|
||||
|
||||
@Resource
|
||||
private SessionContext context;
|
||||
|
||||
@PersistenceContext(unitName = "MoviesPU")
|
||||
private EntityManager em;
|
||||
|
||||
@Inject
|
||||
private SecurityContext securityContext;
|
||||
|
||||
public Actor findActorById(long id) {
|
||||
LOGGER.info("findActorById() >> id=" + id);
|
||||
|
||||
Actor actor = em.find(Actor.class, id);
|
||||
if (actor == null) {
|
||||
throw new EntityNotFoundException("can't find actor with id=" + id);
|
||||
}
|
||||
|
||||
return actor;
|
||||
}
|
||||
|
||||
public List<Actor> findAllActors() {
|
||||
LOGGER.info("findAllActors)");
|
||||
|
||||
return em.createNamedQuery("Actors.selectAllActors", Actor.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
public void removeActorById(long id) {
|
||||
LOGGER.info("removeActorById() >> id=" + id);
|
||||
|
||||
Actor actor = findActorById(id);
|
||||
em.remove(actor);
|
||||
}
|
||||
|
||||
// TODO maybe check if the actor already exists?
|
||||
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
|
||||
public List<Actor> saveActor(List<Actor> actors) {
|
||||
|
||||
try {
|
||||
List<Actor> addedActors = new ArrayList<>();
|
||||
|
||||
LOGGER.info("save() >> actors" + actors);
|
||||
for (Actor actorToImport : actors) {
|
||||
addedActors.add(em.merge(actorToImport));
|
||||
}
|
||||
|
||||
return addedActors;
|
||||
} catch (Exception e) {
|
||||
LOGGER.info("Rollback when adding actor!");
|
||||
context.setRollbackOnly();
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -69,7 +69,7 @@ public class MovieResource {
|
||||
@Path("/{id}")
|
||||
@RolesAllowed("MSWrite")
|
||||
public void update(@PathParam("id") long id, Movie movie) {
|
||||
movie.setId(id); // Make sure that a new movie is added, not overwriting existing one
|
||||
movie.setId(id);
|
||||
moviesService.save(List.of(movie));
|
||||
}
|
||||
}
|
||||
|
@ -147,12 +147,18 @@ public class MoviesService {
|
||||
|
||||
List<Movie> addedMovies = new ArrayList<>();
|
||||
|
||||
try {
|
||||
LOGGER.info("save() >> movies" + movies);
|
||||
for (Movie movieToImport : movies) {
|
||||
addedMovies.add(em.merge(movieToImport));
|
||||
}
|
||||
|
||||
return addedMovies;
|
||||
} catch (Exception e) {
|
||||
LOGGER.info("Rollback when adding movie!");
|
||||
context.setRollbackOnly();
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import java.util.List;
|
||||
@Path("/studio")
|
||||
public class StudioResource {
|
||||
@Inject
|
||||
private MoviesService moviesService;
|
||||
private StudiosService studiosService;
|
||||
|
||||
@Context
|
||||
private UriInfo uriInfo;
|
||||
@ -25,7 +25,7 @@ public class StudioResource {
|
||||
})
|
||||
@RolesAllowed("MSRead")
|
||||
public List<Studio> retrieveAll() {
|
||||
return moviesService.findAllStudios();
|
||||
return studiosService.findAllStudios();
|
||||
}
|
||||
|
||||
@GET
|
||||
@ -36,13 +36,13 @@ public class StudioResource {
|
||||
@Path("/{id}")
|
||||
@RolesAllowed("MSRead")
|
||||
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
|
||||
@ -50,11 +50,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();
|
||||
}
|
||||
@ -65,8 +65,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);
|
||||
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