From e56fb5f4de5d8f08af879a39a412ff501cba35fc Mon Sep 17 00:00:00 2001 From: Madhawa Perera Date: Mon, 22 Jan 2018 09:38:51 +0530 Subject: [PATCH] New Activity Info Provider API resource which accept an array of activity IDs to get activity information (#1161) * New Activity Info Provider API resource which accept an array of activity IDs to get activity information * Modifications to the new activity info provider api * Add acceptable activity list count validation * Modifications to the new activity provider API resource * Added license header to the test class * Modifications to the new activity provider api --- .../mgt/jaxrs/common/ActivityIdList.java | 46 ++++ .../api/ActivityInfoProviderService.java | 83 +++++- .../impl/ActivityProviderServiceImpl.java | 58 ++++- .../impl/util/RequestValidationUtil.java | 30 +-- .../impl/ActivityProviderServiceImplTest.java | 245 ++++++++++++++++++ .../operation/mgt/OperationManager.java | 2 + .../operation/mgt/OperationManagerImpl.java | 29 +++ .../core/operation/mgt/dao/OperationDAO.java | 2 + .../mgt/dao/impl/GenericOperationDAOImpl.java | 139 +++++++++- .../impl/operation/MySQLOperationDAOImpl.java | 149 ++++++++++- .../DeviceManagementProviderService.java | 2 + .../DeviceManagementProviderServiceImpl.java | 5 + 12 files changed, 763 insertions(+), 27 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/common/ActivityIdList.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImplTest.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/common/ActivityIdList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/common/ActivityIdList.java new file mode 100644 index 00000000000..4dce0fc628e --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/common/ActivityIdList.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.jaxrs.common; + +import java.util.Arrays; +import java.util.List; + +public class ActivityIdList { + + private String ids; + private List idList; + + public ActivityIdList(String ids) { + this.ids = ids; + if (ids != null) { + String[] splits = ids.split(","); + if (splits.length > 0 && splits.length < 11 && splits[0] != null && !splits[0].isEmpty()) { + idList = Arrays.asList(splits); + } + } + } + + public List getIdList() { + return idList; + } + + public String getIds() { + return ids; + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java index 655559713a6..302869e107f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/ActivityInfoProviderService.java @@ -18,16 +18,33 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.api; -import io.swagger.annotations.*; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.Extension; +import io.swagger.annotations.ExtensionProperty; +import io.swagger.annotations.Info; +import io.swagger.annotations.ResponseHeader; +import io.swagger.annotations.SwaggerDefinition; +import io.swagger.annotations.Tag; import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.jaxrs.beans.ActivityList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; +import org.wso2.carbon.device.mgt.jaxrs.common.ActivityIdList; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import javax.validation.constraints.Size; -import javax.ws.rs.*; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -139,6 +156,68 @@ public interface ActivityInfoProviderService { required = false) @HeaderParam("If-Modified-Since") String ifModifiedSince); + @GET + @Path("/ids") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Getting Details of activities for given set of activity/operation Ids", + notes = "Retrieve the details of specific activity/operation Ids, such as the meta information of " + + "an operation, including the responses from the devices.", + tags = "Activity Info Provider", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:get-activity") + }) + } + ) + @ApiResponses(value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the activity details.", + response = Activity.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 400, + message = "Bad Request. \n Activity Ids shouldn't be empty", + response = ErrorResponse.class), + @ApiResponse( + code = 401, + message = "Unauthorized. \n Unauthorized operation! Only admin role can perform this " + + "operation."), + @ApiResponse( + code = 404, + message = "Not Found. \n No activity found with the given IDs.", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n ErrorResponse occurred while fetching the activity " + + "list for the supplied ids.", + response = ErrorResponse.class) + }) + Response getActivities( + @ApiParam( + name = "ids", + value = "Comma separated activity/operation ids", + required = true, + defaultValue = "ACTIVITY_0") + @QueryParam("ids") ActivityIdList activityIdList); + @GET @Path("/{id}/{devicetype}/{deviceid}") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java index 4b1d7bee503..45ad77ee78e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImpl.java @@ -26,13 +26,20 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementExcept import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.ActivityList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; +import org.wso2.carbon.device.mgt.jaxrs.common.ActivityIdList; import org.wso2.carbon.device.mgt.jaxrs.service.api.ActivityInfoProviderService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.user.api.UserStoreException; import javax.validation.constraints.Size; -import javax.ws.rs.*; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.text.ParseException; @@ -79,6 +86,55 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService } } + @GET + @Override + @Path("/ids") + public Response getActivities(@QueryParam("ids") ActivityIdList activityIdList) { + + List idList; + idList = activityIdList.getIdList(); + if (idList == null || idList.isEmpty()) { + String msg = "Activity Ids shouldn't be empty"; + log.error(msg); + return Response.status(400).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + Response validationFailedResponse = validateAdminUser(); + if (validationFailedResponse == null) { + List activities; + ActivityList activityList = new ActivityList(); + DeviceManagementProviderService dmService; + try { + for (String id : idList) { + RequestValidationUtil.validateActivityId(id); + } + dmService = DeviceMgtAPIUtils.getDeviceManagementService(); + activities = dmService.getOperationByActivityIds(idList); + if (!activities.isEmpty()) { + activityList.setList(activities); + int count = activities.size(); + if (log.isDebugEnabled()) { + log.debug("Number of activities : " + count); + } + activityList.setCount(count); + return Response.ok().entity(activityList).build(); + } else { + String msg = "No activity found with the given IDs."; + log.error(msg); + return Response.status(404).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + } catch (OperationManagementException e) { + String msg = "ErrorResponse occurred while fetching the activity list for the supplied ids."; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + } else { + return validationFailedResponse; + } + } + @GET @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java index d6e921c0cad..7c1b5dc86a9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java @@ -188,24 +188,24 @@ public class RequestValidationUtil { public static void validateActivityId(String activityId) { if (activityId == null || activityId.isEmpty()) { - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Activity Id " + - "cannot be null or empty. It should be in the form of " + + throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l) + .setMessage("Activity Id cannot be null or empty. It should be in the form of " + "'[ACTIVITY][_][any-positive-integer]' instead").build()); } String[] splits = activityId.split("_"); - if (splits == null || splits[0] == null || splits[0].isEmpty() || !"ACTIVITY".equals(splits[0]) || - splits[1] == null || splits[0].isEmpty()) { - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage( - "Activity Id should be in the form of '[ACTIVITY][_][any-positive-integer]'").build()); - } - try { - Long.parseLong(splits[1]); - } catch (NumberFormatException e) { - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage( - "Activity Id should be in the form of '[ACTIVITY][_][any-positive-integer]'").build()); + if (splits.length > 1 && splits[0] != null && !splits[0].isEmpty() && "ACTIVITY".equals(splits[0])) { + try { + Long.parseLong(splits[1]); + } catch (NumberFormatException e) { + throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l) + .setMessage( + "Activity Id should be in the form of '[ACTIVITY][_][any-positive-integer]'") + .build()); + } + } else { + throw new InputValidationException(new ErrorResponse.ErrorResponseBuilder().setCode(400l) + .setMessage("Activity Id should be in the form of '[ACTIVITY][_][any-positive-integer]'") + .build()); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImplTest.java new file mode 100644 index 00000000000..0db0ac07b7c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ActivityProviderServiceImplTest.java @@ -0,0 +1,245 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.wso2.carbon.device.mgt.jaxrs.service.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.testng.Assert; +import org.testng.IObjectFactory; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.ObjectFactory; +import org.testng.annotations.Test; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; +import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; +import org.wso2.carbon.device.mgt.jaxrs.common.ActivityIdList; +import org.wso2.carbon.device.mgt.jaxrs.service.api.ActivityInfoProviderService; +import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceManagementService; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; +import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; + +import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.List; + +import static org.mockito.MockitoAnnotations.initMocks; + +/** + * This is a test class for {@link ActivityProviderServiceImpl}. + */ +@PowerMockIgnore("javax.ws.rs.*") +@SuppressStaticInitializationFor({ "org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils", + "org.wso2.carbon.context.CarbonContext", "org.wso2.carbon.context.PrivilegedCarbonContext" }) +@PrepareForTest({ DeviceMgtAPIUtils.class, PolicyManagerUtil.class, PrivilegedCarbonContext.class }) +public class ActivityProviderServiceImplTest { + + private static final Log log = LogFactory.getLog(ActivityProviderServiceImplTest.class); + private static final String TEST_ACTIVITY_ID = "ACTIVITY_1"; + private static final String IF_MODIFIED_SINCE = "01Aug2018"; + private static final String DEVICE_TYPE = "android"; + private static final String DEVICE_ID = "1234567"; + private static final String OPERATION_CODE = "111222"; + private static final int OFFSET = 0; + private static final int LIMIT = 5; + private static final String TEST_ACTIVITY_ID_LIST = "ACTIVITY_1,ACTIVITY_2"; + private static final List idList = new ArrayList(); + private static final List activities = new ArrayList<>(); + private static final ActivityIdList activityList = new ActivityIdList(TEST_ACTIVITY_ID_LIST); + private static final ActivityIdList activityListEmpty = new ActivityIdList(""); + + private List idList1; + private Activity activity; + private List activities1; + + private DeviceManagementService deviceManagementService; + private DeviceAccessAuthorizationService deviceAccessAuthorizationService; + private DeviceManagementProviderService deviceManagementProviderService; + private ActivityInfoProviderService activityInfoProviderService; + private DeviceIdentifier deviceIdentifier; + + @ObjectFactory + public IObjectFactory getObjectFactory() { + return new org.powermock.modules.testng.PowerMockObjectFactory(); + } + + @BeforeClass + public void init() { + log.info("Initializing ActivityProviderServiceImplTest tests"); + initMocks(this); + this.deviceManagementProviderService = Mockito.mock(DeviceManagementProviderServiceImpl.class, + Mockito.RETURNS_MOCKS); + this.deviceIdentifier = new DeviceIdentifier(); + this.deviceManagementService = new DeviceManagementServiceImpl(); + this.activityInfoProviderService = new ActivityProviderServiceImpl(); + this.deviceAccessAuthorizationService = Mockito.mock(DeviceAccessAuthorizationServiceImpl.class); + idList.add("ACTIVITY_1"); + idList.add("ACTIVITY_2"); + this.activity = new Activity(); + Activity activity1 = new Activity(); + Activity activity2 = new Activity(); + activity1.setActivityId("ACTIVITY_1"); + activity2.setActivityId("ACTIVITY_2"); + activities.add(activity1); + activities.add(activity2); + } + + @Test(description = + "This method tests getting details of an activity with an admin user with an existing" + + " activity Id") + public void testGetActivitiesWithValidAdminUserWithValidId() throws OperationManagementException { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(true); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); + Mockito.when(this.deviceManagementProviderService.getOperationByActivityId(TEST_ACTIVITY_ID)) + .thenReturn(activity); + Response response = this.activityInfoProviderService.getActivity(TEST_ACTIVITY_ID, IF_MODIFIED_SINCE); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); + Mockito.reset(this.deviceManagementProviderService); + } + + @Test(description = "This method tests getting details of an activity with an invalid admin user") + public void testGetActivitiesWithInvalidAdminUserWithValidId() throws OperationManagementException { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(false); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); + Mockito.when(this.deviceManagementProviderService.getOperationByActivityId(TEST_ACTIVITY_ID)) + .thenReturn(activity); + Response response = this.activityInfoProviderService.getActivity(TEST_ACTIVITY_ID, IF_MODIFIED_SINCE); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode()); + Mockito.reset(this.deviceManagementProviderService); + } + + @Test(description = "This method tests getting details of an activity which does not exists") + public void testGetActivitiesWithNonExistingActivityID() throws OperationManagementException { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(true); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); + Mockito.when(this.deviceManagementProviderService.getOperationByActivityId(TEST_ACTIVITY_ID)) + .thenReturn(null); + Response response = this.activityInfoProviderService.getActivity(TEST_ACTIVITY_ID, IF_MODIFIED_SINCE); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); + Mockito.reset(this.deviceManagementProviderService); + } + + @Test(description = "This method tests the getActivity method under negative conditions.") + public void testGetActivitiesWithOperationManagementException() throws OperationManagementException { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(true); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); + Mockito.when(this.deviceManagementProviderService.getOperationByActivityId(Mockito.any())).thenThrow( + new OperationManagementException()); + Response response = this.activityInfoProviderService.getActivity(TEST_ACTIVITY_ID, IF_MODIFIED_SINCE); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + Mockito.reset(this.deviceManagementProviderService); + } + + @Test(description = "This method tests getting details of list of given activity IDs") + public void testGetActivitiesWithActivityIdList() throws OperationManagementException { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(true); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); + Mockito.when(this.deviceManagementProviderService.getOperationByActivityIds(idList)).thenReturn( + activities); + Response response = this.activityInfoProviderService.getActivities(activityList); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); + Mockito.reset(this.deviceManagementProviderService); + } + + @Test(description = "This method tests trying to get details activity IDs when call with empty list") + public void testGetActivitiesWithEmptyActivityIdList() throws OperationManagementException { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(true); + Response response = this.activityInfoProviderService.getActivities(activityListEmpty); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); + Mockito.reset(this.deviceManagementProviderService); + } + + @Test(description = "This method tests trying to get details of a list activity IDs which does not exists") + public void testGetActivitiesWithNonExistingActivityIdList() throws OperationManagementException { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(true); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); + Mockito.when(this.deviceManagementProviderService.getOperationByActivityIds(idList1)).thenReturn( + activities1); + Response response = this.activityInfoProviderService.getActivities(activityList); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); + Mockito.reset(this.deviceManagementProviderService); + } + + @Test(description = "This method tests getting details of an activity for a given device") + public void testGetActivitiesByDevice() throws OperationManagementException { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); + Mockito.when(this.deviceManagementProviderService + .getOperationByActivityIdAndDevice(TEST_ACTIVITY_ID, deviceIdentifier)).thenReturn(activity); + Response response = this.activityInfoProviderService.getActivityByDevice(TEST_ACTIVITY_ID, + DEVICE_TYPE, DEVICE_ID, IF_MODIFIED_SINCE); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); + Mockito.reset(this.deviceManagementProviderService); + } + + @Test(description = "This method tests getting details of an activity for a given device") + public void testGetActivities() throws OperationManagementException { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(true); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); + Mockito.when( + this.deviceManagementProviderService.getFilteredActivities(OPERATION_CODE, OFFSET, LIMIT)) + .thenReturn(activities); + Response response = this.activityInfoProviderService.getActivities(OPERATION_CODE, OFFSET, LIMIT); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); + Mockito.reset(this.deviceManagementProviderService); + } + + @Test(description = "This method tests getting details of an activity for a given device") + public void testGetActivitiesForInvalidUser() throws OperationManagementException { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(false); + PowerMockito.stub(PowerMockito.method(RequestValidationUtil.class, "validateActivityId")); + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); + Mockito.when( + this.deviceManagementProviderService.getFilteredActivities(OPERATION_CODE, OFFSET, LIMIT)) + .thenReturn(activities); + Response response = this.activityInfoProviderService.getActivities(OPERATION_CODE, OFFSET, LIMIT); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode()); + Mockito.reset(this.deviceManagementProviderService); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java index 19443007ae4..6dc9d55305a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java @@ -91,6 +91,8 @@ public interface OperationManager { Activity getOperationByActivityId(String activity) throws OperationManagementException; + List getOperationByActivityIds(List idList) throws OperationManagementException; + Activity getOperationByActivityIdAndDevice(String activity, DeviceIdentifier deviceId) throws OperationManagementException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 3f80da96939..27ba57b3fe8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -737,6 +737,35 @@ public class OperationManagerImpl implements OperationManager { } } + @Override + public List getOperationByActivityIds(List activities) + throws OperationManagementException { + List operationIds = new ArrayList<>(); + for (String id : activities) { + int operationId = Integer.parseInt( + id.replace(DeviceManagementConstants.OperationAttributes.ACTIVITY, "")); + if (operationId == 0) { + throw new IllegalArgumentException("Operation ID cannot be null or zero (0)."); + } else { + operationIds.add(operationId); + } + } + + try { + OperationManagementDAOFactory.openConnection(); + return operationDAO.getActivityList(operationIds); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source.", e); + } catch (OperationManagementDAOException e) { + throw new OperationManagementException( + "Error occurred while retrieving the operation with activity Id '" + activities + .toString(), e); + } finally { + OperationManagementDAOFactory.closeConnection(); + } + } + public Activity getOperationByActivityIdAndDevice(String activity, DeviceIdentifier deviceId) throws OperationManagementException { // This parses the operation id from activity id (ex : ACTIVITY_23) and converts to the integer. int operationId = Integer.parseInt( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java index 14dfc680903..908068b394c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java @@ -61,6 +61,8 @@ public interface OperationDAO { Activity getActivity(int operationId) throws OperationManagementDAOException; + List getActivityList(List operationIds) throws OperationManagementDAOException; + Activity getActivityByDevice(int operationId, int deviceId) throws OperationManagementDAOException; List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index d897f861db6..6c7225b80de 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -309,6 +309,133 @@ public class GenericOperationDAOImpl implements OperationDAO { return activity; } + + @Override + public List getActivityList(List activityIds) throws OperationManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + Activity activity; + List activities = new ArrayList<>(); + Object[] data = activityIds.toArray(); + + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = + "SELECT eom.ENROLMENT_ID, eom.OPERATION_ID, eom.ID AS EOM_MAPPING_ID, " + + "dor.ID AS OP_RES_ID, de.DEVICE_ID, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID, " + + "dt.NAME AS DEVICE_TYPE_NAME, eom.STATUS, eom.CREATED_TIMESTAMP, " + + "eom.UPDATED_TIMESTAMP, op.OPERATION_CODE, op.TYPE AS OPERATION_TYPE, " + + "dor.OPERATION_RESPONSE, dor.RECEIVED_TIMESTAMP FROM " + + "DM_ENROLMENT_OP_MAPPING eom INNER JOIN DM_OPERATION op " + + "ON op.ID=eom.OPERATION_ID INNER JOIN DM_ENROLMENT de " + + "ON de.ID=eom.ENROLMENT_ID INNER JOIN DM_DEVICE d ON d.ID=de.DEVICE_ID \n" + + "INNER JOIN DM_DEVICE_TYPE dt ON dt.ID=d.DEVICE_TYPE_ID\n" + + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE dor ON dor.ENROLMENT_ID=de.id \n" + + "AND dor.OPERATION_ID = eom.OPERATION_ID WHERE eom.OPERATION_ID " + + "IN (SELECT * FROM TABLE(x INT = ?)) AND de.TENANT_ID = ?"; + + stmt = conn.prepareStatement(sql); + stmt.setObject(1, data); + + stmt.setInt(2, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + rs = stmt.executeQuery(); + + int operationId = 0; + int enrolmentId = 0; + int responseId = 0; + ActivityStatus activityStatus = new ActivityStatus(); + while (rs.next()) { + activity = new Activity(); + + if (operationId != rs.getInt("OPERATION_ID")) { + activities.add(activity); + List statusList = new ArrayList<>(); + activityStatus = new ActivityStatus(); + + operationId = rs.getInt("OPERATION_ID"); + enrolmentId = rs.getInt("ENROLMENT_ID"); + + activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); + activity.setCreatedTimeStamp( + new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); + activity.setCode(rs.getString("OPERATION_CODE")); + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE_NAME")); + + activityStatus.setDeviceIdentifier(deviceIdentifier); + + activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); + + List operationResponses = new ArrayList<>(); + if (rs.getInt("UPDATED_TIMESTAMP") != 0) { + activityStatus.setUpdatedTimestamp( + new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); + + } + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { + operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); + responseId = rs.getInt("OP_RES_ID"); + } + activityStatus.setResponses(operationResponses); + statusList.add(activityStatus); + activity.setActivityStatus(statusList); + activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID"))); + } + + if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { + activityStatus = new ActivityStatus(); + + activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); + activity.setCreatedTimeStamp( + new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); + activity.setCode(rs.getString("OPERATION_CODE")); + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE_NAME")); + activityStatus.setDeviceIdentifier(deviceIdentifier); + + activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); + + List operationResponses = new ArrayList<>(); + if (rs.getInt("UPDATED_TIMESTAMP") != 0) { + activityStatus.setUpdatedTimestamp( + new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); + } + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { + operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); + responseId = rs.getInt("OP_RES_ID"); + } + activityStatus.setResponses(operationResponses); + activity.getActivityStatus().add(activityStatus); + + enrolmentId = rs.getInt("ENROLMENT_ID"); + } + + if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID") && rs.getTimestamp( + "RECEIVED_TIMESTAMP") != null) { + activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); + responseId = rs.getInt("OP_RES_ID"); + } + } + + } catch (SQLException e) { + throw new OperationManagementDAOException( + "Error occurred while getting the operation details from " + "the database.", e); + } catch (ClassNotFoundException e) { + throw new OperationManagementDAOException( + "Error occurred while converting the operation response to string.", e); + } catch (IOException e) { + throw new OperationManagementDAOException( + "IO exception occurred while converting the operations responses.", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + } + return activities; + } + public Activity getActivityByDevice(int operationId, int deviceId) throws OperationManagementDAOException { PreparedStatement stmt = null; @@ -482,7 +609,7 @@ public class GenericOperationDAOImpl implements OperationDAO { rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); responseId = rs.getInt("OP_RES_ID"); } @@ -512,7 +639,7 @@ public class GenericOperationDAOImpl implements OperationDAO { activityStatus.setUpdatedTimestamp(new java.util.Date( rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); responseId = rs.getInt("OP_RES_ID"); } @@ -523,7 +650,7 @@ public class GenericOperationDAOImpl implements OperationDAO { } if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); responseId = rs.getInt("OP_RES_ID"); } @@ -669,7 +796,7 @@ public class GenericOperationDAOImpl implements OperationDAO { rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); responseId = rs.getInt("OP_RES_ID"); } @@ -699,7 +826,7 @@ public class GenericOperationDAOImpl implements OperationDAO { activityStatus.setUpdatedTimestamp(new java.util.Date( rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); responseId = rs.getInt("OP_RES_ID"); } @@ -710,7 +837,7 @@ public class GenericOperationDAOImpl implements OperationDAO { } if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); responseId = rs.getInt("OP_RES_ID"); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java index f935cab57a6..a197769c267 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java @@ -77,6 +77,139 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl { return isUpdated; } + @Override + public List getActivityList(List activityIds) throws OperationManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + Activity activity; + List activities = new ArrayList<>(); + + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + + String sql1 = "SELECT eom.ENROLMENT_ID, eom.OPERATION_ID, eom.ID AS EOM_MAPPING_ID, " + + "dor.ID AS OP_RES_ID, de.DEVICE_ID, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID, " + + "dt.NAME AS DEVICE_TYPE_NAME, eom.STATUS, eom.CREATED_TIMESTAMP, " + + "eom.UPDATED_TIMESTAMP, op.OPERATION_CODE, op.TYPE AS OPERATION_TYPE, " + + "dor.OPERATION_RESPONSE, dor.RECEIVED_TIMESTAMP FROM " + + "DM_ENROLMENT_OP_MAPPING eom INNER JOIN DM_OPERATION op " + + "ON op.ID=eom.OPERATION_ID INNER JOIN DM_ENROLMENT de " + + "ON de.ID=eom.ENROLMENT_ID INNER JOIN DM_DEVICE d ON d.ID=de.DEVICE_ID \n" + + "INNER JOIN DM_DEVICE_TYPE dt ON dt.ID=d.DEVICE_TYPE_ID\n" + + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE dor ON dor.ENROLMENT_ID=de.id \n" + + "AND dor.OPERATION_ID = eom.OPERATION_ID WHERE eom.OPERATION_ID " + "IN ("; + + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < activityIds.size(); i++) { + builder.append("?,"); + } + sql1 += builder.deleteCharAt(builder.length() - 1).toString() + ") AND de.TENANT_ID = ?"; + stmt = conn.prepareStatement(sql1); + int i; + for (i = 0; i < activityIds.size(); i++) { + stmt.setInt(i + 1, activityIds.get(i)); + } + stmt.setInt(i + 1, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + + rs = stmt.executeQuery(); + + int operationId = 0; + int enrolmentId = 0; + int responseId = 0; + ActivityStatus activityStatus = new ActivityStatus(); + while (rs.next()) { + activity = new Activity(); + + if (operationId != rs.getInt("OPERATION_ID")) { + activities.add(activity); + List statusList = new ArrayList<>(); + activityStatus = new ActivityStatus(); + + operationId = rs.getInt("OPERATION_ID"); + enrolmentId = rs.getInt("ENROLMENT_ID"); + + activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); + activity.setCreatedTimeStamp( + new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); + activity.setCode(rs.getString("OPERATION_CODE")); + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE_NAME")); + + activityStatus.setDeviceIdentifier(deviceIdentifier); + + activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); + + List operationResponses = new ArrayList<>(); + if (rs.getInt("UPDATED_TIMESTAMP") != 0) { + activityStatus.setUpdatedTimestamp( + new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); + + } + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { + operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); + responseId = rs.getInt("OP_RES_ID"); + } + activityStatus.setResponses(operationResponses); + statusList.add(activityStatus); + activity.setActivityStatus(statusList); + activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID"))); + } + + if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { + activityStatus = new ActivityStatus(); + + activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); + activity.setCreatedTimeStamp( + new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); + activity.setCode(rs.getString("OPERATION_CODE")); + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); + deviceIdentifier.setType(rs.getString("DEVICE_TYPE_NAME")); + activityStatus.setDeviceIdentifier(deviceIdentifier); + + activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS"))); + + List operationResponses = new ArrayList<>(); + if (rs.getInt("UPDATED_TIMESTAMP") != 0) { + activityStatus.setUpdatedTimestamp( + new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); + } + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { + operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); + responseId = rs.getInt("OP_RES_ID"); + } + activityStatus.setResponses(operationResponses); + activity.getActivityStatus().add(activityStatus); + + enrolmentId = rs.getInt("ENROLMENT_ID"); + } + + if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID") && rs.getTimestamp( + "RECEIVED_TIMESTAMP") != null) { + activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); + responseId = rs.getInt("OP_RES_ID"); + } + } + + } catch (SQLException e) { + throw new OperationManagementDAOException( + "Error occurred while getting the operation details from " + "the database.", e); + } catch (ClassNotFoundException e) { + throw new OperationManagementDAOException( + "Error occurred while converting the operation response to string.", e); + } catch (IOException e) { + throw new OperationManagementDAOException( + "IO exception occurred while converting the operations responses.", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + } + return activities; + } + + @Override public List getActivitiesUpdatedAfter(long timestamp, int limit, @@ -176,7 +309,7 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl { rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); responseId = rs.getInt("OP_RES_ID"); } @@ -206,7 +339,7 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl { activityStatus.setUpdatedTimestamp(new java.util.Date( rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString()); } - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { operationResponses.add(OperationDAOUtil.getOperationResponse(rs)); responseId = rs.getInt("OP_RES_ID"); } @@ -217,7 +350,7 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl { } if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { - if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { + if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) { activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs)); responseId = rs.getInt("OP_RES_ID"); } @@ -235,4 +368,14 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl { } return activities; } + + private Integer[] getIntArrayOfActivityIds(List activityIds) { + Integer[] arr = new Integer[activityIds.size()]; + int x = 0; + for (Integer activityId : activityIds) { + arr[x] = activityId; + x++; + } + return arr; + } } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index a92f968a049..07ff4ba3827 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -564,6 +564,8 @@ public interface DeviceManagementProviderService { Activity getOperationByActivityId(String activity) throws OperationManagementException; + List getOperationByActivityIds(List idList) throws OperationManagementException; + Activity getOperationByActivityIdAndDevice(String activity, DeviceIdentifier deviceId) throws OperationManagementException; List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 6d3af652a38..9f2d4674a8f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1473,6 +1473,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return DeviceManagementDataHolder.getInstance().getOperationManager().getOperationByActivityId(activity); } + @Override + public List getOperationByActivityIds(List idList) throws OperationManagementException{ + return DeviceManagementDataHolder.getInstance().getOperationManager().getOperationByActivityIds(idList); + } + public Activity getOperationByActivityIdAndDevice(String activity, DeviceIdentifier deviceId) throws OperationManagementException { return DeviceManagementDataHolder.getInstance().getOperationManager().getOperationByActivityIdAndDevice(activity, deviceId); }