From 966a878e8e95ebb962a8122974c1442fdb003e42 Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Mon, 16 Dec 2019 16:32:48 +0530 Subject: [PATCH] Create permanant device type deletion --- .../DeviceTypeManagementAdminService.java | 63 ++++++++++-- .../DeviceManagementAdminServiceImpl.java | 2 +- .../DeviceTypeManagementAdminServiceImpl.java | 34 +++++-- .../DeviceTypeManagementAdminServiceTest.java | 29 ++++++ .../src/test/resources/testng.xml | 1 + .../device/mgt/core/dao/DeviceTypeDAO.java | 9 ++ .../mgt/core/dao/impl/DeviceTypeDAOImpl.java | 23 +++++ .../DeviceManagementProviderService.java | 14 ++- .../DeviceManagementProviderServiceImpl.java | 98 ++++++++++++++++++- 9 files changed, 255 insertions(+), 18 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementAdminService.java index b472b5832d..efeaa39363 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementAdminService.java @@ -86,8 +86,6 @@ import javax.ws.rs.core.Response; @Api(value = "Device Type Management Administrative Service", description = "This an API intended to be used by " + "'internal' components to log in as an admin user and do a selected number of operations. " + "Further, this is strictly restricted to admin users only ") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) @Scopes( scopes = { @Scope( @@ -113,6 +111,7 @@ import javax.ws.rs.core.Response; public interface DeviceTypeManagementAdminService { @GET + @Produces(MediaType.APPLICATION_JSON) @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", @@ -164,6 +163,7 @@ public interface DeviceTypeManagementAdminService { Response getDeviceTypes(); @GET + @Produces(MediaType.APPLICATION_JSON) @Path("/{type}") @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -217,6 +217,8 @@ public interface DeviceTypeManagementAdminService { String type); @POST + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "POST", @@ -262,6 +264,8 @@ public interface DeviceTypeManagementAdminService { required = true)DeviceType deviceType); @PUT + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) @Path("/{type}") @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -317,6 +321,8 @@ public interface DeviceTypeManagementAdminService { required = true) DeviceType deviceType); @POST + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) @Path("/{type}/configs") @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -372,8 +378,10 @@ public interface DeviceTypeManagementAdminService { PlatformConfiguration config); - @Path("/versions") @POST + @Path("/versions") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "POST", @@ -425,8 +433,9 @@ public interface DeviceTypeManagementAdminService { value = "The device type version details.", required = true) DeviceTypeVersionWrapper deviceTypeVersion); - @Path("/{deviceTypeName}/versions") @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("/{deviceTypeName}/versions") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", @@ -475,8 +484,10 @@ public interface DeviceTypeManagementAdminService { String deviceTypeName); - @Path("{deviceTypeName}/versions") @PUT + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @Path("{deviceTypeName}/versions") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "PUT", @@ -529,8 +540,8 @@ public interface DeviceTypeManagementAdminService { required = true) DeviceTypeVersionWrapper deviceTypeVersion); - @Path("{deviceTypeName}/versions/{version}") @DELETE + @Path("{deviceTypeName}/versions/{version}") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "DELETE", @@ -581,4 +592,44 @@ public interface DeviceTypeManagementAdminService { required = true) @PathParam("version")String version); + + @DELETE + @Path("{deviceType}/delete") + @ApiOperation( + httpMethod = "DELETE", + value = "Delete device type.", + notes = "This api will permanently delete an existing device type with everything " + + "it's related with.", + tags = "Device Type Management Administrative Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:admin:device-type") + }) + } + ) + @ApiResponses(value = { + @ApiResponse(code = 202, message = "ACCEPTED. \n Successfully deleted the device type.", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body") + }), + @ApiResponse( + code = 401, + message = "Unauthorized.\n The unauthorized access to the requested resource.", + 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 Server error occurred while deleting device type.", + response = ErrorResponse.class) + }) + Response deleteDeviceType( + @ApiParam( + name = "deviceType", + value = "Device type name.", + required = true) + @PathParam("deviceType") String deviceType); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java index 9533c43a42..360a41679f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java @@ -169,7 +169,7 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService(); try { - if (!deviceManagementProviderService.deleteDevices(deviceIdentifiers)) { + if (!deviceManagementProviderService.deleteDevices(deviceIdentifiers, false)) { String msg = "Found un-deployed device type."; log.error(msg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementAdminServiceImpl.java index edae86a311..e3bedfd816 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementAdminServiceImpl.java @@ -43,6 +43,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceTypeVersionWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceTypeManagementAdminService; @@ -63,16 +64,14 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; @Path("/admin/device-types") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) public class DeviceTypeManagementAdminServiceImpl implements DeviceTypeManagementAdminService { private static final Log log = LogFactory.getLog(DeviceTypeManagementAdminServiceImpl.class); private static final String DEVICETYPE_REGEX_PATTERN = "^[^ /]+$"; private static final Pattern patternMatcher = Pattern.compile(DEVICETYPE_REGEX_PATTERN); - @GET @Override + @GET public Response getDeviceTypes() { try { List deviceTypes = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceTypes(); @@ -188,8 +187,8 @@ public class DeviceTypeManagementAdminServiceImpl implements DeviceTypeManagemen } @Override - @Path("{deviceTypeName}/versions") @POST + @Path("{deviceTypeName}/versions") public Response addDeviceTypeVersion(@PathParam("deviceTypeName") String deviceTypeName, DeviceTypeVersionWrapper versionWrapper) { if (versionWrapper != null && deviceTypeName != null && !deviceTypeName.isEmpty() @@ -229,9 +228,9 @@ public class DeviceTypeManagementAdminServiceImpl implements DeviceTypeManagemen } } + @Override @GET @Path("/{deviceTypeName}/versions") - @Override public Response getDeviceTypeVersion(@PathParam("deviceTypeName") String deviceTypeName) { try { List deviceTypes = DeviceMgtAPIUtils.getDeviceManagementService() @@ -244,8 +243,8 @@ public class DeviceTypeManagementAdminServiceImpl implements DeviceTypeManagemen } } - @PUT @Override + @PUT @Path("{deviceTypeName}/versions") public Response updateDeviceTypeVersion(@PathParam("deviceTypeName") String deviceTypeName, DeviceTypeVersionWrapper deviceTypeVersion) { @@ -284,8 +283,8 @@ public class DeviceTypeManagementAdminServiceImpl implements DeviceTypeManagemen } } - @DELETE @Override + @DELETE @Path("{deviceTypeName}/versions/{version}") public Response deleteDeviceTypeVersion(@PathParam("deviceTypeName") String deviceTypeName, @PathParam("version") String version) { @@ -319,4 +318,25 @@ public class DeviceTypeManagementAdminServiceImpl implements DeviceTypeManagemen } } + @Override + @DELETE + @Path("{deviceType}/delete") + public Response deleteDeviceType(@PathParam("deviceType") String deviceType) { + try { + DeviceManagementProviderService deviceManagementProviderService = + DeviceMgtAPIUtils.getDeviceManagementService(); + if (!deviceManagementProviderService.deleteDeviceType(deviceType)){ + String msg = "Error occurred while deleting device of type: " + deviceType; + log.error(msg); + return Response.serverError().entity(msg).build(); + } + return Response.status(Response.Status.ACCEPTED).entity( + "Device of type: " + deviceType + " permanently deleted.").build(); + } catch (DeviceManagementException e) { + String msg = "Error occurred while deleting device of type: " + deviceType; + log.error(msg, e); + return Response.serverError().entity(msg).build(); + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementAdminServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementAdminServiceTest.java index ca151c7bd2..c26c0463ee 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementAdminServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementAdminServiceTest.java @@ -216,4 +216,33 @@ public class DeviceTypeManagementAdminServiceTest { "The Response Status code should be 500."); Mockito.reset(deviceManagementProviderService); } + + @Test(description = "Test delete device type with correct request.") + public void testDeleteDeviceType() { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); + Response response = this.deviceTypeManagementAdminService.deleteDeviceType(TEST_DEVICE_TYPE); + Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); + } + + @Test(description = "Test delete device type when unavailable.") + public void testDeleteNonExistingDeviceType() throws DeviceManagementException { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); + Mockito.when(deviceManagementProviderService.getDeviceType(Mockito.anyString())).thenReturn(null); + Response response = this.deviceTypeManagementAdminService.deleteDeviceType(TEST_DEVICE_TYPE); + Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); + Mockito.reset(this.deviceManagementProviderService); + } + + @Test(description = "Test delete device type when DeviceManagementException is thrown.") + public void testDeleteDeviceTypeWithException() throws DeviceManagementException { + PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) + .toReturn(this.deviceManagementProviderService); + Mockito.when(this.deviceManagementProviderService.deleteDeviceType(TEST_DEVICE_TYPE)) + .thenThrow(new DeviceManagementException()); + Response response = this.deviceTypeManagementAdminService.deleteDeviceType(TEST_DEVICE_TYPE); + Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + Mockito.reset(this.deviceManagementProviderService); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/testng.xml index 8eef632459..b3b9a2da44 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/testng.xml @@ -32,6 +32,7 @@ + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java index 4ae4289993..37bf726c38 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java @@ -128,4 +128,13 @@ public interface DeviceTypeDAO { DeviceTypeVersion getDeviceTypeVersion(int deviceTypeId, String version) throws DeviceManagementDAOException; + + /** + * Permanently remove a device type + * + * @param tenantId current tenant's id + * @param deviceTypeId device type id + * @throws DeviceManagementDAOException Might occur while executing database queries + */ + void deleteDeviceType(int tenantId, int deviceTypeId) throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java index 35e9265190..66f9fced1a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java @@ -411,6 +411,29 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { } + @Override + public void deleteDeviceType(int tenantID, int deviceTypeId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + try { + conn = getConnection(); + String sql = "DELETE FROM DM_DEVICE_TYPE" + + " WHERE" + + " ID = ?" + + " AND PROVIDER_TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceTypeId); + stmt.setInt(2, tenantID); + stmt.execute(); + } catch (SQLException e) { + throw new DeviceManagementDAOException( + "Error occurred while deleting device type of id: " + deviceTypeId + + " for tenant: " + tenantID, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + 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/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 8765ed22e5..cdc5379ea7 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 @@ -611,7 +611,7 @@ public interface DeviceManagementProviderService { boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException; - boolean deleteDevices(List deviceIdentifiers) throws DeviceManagementException, InvalidDeviceException; + boolean deleteDevices(List deviceIdentifiers, boolean transactionAlreadyExists) throws DeviceManagementException, InvalidDeviceException; boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException; @@ -708,7 +708,7 @@ public interface DeviceManagementProviderService { */ boolean changeDeviceStatus(DeviceIdentifier deviceIdentifier, EnrolmentInfo.Status newStatus) throws DeviceManagementException; - + /** * This will handle add and update of device type services. * @param deviceManagementService @@ -771,6 +771,16 @@ public interface DeviceManagementProviderService { DeviceTypeVersion getDeviceTypeVersion(String deviceTypeName, String version) throws DeviceManagementException; + + /** + * Permanently delete a device type + * + * @param deviceTypeName device type name + * @return True if device type successfully removed + * @throws DeviceManagementException + */ + boolean deleteDeviceType(String deviceTypeName) throws DeviceManagementException; + /** * Retrieves a list of configurations of a specific device * using the device's properties 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 1efdb78a74..0c65a79fa8 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 @@ -153,7 +153,6 @@ import java.util.List; import java.util.Arrays; import java.util.Map; import java.util.Properties; -import java.util.Set; public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, PluginInitializationListener { @@ -528,7 +527,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public boolean deleteDevices(List deviceIdentifiers) throws DeviceManagementException, + public boolean deleteDevices(List deviceIdentifiers, boolean transactionAlreadyExists) throws DeviceManagementException, InvalidDeviceException { if (deviceIdentifiers == null || deviceIdentifiers.isEmpty()) { String msg = "Required values of device identifiers are not set to permanently delete device/s."; @@ -3584,6 +3583,101 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return versions; } + @Override + public boolean deleteDeviceType(String deviceTypeName) throws DeviceManagementException { + int tenantId = getTenantId(); + List deviceIdentifiers; + List deviceTypeVersions; + boolean result; + + try { + if (deviceTypeName.isEmpty()) { + String msg = "Error, device type cannot be null or empty"; + log.error(msg); + return false; + } + DeviceManagementDAOFactory.beginTransaction(); + DeviceType deviceTypeObj = deviceTypeDAO.getDeviceType(deviceTypeName, tenantId); + if (deviceTypeObj == null) { + String msg = "Error, device of type: " + deviceTypeName + " does not exist"; + log.error(msg); + return false; + } + List devices = deviceDAO.getDevices(deviceTypeName, this.getTenantId()); + if (devices.isEmpty()) { + if (log.isDebugEnabled()) { + log.debug("No devices found for the device type: " + deviceTypeName); + } + } else { + // dis-enroll devices + deviceIdentifiers = new ArrayList<>(); + for (Device device : devices) { + if (device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) { + if (log.isDebugEnabled()) { + log.debug("Device: " + device.getName() + " has already dis-enrolled"); + } + } else { + device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); + device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED); + enrollmentDAO.updateEnrollment(device.getId(), device.getEnrolmentInfo(), + tenantId); + deviceDAO.updateDevice(device, tenantId); + } + deviceIdentifiers.add(device.getDeviceIdentifier()); + } + // delete devices + deleteDevices(deviceIdentifiers, true); + } + // remove device type versions + deviceTypeVersions = deviceTypeDAO.getDeviceTypeVersions( + deviceTypeObj.getId(), deviceTypeName); + if (deviceTypeVersions.isEmpty()) { + if (log.isDebugEnabled()) { + log.debug("Device type: " + deviceTypeName + "doesn't have any type versions"); + } + } else { + for (DeviceTypeVersion deviceTypeVersion : deviceTypeVersions) { + result = deviceTypeDAO.isDeviceTypeVersionModifiable(deviceTypeObj.getId() + , deviceTypeVersion.getVersionName(), tenantId); + if (!result) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Device type of: " + deviceTypeName + "is unauthorized to modify " + + "version"; + log.error(msg); + return false; + } + result = deviceTypeDAO.updateDeviceTypeVersion(deviceTypeVersion); + if (!result) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Could not delete the version of device type: " + + deviceTypeName; + log.error(msg); + return false; + } + } + } + // delete device type + deviceTypeDAO.deleteDeviceType(tenantId, deviceTypeObj.getId()); + DeviceManagementDAOFactory.commitTransaction(); + } catch (InvalidDeviceException e) { + String msg = "Error occurred while deleting devices of type: " + deviceTypeName; + log.error(msg); + throw new DeviceManagementException(msg, e); + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while deleting device type of: " + deviceTypeName; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return true; + } + @Override public DeviceConfiguration getDeviceConfiguration(Map deviceProps) throws DeviceManagementException, DeviceNotFoundException, UnauthorizedDeviceAccessException,