From 9c215c4ac9f4a6b73267c17912799cf0c3adbd99 Mon Sep 17 00:00:00 2001 From: madhawap Date: Mon, 27 Jun 2016 10:51:09 +0530 Subject: [PATCH] added /priorities end-point to PolicyManagement --- .../service/api/PolicyManagementService.java | 29 +++++++++++++++ .../impl/PolicyManagementServiceImpl.java | 35 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java index c1e268eb7fa..922315c8a6b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java @@ -23,6 +23,7 @@ import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper; import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.device.mgt.jaxrs.beans.PriorityUpdatedPolicyWrapper; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; @@ -387,4 +388,32 @@ public interface PolicyManagementService { required = true) List policyIds); + @PUT + @Path("/priorities") + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "PUT", + value = "Prioritizing policies.", + notes = "", + tags = "Device Policy Management" + ) + @ApiResponses(value = { + @ApiResponse( + code = 200, + message = "Policy Priorities successfully updated."), + @ApiResponse( + code = 400, + message = "Policy priorities did not update. Bad Request.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Exception in updating policy priorities.", + response = ErrorResponse.class) + }) + @Permission(scope = "", permissions = {}) + Response updatePolicyPriorities(@ApiParam(name = "priorityUpdatedPolicies", value = "", + required = true) List priorityUpdatedPolicies); + + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java index e22b3996d17..9063b9231e4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java @@ -40,10 +40,12 @@ import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; +import org.wso2.carbon.device.mgt.jaxrs.beans.PriorityUpdatedPolicyWrapper; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.util.ArrayList; import java.util.List; @Path("/policies") @@ -301,4 +303,37 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { } } + @PUT + @Path("/priorities") + public Response updatePolicyPriorities(List priorityUpdatedPolicies) { + PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); + List policiesToUpdate = new ArrayList<>(priorityUpdatedPolicies.size()); + int i; + for (i = 0; i < priorityUpdatedPolicies.size(); i++) { + Policy policyObj = new Policy(); + policyObj.setId(priorityUpdatedPolicies.get(i).getId()); + policyObj.setPriorityId(priorityUpdatedPolicies.get(i).getPriority()); + policiesToUpdate.add(policyObj); + } + boolean policiesUpdated; + try { + PolicyAdministratorPoint pap = policyManagementService.getPAP(); + policiesUpdated = pap.updatePolicyPriorities(policiesToUpdate); + } catch (PolicyManagementException e) { + String error = "Exception in updating policy priorities."; + log.error(error, e); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(error).build()); + } + if (policiesUpdated) { + return Response.status(Response.Status.OK).entity("Policy Priorities successfully " + + "updated.").build(); + + } else { + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Policy priorities did " + + "not update. Bad Request.").build()); + } + } + }