Roberto Sánchez
2014-01-10 3d0c6e4865d4a0ddd764da533a327faf76e0cb32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package net.curisit.securis.services;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import net.curisit.integrity.commons.Utils;
import net.curisit.securis.SecurisErrorHandler;
import net.curisit.securis.db.Organization;
import net.curisit.securis.db.User;
import net.curisit.securis.utils.TokenHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.inject.persist.Transactional;
/**
 * Organization resource, this service will provide methods to create, modify and delete organizations
 * 
 * @author roberto <roberto.sanchez@curisit.net>
 */
@Path("/organization")
public class OrganizationResource {
   private static final Logger log = LoggerFactory.getLogger(OrganizationResource.class);
   @Inject
   TokenHelper tokenHelper;
   @Inject
   Provider<EntityManager> emProvider;
   public OrganizationResource() {
   }
   /**
    * 
    * @return the server version in format majorVersion.minorVersion
    */
   @GET
   @Path("/")
   @Produces(
       { MediaType.APPLICATION_JSON })
   public Response index() {
       log.info("Getting organizations list ");
       EntityManager em = emProvider.get();
       TypedQuery<Organization> q = em.createNamedQuery("list-organizations", Organization.class);
       List<Organization> list = q.getResultList();
       return Response.ok(list).build();
   }
   /**
    * 
    * @return the server version in format majorVersion.minorVersion
    */
   @GET
   @Path("/{orgid}")
   @Produces(
       { MediaType.APPLICATION_JSON })
   public Response get(@PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
       log.info("Getting organization data for id: {}: ", orgid);
       if (orgid == null || orgid.equals("")) {
           log.error("Organization ID is mandatory");
           return Response.status(Status.NOT_FOUND).build();
       }
       EntityManager em = emProvider.get();
       Organization lt = em.find(Organization.class, Integer.parseInt(orgid));
       if (lt == null) {
           log.error("Organization with id {} not found in DB", orgid);
           return Response.status(Status.NOT_FOUND).build();
       }
       return Response.ok(lt).build();
   }
   @POST
   @Path("/")
   @Consumes(MediaType.APPLICATION_JSON)
   @Produces(
       { MediaType.APPLICATION_JSON })
   @Transactional
   public Response create(Organization org, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
       log.info("Creating new organization");
       EntityManager em = emProvider.get();
       Organization parentOrg = null;
       if (org.getParentOrgId() != null) {
           parentOrg = em.find(Organization.class, org.getParentOrgId());
           if (parentOrg == null) {
               log.error("Organization parent with id {} not found in DB", org.getParentOrgId());
               return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Organization's parent not found with ID: " + org.getParentOrgId()).build();
           }
       }
       List<User> users = null;
       List<String> usersIds = org.getUsersIds();
       if (usersIds != null && usersIds.size() > 0) {
           users = new ArrayList<>();
           for (String username : usersIds) {
               User user = em.find(User.class, username);
               if (user == null) {
                   log.error("Organization user with id {} not found in DB", username);
                   return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Organization's user not found with ID: " + username).build();
               }
               users.add(user);
           }
       }
       org.setUsers(users);
       org.setParentOrganization(parentOrg);
       org.setCreationTimestamp(new Date());
       em.persist(org);
       return Response.ok(org).build();
   }
   @PUT
   @POST
   @Path("/{orgid}")
   @Transactional
   @Consumes(MediaType.APPLICATION_JSON)
   @Produces(
       { MediaType.APPLICATION_JSON })
   public Response modify(Organization org, @PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
       log.info("Modifying organization with id: {}", orgid);
       EntityManager em = emProvider.get();
       Organization currentOrg = em.find(Organization.class, Integer.parseInt(orgid));
       if (currentOrg == null) {
           log.error("Organization with id {} not found in DB", orgid);
           return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Organization not found with ID: " + orgid).build();
       }
       Organization parentOrg = null;
       if (org.getParentOrgId() != null) {
           parentOrg = em.find(Organization.class, org.getParentOrgId());
           if (parentOrg == null) {
               log.error("Organization parent with id {} not found in DB", org.getParentOrgId());
               return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Organization's parent not found with ID: " + org.getParentOrgId()).build();
           }
       }
       List<User> users = null;
       List<String> usersIds = org.getUsersIds();
       if (usersIds != null && usersIds.size() > 0) {
           users = new ArrayList<>();
           for (String username : usersIds) {
               User user = em.find(User.class, username);
               if (parentOrg == null) {
                   log.error("Organization user with id {} not found in DB", username);
                   return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Organization's user not found with ID: " + username).build();
               }
               users.add(user);
           }
       }
       currentOrg.setUsers(users);
       currentOrg.setParentOrganization(parentOrg);
       currentOrg.setCode(org.getCode());
       currentOrg.setName(org.getName());
       currentOrg.setDescription(org.getDescription());
       em.persist(currentOrg);
       return Response.ok(currentOrg).build();
   }
   @DELETE
   @Path("/{orgid}")
   @Transactional
   @Produces(
       { MediaType.APPLICATION_JSON })
   public Response delete(@PathParam("orgid") String orgid, @Context HttpServletRequest request) {
       log.info("Deleting app with id: {}", orgid);
       EntityManager em = emProvider.get();
       Organization org = em.find(Organization.class, Integer.parseInt(orgid));
       if (org == null) {
           log.error("Organization with id {} can not be deleted, It was not found in DB", orgid);
           return Response.status(Status.NOT_FOUND).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Organization was not found, ID: " + orgid).build();
       }
       if (org.getChildOrganizations() != null && org.getChildOrganizations().size() > 0) {
           log.error("Organization has children and can not be deleted, ID: " + orgid);
           return Response.status(Status.FORBIDDEN).header(SecurisErrorHandler.HEADER_ERROR_MESSAGE, "Organization has children and can not be deleted, ID: " + orgid).build();
       }
       em.remove(org);
       return Response.ok(Utils.createMap("success", true, "id", orgid)).build();
   }
}