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 5ac01b3fc31..1b85a4c9ba3 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 @@ -102,6 +102,7 @@ import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; import org.wso2.carbon.device.mgt.core.search.mgt.SearchMgtException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; +import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService; import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl; import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; @@ -492,7 +493,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { if (HttpReportingUtil.isTrackerEnabled()) { String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); - JSONObject obj = new JSONObject(DeviceAPIClientServiceImpl.returnUser(currentUser)); + JSONObject obj = new JSONObject(DeviceMgtAPIUtils.getDeviceAPIClientService().returnUser(currentUser)); if (obj.has("error")) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(obj.getString("error")).build(); @@ -502,6 +503,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { /*Get Device Id List*/ try { DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); + DeviceAPIClientService deviceAPIClientService = DeviceMgtAPIUtils.getDeviceAPIClientService(); DeviceAccessAuthorizationService deviceAccessAuthorizationService = DeviceMgtAPIUtils.getDeviceAccessAuthorizationService(); PaginationRequest request = new PaginationRequest(0, 0); @@ -532,21 +534,25 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); TrackerDeviceInfo trackerDevice; for (Device device : devices.getList()) { - trackerDevice = DeviceAPIClientServiceImpl.getTrackerDevice(device.getId(), tenantId); - if (trackerDevice != null) { + trackerDevice = deviceAPIClientService.getTrackerDevice(device.getId(), tenantId); + if(trackerDevice == null) { + String msg = "Tracker device not found for the device ID: " + device.getId(); + log.error(msg); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } else { int traccarDeviceId = trackerDevice.getTraccarDeviceId(); - boolean getPermission = DeviceAPIClientServiceImpl.getUserIdofPermissionByDeviceIdNUserId(traccarDeviceId, userId); + boolean getPermission = deviceAPIClientService.getUserIdofPermissionByDeviceIdNUserId(traccarDeviceId, userId); traccarValidIdList.add(traccarDeviceId); if (!getPermission) { - DeviceAPIClientServiceImpl.addTrackerUserDevicePermission(userId, traccarDeviceId); + deviceAPIClientService.addTrackerUserDevicePermission(userId, traccarDeviceId); } } } //Remove necessary List getAllUserDevices = - DeviceAPIClientServiceImpl.getUserIdofPermissionByUserIdNIdList(userId, traccarValidIdList); + deviceAPIClientService.getUserIdofPermissionByUserIdNIdList(userId, traccarValidIdList); for (TrackerPermissionInfo getAllUserDevice : getAllUserDevices) { - DeviceAPIClientServiceImpl.removeTrackerUserDevicePermission( + deviceAPIClientService.removeTrackerUserDevicePermission( getAllUserDevice.getTraccarUserId(), getAllUserDevice.getTraccarDeviceId(), TraccarHandlerConstants.Types.REMOVE_TYPE_SINGLE); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java index 8861833f5aa..92bc2790d49 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java @@ -87,6 +87,7 @@ import org.wso2.carbon.device.mgt.core.privacy.PrivacyComplianceProvider; import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; +import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService; 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.beans.OperationStatusBean; @@ -592,6 +593,19 @@ public class DeviceMgtAPIUtils { return analyticsDataAPI; } + public static DeviceAPIClientService getDeviceAPIClientService() { + DeviceAPIClientService deviceAPIClientService; + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + deviceAPIClientService = (DeviceAPIClientService) ctx.getOSGiService( + DeviceAPIClientService.class, null); + if (deviceAPIClientService == null) { + String msg = "Device API Client service not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return deviceAPIClientService; + } + public static int getTenantId(String tenantDomain) throws DeviceManagementException { RealmService realmService = (RealmService) PrivilegedCarbonContext.getThreadLocalCarbonContext().getOSGiService(RealmService.class, null); 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 43d21b4d0e9..a3d1ccf8afe 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 @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.dao.impl.tracker; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo; @@ -26,13 +27,11 @@ import org.wso2.carbon.device.mgt.common.TrackerPermissionInfo; import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.TrackerDAO; +import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.TrackerManagementDAOUtil; import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.ArrayList; import java.util.List; @@ -104,27 +103,26 @@ public class TrackerDAOImpl implements TrackerDAO { @Override 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 " + "DEVICE_ID = ? AND TENANT_ID = ? ORDER BY ID DESC LIMIT 1"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - if (rs.next()) { - return TrackerManagementDAOUtil.loadTrackerDevice(rs); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, deviceId); + stmt.setInt(2, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + trackerDeviceInfo = TrackerManagementDAOUtil.loadTrackerDevice(rs); + } + } } - return null; } catch (SQLException e) { String msg = "Error occurred while retrieving data from the trackerDevice table "; log.error(msg, e); throw new TrackerManagementDAOException(msg, e); - } finally { - TrackerManagementDAOUtil.cleanupResources(stmt, rs); } + return trackerDeviceInfo; } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java index 605d4f7a46b..73aca76059b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java @@ -20,10 +20,14 @@ package org.wso2.carbon.device.mgt.core.traccar.api.service; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo; +import org.wso2.carbon.device.mgt.common.TrackerPermissionInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; import org.wso2.carbon.device.mgt.common.exceptions.TrackerAlreadyExistException; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOException; + +import java.util.List; import java.util.concurrent.ExecutionException; public interface DeviceAPIClientService { @@ -81,4 +85,22 @@ public interface DeviceAPIClientService { */ void deleteGroup(int groupId, int tenantId) throws TrackerManagementDAOException, ExecutionException, InterruptedException; + + String returnUser(String username); + + TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws + TrackerManagementDAOException; + + boolean getUserIdofPermissionByDeviceIdNUserId(int deviceId, int userId) throws + TrackerManagementDAOException; + + void addTrackerUserDevicePermission(int userId, int deviceId) throws + TrackerManagementDAOException, ExecutionException, InterruptedException; + + List getUserIdofPermissionByUserIdNIdList(int userId, List NotInDeviceIdList) throws + TrackerManagementDAOException; + + void removeTrackerUserDevicePermission(int userId, int deviceId, int removeType) throws + TrackerManagementDAOException, ExecutionException, InterruptedException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java index 547c4e8b25b..65734d1f3ca 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java @@ -352,7 +352,16 @@ public class TraccarClientFactory { public TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException { try { TrackerManagementDAOFactory.openConnection(); - return trackerDAO.getTrackerDevice(deviceId, tenantId); + TrackerDeviceInfo trackerDeviceInfo = trackerDAO.getTrackerDevice(deviceId, tenantId); + if(trackerDeviceInfo == null) { + String msg = "No tracker device is found upon the device ID of: " + deviceId; + if (log.isDebugEnabled()) { + log.debug(msg); + } + return null; + } else { + return trackerDeviceInfo; + } } catch (SQLException e) { String msg = "Error occurred establishing the DB connection ."; log.error(msg, e); 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 1361bbdc173..6a113ca72e4 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 @@ -45,6 +45,7 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { private static final Log log = LogFactory.getLog(DeviceAPIClientServiceImpl.class); TraccarClientFactory client = TraccarClientFactory.getInstance(); + @Override public void addDevice(Device device, int tenantId) throws ExecutionException, InterruptedException { String lastUpdatedTime = String.valueOf((new Date().getTime())); TraccarDevice traccarDevice = new TraccarDevice(device.getId(), device.getName(), device.getDeviceIdentifier(), @@ -61,6 +62,7 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { } } + @Override public void updateLocation(Device device, DeviceLocation deviceLocation, int tenantId) throws ExecutionException, InterruptedException { TraccarPosition traccarPosition = new TraccarPosition(device.getDeviceIdentifier(), deviceLocation.getUpdatedTime().getTime(), @@ -82,6 +84,7 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { } } + @Override public void disEnrollDevice(int deviceId, int tenantId) throws ExecutionException, InterruptedException{ try { client.disEnrollDevice(deviceId, tenantId); @@ -91,79 +94,88 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { } } + @Override public void addGroup(DeviceGroup group, int groupId, int tenantId) throws TrackerManagementDAOException, TrackerAlreadyExistException, ExecutionException, InterruptedException { TraccarGroups traccarGroups = new TraccarGroups(group.getName()); client.addGroup(traccarGroups, groupId, tenantId); } + @Override public void updateGroup(DeviceGroup group, int groupId, int tenantId) throws TrackerManagementDAOException, TrackerAlreadyExistException, ExecutionException, InterruptedException { TraccarGroups traccarGroups = new TraccarGroups(group.getName()); client.updateGroup(traccarGroups, groupId, tenantId); } + @Override public void deleteGroup(int groupId, int tenantId) throws TrackerManagementDAOException, ExecutionException, InterruptedException { client.deleteGroup(groupId, tenantId); } - public static String fetchUserInfo(String userName) throws ExecutionException, InterruptedException { + @Override + public String returnUser(String userName) { TraccarClientFactory client = TraccarClientFactory.getInstance(); - return client.fetchUserInfo(userName); + try { + return client.returnUser(userName); + } catch (TrackerManagementDAOException e) { + JSONObject obj = new JSONObject(); + String msg = "Error occurred while creating a user: "+ e; + obj.put("error", msg); + return obj.toString(); + } } - public static TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws + @Override + public TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException { TraccarClientFactory client = TraccarClientFactory.getInstance(); return client.getTrackerDevice(deviceId, tenantId); } - public static boolean getUserIdofPermissionByDeviceIdNUserId(int deviceId, int userId) throws + @Override + public boolean getUserIdofPermissionByDeviceIdNUserId(int deviceId, int userId) throws TrackerManagementDAOException { TraccarClientFactory client = TraccarClientFactory.getInstance(); return client.getUserIdofPermissionByDeviceIdNUserId(deviceId, userId); } - public static String createUser(TraccarUser traccarUser) throws ExecutionException, InterruptedException { + @Override + public void addTrackerUserDevicePermission(int userId, int deviceId) throws + TrackerManagementDAOException, ExecutionException, InterruptedException { TraccarClientFactory client = TraccarClientFactory.getInstance(); - return client.createUser(traccarUser); + client.setPermission(userId, deviceId); } - public static String updateUser(TraccarUser traccarUser, int userId) throws - ExecutionException, InterruptedException { + @Override + public List getUserIdofPermissionByUserIdNIdList(int userId, List NotInDeviceIdList) throws + TrackerManagementDAOException { TraccarClientFactory client = TraccarClientFactory.getInstance(); - return client.updateUser(traccarUser, userId); + return client.getUserIdofPermissionByUserIdNIdList(userId, NotInDeviceIdList); } - public static String returnUser(String userName) { + @Override + public void removeTrackerUserDevicePermission(int userId, int deviceId, int removeType) throws + TrackerManagementDAOException, ExecutionException, InterruptedException { TraccarClientFactory client = TraccarClientFactory.getInstance(); - try { - return client.returnUser(userName); - } catch (TrackerManagementDAOException e) { - JSONObject obj = new JSONObject(); - String msg = "Error occurred while creating a user: "+ e; - obj.put("error", msg); - return obj.toString(); - } + client.removePermission(userId, deviceId, removeType); } - public static void addTrackerUserDevicePermission(int userId, int deviceId) throws - TrackerManagementDAOException, ExecutionException, InterruptedException { + public static String fetchUserInfo(String userName) throws ExecutionException, InterruptedException { TraccarClientFactory client = TraccarClientFactory.getInstance(); - client.setPermission(userId, deviceId); + return client.fetchUserInfo(userName); } - public static void removeTrackerUserDevicePermission(int userId, int deviceId, int removeType) throws - TrackerManagementDAOException, ExecutionException, InterruptedException { + public static String createUser(TraccarUser traccarUser) throws ExecutionException, InterruptedException { TraccarClientFactory client = TraccarClientFactory.getInstance(); - client.removePermission(userId, deviceId, removeType); + return client.createUser(traccarUser); } - public static List getUserIdofPermissionByUserIdNIdList(int userId, List NotInDeviceIdList) throws - TrackerManagementDAOException { + public static String updateUser(TraccarUser traccarUser, int userId) throws + ExecutionException, InterruptedException { TraccarClientFactory client = TraccarClientFactory.getInstance(); - return client.getUserIdofPermissionByUserIdNIdList(userId, NotInDeviceIdList); + return client.updateUser(traccarUser, userId); } public static String generateRandomString(int len) {