@GET @Produces({"application/xml", "application/json"}) public Collection<Project> findAll(@Context HttpServletRequest request) { Authentication.assertUserHasAccess(request); Object idUser = Authentication.getUserId(request); User user = em.find(User.class, (Integer) idUser); System.out.println( "Username: " + user.getUsername() + user.getIdUser() + user.getProjectCollection().toString()); return user.getProjectCollection(); }
@GET @Path("{id}/remove-investigator") @Consumes({"application/xml", "application/json"}) public void removeInvestigator( @PathParam("id") Integer id, @QueryParam("user") Integer idUser, @Context HttpServletRequest request) { Authentication.assertUserIsAdmin(request, em); User user = em.find(User.class, idUser); Project project = em.find(Project.class, id); project.getUserCollection().remove(user); // useful to maintain coherence, but ignored by JPA user.getProjectCollection().remove(project); getEntityManager().persist(project); getEntityManager().persist(user); }
@POST @Consumes({"application/xml", "application/json"}) public void create(Project entity, @Context HttpServletRequest request) { Authentication.assertUserHasAccess(request); HttpSession session = request.getSession(); if (session == null) { throw new ServiceException(401, "Not authorized"); } Object idUser = session.getAttribute("user"); if (idUser == null) { throw new ServiceException(401, "Not authorized"); } User user = em.find(User.class, (Integer) idUser); Collection<User> users = new ArrayList<User>(); users.add(user); entity.setUserCollection(users); // useful to maintain coherence, but ignored by JPA entity.setCreatedDate(new Date()); entity.setStatus(1); getEntityManager().persist(entity); user.getProjectCollection().add(entity); }