From 83be95358336ba0eb855d715f859fbdc75939dd0 Mon Sep 17 00:00:00 2001 From: shamalka Date: Fri, 2 Sep 2022 00:33:22 +0530 Subject: [PATCH] Fix issues --- .../service/api/DeviceManagementService.java | 4 +- .../impl/DeviceManagementServiceImpl.java | 28 +++------ .../device/mgt/common/TrackerDeviceInfo.java | 2 +- .../device/mgt/common/TrackerGroupInfo.java | 2 +- .../mgt/common/TrackerPermissionInfo.java | 2 +- .../device/mgt/common/TrackerUserInfo.java | 2 +- .../TrackerAlreadyExistException.java | 4 +- .../device/mgt/core/dao/TrackerDAO.java | 10 ++- .../core/dao/impl/tracker/TrackerDAOImpl.java | 30 +++------ .../dao/util/TrackerManagementDAOUtil.java | 17 ----- .../impl/DeviceInformationManagerImpl.java | 9 ++- .../DeviceManagementProviderServiceImpl.java | 2 + .../api/service/addons/TraccarClientImpl.java | 63 +++++++++---------- .../impl/DeviceAPIClientServiceImpl.java | 2 +- .../common/TraccarHandlerConstants.java | 1 + .../traccar/common/beans/TraccarUser.java | 2 +- .../core/traccar/common/util/TraccarUtil.java | 4 +- 17 files changed, 71 insertions(+), 113 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 8ea4cd2f323..07ecbf8a236 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 @@ -588,8 +588,8 @@ public interface DeviceManagementService { @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", - value = "Getting Details of Registered Devices Owned by an Authenticated User", - notes = "Provides details of devices enrolled by authenticated users.", + 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 = { 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 6600a20c1ba..576dfbf0fa3 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 @@ -511,17 +511,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { status.add("INACTIVE"); status.add("CREATED"); status.add("UNREACHABLE"); - boolean isStatusEmpty = true; - for (String statusString : status) { - if (StringUtils.isNotBlank(statusString)) { - isStatusEmpty = false; - break; - } - } - if (!isStatusEmpty) { - RequestValidationUtil.validateStatus(status); - request.setStatusList(status); - } + request.setStatusList(status); // this is the user who initiates the request String authorizedUser = MultitenantUtils.getTenantAwareUsername(currentUser); // check whether the user is device-mgt admin @@ -539,9 +529,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - for (int i = 0; i < devices.getCount(); i++) { - TrackerDeviceInfo trackerDevice = DeviceAPIClientServiceImpl.getTrackerDevice( - devices.getList().get(i).getId(), tenantId); + for (Device device : devices.getList()) { + TrackerDeviceInfo trackerDevice = DeviceAPIClientServiceImpl + .getTrackerDevice(device.getId(), tenantId); int traccarDeviceId = trackerDevice.getTraccarDeviceId(); boolean getPermission = DeviceAPIClientServiceImpl.getUserIdofPermissionByDeviceIdNUserId(traccarDeviceId, userId); traccarValidIdList.add(traccarDeviceId); @@ -549,7 +539,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { DeviceAPIClientServiceImpl.addTrackerUserDevicePermission(userId, traccarDeviceId); } } - //Remove neecessary + //Remove necessary List getAllUserDevices = DeviceAPIClientServiceImpl.getUserIdofPermissionByUserIdNIdList(userId, traccarValidIdList); for (TrackerPermissionInfo getAllUserDevice : getAllUserDevices) { @@ -558,10 +548,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { getAllUserDevice.getTraccarDeviceId(), TraccarHandlerConstants.Types.REMOVE_TYPE_SINGLE); } - } catch (JSONException e) { - String msg = "not a JSONObject. "; - log.error(msg); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (DeviceManagementException e) { String msg = "Error occurred while fetching all enrolled devices. "; log.error(msg, e); @@ -575,11 +561,11 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (ExecutionException e) { - String msg = "ExecutionException occurred "; + 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 = "InterruptedException occurred "; + String msg = "Interruption error occurred handling traccar device permissions"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerDeviceInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerDeviceInfo.java index ad55f924298..c97c2a4d9dc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerDeviceInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerDeviceInfo.java @@ -22,7 +22,7 @@ import java.io.Serializable; public class TrackerDeviceInfo implements Serializable { - private static final long serialVersionUID = 1998101712L; + private static final long serialVersionUID = 11545107900391993L; private int id; private int traccarDeviceId; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerGroupInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerGroupInfo.java index 9f7e6b63dee..03a9cfef0c8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerGroupInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerGroupInfo.java @@ -22,7 +22,7 @@ import java.io.Serializable; public class TrackerGroupInfo implements Serializable { - private static final long serialVersionUID = 1998101712L; + private static final long serialVersionUID = 2156646949297006690L; private int id; private int traccarGroupId; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerPermissionInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerPermissionInfo.java index c8509efa291..25341ea19ac 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerPermissionInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerPermissionInfo.java @@ -22,7 +22,7 @@ import java.io.Serializable; public class TrackerPermissionInfo implements Serializable { - private static final long serialVersionUID = 1998101712L; + private static final long serialVersionUID = -8648202903090812913L; private int traccarUserId; private int traccarDeviceId; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerUserInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerUserInfo.java index 4985c601fd9..3354dc4e011 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerUserInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerUserInfo.java @@ -26,7 +26,7 @@ import java.io.Serializable; @ApiModel(value = "User", description = "This class carries all information related to a managed Traccar User.") public class TrackerUserInfo implements Serializable { - private static final long serialVersionUID = 1998101712L; + private static final long serialVersionUID = -6808358733610879805L; @ApiModelProperty(name = "userName", value = "The user's name that can be set on the device by the device user.", required = true) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/exceptions/TrackerAlreadyExistException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/exceptions/TrackerAlreadyExistException.java index bc7a92684fb..114ab5cb757 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/exceptions/TrackerAlreadyExistException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/exceptions/TrackerAlreadyExistException.java @@ -23,7 +23,7 @@ package org.wso2.carbon.device.mgt.common.exceptions; */ public class TrackerAlreadyExistException extends Exception { - private static final long serialVersionUID = -312678379574816874L; + private static final long serialVersionUID = 5241722939654800965L; private String errorMessage; public TrackerAlreadyExistException(String msg, Exception nestedEx) { @@ -57,4 +57,4 @@ public class TrackerAlreadyExistException extends Exception { this.errorMessage = errorMessage; } -} \ No newline at end of file +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/TrackerDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/TrackerDAO.java index 3175b3018e8..c7ce47b0c34 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/TrackerDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/TrackerDAO.java @@ -34,7 +34,7 @@ public interface TrackerDAO { * @return boolean value. * @throws TrackerManagementDAOException */ - Boolean addTrackerDevice(int traccarDeviceId, int deviceId, int tenantId) throws TrackerManagementDAOException; + void addTrackerDevice(int traccarDeviceId, int deviceId, int tenantId) throws TrackerManagementDAOException; /** * get trackerDevice info. @@ -51,10 +51,9 @@ public interface TrackerDAO { * @param deviceId of the device. * @param tenantId of the group. * @param status of the device. - * @return boolean value. * @throws TrackerManagementDAOException */ - Boolean updateTrackerDeviceIdANDStatus(int traccarDeviceId, int deviceId, int tenantId, int status) throws TrackerManagementDAOException; + void updateTrackerDeviceIdANDStatus(int traccarDeviceId, int deviceId, int tenantId, int status) throws TrackerManagementDAOException; /** * Remove a Device. @@ -63,17 +62,16 @@ public interface TrackerDAO { * @return sql execution result. * @throws TrackerManagementDAOException */ - int removeTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException; + void removeTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException; /** * Add new Group. * @param traccarGroupId to be added. * @param groupId of the group. * @param tenantId of the group. - * @return boolean value. * @throws TrackerManagementDAOException */ - Boolean addTrackerGroup(int traccarGroupId, int groupId, int tenantId) throws TrackerManagementDAOException; + void addTrackerGroup(int traccarGroupId, int groupId, int tenantId) throws TrackerManagementDAOException; /** * Update status and traccarGroupId of a Group. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java index d98b630de33..43d21b4d0e9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java @@ -41,7 +41,7 @@ public class TrackerDAOImpl implements TrackerDAO { private static final Log log = LogFactory.getLog(TrackerDAOImpl.class); @Override - public Boolean addTrackerDevice(int traccarDeviceId, int deviceId, int tenantId) + public void addTrackerDevice(int traccarDeviceId, int deviceId, int tenantId) throws TrackerManagementDAOException { PreparedStatement stmt = null; try { @@ -52,8 +52,6 @@ public class TrackerDAOImpl implements TrackerDAO { stmt.setInt(2, deviceId); stmt.setInt(3, tenantId); stmt.execute(); - - return true; } catch (SQLException e) { String msg = "Error occurred while adding on trackerDevice mapping table"; log.error(msg, e); @@ -64,7 +62,7 @@ public class TrackerDAOImpl implements TrackerDAO { } @Override - public Boolean updateTrackerDeviceIdANDStatus(int traccarDeviceId, int deviceId, int tenantId, int status) + public void updateTrackerDeviceIdANDStatus(int traccarDeviceId, int deviceId, int tenantId, int status) throws TrackerManagementDAOException { PreparedStatement stmt = null; try { @@ -76,8 +74,6 @@ public class TrackerDAOImpl implements TrackerDAO { stmt.setInt(3, deviceId); stmt.setInt(4, tenantId); stmt.execute(); - - return true; } catch (SQLException e) { String msg = "Error occurred while updating status on trackerDevice mapping table"; log.error(msg, e); @@ -88,10 +84,8 @@ public class TrackerDAOImpl implements TrackerDAO { } @Override - public int removeTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException { + public void removeTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException { PreparedStatement stmt = null; - ResultSet rs = null; - int status = -1; try { Connection conn = TrackerManagementDAOFactory.getConnection(); String sql = "DELETE FROM DM_EXT_DEVICE_MAPPING WHERE DEVICE_ID = ? AND TENANT_ID = ? "; @@ -99,11 +93,6 @@ public class TrackerDAOImpl implements TrackerDAO { stmt.setInt(1, deviceId); stmt.setInt(2, tenantId); stmt.executeUpdate(); - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - status = 1; - } - return status; } catch (SQLException e) { String msg = "Error occurred while removing on trackerDevice table"; log.error(msg, e); @@ -117,7 +106,6 @@ public class TrackerDAOImpl implements TrackerDAO { public TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; - TrackerDeviceInfo trackerDeviceInfo = null; try { Connection conn = TrackerManagementDAOFactory.getConnection(); String sql = "SELECT ID, TRACCAR_DEVICE_ID, DEVICE_ID, TENANT_ID, STATUS FROM DM_EXT_DEVICE_MAPPING WHERE " + @@ -127,9 +115,9 @@ public class TrackerDAOImpl implements TrackerDAO { stmt.setInt(2, tenantId); rs = stmt.executeQuery(); if (rs.next()) { - trackerDeviceInfo = TrackerManagementDAOUtil.loadTrackerDevice(rs); + return TrackerManagementDAOUtil.loadTrackerDevice(rs); } - return trackerDeviceInfo; + return null; } catch (SQLException e) { String msg = "Error occurred while retrieving data from the trackerDevice table "; log.error(msg, e); @@ -140,10 +128,9 @@ public class TrackerDAOImpl implements TrackerDAO { } @Override - public Boolean addTrackerGroup(int traccarGroupId, int groupId, int tenantId) + public void addTrackerGroup(int traccarGroupId, int groupId, int tenantId) throws TrackerManagementDAOException { PreparedStatement stmt = null; - int status = 1; try { Connection conn = TrackerManagementDAOFactory.getConnection(); String sql = "INSERT INTO DM_EXT_GROUP_MAPPING(TRACCAR_GROUP_ID, GROUP_ID, TENANT_ID, STATUS) VALUES(?, ?, ?, ?)"; @@ -151,10 +138,8 @@ public class TrackerDAOImpl implements TrackerDAO { stmt.setInt(1, traccarGroupId); stmt.setInt(2, groupId); stmt.setInt(3, tenantId); - stmt.setInt(4, status); + stmt.setInt(4, 1); stmt.execute(); - - return true; } catch (SQLException e) { String msg = "Error occurred while adding on traccarGroup mapping table"; log.error(msg, e); @@ -268,6 +253,7 @@ public class TrackerDAOImpl implements TrackerDAO { try { Connection conn = TrackerManagementDAOFactory.getConnection(); String sql = "DELETE FROM DM_EXT_PERMISSION_MAPPING WHERE TRACCAR_DEVICE_ID = ?"; + // TODO: Recheck the usage of below if condition if (removeType != TraccarHandlerConstants.Types.REMOVE_TYPE_MULTIPLE) { sql = sql + " AND TRACCAR_USER_ID = ? "; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/TrackerManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/TrackerManagementDAOUtil.java index fb06d34d012..b36d3ec6683 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/TrackerManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/TrackerManagementDAOUtil.java @@ -1,20 +1,3 @@ -/* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ /* Copyright (c) 2022, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. * * Entgra (pvt) Ltd. licenses this file to you under the Apache License, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 553d1c3fa5c..e9e5b0aaf5c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -395,12 +395,19 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } catch (ExecutionException e) { log.error("ExecutionException : " + e); //throw new RuntimeException(e); + //Exception was not thrown due to being conflicted with non-traccar features } catch (InterruptedException e) { log.error("InterruptedException : " + e); //throw new RuntimeException(e); + //Exception was not thrown due to being conflicted with non-traccar features } } else { - log.info("location publishing is disabled and traccan disabled"); + if(!HttpReportingUtil.isLocationPublishing()) { + log.info("Location publishing is disabled"); + } + if (!HttpReportingUtil.isTrackerEnabled()) { + log.info("Traccar is disabled"); + } } //Tracker update GPS Location 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 c7f42afb463..c777ea87995 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 @@ -441,6 +441,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv //throw new RuntimeException(e); //Exception was not thrown due to being conflicted with non-traccar features } + } else { + log.info("Traccar is disabled"); } //enroll Traccar device diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java index 016e083bf95..526de67f616 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * Copyright (C) 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved. * * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. * @@ -120,7 +120,7 @@ public class TraccarClientImpl implements TraccarClient { requestBody = RequestBody.create(payload.toString(), MediaType.parse("application/json; charset=utf-8")); builder = builder.put(requestBody); } else if (Objects.equals(method, TraccarHandlerConstants.Methods.DELETE)) { - if (publisherUrlWithContext.indexOf("permission") != -1) { + if (publisherUrlWithContext.contains("permission")) { requestBody = RequestBody.create(payload.toString(), MediaType.parse("application/json; charset=utf-8")); builder = builder.delete(requestBody); } else { @@ -141,8 +141,7 @@ public class TraccarClientImpl implements TraccarClient { Future result = executor.submit(new OkHttpClientThreadPool(url, null, method, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); - String res = result.get(); - return res; + return result.get(); } public String fetchUserInfo(String userName) throws ExecutionException, InterruptedException { @@ -177,7 +176,7 @@ public class TraccarClientImpl implements TraccarClient { if (Objects.equals(result, TraccarHandlerConstants.Types.USER_NOT_FOUND)) { //create user - log.info("Creating a user"); + log.info("Creating a user on Traccar client"); traccarUser.setName(userName); traccarUser.setLogin(userName); traccarUser.setEmail(userName); @@ -188,7 +187,7 @@ public class TraccarClientImpl implements TraccarClient { DeviceAPIClientServiceImpl.createUser(traccarUser); } else { //update user - log.info("Updating the user"); + log.info("Updating the user on Traccar client"); JSONObject obj = new JSONObject(result); traccarUser.setId(obj.getInt("id")); @@ -229,8 +228,7 @@ public class TraccarClientImpl implements TraccarClient { Future res = executor.submit(new OkHttpClientThreadPool(url, payload, method, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); - String result = res.get(); - return result; + return res.get(); } public String updateUser(TraccarUser traccarUser, int userId) throws ExecutionException, InterruptedException { @@ -241,8 +239,7 @@ public class TraccarClientImpl implements TraccarClient { Future res = executor.submit(new OkHttpClientThreadPool(url, payload, method, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); - String result = res.get(); - return result; + return res.get(); } public void setPermission(int userId, int deviceId) @@ -259,7 +256,7 @@ public class TraccarClientImpl implements TraccarClient { serverUrl(HttpReportingUtil.trackerServer()))); String result = res.get(); - if (result.equals("")) { + if (("").equals(result)) { try { TrackerManagementDAOFactory.beginTransaction(); trackerDAO.addTrackerUserDevicePermission(userId, deviceId); @@ -294,9 +291,8 @@ public class TraccarClientImpl implements TraccarClient { Future res = executor.submit(new OkHttpClientThreadPool(url, payload, method, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); - String result = res.get(); - if (result != null) { + if (res.get() != null) { try { TrackerManagementDAOFactory.beginTransaction(); trackerDAO.removeTrackerUserDevicePermission(deviceId, userId, removeType); @@ -386,11 +382,6 @@ public class TraccarClientImpl implements TraccarClient { try { TrackerManagementDAOFactory.openConnection(); trackerDeviceInfo = trackerDAO.getTrackerDevice(traccarDevice.getId(), tenantId); - if (trackerDeviceInfo != null) { - String msg = "The device already exist"; - log.error(msg); - throw new TrackerAlreadyExistException(msg); - } } catch (TrackerManagementDAOException e) { String msg = "Error occurred while mapping with deviceId ."; log.error(msg, e); @@ -405,11 +396,10 @@ public class TraccarClientImpl implements TraccarClient { if (trackerDeviceInfo == null) { //add the device - String method = TraccarHandlerConstants.Methods.POST; String url = defaultPort + "/api/devices"; JSONObject payload = TraccarUtil.TraccarDevicePayload(traccarDevice); - Future res = executor.submit(new OkHttpClientThreadPool(url, payload, method, + Future res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.POST, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); String result = res.get(); @@ -423,7 +413,7 @@ public class TraccarClientImpl implements TraccarClient { TrackerManagementDAOFactory.beginTransaction(); trackerDAO.addTrackerDevice(traccarDeviceId, deviceId, tenantId); trackerDeviceInfo = trackerDAO.getTrackerDevice(deviceId, tenantId); - if (trackerDeviceInfo.getStatus() == 0) { + if (trackerDeviceInfo != null && trackerDeviceInfo.getStatus() == 0) { trackerDAO.updateTrackerDeviceIdANDStatus(trackerDeviceInfo.getTraccarDeviceId(), deviceId, tenantId, 1); } TrackerManagementDAOFactory.commitTransaction(); @@ -445,6 +435,10 @@ public class TraccarClientImpl implements TraccarClient { setPermission(returnUserInfo.getInt("id"), traccarDeviceId); } } + } else { + String msg = "The device already exist"; + log.error(msg); + throw new TrackerAlreadyExistException(msg); } } @@ -477,14 +471,15 @@ public class TraccarClientImpl implements TraccarClient { addDevice(device, tenantId); } else { //Update Location - log.info("Updating Location"); - String method = TraccarHandlerConstants.Methods.GET; + if (log.isDebugEnabled()) { + log.info("Updating Location of the device: " + device.getId()); + } String url = locationUpdatePort + "/?id=" + deviceInfo.getDeviceIdentifier() + "×tamp=" + deviceInfo.getTimestamp() + "&lat=" + deviceInfo.getLat() + "&lon=" + deviceInfo.getLon() + "&bearing=" + deviceInfo.getBearing() + "&speed=" + deviceInfo.getSpeed() + "&ignition=true"; - executor.submit(new OkHttpClientThreadPool(url, null, method, + executor.submit(new OkHttpClientThreadPool(url, null, TraccarHandlerConstants.Methods.GET, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), "http://localhost:")); } @@ -502,13 +497,10 @@ public class TraccarClientImpl implements TraccarClient { try { TrackerManagementDAOFactory.beginTransaction(); trackerDeviceInfo = trackerDAO.getTrackerDevice(deviceId, tenantId); - log.info("deviceId - " + deviceId); if (trackerDeviceInfo != null) { - int status = trackerDAO.removeTrackerDevice(deviceId, tenantId); - log.info("Status" + status); + trackerDAO.removeTrackerDevice(deviceId, tenantId); TrackerManagementDAOFactory.commitTransaction(); - trackerPermissionInfo = trackerDAO.getUserIdofPermissionByDeviceId(trackerDeviceInfo.getTraccarDeviceId()); } } catch (TransactionManagementException e) { @@ -524,7 +516,7 @@ public class TraccarClientImpl implements TraccarClient { } finally { TrackerManagementDAOFactory.closeConnection(); } - log.info("--------Disenrolling Device--------"); + log.info("--------Disenrolling Device with device id " + deviceId + " from traccar client--------"); //Delete from traccar if (trackerDeviceInfo != null) { String method = TraccarHandlerConstants.Methods.DELETE; @@ -601,9 +593,9 @@ public class TraccarClientImpl implements TraccarClient { trackerDAO.updateTrackerGroupIdANDStatus(trackerGroupInfo.getTraccarGroupId(), groupId, tenantId, 1); } } catch (TrackerManagementDAOException e) { + TrackerManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while mapping with deviceId. "; log.error(msg, e); - TrackerManagementDAOFactory.rollbackTransaction(); throw new TrackerManagementDAOException(msg, e); } catch (TransactionManagementException e) { String msg = "Error occurred establishing the DB connection. "; @@ -614,10 +606,12 @@ public class TraccarClientImpl implements TraccarClient { TrackerManagementDAOFactory.closeConnection(); } } else { - log.error("Something went wrong_1: " + result); + // TODO: Assumed the error message change if wrong + log.error("Response does not contains the key id: " + result); } } else { - log.error("Something went wrong_2: " + result); + // TODO: Assumed the error message change if wrong + log.error("Response does not contains a JSON object " + result); } } @@ -636,9 +630,11 @@ public class TraccarClientImpl implements TraccarClient { } catch (SQLException e) { String msg = "Error occurred establishing the DB connection. "; log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); } catch (TrackerManagementDAOException e) { String msg = "Could not find traccar group details. "; log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); } finally { TrackerManagementDAOFactory.closeConnection(); } @@ -699,10 +695,9 @@ public class TraccarClientImpl implements TraccarClient { trackerDAO.removeTrackerGroup(obj.getInt("id")); TrackerManagementDAOFactory.commitTransaction(); - String method = TraccarHandlerConstants.Methods.DELETE; String url = defaultPort + "/api/groups/" + obj.getInt("traccarGroupId"); - executor.submit(new OkHttpClientThreadPool(url, null, method, + executor.submit(new OkHttpClientThreadPool(url, null, TraccarHandlerConstants.Methods.DELETE, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java index 540dcfb8df1..f1f71cb99a8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * Copyright (C) 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved. * * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarHandlerConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarHandlerConstants.java index 453fb5290fe..e99832e9398 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarHandlerConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarHandlerConstants.java @@ -31,6 +31,7 @@ public class TraccarHandlerConstants { public static final String LOCATION_UPDATE_PORT = "location-update-port"; } + // TODO: Get these from http client public static class Methods { public static final String POST = "POST"; public static final String GET = "GET"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarUser.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarUser.java index 37557ef0948..4531e1d122d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarUser.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarUser.java @@ -23,7 +23,7 @@ import java.io.Serializable; public class TraccarUser implements Serializable { - private static final long serialVersionUID = 1998101712L; + private static final long serialVersionUID = -1916365851689588289L; private int id; private String name; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/util/TraccarUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/util/TraccarUtil.java index 6063d4d0c15..986b10624d8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/util/TraccarUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/util/TraccarUtil.java @@ -27,7 +27,7 @@ import java.util.ArrayList; import java.util.List; public class TraccarUtil { - public static JSONObject TraccarUserPayload(TraccarUser traccarUser){ + public static JSONObject TraccarUserPayload(TraccarUser traccarUser) { JSONObject payload = new JSONObject(); JSONObject attribute = new JSONObject(); attribute.put("speedUnit", "kmh"); @@ -48,7 +48,7 @@ public class TraccarUtil { return payload; } - public static JSONObject TraccarDevicePayload(TraccarDevice deviceInfo){ + public static JSONObject TraccarDevicePayload(TraccarDevice deviceInfo) { JSONObject payload = new JSONObject(); payload.put("name", deviceInfo.getDeviceName()); payload.put("uniqueId", deviceInfo.getUniqueId());