From 994f49db16bc2e641b2e5fbdfdf4824037a85590 Mon Sep 17 00:00:00 2001 From: shamalka Date: Tue, 11 Apr 2023 11:03:36 +0530 Subject: [PATCH] Add missing db scripts --- .../service/api/DeviceManagementService.java | 53 ---------- .../impl/DeviceManagementServiceImpl.java | 97 ------------------- .../GroupManagementProviderServiceImpl.java | 43 +------- .../main/resources/dbscripts/cdm/mssql.sql | 15 ++- .../main/resources/dbscripts/cdm/mysql.sql | 13 +++ .../main/resources/dbscripts/cdm/oracle.sql | 13 +++ .../resources/dbscripts/cdm/postgresql.sql | 13 +++ 7 files changed, 54 insertions(+), 193 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index a080208de2..16a3b1ba6d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -425,59 +425,6 @@ public interface DeviceManagementService { @QueryParam("limit") int limit); - @GET - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation( - produces = MediaType.APPLICATION_JSON, - httpMethod = "GET", - value = "Getting Details of Registered Devices Owned by an Authenticated User to generate token for Traccar", - notes = "Provides details of devices enrolled by authenticated users to generate token for Traccar.", - tags = "Device Management", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:view") - }) - } - ) - @ApiResponses(value = { - @ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of devices.", - response = DeviceList.class, - responseHeaders = { - @ResponseHeader( - name = "Content-Type", - description = "The content type of the body"), - @ResponseHeader( - name = "ETag", - description = "Entity Tag of the response resource.\n" + - "Used by caches, or in conditional requests."), - @ResponseHeader( - name = "Last-Modified", - description = "Date and time the resource was last modified.\n" + - "Used by caches, or in conditional requests."), - }), - @ApiResponse( - code = 304, - message = "Not Modified. \n Empty body because the client already has the latest version of " + - "the requested resource.\n"), - @ApiResponse( - code = 400, - message = "The incoming request has more than one selection criteria defined via the query parameters.", - response = ErrorResponse.class), - @ApiResponse( - code = 404, - message = "The search criteria did not match any device registered with the server.", - 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 fetching the device list.", - response = ErrorResponse.class) - }) - @Path("/traccar-user-token") - Response getTraccarUserToken(); - @GET @Produces(MediaType.APPLICATION_JSON) @Path("/{groupId}/location-history") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 1e9d741aa9..46a37b9f59 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -353,103 +353,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } } - @GET - @Override - @Path("/traccar-user-token") - public Response getTraccarUserToken() { - - if (HttpReportingUtil.isTrackerEnabled()) { - String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); - DeviceAPIClientService deviceAPIClientService = DeviceMgtAPIUtils.getDeviceAPIClientService(); - JSONObject obj = new JSONObject(deviceAPIClientService.returnUser(currentUser)); - - if (obj.has("error")) { - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(obj.getString("error")).build(); - } else { - int userId = obj.getInt("id"); - List traccarValidIdList = new ArrayList<>(); - /*Get Device Id List*/ - try { - DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); - DeviceAccessAuthorizationService deviceAccessAuthorizationService = - DeviceMgtAPIUtils.getDeviceAccessAuthorizationService(); - PaginationRequest request = new PaginationRequest(0, 0); - PaginationResult result; - DeviceList devices = new DeviceList(); - List status = new ArrayList<>(); - status.add("ACTIVE"); - status.add("INACTIVE"); - status.add("CREATED"); - status.add("UNREACHABLE"); - request.setStatusList(status); - // this is the user who initiates the request - String authorizedUser = MultitenantUtils.getTenantAwareUsername(currentUser); - // check whether the user is device-mgt admin - if (!deviceAccessAuthorizationService.isDeviceAdminUser()) { - request.setOwner(authorizedUser); - } - - result = dms.getAllDevicesIds(request); - if (result == null || result.getData() == null || result.getData().isEmpty()) { - devices.setList(new ArrayList()); - devices.setCount(0); - } else { - devices.setList((List) result.getData()); - devices.setCount(result.getRecordsTotal()); - } - - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - TrackerDeviceInfo trackerDevice; - for (Device device : devices.getList()) { - trackerDevice = deviceAPIClientService.getTrackerDevice(device.getId(), tenantId); - if(trackerDevice != null) { - int traccarDeviceId = trackerDevice.getTraccarDeviceId(); - boolean getPermission = deviceAPIClientService.getUserIdofPermissionByDeviceIdNUserId(traccarDeviceId, userId); - traccarValidIdList.add(traccarDeviceId); - if (!getPermission) { - deviceAPIClientService.addTrackerUserDevicePermission(userId, traccarDeviceId); - } - } - } - //Remove necessary - List getAllUserDevices = - deviceAPIClientService.getUserIdofPermissionByUserIdNIdList(userId, traccarValidIdList); - for (TrackerPermissionInfo getAllUserDevice : getAllUserDevices) { - deviceAPIClientService.removeTrackerUserDevicePermission( - getAllUserDevice.getTraccarUserId(), - getAllUserDevice.getTraccarDeviceId(), - TraccarHandlerConstants.Types.REMOVE_TYPE_SINGLE); - } - } catch (DeviceManagementException e) { - String msg = "Error occurred while fetching all enrolled devices. "; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (DeviceAccessAuthorizationException e) { - String msg = "Error occurred while checking device access authorization. "; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (TrackerManagementDAOException e) { - String msg = "Error occurred while mapping with deviceId ."; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (ExecutionException e) { - String msg = "Execution error occurred handling traccar device permissions"; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (InterruptedException e) { - String msg = "Interruption error occurred handling traccar device permissions"; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - - /*Get Device Id List*/ - return Response.status(Response.Status.OK).entity(obj.getString("token")).build(); - } - } else { - return Response.status(Response.Status.BAD_REQUEST).entity("Traccar is not enabled").build(); - } - } - /** * Validate group Id and group Id greater than 0 and exist. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 9981333428..1881aeed55 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -132,27 +132,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid this.groupDAO.addGroupProperties(deviceGroup, updatedGroupID, tenantId); } GroupManagementDAOFactory.commitTransaction(); - - //add new group in traccar - if (HttpReportingUtil.isTrackerEnabled()) { - DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() - .addGroup(deviceGroup, updatedGroupID, tenantId); - } - //add new group in traccar } else { - // add a group if not exist in traccar starts - if (HttpReportingUtil.isTrackerEnabled()){ - existingGroup = this.groupDAO.getGroup(deviceGroup.getName(), tenantId); - int groupId = existingGroup.getGroupId(); - try { - DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() - .addGroup(deviceGroup, groupId, tenantId); - } catch (TrackerAlreadyExistException e) { - throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName()); - } - } else { - throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName()); - } + throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName()); } } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); @@ -234,13 +215,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid this.groupDAO.updateGroupProperties(deviceGroup, groupId, tenantId); } - //procees to update a group in traccar starts - if (HttpReportingUtil.isTrackerEnabled()) { - DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() - .updateGroup(deviceGroup, groupId, tenantId); - } - //procees to update a group in traccar starts - GroupManagementDAOFactory.commitTransaction(); } else { throw new GroupNotExistException("Group with ID - '" + groupId + "' doesn't exists!"); @@ -300,21 +274,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } - //procees to delete a group from traccar starts - if (HttpReportingUtil.isTrackerEnabled()) { - try { - DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() - .deleteGroup(groupId, tenantId); - } catch (TrackerManagementDAOException e) { - String msg = "Failed while deleting traccar group " + groupId; - log.error(msg, e); - } catch (ExecutionException | InterruptedException e) { - String msg = "Failed while deleting traccar group "+groupId+" due to concurrent execution failure"; - log.error(msg, e); - } - } - //procees to delete a group from traccar ends - if (isDeleteChildren) { groupIdsToDelete.add(groupId); groupDAO.deleteGroupsMapping(groupIdsToDelete, tenantId); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 4d05ebab17..d43294bd34 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -754,4 +754,17 @@ CREATE TABLE DYNAMIC_TASK_PROPERTIES ( CONSTRAINT FK_DYNAMIC_TASK_TASK_PROPERTIES FOREIGN KEY (DYNAMIC_TASK_ID) REFERENCES DYNAMIC_TASK (DYNAMIC_TASK_ID) ON DELETE CASCADE ON UPDATE CASCADE ); --- END OF DYNAMIC TASK TABLE-- \ No newline at end of file +-- END OF DYNAMIC TASK TABLE-- + +-- DM_TRACCAR_UNSYNCED_DEVICES TABLE -- +CREATE TABLE IF NOT EXISTS DM_TRACCAR_UNSYNCED_DEVICES ( + ID INT NOT NULL IDENTITY(1,1), + DEVICE_NAME VARCHAR(100) NOT NULL, + IOTS_DEVICE_IDENTIFIER VARCHAR(300) NULL UNIQUE, + TRACCAR_DEVICE_UNIQUE_ID INT NOT NULL, + TRACCAR_USENAME VARCHAR(100) NULL, + STATUS VARCHAR(100) NULL, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID) +); +-- END OF DM_TRACCAR_UNSYNCED_DEVICES TABLE -- diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 17c7460c97..4981e15870 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -851,3 +851,16 @@ CREATE TABLE IF NOT EXISTS DYNAMIC_TASK_PROPERTIES ( DYNAMIC_TASK (DYNAMIC_TASK_ID) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB; -- END OF DYNAMIC TASK TABLE-- + +-- DM_TRACCAR_UNSYNCED_DEVICES TABLE -- +CREATE TABLE IF NOT EXISTS DM_TRACCAR_UNSYNCED_DEVICES ( + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_NAME VARCHAR(100) NOT NULL, + IOTS_DEVICE_IDENTIFIER VARCHAR(300) DEFAULT NULL UNIQUE, + TRACCAR_DEVICE_UNIQUE_ID INT NOT NULL, + TRACCAR_USENAME VARCHAR(100) NULL, + STATUS VARCHAR(100) NULL, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID) +); +-- END OF DM_TRACCAR_UNSYNCED_DEVICES TABLE -- diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index a5d3725548..10554077ab 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -1119,3 +1119,16 @@ CREATE TABLE IF NOT EXISTS DYNAMIC_TASK_PROPERTIES ( DYNAMIC_TASK (DYNAMIC_TASK_ID) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB; -- END OF DYNAMIC TASK TABLE-- + +-- DM_TRACCAR_UNSYNCED_DEVICES TABLE -- +CREATE TABLE DM_TRACCAR_UNSYNCED_DEVICES ( + ID NUMBER(10) NOT NULL, + DEVICE_NAME VARCHAR2(100) NOT NULL, + IOTS_DEVICE_IDENTIFIER VARCHAR2(300) UNIQUE, + TRACCAR_DEVICE_UNIQUE_ID NUMBER(10) NOT NULL, + TRACCAR_USENAME VARCHAR2(100), + STATUS VARCHAR2(100), + TENANT_ID NUMBER(10) DEFAULT 0, + CONSTRAINT DM_TRACCAR_UNSYNCED_DEVICES_PK PRIMARY KEY (ID) +); +-- END OF DM_TRACCAR_UNSYNCED_DEVICES TABLE -- diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 731f086c61..dca570ebf0 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -772,3 +772,16 @@ CREATE TABLE IF NOT EXISTS DYNAMIC_TASK_PROPERTIES ( DYNAMIC_TASK (DYNAMIC_TASK_ID) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB; -- END OF DYNAMIC TASK TABLE-- + +-- DM_TRACCAR_UNSYNCED_DEVICES TABLE -- +CREATE TABLE IF NOT EXISTS DM_TRACCAR_UNSYNCED_DEVICES ( + ID SERIAL NOT NULL, + DEVICE_NAME VARCHAR(100) NOT NULL, + IOTS_DEVICE_IDENTIFIER VARCHAR(300) UNIQUE, + TRACCAR_DEVICE_UNIQUE_ID INT NOT NULL, + TRACCAR_USENAME VARCHAR(100), + STATUS VARCHAR(100), + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID) +); +-- END OF DM_TRACCAR_UNSYNCED_DEVICES TABLE --