| .. | .. |
|---|
| 40 | 40 | import com.google.inject.persist.Transactional; |
|---|
| 41 | 41 | |
|---|
| 42 | 42 | /** |
|---|
| 43 | | - * Organization resource, this service will provide methods to create, modify and delete organizations |
|---|
| 43 | + * Organization resource, this service will provide methods to create, modify |
|---|
| 44 | + * and delete organizations |
|---|
| 44 | 45 | * |
|---|
| 45 | 46 | * @author roberto <roberto.sanchez@curisit.net> |
|---|
| 46 | 47 | */ |
|---|
| 47 | 48 | @Path("/organization") |
|---|
| 48 | 49 | public class OrganizationResource { |
|---|
| 49 | 50 | |
|---|
| 50 | | - private static final Logger LOG = LogManager.getLogger(OrganizationResource.class); |
|---|
| 51 | + private static final Logger LOG = LogManager.getLogger(OrganizationResource.class); |
|---|
| 51 | 52 | |
|---|
| 52 | | - @Inject |
|---|
| 53 | | - private Provider<EntityManager> emProvider; |
|---|
| 53 | + @Inject |
|---|
| 54 | + private Provider<EntityManager> emProvider; |
|---|
| 54 | 55 | |
|---|
| 55 | | - public OrganizationResource() { |
|---|
| 56 | | - } |
|---|
| 56 | + public OrganizationResource() {} |
|---|
| 57 | 57 | |
|---|
| 58 | | - /** |
|---|
| 59 | | - * |
|---|
| 60 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 61 | | - */ |
|---|
| 62 | | - @GET |
|---|
| 63 | | - @Path("/") |
|---|
| 64 | | - @Produces( |
|---|
| 65 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 66 | | - @Securable |
|---|
| 67 | | - // @RolesAllowed(SecurityContextWrapper.ROL_ADVANCE) |
|---|
| 68 | | - public Response index(@Context BasicSecurityContext bsc) { |
|---|
| 69 | | - LOG.info("Getting organizations list "); |
|---|
| 58 | + /** |
|---|
| 59 | + * |
|---|
| 60 | + * @return the server version in format majorVersion.minorVersion |
|---|
| 61 | + */ |
|---|
| 62 | + @GET |
|---|
| 63 | + @Path("/") |
|---|
| 64 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 65 | + @Securable |
|---|
| 66 | + public Response index(@Context BasicSecurityContext bsc) { |
|---|
| 67 | + LOG.info("Getting organizations list "); |
|---|
| 70 | 68 | |
|---|
| 71 | | - // LOG.info("User orgs: {}", request.getAttribute("oser_orgs")); |
|---|
| 72 | | - BasicSecurityContext bsc2 = ResteasyProviderFactory.getContextData(BasicSecurityContext.class); |
|---|
| 73 | | - LOG.info("bsc: {}", bsc); |
|---|
| 74 | | - LOG.info("bsc2: {}", bsc2); |
|---|
| 75 | | - // LOG.info("securityContext: {}", scw); |
|---|
| 76 | | - LOG.info("securityContext ROL_ADMIN?: {}", bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)); |
|---|
| 77 | | - EntityManager em = emProvider.get(); |
|---|
| 78 | | - TypedQuery<Organization> q; |
|---|
| 79 | | - if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 80 | | - LOG.info("GEtting all orgs for user: " + bsc.getUserPrincipal()); |
|---|
| 81 | | - q = em.createNamedQuery("list-organizations", Organization.class); |
|---|
| 82 | | - } else { |
|---|
| 83 | | - q = em.createNamedQuery("list-organizations", Organization.class); |
|---|
| 84 | | - // if (securityContext.getOrganizationsIds() == null) |
|---|
| 85 | | - // Response.ok().build(); |
|---|
| 86 | | - // LOG.info("Getting only {} orgs for user: {}", securityContext.getOrganizationsIds(), securityContext.getUserPrincipal()); |
|---|
| 87 | | - // q = em.createNamedQuery("list-organizations-by-ids", Organization.class); |
|---|
| 88 | | - // q.setParameter("list_ids", securityContext.getOrganizationsIds()); |
|---|
| 89 | | - } |
|---|
| 69 | + BasicSecurityContext bsc2 = ResteasyProviderFactory.getContextData(BasicSecurityContext.class); |
|---|
| 70 | + LOG.debug("securityContext ROL_ADMIN?: {}", bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)); |
|---|
| 71 | + EntityManager em = emProvider.get(); |
|---|
| 72 | + TypedQuery<Organization> q; |
|---|
| 73 | + if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 74 | + LOG.info("GEtting all orgs for user: " + bsc.getUserPrincipal()); |
|---|
| 75 | + q = em.createNamedQuery("list-organizations", Organization.class); |
|---|
| 76 | + } else { |
|---|
| 77 | + q = em.createNamedQuery("list-organizations", Organization.class); |
|---|
| 78 | + } |
|---|
| 90 | 79 | |
|---|
| 91 | | - List<Organization> list = q.getResultList(); |
|---|
| 80 | + List<Organization> list = q.getResultList(); |
|---|
| 92 | 81 | |
|---|
| 93 | | - return Response.ok(list).build(); |
|---|
| 94 | | - } |
|---|
| 82 | + return Response.ok(list).build(); |
|---|
| 83 | + } |
|---|
| 95 | 84 | |
|---|
| 96 | | - /** |
|---|
| 97 | | - * |
|---|
| 98 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 99 | | - */ |
|---|
| 100 | | - @GET |
|---|
| 101 | | - @Path("/{orgid}") |
|---|
| 102 | | - @Produces( |
|---|
| 103 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 104 | | - @Securable |
|---|
| 105 | | - public Response get(@PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 106 | | - LOG.info("Getting organization data for id: {}: ", orgid); |
|---|
| 107 | | - if (orgid == null || orgid.equals("")) { |
|---|
| 108 | | - LOG.error("Organization ID is mandatory"); |
|---|
| 109 | | - return Response.status(Status.NOT_FOUND).build(); |
|---|
| 110 | | - } |
|---|
| 111 | | - // if (!securityContext.isOrgAccesible(Integer.parseInt(orgid))) { |
|---|
| 112 | | - // LOG.error("Organization with id {} not accessible for user: {}", orgid, securityContext.getUserPrincipal()); |
|---|
| 113 | | - // return Response.status(Status.UNAUTHORIZED).build(); |
|---|
| 114 | | - // } |
|---|
| 85 | + /** |
|---|
| 86 | + * |
|---|
| 87 | + * @return the server version in format majorVersion.minorVersion |
|---|
| 88 | + */ |
|---|
| 89 | + @GET |
|---|
| 90 | + @Path("/{orgid}") |
|---|
| 91 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 92 | + @Securable |
|---|
| 93 | + public Response get(@PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 94 | + LOG.info("Getting organization data for id: {}: ", orgid); |
|---|
| 95 | + if (orgid == null || "".equals(orgid)) { |
|---|
| 96 | + LOG.error("Organization ID is mandatory"); |
|---|
| 97 | + return Response.status(Status.NOT_FOUND).build(); |
|---|
| 98 | + } |
|---|
| 115 | 99 | |
|---|
| 116 | | - EntityManager em = emProvider.get(); |
|---|
| 117 | | - Organization org = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 118 | | - if (org == null) { |
|---|
| 119 | | - LOG.error("Organization with id {} not found in DB", orgid); |
|---|
| 120 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization not found, id: " + orgid).build(); |
|---|
| 121 | | - } |
|---|
| 122 | | - return Response.ok(org).build(); |
|---|
| 123 | | - } |
|---|
| 100 | + EntityManager em = emProvider.get(); |
|---|
| 101 | + Organization org = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 102 | + if (org == null) { |
|---|
| 103 | + LOG.error("Organization with id {} not found in DB", orgid); |
|---|
| 104 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization not found, id: " + orgid) |
|---|
| 105 | + .build(); |
|---|
| 106 | + } |
|---|
| 107 | + return Response.ok(org).build(); |
|---|
| 108 | + } |
|---|
| 124 | 109 | |
|---|
| 125 | | - private boolean isCyclicalRelationship(int currentId, Organization parent) { |
|---|
| 126 | | - while (parent != null) { |
|---|
| 127 | | - if (parent.getId() == currentId) |
|---|
| 128 | | - return true; |
|---|
| 129 | | - parent = parent.getParentOrganization(); |
|---|
| 130 | | - } |
|---|
| 131 | | - return false; |
|---|
| 132 | | - } |
|---|
| 110 | + private boolean isCyclicalRelationship(int currentId, Organization parent) { |
|---|
| 111 | + while (parent != null) { |
|---|
| 112 | + if (parent.getId() == currentId) { |
|---|
| 113 | + return true; |
|---|
| 114 | + } |
|---|
| 115 | + parent = parent.getParentOrganization(); |
|---|
| 116 | + } |
|---|
| 117 | + return false; |
|---|
| 118 | + } |
|---|
| 133 | 119 | |
|---|
| 134 | | - @POST |
|---|
| 135 | | - @Path("/") |
|---|
| 136 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 137 | | - @Produces( |
|---|
| 138 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 139 | | - @Transactional |
|---|
| 140 | | - @Securable |
|---|
| 141 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 142 | | - public Response create(Organization org, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 143 | | - LOG.info("Creating new organization"); |
|---|
| 144 | | - EntityManager em = emProvider.get(); |
|---|
| 145 | | - |
|---|
| 146 | | - try { |
|---|
| 147 | | - this.setParentOrg(org, org.getParentOrgId(), em); |
|---|
| 148 | | - } catch (SeCurisException e) { |
|---|
| 149 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 150 | | - } |
|---|
| 151 | | - |
|---|
| 152 | | - List<User> users = null; |
|---|
| 153 | | - List<String> usersIds = org.getUsersIds(); |
|---|
| 154 | | - if (usersIds != null && usersIds.size() > 0) { |
|---|
| 155 | | - users = new ArrayList<>(); |
|---|
| 156 | | - for (String username : usersIds) { |
|---|
| 157 | | - User user = em.find(User.class, username); |
|---|
| 158 | | - if (user == null) { |
|---|
| 159 | | - LOG.error("Organization user with id {} not found in DB", username); |
|---|
| 160 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's user not found with ID: " + username).build(); |
|---|
| 161 | | - } |
|---|
| 162 | | - users.add(user); |
|---|
| 163 | | - } |
|---|
| 164 | | - } |
|---|
| 120 | + @POST |
|---|
| 121 | + @Path("/") |
|---|
| 122 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 123 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 124 | + @Transactional |
|---|
| 125 | + @Securable |
|---|
| 126 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 127 | + public Response create(Organization org, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 128 | + LOG.info("Creating new organization"); |
|---|
| 129 | + EntityManager em = emProvider.get(); |
|---|
| 165 | 130 | |
|---|
| 166 | | - org.setUsers(users); |
|---|
| 167 | | - org.setCreationTimestamp(new Date()); |
|---|
| 168 | | - em.persist(org); |
|---|
| 131 | + try { |
|---|
| 132 | + this.setParentOrg(org, org.getParentOrgId(), em); |
|---|
| 133 | + } catch (SeCurisException e) { |
|---|
| 134 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 135 | + } |
|---|
| 169 | 136 | |
|---|
| 170 | | - return Response.ok(org).build(); |
|---|
| 171 | | - } |
|---|
| 172 | | - |
|---|
| 173 | | - private void setParentOrg(Organization org, Integer parentOrgId, EntityManager em) throws SeCurisException { |
|---|
| 174 | | - Organization parentOrg = null; |
|---|
| 175 | | - if (parentOrgId != null) { |
|---|
| 176 | | - parentOrg = em.find(Organization.class, parentOrgId); |
|---|
| 177 | | - if (parentOrg == null) { |
|---|
| 178 | | - LOG.error("Organization parent with id {} not found in DB", org.getParentOrgId()); |
|---|
| 179 | | - throw new SecurityException("Organization's parent not found with ID: " + org.getParentOrgId()); |
|---|
| 180 | | - } |
|---|
| 181 | | - } |
|---|
| 137 | + List<User> users = null; |
|---|
| 138 | + List<String> usersIds = org.getUsersIds(); |
|---|
| 139 | + if (usersIds != null && usersIds.size() > 0) { |
|---|
| 140 | + users = new ArrayList<>(); |
|---|
| 141 | + for (String username : usersIds) { |
|---|
| 142 | + User user = em.find(User.class, username); |
|---|
| 143 | + if (user == null) { |
|---|
| 144 | + LOG.error("Organization user with id {} not found in DB", username); |
|---|
| 145 | + return Response.status(Status.NOT_FOUND) |
|---|
| 146 | + .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization's user not found with ID: " + username).build(); |
|---|
| 147 | + } |
|---|
| 148 | + users.add(user); |
|---|
| 149 | + } |
|---|
| 150 | + } |
|---|
| 182 | 151 | |
|---|
| 183 | | - org.setParentOrganization(parentOrg); |
|---|
| 184 | | - } |
|---|
| 152 | + org.setUsers(users); |
|---|
| 153 | + org.setCreationTimestamp(new Date()); |
|---|
| 154 | + em.persist(org); |
|---|
| 185 | 155 | |
|---|
| 186 | | - private void setOrgUsers(Organization org, List<String> usersIds, EntityManager em) throws SeCurisException { |
|---|
| 187 | | - List<User> users = null; |
|---|
| 188 | | - if (usersIds != null && usersIds.size() > 0) { |
|---|
| 189 | | - users = new ArrayList<>(); |
|---|
| 190 | | - for (String username : usersIds) { |
|---|
| 191 | | - User user = em.find(User.class, username); |
|---|
| 192 | | - if (user == null) { |
|---|
| 193 | | - LOG.error("Organization user with id '{}' not found in DB", username); |
|---|
| 194 | | - throw new SecurityException("Organization's user not found with ID: " + username); |
|---|
| 195 | | - } |
|---|
| 196 | | - users.add(user); |
|---|
| 197 | | - } |
|---|
| 198 | | - } |
|---|
| 156 | + return Response.ok(org).build(); |
|---|
| 157 | + } |
|---|
| 199 | 158 | |
|---|
| 200 | | - org.setUsers(users); |
|---|
| 201 | | - } |
|---|
| 159 | + private void setParentOrg(Organization org, Integer parentOrgId, EntityManager em) throws SeCurisException { |
|---|
| 160 | + Organization parentOrg = null; |
|---|
| 161 | + if (parentOrgId != null) { |
|---|
| 162 | + parentOrg = em.find(Organization.class, parentOrgId); |
|---|
| 163 | + if (parentOrg == null) { |
|---|
| 164 | + LOG.error("Organization parent with id {} not found in DB", org.getParentOrgId()); |
|---|
| 165 | + throw new SecurityException("Organization's parent not found with ID: " + org.getParentOrgId()); |
|---|
| 166 | + } |
|---|
| 167 | + } |
|---|
| 202 | 168 | |
|---|
| 203 | | - @PUT |
|---|
| 204 | | - @POST |
|---|
| 205 | | - @Path("/{orgid}") |
|---|
| 206 | | - @Transactional |
|---|
| 207 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 208 | | - @Produces( |
|---|
| 209 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 210 | | - @Securable |
|---|
| 211 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 212 | | - public Response modify(Organization org, @PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 213 | | - LOG.info("Modifying organization with id: {}", orgid); |
|---|
| 214 | | - EntityManager em = emProvider.get(); |
|---|
| 215 | | - Organization currentOrg = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 216 | | - if (currentOrg == null) { |
|---|
| 217 | | - LOG.error("Organization with id {} not found in DB", orgid); |
|---|
| 218 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization not found with ID: " + orgid).build(); |
|---|
| 219 | | - } |
|---|
| 220 | | - try { |
|---|
| 221 | | - this.setParentOrg(currentOrg, org.getParentOrgId(), em); |
|---|
| 222 | | - } catch (SeCurisException e) { |
|---|
| 223 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 224 | | - } |
|---|
| 225 | | - if (org.getParentOrganization() != null) { |
|---|
| 226 | | - if (isCyclicalRelationship(currentOrg.getId(), org.getParentOrganization())) { |
|---|
| 227 | | - LOG.error("Organization parent generate a cyclical relationship, parent id {}, current id: {}", org.getParentOrgId(), currentOrg.getId()); |
|---|
| 228 | | - return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Cyclical relationships are not allowed, please change the parent organization, current Parent: " + org.getParentOrganization().getName()).build(); |
|---|
| 229 | | - } |
|---|
| 230 | | - } |
|---|
| 231 | | - |
|---|
| 232 | | - try { |
|---|
| 233 | | - setOrgUsers(currentOrg, org.getUsersIds(), em); |
|---|
| 234 | | - } catch (SeCurisException e) { |
|---|
| 235 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 236 | | - } |
|---|
| 169 | + org.setParentOrganization(parentOrg); |
|---|
| 170 | + } |
|---|
| 237 | 171 | |
|---|
| 238 | | - currentOrg.setCode(org.getCode()); |
|---|
| 239 | | - currentOrg.setName(org.getName()); |
|---|
| 240 | | - currentOrg.setDescription(org.getDescription()); |
|---|
| 241 | | - em.persist(currentOrg); |
|---|
| 172 | + private void setOrgUsers(Organization org, List<String> usersIds, EntityManager em) throws SeCurisException { |
|---|
| 173 | + List<User> users = null; |
|---|
| 174 | + if (usersIds != null && usersIds.size() > 0) { |
|---|
| 175 | + users = new ArrayList<>(); |
|---|
| 176 | + for (String username : usersIds) { |
|---|
| 177 | + User user = em.find(User.class, username); |
|---|
| 178 | + if (user == null) { |
|---|
| 179 | + LOG.error("Organization user with id '{}' not found in DB", username); |
|---|
| 180 | + throw new SecurityException("Organization's user not found with ID: " + username); |
|---|
| 181 | + } |
|---|
| 182 | + users.add(user); |
|---|
| 183 | + } |
|---|
| 184 | + } |
|---|
| 242 | 185 | |
|---|
| 243 | | - return Response.ok(currentOrg).build(); |
|---|
| 244 | | - } |
|---|
| 186 | + org.setUsers(users); |
|---|
| 187 | + } |
|---|
| 245 | 188 | |
|---|
| 246 | | - @DELETE |
|---|
| 247 | | - @Path("/{orgid}") |
|---|
| 248 | | - @Transactional |
|---|
| 249 | | - @Produces( |
|---|
| 250 | | - { MediaType.APPLICATION_JSON }) |
|---|
| 251 | | - @Securable |
|---|
| 252 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 253 | | - public Response delete(@PathParam("orgid") String orgid, @Context HttpServletRequest request) { |
|---|
| 254 | | - LOG.info("Deleting organization with id: {}", orgid); |
|---|
| 255 | | - EntityManager em = emProvider.get(); |
|---|
| 256 | | - Organization org = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 257 | | - if (org == null) { |
|---|
| 258 | | - LOG.error("Organization with id {} can not be deleted, It was not found in DB", orgid); |
|---|
| 259 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization was not found, ID: " + orgid).build(); |
|---|
| 260 | | - } |
|---|
| 261 | | - if (org.getChildOrganizations() != null && org.getChildOrganizations().size() > 0) { |
|---|
| 262 | | - LOG.error("Organization has children and can not be deleted, ID: " + orgid); |
|---|
| 263 | | - return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization has children and can not be deleted, ID: " + orgid).build(); |
|---|
| 264 | | - } |
|---|
| 189 | + @PUT |
|---|
| 190 | + @POST |
|---|
| 191 | + @Path("/{orgid}") |
|---|
| 192 | + @Transactional |
|---|
| 193 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 194 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 195 | + @Securable |
|---|
| 196 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 197 | + public Response modify(Organization org, @PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 198 | + LOG.info("Modifying organization with id: {}", orgid); |
|---|
| 199 | + EntityManager em = emProvider.get(); |
|---|
| 200 | + Organization currentOrg = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 201 | + if (currentOrg == null) { |
|---|
| 202 | + LOG.error("Organization with id {} not found in DB", orgid); |
|---|
| 203 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization not found with ID: " + orgid) |
|---|
| 204 | + .build(); |
|---|
| 205 | + } |
|---|
| 206 | + try { |
|---|
| 207 | + this.setParentOrg(currentOrg, org.getParentOrgId(), em); |
|---|
| 208 | + } catch (SeCurisException e) { |
|---|
| 209 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 210 | + } |
|---|
| 211 | + if (org.getParentOrganization() != null) { |
|---|
| 212 | + if (isCyclicalRelationship(currentOrg.getId(), org.getParentOrganization())) { |
|---|
| 213 | + LOG.error("Organization parent generate a cyclical relationship, parent id {}, current id: {}", org.getParentOrgId(), |
|---|
| 214 | + currentOrg.getId()); |
|---|
| 215 | + return Response |
|---|
| 216 | + .status(Status.FORBIDDEN) |
|---|
| 217 | + .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, |
|---|
| 218 | + "Cyclical relationships are not allowed, please change the parent organization, current Parent: " |
|---|
| 219 | + + org.getParentOrganization().getName()).build(); |
|---|
| 220 | + } |
|---|
| 221 | + } |
|---|
| 265 | 222 | |
|---|
| 266 | | - em.remove(org); |
|---|
| 267 | | - return Response.ok(Utils.createMap("success", true, "id", orgid)).build(); |
|---|
| 268 | | - } |
|---|
| 223 | + try { |
|---|
| 224 | + setOrgUsers(currentOrg, org.getUsersIds(), em); |
|---|
| 225 | + } catch (SeCurisException e) { |
|---|
| 226 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 227 | + } |
|---|
| 228 | + |
|---|
| 229 | + currentOrg.setCode(org.getCode()); |
|---|
| 230 | + currentOrg.setName(org.getName()); |
|---|
| 231 | + currentOrg.setDescription(org.getDescription()); |
|---|
| 232 | + em.persist(currentOrg); |
|---|
| 233 | + |
|---|
| 234 | + return Response.ok(currentOrg).build(); |
|---|
| 235 | + } |
|---|
| 236 | + |
|---|
| 237 | + @DELETE |
|---|
| 238 | + @Path("/{orgid}") |
|---|
| 239 | + @Transactional |
|---|
| 240 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 241 | + @Securable |
|---|
| 242 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 243 | + public Response delete(@PathParam("orgid") String orgid, @Context HttpServletRequest request) { |
|---|
| 244 | + LOG.info("Deleting organization with id: {}", orgid); |
|---|
| 245 | + EntityManager em = emProvider.get(); |
|---|
| 246 | + Organization org = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 247 | + if (org == null) { |
|---|
| 248 | + LOG.error("Organization with id {} can not be deleted, It was not found in DB", orgid); |
|---|
| 249 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization was not found, ID: " + orgid) |
|---|
| 250 | + .build(); |
|---|
| 251 | + } |
|---|
| 252 | + if (org.getChildOrganizations() != null && org.getChildOrganizations().size() > 0) { |
|---|
| 253 | + LOG.error("Organization has children and can not be deleted, ID: " + orgid); |
|---|
| 254 | + return Response.status(Status.FORBIDDEN) |
|---|
| 255 | + .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Organization has children and can not be deleted, ID: " + orgid).build(); |
|---|
| 256 | + } |
|---|
| 257 | + |
|---|
| 258 | + em.remove(org); |
|---|
| 259 | + return Response.ok(Utils.createMap("success", true, "id", orgid)).build(); |
|---|
| 260 | + } |
|---|
| 269 | 261 | |
|---|
| 270 | 262 | } |
|---|