| .. | .. |
|---|
| 6 | 6 | import java.util.Set; |
|---|
| 7 | 7 | |
|---|
| 8 | 8 | import javax.annotation.security.RolesAllowed; |
|---|
| 9 | | -import javax.inject.Inject; |
|---|
| 10 | | -import javax.inject.Provider; |
|---|
| 9 | +import javax.enterprise.context.RequestScoped; |
|---|
| 11 | 10 | import javax.persistence.EntityManager; |
|---|
| 12 | 11 | import javax.persistence.TypedQuery; |
|---|
| 13 | 12 | import javax.servlet.http.HttpServletRequest; |
|---|
| 13 | +import javax.transaction.Transactional; |
|---|
| 14 | 14 | import javax.ws.rs.Consumes; |
|---|
| 15 | 15 | import javax.ws.rs.DELETE; |
|---|
| 16 | 16 | import javax.ws.rs.GET; |
|---|
| .. | .. |
|---|
| 30 | 30 | import net.curisit.securis.SeCurisException; |
|---|
| 31 | 31 | import net.curisit.securis.db.Organization; |
|---|
| 32 | 32 | import net.curisit.securis.db.User; |
|---|
| 33 | +import net.curisit.securis.ioc.MyTrans; |
|---|
| 33 | 34 | import net.curisit.securis.security.BasicSecurityContext; |
|---|
| 34 | 35 | import net.curisit.securis.security.Securable; |
|---|
| 35 | 36 | import net.curisit.securis.utils.TokenHelper; |
|---|
| 36 | 37 | |
|---|
| 37 | 38 | import org.apache.logging.log4j.LogManager; |
|---|
| 38 | 39 | import org.apache.logging.log4j.Logger; |
|---|
| 39 | | - |
|---|
| 40 | | -import com.google.inject.persist.Transactional; |
|---|
| 41 | 40 | |
|---|
| 42 | 41 | /** |
|---|
| 43 | 42 | * Organization resource, this service will provide methods to create, modify |
|---|
| .. | .. |
|---|
| 46 | 45 | * @author roberto <roberto.sanchez@curisit.net> |
|---|
| 47 | 46 | */ |
|---|
| 48 | 47 | @Path("/organization") |
|---|
| 48 | +@RequestScoped |
|---|
| 49 | 49 | public class OrganizationResource { |
|---|
| 50 | 50 | |
|---|
| 51 | 51 | private static final Logger LOG = LogManager.getLogger(OrganizationResource.class); |
|---|
| 52 | 52 | |
|---|
| 53 | | - @Inject |
|---|
| 54 | | - private Provider<EntityManager> emProvider; |
|---|
| 53 | + @Context |
|---|
| 54 | + EntityManager em; |
|---|
| 55 | + |
|---|
| 56 | + @Context |
|---|
| 57 | + BasicSecurityContext bsc; |
|---|
| 55 | 58 | |
|---|
| 56 | 59 | public OrganizationResource() { |
|---|
| 57 | 60 | } |
|---|
| .. | .. |
|---|
| 66 | 69 | MediaType.APPLICATION_JSON |
|---|
| 67 | 70 | }) |
|---|
| 68 | 71 | @Securable |
|---|
| 69 | | - public Response index(@Context BasicSecurityContext bsc) { |
|---|
| 72 | + public Response index() { |
|---|
| 70 | 73 | LOG.info("Getting organizations list "); |
|---|
| 71 | 74 | |
|---|
| 72 | | - EntityManager em = emProvider.get(); |
|---|
| 75 | + // EntityManager em = emProvider.get(); |
|---|
| 73 | 76 | em.clear(); |
|---|
| 74 | 77 | TypedQuery<Organization> q; |
|---|
| 75 | 78 | if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| .. | .. |
|---|
| 101 | 104 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 102 | 105 | } |
|---|
| 103 | 106 | |
|---|
| 104 | | - EntityManager em = emProvider.get(); |
|---|
| 107 | + // EntityManager em = emProvider.get(); |
|---|
| 105 | 108 | em.clear(); |
|---|
| 106 | 109 | Organization org = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 107 | 110 | if (org == null) { |
|---|
| .. | .. |
|---|
| 128 | 131 | @Produces({ |
|---|
| 129 | 132 | MediaType.APPLICATION_JSON |
|---|
| 130 | 133 | }) |
|---|
| 131 | | - @Transactional |
|---|
| 134 | + @MyTrans |
|---|
| 132 | 135 | @Securable |
|---|
| 133 | 136 | @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 134 | | - public Response create(Organization org, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 137 | + public Response create(Organization org) { |
|---|
| 135 | 138 | LOG.info("Creating new organization"); |
|---|
| 136 | | - EntityManager em = emProvider.get(); |
|---|
| 139 | + // EntityManager em = emProvider.get(); |
|---|
| 137 | 140 | |
|---|
| 138 | 141 | try { |
|---|
| 139 | 142 | this.setParentOrg(org, org.getParentOrgId(), em); |
|---|
| .. | .. |
|---|
| 159 | 162 | org.setUsers(users); |
|---|
| 160 | 163 | org.setCreationTimestamp(new Date()); |
|---|
| 161 | 164 | em.persist(org); |
|---|
| 162 | | - |
|---|
| 163 | 165 | return Response.ok(org).build(); |
|---|
| 164 | 166 | } |
|---|
| 165 | 167 | |
|---|
| .. | .. |
|---|
| 205 | 207 | @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 206 | 208 | public Response modify(Organization org, @PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 207 | 209 | LOG.info("Modifying organization with id: {}", orgid); |
|---|
| 208 | | - EntityManager em = emProvider.get(); |
|---|
| 210 | + // EntityManager em = emProvider.get(); |
|---|
| 209 | 211 | Organization currentOrg = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 210 | 212 | if (currentOrg == null) { |
|---|
| 211 | 213 | LOG.error("Organization with id {} not found in DB", orgid); |
|---|
| .. | .. |
|---|
| 250 | 252 | @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 251 | 253 | public Response delete(@PathParam("orgid") String orgid, @Context HttpServletRequest request) { |
|---|
| 252 | 254 | LOG.info("Deleting organization with id: {}", orgid); |
|---|
| 253 | | - EntityManager em = emProvider.get(); |
|---|
| 255 | + // EntityManager em = emProvider.get(); |
|---|
| 254 | 256 | Organization org = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 255 | 257 | if (org == null) { |
|---|
| 256 | 258 | LOG.error("Organization with id {} can not be deleted, It was not found in DB", orgid); |
|---|