From 99fee36f2c65c99d865abe5cfa73b38f4c0d1d6f Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Tue, 27 Sep 2016 21:13:00 +0530 Subject: [PATCH 01/13] Fixing error when properties is null --- .../modules/business-controllers/device.js | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js index 6c7dbcc174..f5d57cacdf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js @@ -49,17 +49,17 @@ deviceModule = function () { privateMethods.callBackend = function (url, method) { if (constants["HTTP_GET"] == method) { return serviceInvokers.XMLHttp.get(url, - function (backendResponse) { - var response = {}; - response.content = backendResponse.responseText; - if (backendResponse.status == 200) { - response.status = "success"; - } else if (backendResponse.status == 400 || backendResponse.status == 401 || - backendResponse.status == 404 || backendResponse.status == 500) { - response.status = "error"; - } - return response; - } + function (backendResponse) { + var response = {}; + response.content = backendResponse.responseText; + if (backendResponse.status == 200) { + response.status = "success"; + } else if (backendResponse.status == 400 || backendResponse.status == 401 || + backendResponse.status == 404 || backendResponse.status == 500) { + response.status = "error"; + } + return response; + } ); } else { log.error("Runtime error : This method only support HTTP GET requests."); @@ -254,9 +254,11 @@ deviceModule = function () { var device = parse(backendResponse.responseText); var propertiesList = device["properties"]; var properties = {}; - for (var i = 0; i < propertiesList.length; i++) { - properties[propertiesList[i]["name"]] = - propertiesList[i]["value"]; + if (propertiesList) { + for (var i = 0; i < propertiesList.length; i++) { + properties[propertiesList[i]["name"]] = + propertiesList[i]["value"]; + } } var deviceObject = {}; deviceObject[constants["DEVICE_IDENTIFIER"]] = device["deviceIdentifier"]; @@ -356,7 +358,7 @@ deviceModule = function () { publicMethods.getDevices = function (userName) { var url = devicemgtProps["httpsURL"] + - devicemgtProps["backendRestEndpoints"]["deviceMgt"] + "/devices/user/" + userName; + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + "/devices/user/" + userName; return serviceInvokers.XMLHttp.get( url, function (responsePayload) { for (var i = 0; i < responsePayload.length; i++) { From 1bae3d2213f974a0ba572cd87dc103e3c437385c Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Tue, 27 Sep 2016 22:06:48 +0530 Subject: [PATCH 02/13] Fixing policy retrieve api --- .../public/js/device-view.js | 105 ++++++------------ 1 file changed, 37 insertions(+), 68 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/public/js/device-view.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/public/js/device-view.js index 3acdc05387..20cc6f96f5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/public/js/device-view.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/public/js/device-view.js @@ -17,7 +17,6 @@ */ (function () { - var deviceId = $(".device-id"); var deviceIdentifier = deviceId.data("deviceid"); var deviceType = deviceId.data("type"); @@ -46,7 +45,7 @@ }); }); - + function loadOperationsLog(update) { var operationsLogTable = "#operations-log-table"; if (update) { @@ -58,92 +57,67 @@ serverSide: true, processing: false, searching: false, - ordering: false, - pageLength: 10, + ordering: false, + pageLength : 10, order: [], ajax: { - url: "/emm/api/operation/paginate", - data: { - deviceId: deviceIdentifier, - deviceType: deviceType - }, + url: context + "/api/operation/paginate", + data: {deviceId : deviceIdentifier, deviceType: deviceType}, dataSrc: function (json) { - $("#operations-spinner").addClass( - "hidden"); + $("#operations-spinner").addClass("hidden"); $("#operations-log-container").empty(); return json.data; } }, columnDefs: [ - {targets: 0, data: "code"}, - { - targets: 1, - data: "status", - render: function (status) { + {targets: 0, data: "code" }, + {targets: 1, data: "status", render: + function (status) { var html; switch (status) { case "COMPLETED" : - html = - " Completed"; + html = " Completed"; break; case "PENDING" : - html = - " Pending"; + html = " Pending"; break; case "ERROR" : - html = - " Error"; + html = " Error"; break; case "IN_PROGRESS" : - html = - " In Progress"; + html = " In Progress"; break; case "REPEATED" : - html = - " Repeated"; + html = " Repeated"; break; } return html; } }, - { - targets: 2, - data: "createdTimeStamp", - render: function (date) { + {targets: 2, data: "createdTimeStamp", render: + function (date) { var value = String(date); return value.slice(0, 16); } } ], - "createdRow": function (row, data) { + "createdRow": function(row, data) { $(row).attr("data-type", "selectable"); $(row).attr("data-id", data["id"]); $.each($("td", row), - function (colIndex) { - switch (colIndex) { + function(colIndex) { + switch(colIndex) { case 1: - $(this).attr( - "data-grid-label", - "Code"); - $(this).attr( - "data-display", - data["code"]); + $(this).attr("data-grid-label", "Code"); + $(this).attr("data-display", data["code"]); break; case 2: - $(this).attr( - "data-grid-label", - "Status"); - $(this).attr( - "data-display", - data["status"]); + $(this).attr("data-grid-label", "Status"); + $(this).attr("data-display", data["status"]); break; case 3: - $(this).attr( - "data-grid-label", - "Created Timestamp"); - $(this).attr( - "data-display", - data["createdTimeStamp"]); + $(this).attr("data-grid-label", "Created Timestamp"); + $(this).attr("data-display", data["createdTimeStamp"]); break; } } @@ -163,10 +137,8 @@ "policy-view", policyComplianceTemplate, function (template) { - var getEffectivePolicyURL = "/api/device-mgt/v1.0/devices/" + deviceType + "/" + deviceId - + "/effective-policy"; - var getDeviceComplianceURL = "/api/device-mgt/v1.0/devices/" + deviceType + "/" + deviceId - + "/compliance-data"; + var getEffectivePolicyURL = "/api/device-mgt/v1.0/devices/" + deviceType + "/" + deviceId + "/effective-policy"; + var getDeviceComplianceURL = "/api/device-mgt/v1.0/devices/" + deviceType + "/" + deviceId + "/compliance-data"; invokerUtil.get( getEffectivePolicyURL, @@ -191,8 +163,7 @@ if (data["complianceData"]["complianceFeatures"] && data["complianceData"]["complianceFeatures"].length > 0) { viewModel["compliance"] = "NON-COMPLIANT"; - viewModel["complianceFeatures"] = - data["complianceData"]["complianceFeatures"]; + viewModel["complianceFeatures"] = data["complianceData"]["complianceFeatures"]; content = template(viewModel); $("#policy-list-container").html(content); } else { @@ -202,18 +173,17 @@ $("#policy-compliance-table").addClass("hidden"); } } else { - $("#policy-list-container").html( - "

This device " + - "has no policy applied.

"); + $("#policy-list-container"). + html("

This device " + + "has no policy applied.

"); } } }, // error-callback function () { - $("#policy-list-container").html( - "

Loading policy compliance related data " - + - "was not successful. please try refreshing data in a while.

"); + $("#policy-list-container"). + html("

Loading policy compliance related data " + + "was not successful. please try refreshing data in a while.

"); } ); } @@ -221,10 +191,9 @@ }, // error-callback function () { - $("#policy-list-container").html( - "

Loading policy compliance related data " - + - "was not successful. please try refreshing data in a while.

"); + $("#policy-list-container"). + html("

Loading policy compliance related data " + + "was not successful. please try refreshing data in a while.

"); } ); } From 6d4ddf7b549f82e8bd56d76a2040012143482cd8 Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Tue, 27 Sep 2016 23:19:14 +0530 Subject: [PATCH 03/13] Removed info log on view device --- .../devicemgt/app/units/cdmf.unit.device.view/view.js | 1 - 1 file changed, 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/view.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/view.js index 8ba043981f..bfeed404ff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/view.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/view.js @@ -111,7 +111,6 @@ function onRequest(context) { } } - log.info(device); var utility = require("/app/modules/utility.js").utility; var configs = utility.getDeviceTypeConfig(deviceType); return {"device": device, "label" : configs["deviceType"]["label"]}; From b2f31be1d679df7574c57d1eb39c8555ad806464 Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Wed, 28 Sep 2016 23:42:15 +0530 Subject: [PATCH 04/13] Fixing iot operation bar not listing operations --- .../modules/business-controllers/operation.js | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/operation.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/operation.js index 6be734ec27..c34cc04302 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/operation.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/operation.js @@ -38,42 +38,42 @@ var operationModule = function () { } privateMethods.getOperationsFromFeatures = function (deviceType, operationType) { - var url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + "/features/" + deviceType; + var url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + "/device-types/" + deviceType + "/features"; var featuresList = serviceInvokers.XMLHttp.get(url, function (responsePayload) { - var features = responsePayload; - var featureList = []; - var feature; - for (var i = 0; i < features.length; i++) { - feature = {}; - var analyticStreams = utility.getDeviceTypeConfig(deviceType)["analyticStreams"]; - if (analyticStreams) { - for (var stream in analyticStreams) { - if (analyticStreams[stream].name == features[i].name) { - feature.ui_unit = analyticStreams[stream].ui_unit; - break; - } - } - } + var features = JSON.parse(responsePayload.responseText); + var featureList = []; + var feature; + for (var i = 0; i < features.length; i++) { + feature = {}; + var analyticStreams = utility.getDeviceTypeConfig(deviceType)["analyticStreams"]; + if (analyticStreams) { + for (var stream in analyticStreams) { + if (analyticStreams[stream].name == features[i].name) { + feature.ui_unit = analyticStreams[stream].ui_unit; + break; + } + } + } - feature["operation"] = features[i].code; - feature["name"] = features[i].name; - feature["description"] = features[i].description; - feature["deviceType"] = deviceType; - feature["params"] = []; - var metaData = features[i].metadataEntries; - if (metaData) { - for (var j = 0; j < metaData.length; j++) { - feature["params"].push(metaData[j].value); - } - featureList.push(feature); - } - } - return featureList; - }, function (responsePayload) { - var response = {}; - response["status"] = "error"; - return response; - } + feature["operation"] = features[i].code; + feature["name"] = features[i].name; + feature["description"] = features[i].description; + feature["deviceType"] = deviceType; + feature["params"] = []; + var metaData = features[i].metadataEntries; + if (metaData) { + for (var j = 0; j < metaData.length; j++) { + feature["params"].push(metaData[j].value); + } + featureList.push(feature); + } + } + return featureList; + }, function (responsePayload) { + var response = {}; + response["status"] = "error"; + return response; + } ); return featuresList; }; From 237b9a707856b5eca8b9be6fa7c2f39650f535ae Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Wed, 28 Sep 2016 23:42:44 +0530 Subject: [PATCH 05/13] Creating api endpoint to retrieve features of a device-type --- .../api/DeviceTypeManagementService.java | 91 +++++++++++++++++-- .../impl/DeviceTypeManagementServiceImpl.java | 30 ++++++ 2 files changed, 113 insertions(+), 8 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceTypeManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceTypeManagementService.java index 79fbf42489..8aa54a5378 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceTypeManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceTypeManagementService.java @@ -18,17 +18,29 @@ */ 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.ResponseHeader; import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceTypeList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; -import javax.ws.rs.*; +import javax.validation.constraints.Size; +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.core.MediaType; import javax.ws.rs.core.Response; -@API(name = "Device Type Management", version = "1.0.0", context = "/api/device-mgt/v1.0/admin/device-types", tags = {"devicemgt_admin"}) +@API(name = "Device Type Management", version = "1.0.0", context = "/api/device-mgt/v1.0/admin/device-types", + tags = {"devicemgt_admin"}) @Path("/device-types") @Api(value = "Device Type Management", description = "This API corresponds to all tasks related to device " + @@ -60,14 +72,17 @@ public interface DeviceTypeManagementService { "Used by caches, or in conditional requests."), @ResponseHeader( name = "Last-Modified", - description = "Date and time the resource has been modified the last time.\n" + - "Used by caches, or in conditional requests."), + description = + "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), } ), @ApiResponse( code = 304, - message = "Not Modified. \n Empty body because the client has already the latest version of " + - "the requested resource."), + message = + "Not Modified. \n Empty body because the client has already the latest version of" + + " " + + "the requested resource."), @ApiResponse( code = 406, message = "Not Acceptable.\n The requested media type is not supported"), @@ -85,6 +100,66 @@ public interface DeviceTypeManagementService { value = "Validates if the requested variant has not been modified since the time specified", required = false) @HeaderParam("If-Modified-Since") - String ifModifiedSince); + String ifModifiedSince); + + @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Get the list of features for a give device type.", + notes = "Retrieves the list of features of a given device type.", + tags = "Device Type Management") + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the list of supported features.", + response = DeviceTypeList.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 has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + } + ), + @ApiResponse( + code = 304, + message = + "Not Modified. \n Empty body because the client has already the latest version of" + + " " + + "the requested resource."), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the " + + "list of supported device types.", + response = ErrorResponse.class) + } + ) + @Permission(name = "View Device Types", permission = "/device-mgt/devices/owning-device/view") + Response getFeatures( + @ApiParam( + name = "type", + value = "The device type, such as ios, android or windows.", + required = true) + @PathParam("type") + @Size(max = 45) + String type, + @ApiParam( + name = "If-Modified-Since", + value = "Validates if the requested variant has not been modified since the time specified", + required = false) + @HeaderParam("If-Modified-Since") + String ifModifiedSince); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementServiceImpl.java index e38f02a027..3bd43681e9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementServiceImpl.java @@ -21,14 +21,20 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.Feature; +import org.wso2.carbon.device.mgt.common.FeatureManager; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceTypeList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceTypeManagementService; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; +import javax.validation.constraints.Size; import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; import java.util.List; @@ -56,4 +62,28 @@ public class DeviceTypeManagementServiceImpl implements DeviceTypeManagementServ } } + @GET + @Override + @Path("/{type}/features") + public Response getFeatures(@PathParam("type") @Size(max = 45) String type, @HeaderParam("If-Modified-Since") String ifModifiedSince) { + List features; + DeviceManagementProviderService dms; + try { + dms = DeviceMgtAPIUtils.getDeviceManagementService(); + FeatureManager fm = dms.getFeatureManager(type); + if (fm == null) { + return Response.status(Response.Status.NOT_FOUND).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage("No feature manager is " + + "registered with the given type '" + type + "'").build()).build(); + } + features = fm.getFeatures(); + } catch (DeviceManagementException e) { + String msg = "Error occurred while retrieving the list of features of '" + type + "' device type"; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + return Response.status(Response.Status.OK).entity(features).build(); + } + } From 47d6268c9e0ebf2dfdbab1c16a761f7800e6eeb9 Mon Sep 17 00:00:00 2001 From: Megala Date: Thu, 29 Sep 2016 01:43:33 +0530 Subject: [PATCH 06/13] Correcting the problem related with oracle syntax --- .../dao/impl/OracleCertificateDAOImpl.java | 2 +- .../impl/OracleGadgetDataServiceDAOImpl.java | 24 ++--- .../core/dao/impl/AbstractDeviceDAOImpl.java | 4 +- .../dao/impl/device/OracleDeviceDAOImpl.java | 95 +++++++++---------- .../mgt/core/group/mgt/dao/GroupDAOImpl.java | 2 +- .../dao/impl/OracleNotificationDAOImpl.java | 2 +- .../mgt/dao/impl/GenericOperationDAOImpl.java | 12 +-- .../operation/OracleOperationDAOImpl.java | 18 ++-- .../main/resources/dbscripts/cdm/oracle.sql | 2 +- 9 files changed, 78 insertions(+), 83 deletions(-) diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/impl/OracleCertificateDAOImpl.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/impl/OracleCertificateDAOImpl.java index 69e61138bb..a206162518 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/impl/OracleCertificateDAOImpl.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/impl/OracleCertificateDAOImpl.java @@ -54,7 +54,7 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl { try { Connection conn = this.getConnection(); String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID, USERNAME FROM " - + "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC WHERE OFFSET >= ? AND ROWNUM <= ?"; + + "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setInt(2, rowNum); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/OracleGadgetDataServiceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/OracleGadgetDataServiceDAOImpl.java index 7fdc731b47..b90a9046b0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/OracleGadgetDataServiceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/src/main/java/org/wso2/carbon/device/mgt/analytics/dashboard/dao/impl/OracleGadgetDataServiceDAOImpl.java @@ -61,11 +61,10 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO int totalRecordsCount = 0; try { con = this.getConnection(); - String sql = "SELECT * FROM (SELECT ROWNUM offset, rs.* FROM (SELECT FEATURE_CODE, COUNT(DEVICE_ID) " + - "AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 + - " WHERE TENANT_ID = ? GROUP BY FEATURE_CODE ORDER BY DEVICE_COUNT DESC) rs) " + - "WHERE offset >= ? AND ROWNUM <= ?"; - + String sql = "SELECT FEATURE_CODE, COUNT(DEVICE_ID) AS DEVICE_COUNT FROM " + GadgetDataServiceDAOConstants. + DatabaseView.DEVICES_VIEW_2 + + " WHERE TENANT_ID = ? GROUP BY FEATURE_CODE ORDER BY DEVICE_COUNT DESC " + + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = con.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setInt(2, startIndex); @@ -138,11 +137,9 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; } } - sql = "SELECT * FROM (SELECT ROWNUM offset, rs.* FROM (SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, " + - "OWNERSHIP, CONNECTIVITY_STATUS FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_1 + - " WHERE TENANT_ID = ? " + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC) rs) " + - "WHERE offset >= ? AND ROWNUM <= ?"; - + sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " + + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_1 + " WHERE TENANT_ID = ? " + + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = con.prepareStatement(sql); // [2] appending filter column values, if exist stmt.setInt(1, tenantId); @@ -235,10 +232,9 @@ public class OracleGadgetDataServiceDAOImpl extends AbstractGadgetDataServiceDAO advancedSqlFiltering = advancedSqlFiltering + "AND " + column + " = ? "; } } - sql = "SELECT * FROM (SELECT ROWNUM offset, rs.* FROM (SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, " + - "OWNERSHIP, CONNECTIVITY_STATUS FROM " + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 + - " WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + advancedSqlFiltering + - "ORDER BY DEVICE_ID ASC) rs) WHERE offset >= ? AND ROWNUM <= ?"; + sql = "SELECT DEVICE_ID, DEVICE_IDENTIFICATION, PLATFORM, OWNERSHIP, CONNECTIVITY_STATUS FROM " + + GadgetDataServiceDAOConstants.DatabaseView.DEVICES_VIEW_2 + " WHERE TENANT_ID = ? AND FEATURE_CODE = ? " + + advancedSqlFiltering + "ORDER BY DEVICE_ID ASC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = con.prepareStatement(sql); // [2] appending filter column values, if exist stmt.setInt(1, tenantId); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index a7af240b3a..90c7aa2855 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -798,9 +798,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { ResultSet rs = null; try { conn = this.getConnection(); - String sql = "SELECT e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " + + String sql = "SELECT e.ID ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) dtm " + - "WHERE e.DEVICE_ID = dtm.ID AND e.STATUS = ? AND e.TENANT_ID = ?;"; + "WHERE e.DEVICE_ID = dtm.ID AND e.STATUS = ? AND e.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getId()); stmt.setString(2, deviceId.getType()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java index d070660d7a..bad8e5a4d6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java @@ -60,12 +60,11 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { boolean isSinceProvided = false; try { conn = this.getConnection(); - String sql = "SELECT * FROM (SELECT ROWNUM offset, rs.* FROM (SELECT d1.ID AS DEVICE_ID, " + - "d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, e.OWNER, " + - "e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, " + - "e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + - "d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t "; + + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " + + "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " + "FROM DM_DEVICE d, DM_DEVICE_TYPE t "; //Add the query to filter active devices on timestamp if (since != null) { @@ -109,7 +108,7 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { isStatusProvided = true; } - sql = sql + " ) rs ) WHERE OFFSET >= ? AND ROWNUM <= ?"; + sql = sql + " ORDER BY ENROLMENT_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -159,13 +158,13 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { List devices = null; try { conn = this.getConnection(); - String sql = "SELECT * FROM ( SELECT ROWNUM offset, rs.* FROM ( SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, " + - "d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, " + - "e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " + - "(SELECT d.ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, " + - "t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND " + - "t.NAME = ? AND d.TENANT_ID = ? ) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = " + - "? ) rs ) WHERE offset >= ? AND ROWNUM <= ?"; + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " + + "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + + "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? ORDER BY ENROLMENT_ID" + + " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setString(1, request.getDeviceType()); stmt.setInt(2, tenantId); @@ -194,13 +193,12 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT * FROM ( SELECT ROWNUM offset, rs.* FROM ( SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, " + - "e1.DATE_OF_LAST_UPDATE, e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, " + - "d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, (SELECT e.OWNER, e.OWNERSHIP, " + - "e.ID AS ENROLMENT_ID, e.DEVICE_ID, e.STATUS, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE e.TENANT_ID = ? AND e.OWNER = ? ) e1, " + - "DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID AND t.ID = d.DEVICE_TYPE_ID ) rs ) WHERE offset >= " + - "? AND ROWNUM <= ?"; + String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.DATE_OF_LAST_UPDATE," + + " e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DEVICE_IDENTIFICATION, t.NAME " + + "AS DEVICE_TYPE FROM DM_DEVICE d, (SELECT e.OWNER, e.OWNERSHIP, e.ID AS ENROLMENT_ID, " + + "e.DEVICE_ID, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE " + + "e.TENANT_ID = ? AND e.OWNER = ?) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " + + "AND t.ID = d.DEVICE_TYPE_ID ORDER BY ENROLMENT_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, request.getOwner()); @@ -229,13 +227,13 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT * FROM ( SELECT ROWNUM offset, rs.* FROM ( SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, " + - "d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, " + - "e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " + - "(SELECT d.ID, d.NAME, d.DESCRIPTION, t.NAME AS DEVICE_TYPE, " + - "d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = " + - "t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ? ) d1 WHERE DEVICE_ID = " + - "e.DEVICE_ID AND TENANT_ID = ? ) rs ) WHERE offset >= ? AND ROWNUM <= ?"; + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, " + + "d.DESCRIPTION, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " + + "WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? ORDER BY ENROLMENT_ID " + + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setString(1, request.getDeviceName() + "%"); stmt.setInt(2, tenantId); @@ -265,13 +263,13 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT * FROM ( SELECT ROWNUM offset, rs.* FROM ( SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, " + - "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, " + - "e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, " + - "e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, " + - "e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e WHERE TENANT_ID = ? AND OWNERSHIP = ?) e, " + - "DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = " + - "t.ID AND d.TENANT_ID = ? ) rs ) WHERE offset >= ? AND ROWNUM <= ?"; + String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " + + "d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " + + "e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e " + + "WHERE TENANT_ID = ? AND OWNERSHIP = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " + + "WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? ORDER BY ENROLMENT_ID " + + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, request.getOwnership()); @@ -301,13 +299,13 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { List devices = new ArrayList<>(); try { conn = this.getConnection(); - String sql = "SELECT * FROM ( SELECT ROWNUM offset, rs.* FROM ( SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, " + - "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, " + - "e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, " + - "e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, " + - "e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e WHERE TENANT_ID = ? AND STATUS = ?) e, " + - "DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = " + - "t.ID AND d.TENANT_ID = ? ) rs ) WHERE offset >= ? AND ROWNUM <= ?"; + String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " + + "d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " + + "e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e " + + "WHERE TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " + + "WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? ORDER BY ENROLMENT_ID " + + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, request.getStatus()); @@ -356,13 +354,12 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { ResultSet rs = null; try { conn = this.getConnection(); - String sql = "SELECT * FROM (SELECT ROWNUM offset, rs.* FROM (SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, " + - "d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, " + - "e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " + - "(SELECT d.ID, d.NAME, d.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM " + - "DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" + filteringString + - ") d1 WHERE d1.ID = e.DEVICE_ID) rs) WHERE offset >= ? AND ROWNUM <= ?"; - + String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, " + + "d.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" + filteringString + + ") d1 WHERE d1.ID = e.DEVICE_ID ORDER BY ENROLMENT_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java index 1dc62b8046..df70f43502 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java @@ -187,7 +187,7 @@ public class GroupDAOImpl implements GroupDAO { try { Connection conn = GroupManagementDAOFactory.getConnection(); String sql = "SELECT G.ID, G.GROUP_NAME, G.DESCRIPTION, G.DATE_OF_CREATE, G.DATE_OF_LAST_UPDATE, \n" + - "G.OWNER FROM DM_GROUP AS G INNER JOIN DM_DEVICE_GROUP_MAP AS GM ON G.ID = GM.GROUP_ID " + + "G.OWNER FROM DM_GROUP G INNER JOIN DM_DEVICE_GROUP_MAP GM ON G.ID = GM.GROUP_ID " + "WHERE GM.DEVICE_ID = ? AND GM.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, deviceId); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/OracleNotificationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/OracleNotificationDAOImpl.java index f5ca55ebbd..1ced4b898f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/OracleNotificationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/OracleNotificationDAOImpl.java @@ -51,7 +51,7 @@ public class OracleNotificationDAOImpl extends AbstractNotificationDAOImpl { "NOTIFICATION_ID, DEVICE_ID, OPERATION_ID, STATUS, DESCRIPTION FROM DM_NOTIFICATION WHERE " + "TENANT_ID = ?) n1 WHERE n1.DEVICE_ID = d.ID AND d.DEVICE_TYPE_ID=t.ID AND TENANT_ID = ?"; - sql = sql + " WHERE OFFSET >= ? AND ROWNUM <= ?"; + sql = sql + " ORDER BY n1.NOTIFICATION_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); 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 760a2f6e75..089ae9f276 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 @@ -125,9 +125,9 @@ public class GenericOperationDAOImpl implements OperationDAO { ResultSet rs = null; try { Connection connection = OperationManagementDAOFactory.getConnection(); - String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING AS EOM INNER JOIN DM_OPERATION DM " + + String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING EOM INNER JOIN DM_OPERATION DM " + "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? " + - "AND EOM.STATUS = ?;"; + "AND EOM.STATUS = ?"; stmt = connection.prepareStatement(query); stmt.setInt(1, enrolmentId); stmt.setString(2, operationCode); @@ -163,9 +163,9 @@ public class GenericOperationDAOImpl implements OperationDAO { boolean result = false; try { Connection connection = OperationManagementDAOFactory.getConnection(); - String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING AS EOM INNER JOIN DM_OPERATION DM " + + String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING EOM INNER JOIN DM_OPERATION DM " + "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? AND " + - "EOM.STATUS = ?;"; + "EOM.STATUS = ?"; stmt = connection.prepareStatement(query); stmt.setInt(1, enrolmentId); stmt.setString(2, operationCode); @@ -423,7 +423,7 @@ public class GenericOperationDAOImpl implements OperationDAO { "AND de.TENANT_ID = ? \n"; if(timestamp == 0){ - sql += "ORDER BY opm.OPERATION_ID LIMIT ? OFFSET ?;"; + sql += "ORDER BY opm.OPERATION_ID LIMIT ? OFFSET ?"; }else{ sql += "ORDER BY opm.UPDATED_TIMESTAMP asc LIMIT ? OFFSET ?"; } @@ -541,7 +541,7 @@ public class GenericOperationDAOImpl implements OperationDAO { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT COUNT(*) AS COUNT FROM DM_ENROLMENT_OP_MAPPING AS m \n" + "INNER JOIN DM_ENROLMENT AS d ON m.ENROLMENT_ID = d.ID \n" + - "WHERE m.UPDATED_TIMESTAMP > ? AND d.TENANT_ID = ?;"; + "WHERE m.UPDATED_TIMESTAMP > ? AND d.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setLong(1, timestamp); stmt.setInt(2, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); 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/OracleOperationDAOImpl.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/OracleOperationDAOImpl.java index 0a3bfadad2..9bbf3371fb 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/OracleOperationDAOImpl.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/OracleOperationDAOImpl.java @@ -46,10 +46,11 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { List operations = new ArrayList(); try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT * FROM ( SELECT ROWNUM offset, rs.* FROM ( SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + - "OPERATION_CODE, om.STATUS FROM DM_OPERATION o INNER JOIN (SELECT * " + - "FROM DM_ENROLMENT_OP_MAPPING dm WHERE dm.ENROLMENT_ID = ?) om ON o.ID = " + - "om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC ) rs ) WHERE offset >= ? AND ROWNUM <= ?"; + String sql = "SELECT o.ID, TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, " + + "o.OPERATION_CODE, om.STATUS, om.ID AS OM_MAPPING_ID, om.UPDATED_TIMESTAMP FROM DM_OPERATION o " + + "INNER JOIN (SELECT dm.OPERATION_ID, dm.ID, dm.STATUS, dm.UPDATED_TIMESTAMP FROM DM_ENROLMENT_OP_MAPPING dm " + + "WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC " + + "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); stmt.setInt(2, request.getStartIndex()); @@ -89,10 +90,11 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { List operations = new ArrayList(); try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT * FROM ( SELECT ROWNUM offset, rs.* FROM ( SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + - "OPERATION_CODE FROM DM_OPERATION o INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm WHERE " + - "dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY o." + - "CREATED_TIMESTAMP DESC ) rs ) WHERE offset >= ? AND ROWNUM <= ?"; + String sql = "SELECT o.ID, TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.OPERATION_CODE, " + + "om.ID AS OM_MAPPING_ID, om.UPDATED_TIMESTAMP FROM DM_OPERATION o " + + "INNER JOIN (SELECT dm.OPERATION_ID, dm.ID, dm.STATUS, dm.UPDATED_TIMESTAMP FROM DM_ENROLMENT_OP_MAPPING dm " + + "WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " + + "o.CREATED_TIMESTAMP DESC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); stmt.setString(2, status.toString()); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql index f3883d8c3c..9f5013c47c 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -25,7 +25,7 @@ CREATE TABLE DM_GROUP ( ID NUMBER(10) NOT NULL, DESCRIPTION CLOB DEFAULT NULL, GROUP_NAME VARCHAR2(100) DEFAULT NULL, - DATE_OF_ENROLLMENT TIMESTAMP(0) DEFAULT NULL, + DATE_OF_CREATE TIMESTAMP(0) DEFAULT NULL, DATE_OF_LAST_UPDATE TIMESTAMP(0) DEFAULT NULL, OWNER VARCHAR2(45) DEFAULT NULL, TENANT_ID NUMBER(10) DEFAULT 0, From da0977bd852936e6bbef9d1eb2ef901e7f08526d Mon Sep 17 00:00:00 2001 From: charitha Date: Thu, 29 Sep 2016 11:12:28 +0530 Subject: [PATCH 07/13] Fix SQL error when could happen due to large response payloads --- .../src/test/resources/sql/h2.sql | 2 +- .../src/main/resources/dbscripts/cdm/h2.sql | 2 +- .../src/main/resources/dbscripts/cdm/mysql.sql | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index 9b734652da..a93bc172f4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -123,7 +123,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_RESPONSE ( ID INTEGER AUTO_INCREMENT NOT NULL, ENROLMENT_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL, - OPERATION_RESPONSE BLOB DEFAULT NULL, + OPERATION_RESPONSE LONGBLOB DEFAULT NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_device_operation_response_enrollment FOREIGN KEY (ENROLMENT_ID) REFERENCES DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index d561d89e88..848ab0d37c 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -119,7 +119,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_RESPONSE ( ID INTEGER AUTO_INCREMENT NOT NULL, ENROLMENT_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL, - OPERATION_RESPONSE BLOB DEFAULT NULL, + OPERATION_RESPONSE LONGBLOB DEFAULT NULL, RECEIVED_TIMESTAMP TIMESTAMP NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_device_operation_response_enrollment FOREIGN KEY (ENROLMENT_ID) REFERENCES diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql index 0bb2700f76..75a416018c 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -108,7 +108,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_RESPONSE ( ID INTEGER AUTO_INCREMENT NOT NULL, ENROLMENT_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL, - OPERATION_RESPONSE BLOB DEFAULT NULL, + OPERATION_RESPONSE LONGBLOB DEFAULT NULL, RECEIVED_TIMESTAMP TIMESTAMP NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_device_operation_response_enrollment FOREIGN KEY (ENROLMENT_ID) REFERENCES From ecc87f370ccf0344929b56660b5363ca048c04d9 Mon Sep 17 00:00:00 2001 From: Megala Date: Thu, 29 Sep 2016 13:19:33 +0530 Subject: [PATCH 08/13] Fixing oracle error --- .../mgt/dao/impl/GenericOperationDAOImpl.java | 43 ++++++++----------- 1 file changed, 18 insertions(+), 25 deletions(-) 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 089ae9f276..a7e2d06728 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 @@ -407,36 +407,29 @@ public class GenericOperationDAOImpl implements OperationDAO { // sql = sql + " OFFSET ?"; // } - - String sql = "SELECT opm.ENROLMENT_ID, opm.CREATED_TIMESTAMP, opm.UPDATED_TIMESTAMP, opm.OPERATION_ID,\n" + - "op.OPERATION_CODE, op.TYPE as OPERATION_TYPE, opm.STATUS, en.DEVICE_ID,\n" + - "ops.RECEIVED_TIMESTAMP, ops.ID as OP_RES_ID, ops.OPERATION_RESPONSE,\n" + - "de.DEVICE_IDENTIFICATION, dt.NAME as DEVICE_TYPE\n" + - "FROM DM_ENROLMENT_OP_MAPPING AS opm\n" + - "LEFT JOIN DM_OPERATION AS op ON opm.OPERATION_ID = op.ID \n" + - "LEFT JOIN DM_ENROLMENT as en ON opm.ENROLMENT_ID = en.ID \n" + - "LEFT JOIN DM_DEVICE as de ON en.DEVICE_ID = de.ID \n" + - "LEFT JOIN DM_DEVICE_TYPE as dt ON dt.ID = de.DEVICE_TYPE_ID \n" + - "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE as ops ON \n" + - "opm.ENROLMENT_ID = ops.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \n" + - "WHERE opm.UPDATED_TIMESTAMP > ? \n" + - "AND de.TENANT_ID = ? \n"; - - if(timestamp == 0){ - sql += "ORDER BY opm.OPERATION_ID LIMIT ? OFFSET ?"; - }else{ - sql += "ORDER BY opm.UPDATED_TIMESTAMP asc LIMIT ? OFFSET ?"; + String sql = "SELECT opm.ENROLMENT_ID, opm.CREATED_TIMESTAMP, opm.UPDATED_TIMESTAMP, opm.OPERATION_ID,\n" + + "op.OPERATION_CODE, op.TYPE OPERATION_TYPE, opm.STATUS, en.DEVICE_ID,\n" + + "ops.RECEIVED_TIMESTAMP, ops.ID OP_RES_ID, ops.OPERATION_RESPONSE,\n" + + "de.DEVICE_IDENTIFICATION, dt.NAME DEVICE_TYPE\n" + "FROM DM_ENROLMENT_OP_MAPPING opm\n" + + "LEFT JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID \n" + + "LEFT JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID \n" + + "LEFT JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID \n" + + "LEFT JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID \n" + + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON \n" + + "opm.ENROLMENT_ID = ops.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \n" + + "WHERE opm.UPDATED_TIMESTAMP > ? \n" + "AND de.TENANT_ID = ? \n"; + + if (timestamp == 0) { + sql += "ORDER BY opm.OPERATION_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + } else { + sql += "ORDER BY opm.UPDATED_TIMESTAMP asc OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; } - - - stmt = conn.prepareStatement(sql); - stmt.setLong(1, timestamp); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); stmt.setInt(2, tenantId); - stmt.setInt(3, limit); - stmt.setInt(4, offset); + stmt.setInt(3, offset); + stmt.setInt(4, limit); rs = stmt.executeQuery(); From 2cdaf5aa212e3f224e2f29a3548c32acceaa14cc Mon Sep 17 00:00:00 2001 From: Megala Date: Thu, 29 Sep 2016 13:44:33 +0530 Subject: [PATCH 09/13] Reverting changes made to generic implementations and moving them to oracle specific ones --- .../core/dao/impl/AbstractDeviceDAOImpl.java | 4 +- .../dao/impl/device/OracleDeviceDAOImpl.java | 32 +++ .../mgt/dao/impl/GenericOperationDAOImpl.java | 53 ++-- .../operation/OracleOperationDAOImpl.java | 254 +++++++++++++++++- 4 files changed, 307 insertions(+), 36 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 90c7aa2855..a7af240b3a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -798,9 +798,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { ResultSet rs = null; try { conn = this.getConnection(); - String sql = "SELECT e.ID ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " + + String sql = "SELECT e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) dtm " + - "WHERE e.DEVICE_ID = dtm.ID AND e.STATUS = ? AND e.TENANT_ID = ?"; + "WHERE e.DEVICE_ID = dtm.ID AND e.STATUS = ? AND e.TENANT_ID = ?;"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getId()); stmt.setString(2, deviceId.getType()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java index bad8e5a4d6..a1773f505f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java @@ -19,6 +19,8 @@ package org.wso2.carbon.device.mgt.core.dao.impl.device; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; @@ -391,6 +393,36 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { return devices; } + public int getEnrolmentByStatus(DeviceIdentifier deviceId, EnrolmentInfo.Status status, + int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getConnection(); + String sql = "SELECT e.ID ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " + + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) dtm " + + "WHERE e.DEVICE_ID = dtm.ID AND e.STATUS = ? AND e.TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, deviceId.getId()); + stmt.setString(2, deviceId.getType()); + stmt.setInt(3, tenantId); + stmt.setString(4, status.toString()); + stmt.setInt(5, tenantId); + rs = stmt.executeQuery(); + if (rs.next()) { + return rs.getInt("ENROLMENT_ID"); + } else { + return -1; // if no results found + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + + "id of device '" + deviceId + "'", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + private Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } 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 a7e2d06728..760a2f6e75 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 @@ -125,9 +125,9 @@ public class GenericOperationDAOImpl implements OperationDAO { ResultSet rs = null; try { Connection connection = OperationManagementDAOFactory.getConnection(); - String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING EOM INNER JOIN DM_OPERATION DM " + + String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING AS EOM INNER JOIN DM_OPERATION DM " + "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? " + - "AND EOM.STATUS = ?"; + "AND EOM.STATUS = ?;"; stmt = connection.prepareStatement(query); stmt.setInt(1, enrolmentId); stmt.setString(2, operationCode); @@ -163,9 +163,9 @@ public class GenericOperationDAOImpl implements OperationDAO { boolean result = false; try { Connection connection = OperationManagementDAOFactory.getConnection(); - String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING EOM INNER JOIN DM_OPERATION DM " + + String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING AS EOM INNER JOIN DM_OPERATION DM " + "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? AND " + - "EOM.STATUS = ?"; + "EOM.STATUS = ?;"; stmt = connection.prepareStatement(query); stmt.setInt(1, enrolmentId); stmt.setString(2, operationCode); @@ -407,29 +407,36 @@ public class GenericOperationDAOImpl implements OperationDAO { // sql = sql + " OFFSET ?"; // } - String sql = "SELECT opm.ENROLMENT_ID, opm.CREATED_TIMESTAMP, opm.UPDATED_TIMESTAMP, opm.OPERATION_ID,\n" - + "op.OPERATION_CODE, op.TYPE OPERATION_TYPE, opm.STATUS, en.DEVICE_ID,\n" - + "ops.RECEIVED_TIMESTAMP, ops.ID OP_RES_ID, ops.OPERATION_RESPONSE,\n" - + "de.DEVICE_IDENTIFICATION, dt.NAME DEVICE_TYPE\n" + "FROM DM_ENROLMENT_OP_MAPPING opm\n" - + "LEFT JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID \n" - + "LEFT JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID \n" - + "LEFT JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID \n" - + "LEFT JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID \n" - + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON \n" - + "opm.ENROLMENT_ID = ops.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \n" - + "WHERE opm.UPDATED_TIMESTAMP > ? \n" + "AND de.TENANT_ID = ? \n"; - - if (timestamp == 0) { - sql += "ORDER BY opm.OPERATION_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; - } else { - sql += "ORDER BY opm.UPDATED_TIMESTAMP asc OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + String sql = "SELECT opm.ENROLMENT_ID, opm.CREATED_TIMESTAMP, opm.UPDATED_TIMESTAMP, opm.OPERATION_ID,\n" + + "op.OPERATION_CODE, op.TYPE as OPERATION_TYPE, opm.STATUS, en.DEVICE_ID,\n" + + "ops.RECEIVED_TIMESTAMP, ops.ID as OP_RES_ID, ops.OPERATION_RESPONSE,\n" + + "de.DEVICE_IDENTIFICATION, dt.NAME as DEVICE_TYPE\n" + + "FROM DM_ENROLMENT_OP_MAPPING AS opm\n" + + "LEFT JOIN DM_OPERATION AS op ON opm.OPERATION_ID = op.ID \n" + + "LEFT JOIN DM_ENROLMENT as en ON opm.ENROLMENT_ID = en.ID \n" + + "LEFT JOIN DM_DEVICE as de ON en.DEVICE_ID = de.ID \n" + + "LEFT JOIN DM_DEVICE_TYPE as dt ON dt.ID = de.DEVICE_TYPE_ID \n" + + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE as ops ON \n" + + "opm.ENROLMENT_ID = ops.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \n" + + "WHERE opm.UPDATED_TIMESTAMP > ? \n" + + "AND de.TENANT_ID = ? \n"; + + if(timestamp == 0){ + sql += "ORDER BY opm.OPERATION_ID LIMIT ? OFFSET ?;"; + }else{ + sql += "ORDER BY opm.UPDATED_TIMESTAMP asc LIMIT ? OFFSET ?"; } + + + stmt = conn.prepareStatement(sql); + stmt.setLong(1, timestamp); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); stmt.setInt(2, tenantId); - stmt.setInt(3, offset); - stmt.setInt(4, limit); + stmt.setInt(3, limit); + stmt.setInt(4, offset); rs = stmt.executeQuery(); @@ -534,7 +541,7 @@ public class GenericOperationDAOImpl implements OperationDAO { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT COUNT(*) AS COUNT FROM DM_ENROLMENT_OP_MAPPING AS m \n" + "INNER JOIN DM_ENROLMENT AS d ON m.ENROLMENT_ID = d.ID \n" + - "WHERE m.UPDATED_TIMESTAMP > ? AND d.TENANT_ID = ?"; + "WHERE m.UPDATED_TIMESTAMP > ? AND d.TENANT_ID = ?;"; stmt = conn.prepareStatement(sql); stmt.setLong(1, timestamp); stmt.setInt(2, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); 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/OracleOperationDAOImpl.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/OracleOperationDAOImpl.java index 9bbf3371fb..07d2f31b7e 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/OracleOperationDAOImpl.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/OracleOperationDAOImpl.java @@ -18,13 +18,20 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.PaginationRequest; +import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.GenericOperationDAOImpl; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; +import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -37,8 +44,7 @@ import java.util.List; */ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { - @Override - public List getOperationsForDevice(int enrolmentId, PaginationRequest request) + @Override public List getOperationsForDevice(int enrolmentId, PaginationRequest request) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; @@ -72,18 +78,17 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { operations.add(operation); } } catch (SQLException e) { - throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " + - "available for the device'" + enrolmentId + "' with status '", e); + throw new OperationManagementDAOException( + "SQL error occurred while retrieving the operation " + "available for the device'" + enrolmentId + + "' with status '", e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); } return operations; } - @Override - public List getOperationsByDeviceAndStatus(int enrolmentId, PaginationRequest request, - Operation.Status status) - throws OperationManagementDAOException { + @Override public List getOperationsByDeviceAndStatus(int enrolmentId, + PaginationRequest request, Operation.Status status) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; Operation operation; @@ -117,12 +122,239 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { operations.add(operation); } } catch (SQLException e) { - throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " + - "available for the device'" + enrolmentId + "' with status '" - + status.toString(), e); + throw new OperationManagementDAOException( + "SQL error occurred while retrieving the operation " + "available for the device'" + enrolmentId + + "' with status '" + status.toString(), e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); } return operations; } + + @Override public void updateEnrollmentOperationsStatus(int enrolmentId, String operationCode, + Operation.Status existingStatus, Operation.Status newStatus) throws OperationManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + Connection connection = OperationManagementDAOFactory.getConnection(); + String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING EOM INNER JOIN DM_OPERATION DM " + + "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? " + + "AND EOM.STATUS = ?"; + stmt = connection.prepareStatement(query); + stmt.setInt(1, enrolmentId); + stmt.setString(2, operationCode); + stmt.setString(3, existingStatus.toString()); + // This will return only one result always. + rs = stmt.executeQuery(); + int id = 0; + while (rs.next()) { + id = rs.getInt("ID"); + } + if (id != 0) { + stmt = connection.prepareStatement( + "UPDATE DM_ENROLMENT_OP_MAPPING SET STATUS = ?, " + "UPDATED_TIMESTAMP = ? WHERE ID = ?"); + stmt.setString(1, newStatus.toString()); + stmt.setLong(2, System.currentTimeMillis() / 1000); + stmt.setInt(3, id); + stmt.executeUpdate(); + } + + } catch (SQLException e) { + throw new OperationManagementDAOException( + "Error occurred while update device mapping operation status " + "metadata", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt); + } + } + + @Override public boolean updateTaskOperation(int enrolmentId, String operationCode) + throws OperationManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + boolean result = false; + try { + Connection connection = OperationManagementDAOFactory.getConnection(); + String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING EOM INNER JOIN DM_OPERATION DM " + + "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? AND " + + "EOM.STATUS = ?"; + stmt = connection.prepareStatement(query); + stmt.setInt(1, enrolmentId); + stmt.setString(2, operationCode); + stmt.setString(3, Operation.Status.PENDING.toString()); + // This will return only one result always. + rs = stmt.executeQuery(); + int id = 0; + if (rs.next()) { + id = rs.getInt("ID"); + } + if (id != 0) { + stmt = connection.prepareStatement( + "UPDATE DM_ENROLMENT_OP_MAPPING SET UPDATED_TIMESTAMP = ? " + "WHERE ID = ?"); + stmt.setLong(1, System.currentTimeMillis() / 1000); + stmt.setInt(2, id); + stmt.executeUpdate(); + result = true; + } + } catch (SQLException e) { + throw new OperationManagementDAOException( + "Error occurred while update device mapping operation status " + "metadata", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt); + } + return result; + } + + @Override public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) + throws OperationManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + List activities = new ArrayList<>(); + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT opm.ENROLMENT_ID, opm.CREATED_TIMESTAMP, opm.UPDATED_TIMESTAMP, opm.OPERATION_ID,\n" + + "op.OPERATION_CODE, op.TYPE OPERATION_TYPE, opm.STATUS, en.DEVICE_ID,\n" + + "ops.RECEIVED_TIMESTAMP, ops.ID OP_RES_ID, ops.OPERATION_RESPONSE,\n" + + "de.DEVICE_IDENTIFICATION, dt.NAME DEVICE_TYPE\n" + "FROM DM_ENROLMENT_OP_MAPPING opm\n" + + "LEFT JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID \n" + + "LEFT JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID \n" + + "LEFT JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID \n" + + "LEFT JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID \n" + + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON \n" + + "opm.ENROLMENT_ID = ops.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \n" + + "WHERE opm.UPDATED_TIMESTAMP > ? \n" + "AND de.TENANT_ID = ? \n"; + + if (timestamp == 0) { + sql += "ORDER BY opm.OPERATION_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + } else { + sql += "ORDER BY opm.UPDATED_TIMESTAMP asc OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + } + stmt = conn.prepareStatement(sql); + stmt.setLong(1, timestamp); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + stmt.setInt(2, tenantId); + stmt.setInt(3, offset); + stmt.setInt(4, limit); + + rs = stmt.executeQuery(); + + int operationId = 0; + int enrolmentId = 0; + int responseId = 0; + Activity activity = null; + ActivityStatus activityStatus = null; + while (rs.next()) { + + if (operationId != rs.getInt("OPERATION_ID")) { + activity = new Activity(); + 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")); + 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")); + 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")) { + if (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 int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT COUNT(*) COUNT FROM DM_ENROLMENT_OP_MAPPING m \n" + + "INNER JOIN DM_ENROLMENT d ON m.ENROLMENT_ID = d.ID \n" + + "WHERE m.UPDATED_TIMESTAMP > ? AND d.TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setLong(1, timestamp); + stmt.setInt(2, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + rs = stmt.executeQuery(); + if (rs.next()) { + return rs.getInt("COUNT"); + } + } catch (SQLException e) { + throw new OperationManagementDAOException( + "Error occurred while getting the activity count from " + "the database.", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, rs); + } + return 0; + } } \ No newline at end of file From 12aed521e5dd8487161bc93de199db624e1258c5 Mon Sep 17 00:00:00 2001 From: Megala Date: Thu, 29 Sep 2016 13:57:06 +0530 Subject: [PATCH 10/13] Refactoring code --- .../operation/OracleOperationDAOImpl.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) 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/OracleOperationDAOImpl.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/OracleOperationDAOImpl.java index 07d2f31b7e..f37419422d 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/OracleOperationDAOImpl.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/OracleOperationDAOImpl.java @@ -44,7 +44,8 @@ import java.util.List; */ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { - @Override public List getOperationsForDevice(int enrolmentId, PaginationRequest request) + @Override + public List getOperationsForDevice(int enrolmentId, PaginationRequest request) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; @@ -87,7 +88,8 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { return operations; } - @Override public List getOperationsByDeviceAndStatus(int enrolmentId, + @Override + public List getOperationsByDeviceAndStatus(int enrolmentId, PaginationRequest request, Operation.Status status) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; @@ -131,7 +133,8 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { return operations; } - @Override public void updateEnrollmentOperationsStatus(int enrolmentId, String operationCode, + @Override + public void updateEnrollmentOperationsStatus(int enrolmentId, String operationCode, Operation.Status existingStatus, Operation.Status newStatus) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; @@ -167,8 +170,8 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { } } - @Override public boolean updateTaskOperation(int enrolmentId, String operationCode) - throws OperationManagementDAOException { + @Override + public boolean updateTaskOperation(int enrolmentId, String operationCode) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; boolean result = false; @@ -204,8 +207,8 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { return result; } - @Override public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) - throws OperationManagementDAOException { + @Override + public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; List activities = new ArrayList<>(); @@ -334,7 +337,8 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl { return activities; } - @Override public int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementDAOException { + @Override + public int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; try { From acae51d756acc1e4751adf3084df78e4821d2adc Mon Sep 17 00:00:00 2001 From: dilanua Date: Thu, 29 Sep 2016 14:06:44 +0530 Subject: [PATCH 11/13] Fixing MSSQL dashboard create view script issues --- .../src/main/resources/dbscripts/cdm/mssql.sql | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql index e54e5705ca..0495a390d9 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -435,11 +435,10 @@ CREATE TABLE DM_DEVICE_DETAIL ( ON DELETE NO ACTION ON UPDATE NO ACTION ); -GO -- DASHBOARD RELATED VIEWS -- -CREATE VIEW POLICY_COMPLIANCE_INFO AS +exec('CREATE VIEW POLICY_COMPLIANCE_INFO AS SELECT TOP 100 PERCENT DEVICE_INFO.DEVICE_ID, DEVICE_INFO.DEVICE_IDENTIFICATION, @@ -468,10 +467,9 @@ STATUS AS IS_COMPLIANT FROM DM_POLICY_COMPLIANCE_STATUS) DEVICE_WITH_POLICY_INFO ON DEVICE_INFO.DEVICE_ID = DEVICE_WITH_POLICY_INFO.DEVICE_ID -ORDER BY DEVICE_INFO.DEVICE_ID; -GO +ORDER BY DEVICE_INFO.DEVICE_ID'); -CREATE VIEW FEATURE_NON_COMPLIANCE_INFO AS +exec('CREATE VIEW FEATURE_NON_COMPLIANCE_INFO AS SELECT TOP 100 PERCENT DM_DEVICE.ID AS DEVICE_ID, DM_DEVICE.DEVICE_IDENTIFICATION, @@ -496,7 +494,6 @@ DM_ENROLMENT.ID AND DM_POLICY_COMPLIANCE_STATUS.DEVICE_ID = DM_DEVICE.ID AND DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID AND DM_DEVICE.ID = DM_DEVICE_DETAIL.DEVICE_ID -ORDER BY TENANT_ID, DEVICE_ID; -GO +ORDER BY TENANT_ID, DEVICE_ID'); -- END OF DASHBOARD RELATED VIEWS -- From 38677f84e80eb0c1201a276fcf9c2dcb6782b340 Mon Sep 17 00:00:00 2001 From: Megala Date: Thu, 29 Sep 2016 14:09:44 +0530 Subject: [PATCH 12/13] Fixing SQL syntax problem --- .../operation/SQLServerOperationDAOImpl.java | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) 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/SQLServerOperationDAOImpl.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/SQLServerOperationDAOImpl.java index e1189da1a3..b98ca93f7b 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/SQLServerOperationDAOImpl.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/SQLServerOperationDAOImpl.java @@ -18,13 +18,20 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.PaginationRequest; +import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.GenericOperationDAOImpl; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; +import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -126,4 +133,135 @@ public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl { } return operations; } + + @Override + public List getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + List activities = new ArrayList<>(); + try { + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "SELECT opm.ENROLMENT_ID, opm.CREATED_TIMESTAMP, opm.UPDATED_TIMESTAMP, opm.OPERATION_ID,\n" + + "op.OPERATION_CODE, op.TYPE OPERATION_TYPE, opm.STATUS, en.DEVICE_ID,\n" + + "ops.RECEIVED_TIMESTAMP, ops.ID OP_RES_ID, ops.OPERATION_RESPONSE,\n" + + "de.DEVICE_IDENTIFICATION, dt.NAME DEVICE_TYPE\n" + "FROM DM_ENROLMENT_OP_MAPPING opm\n" + + "LEFT JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID \n" + + "LEFT JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID \n" + + "LEFT JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID \n" + + "LEFT JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID \n" + + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON \n" + + "opm.ENROLMENT_ID = ops.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \n" + + "WHERE opm.UPDATED_TIMESTAMP > ? \n" + "AND de.TENANT_ID = ? \n"; + + if (timestamp == 0) { + sql += "ORDER BY opm.OPERATION_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + } else { + sql += "ORDER BY opm.UPDATED_TIMESTAMP asc OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + } + stmt = conn.prepareStatement(sql); + stmt.setLong(1, timestamp); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + stmt.setInt(2, tenantId); + stmt.setInt(3, offset); + stmt.setInt(4, limit); + + rs = stmt.executeQuery(); + + int operationId = 0; + int enrolmentId = 0; + int responseId = 0; + Activity activity = null; + ActivityStatus activityStatus = null; + while (rs.next()) { + + if (operationId != rs.getInt("OPERATION_ID")) { + activity = new Activity(); + 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")); + 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")); + 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")) { + if (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; + } + } From 25ed0cf370a1467134e775042db9e47adafaa389 Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Thu, 29 Sep 2016 18:28:09 +0530 Subject: [PATCH 13/13] Fix build issue with swagger version --- components/device-mgt/org.wso2.carbon.device.mgt.common/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.common/pom.xml index 87ca742be5..b6e11b75fe 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/pom.xml @@ -51,7 +51,7 @@ javax.xml.bind.annotation; version="${javax.xml.bind.imp.pkg.version}", com.fasterxml.jackson.annotation;version="${jackson-annotations.version}", org.wso2.carbon.apimgt.api.model.*;version="${carbon.api.mgt.version.range}", - io.swagger.annotations; version="${swagger.annotations.version}" + io.swagger.annotations; version="${swagger.annotations.version}"; resolution:=optional