rsanchez
2015-01-28 da889d489da5d7fa8c71d9f21f24b1dc2e29d8e1
securis/src/main/java/net/curisit/securis/services/OrganizationResource.java
....@@ -6,11 +6,11 @@
66 import java.util.Set;
77
88 import javax.annotation.security.RolesAllowed;
9
-import javax.inject.Inject;
10
-import javax.inject.Provider;
9
+import javax.enterprise.context.RequestScoped;
1110 import javax.persistence.EntityManager;
1211 import javax.persistence.TypedQuery;
1312 import javax.servlet.http.HttpServletRequest;
13
+import javax.transaction.Transactional;
1414 import javax.ws.rs.Consumes;
1515 import javax.ws.rs.DELETE;
1616 import javax.ws.rs.GET;
....@@ -30,14 +30,13 @@
3030 import net.curisit.securis.SeCurisException;
3131 import net.curisit.securis.db.Organization;
3232 import net.curisit.securis.db.User;
33
+import net.curisit.securis.ioc.MyTrans;
3334 import net.curisit.securis.security.BasicSecurityContext;
3435 import net.curisit.securis.security.Securable;
3536 import net.curisit.securis.utils.TokenHelper;
3637
3738 import org.apache.logging.log4j.LogManager;
3839 import org.apache.logging.log4j.Logger;
39
-
40
-import com.google.inject.persist.Transactional;
4140
4241 /**
4342 * Organization resource, this service will provide methods to create, modify
....@@ -46,12 +45,16 @@
4645 * @author roberto <roberto.sanchez@curisit.net>
4746 */
4847 @Path("/organization")
48
+@RequestScoped
4949 public class OrganizationResource {
5050
5151 private static final Logger LOG = LogManager.getLogger(OrganizationResource.class);
5252
53
- @Inject
54
- private Provider<EntityManager> emProvider;
53
+ @Context
54
+ EntityManager em;
55
+
56
+ @Context
57
+ BasicSecurityContext bsc;
5558
5659 public OrganizationResource() {
5760 }
....@@ -66,10 +69,10 @@
6669 MediaType.APPLICATION_JSON
6770 })
6871 @Securable
69
- public Response index(@Context BasicSecurityContext bsc) {
72
+ public Response index() {
7073 LOG.info("Getting organizations list ");
7174
72
- EntityManager em = emProvider.get();
75
+ // EntityManager em = emProvider.get();
7376 em.clear();
7477 TypedQuery<Organization> q;
7578 if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
....@@ -101,7 +104,7 @@
101104 return Response.status(Status.NOT_FOUND).build();
102105 }
103106
104
- EntityManager em = emProvider.get();
107
+ // EntityManager em = emProvider.get();
105108 em.clear();
106109 Organization org = em.find(Organization.class, Integer.parseInt(orgid));
107110 if (org == null) {
....@@ -128,12 +131,12 @@
128131 @Produces({
129132 MediaType.APPLICATION_JSON
130133 })
131
- @Transactional
134
+ @MyTrans
132135 @Securable
133136 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
134
- public Response create(Organization org, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
137
+ public Response create(Organization org) {
135138 LOG.info("Creating new organization");
136
- EntityManager em = emProvider.get();
139
+ // EntityManager em = emProvider.get();
137140
138141 try {
139142 this.setParentOrg(org, org.getParentOrgId(), em);
....@@ -159,7 +162,6 @@
159162 org.setUsers(users);
160163 org.setCreationTimestamp(new Date());
161164 em.persist(org);
162
-
163165 return Response.ok(org).build();
164166 }
165167
....@@ -205,7 +207,7 @@
205207 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
206208 public Response modify(Organization org, @PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
207209 LOG.info("Modifying organization with id: {}", orgid);
208
- EntityManager em = emProvider.get();
210
+ // EntityManager em = emProvider.get();
209211 Organization currentOrg = em.find(Organization.class, Integer.parseInt(orgid));
210212 if (currentOrg == null) {
211213 LOG.error("Organization with id {} not found in DB", orgid);
....@@ -250,7 +252,7 @@
250252 @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
251253 public Response delete(@PathParam("orgid") String orgid, @Context HttpServletRequest request) {
252254 LOG.info("Deleting organization with id: {}", orgid);
253
- EntityManager em = emProvider.get();
255
+ // EntityManager em = emProvider.get();
254256 Organization org = em.find(Organization.class, Integer.parseInt(orgid));
255257 if (org == null) {
256258 LOG.error("Organization with id {} can not be deleted, It was not found in DB", orgid);