Refactor traccar implementation

5011-traccar
shamalka 2 years ago
parent f840ee97a8
commit e5095b287a

@ -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.search.mgt.SearchMgtException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; 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.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.api.service.impl.DeviceAPIClientServiceImpl;
import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants; import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
@ -492,7 +493,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
if (HttpReportingUtil.isTrackerEnabled()) { if (HttpReportingUtil.isTrackerEnabled()) {
String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername();
JSONObject obj = new JSONObject(DeviceAPIClientServiceImpl.returnUser(currentUser)); JSONObject obj = new JSONObject(DeviceMgtAPIUtils.getDeviceAPIClientService().returnUser(currentUser));
if (obj.has("error")) { if (obj.has("error")) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(obj.getString("error")).build(); 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*/ /*Get Device Id List*/
try { try {
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
DeviceAPIClientService deviceAPIClientService = DeviceMgtAPIUtils.getDeviceAPIClientService();
DeviceAccessAuthorizationService deviceAccessAuthorizationService = DeviceAccessAuthorizationService deviceAccessAuthorizationService =
DeviceMgtAPIUtils.getDeviceAccessAuthorizationService(); DeviceMgtAPIUtils.getDeviceAccessAuthorizationService();
PaginationRequest request = new PaginationRequest(0, 0); PaginationRequest request = new PaginationRequest(0, 0);
@ -532,21 +534,25 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
TrackerDeviceInfo trackerDevice; TrackerDeviceInfo trackerDevice;
for (Device device : devices.getList()) { for (Device device : devices.getList()) {
trackerDevice = DeviceAPIClientServiceImpl.getTrackerDevice(device.getId(), tenantId); trackerDevice = deviceAPIClientService.getTrackerDevice(device.getId(), tenantId);
if (trackerDevice != null) { 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(); int traccarDeviceId = trackerDevice.getTraccarDeviceId();
boolean getPermission = DeviceAPIClientServiceImpl.getUserIdofPermissionByDeviceIdNUserId(traccarDeviceId, userId); boolean getPermission = deviceAPIClientService.getUserIdofPermissionByDeviceIdNUserId(traccarDeviceId, userId);
traccarValidIdList.add(traccarDeviceId); traccarValidIdList.add(traccarDeviceId);
if (!getPermission) { if (!getPermission) {
DeviceAPIClientServiceImpl.addTrackerUserDevicePermission(userId, traccarDeviceId); deviceAPIClientService.addTrackerUserDevicePermission(userId, traccarDeviceId);
} }
} }
} }
//Remove necessary //Remove necessary
List<TrackerPermissionInfo> getAllUserDevices = List<TrackerPermissionInfo> getAllUserDevices =
DeviceAPIClientServiceImpl.getUserIdofPermissionByUserIdNIdList(userId, traccarValidIdList); deviceAPIClientService.getUserIdofPermissionByUserIdNIdList(userId, traccarValidIdList);
for (TrackerPermissionInfo getAllUserDevice : getAllUserDevices) { for (TrackerPermissionInfo getAllUserDevice : getAllUserDevices) {
DeviceAPIClientServiceImpl.removeTrackerUserDevicePermission( deviceAPIClientService.removeTrackerUserDevicePermission(
getAllUserDevice.getTraccarUserId(), getAllUserDevice.getTraccarUserId(),
getAllUserDevice.getTraccarDeviceId(), getAllUserDevice.getTraccarDeviceId(),
TraccarHandlerConstants.Types.REMOVE_TYPE_SINGLE); TraccarHandlerConstants.Types.REMOVE_TYPE_SINGLE);

@ -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.search.mgt.SearchManagerService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; 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.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.DeviceTypeVersionWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.OperationStatusBean; import org.wso2.carbon.device.mgt.jaxrs.beans.OperationStatusBean;
@ -592,6 +593,19 @@ public class DeviceMgtAPIUtils {
return analyticsDataAPI; 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 { public static int getTenantId(String tenantDomain) throws DeviceManagementException {
RealmService realmService = RealmService realmService =
(RealmService) PrivilegedCarbonContext.getThreadLocalCarbonContext().getOSGiService(RealmService.class, null); (RealmService) PrivilegedCarbonContext.getThreadLocalCarbonContext().getOSGiService(RealmService.class, null);

@ -18,6 +18,7 @@
package org.wso2.carbon.device.mgt.core.dao.impl.tracker; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo; 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.TrackerManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOException; 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.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.dao.util.TrackerManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants; import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants;
import java.sql.Connection; import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -104,27 +103,26 @@ public class TrackerDAOImpl implements TrackerDAO {
@Override @Override
public TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException { public TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException {
PreparedStatement stmt = null; TrackerDeviceInfo trackerDeviceInfo = null;
ResultSet rs = null;
try { try {
Connection conn = TrackerManagementDAOFactory.getConnection(); Connection conn = TrackerManagementDAOFactory.getConnection();
String sql = "SELECT ID, TRACCAR_DEVICE_ID, DEVICE_ID, TENANT_ID, STATUS FROM DM_EXT_DEVICE_MAPPING WHERE " + 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"; "DEVICE_ID = ? AND TENANT_ID = ? ORDER BY ID DESC LIMIT 1";
stmt = conn.prepareStatement(sql); try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
rs = stmt.executeQuery(); try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) { if (rs.next()) {
return TrackerManagementDAOUtil.loadTrackerDevice(rs); trackerDeviceInfo = TrackerManagementDAOUtil.loadTrackerDevice(rs);
}
}
} }
return null;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while retrieving data from the trackerDevice table "; String msg = "Error occurred while retrieving data from the trackerDevice table ";
log.error(msg, e); log.error(msg, e);
throw new TrackerManagementDAOException(msg, e); throw new TrackerManagementDAOException(msg, e);
} finally {
TrackerManagementDAOUtil.cleanupResources(stmt, rs);
} }
return trackerDeviceInfo;
} }
@Override @Override

@ -20,10 +20,14 @@
package org.wso2.carbon.device.mgt.core.traccar.api.service; 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.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.device.details.DeviceLocation;
import org.wso2.carbon.device.mgt.common.exceptions.TrackerAlreadyExistException; 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.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOException;
import java.util.List;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
public interface DeviceAPIClientService { public interface DeviceAPIClientService {
@ -81,4 +85,22 @@ public interface DeviceAPIClientService {
*/ */
void deleteGroup(int groupId, int tenantId) throws void deleteGroup(int groupId, int tenantId) throws
TrackerManagementDAOException, ExecutionException, InterruptedException; 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<TrackerPermissionInfo> getUserIdofPermissionByUserIdNIdList(int userId, List<Integer> NotInDeviceIdList) throws
TrackerManagementDAOException;
void removeTrackerUserDevicePermission(int userId, int deviceId, int removeType) throws
TrackerManagementDAOException, ExecutionException, InterruptedException;
} }

@ -352,7 +352,16 @@ public class TraccarClientFactory {
public TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException { public TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException {
try { try {
TrackerManagementDAOFactory.openConnection(); 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) { } catch (SQLException e) {
String msg = "Error occurred establishing the DB connection ."; String msg = "Error occurred establishing the DB connection .";
log.error(msg, e); log.error(msg, e);

@ -45,6 +45,7 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService {
private static final Log log = LogFactory.getLog(DeviceAPIClientServiceImpl.class); private static final Log log = LogFactory.getLog(DeviceAPIClientServiceImpl.class);
TraccarClientFactory client = TraccarClientFactory.getInstance(); TraccarClientFactory client = TraccarClientFactory.getInstance();
@Override
public void addDevice(Device device, int tenantId) throws ExecutionException, InterruptedException { public void addDevice(Device device, int tenantId) throws ExecutionException, InterruptedException {
String lastUpdatedTime = String.valueOf((new Date().getTime())); String lastUpdatedTime = String.valueOf((new Date().getTime()));
TraccarDevice traccarDevice = new TraccarDevice(device.getId(), device.getName(), device.getDeviceIdentifier(), 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 { public void updateLocation(Device device, DeviceLocation deviceLocation, int tenantId) throws ExecutionException, InterruptedException {
TraccarPosition traccarPosition = new TraccarPosition(device.getDeviceIdentifier(), TraccarPosition traccarPosition = new TraccarPosition(device.getDeviceIdentifier(),
deviceLocation.getUpdatedTime().getTime(), deviceLocation.getUpdatedTime().getTime(),
@ -82,6 +84,7 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService {
} }
} }
@Override
public void disEnrollDevice(int deviceId, int tenantId) throws ExecutionException, InterruptedException{ public void disEnrollDevice(int deviceId, int tenantId) throws ExecutionException, InterruptedException{
try { try {
client.disEnrollDevice(deviceId, tenantId); client.disEnrollDevice(deviceId, tenantId);
@ -91,79 +94,88 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService {
} }
} }
@Override
public void addGroup(DeviceGroup group, int groupId, int tenantId) throws public void addGroup(DeviceGroup group, int groupId, int tenantId) throws
TrackerManagementDAOException, TrackerAlreadyExistException, ExecutionException, InterruptedException { TrackerManagementDAOException, TrackerAlreadyExistException, ExecutionException, InterruptedException {
TraccarGroups traccarGroups = new TraccarGroups(group.getName()); TraccarGroups traccarGroups = new TraccarGroups(group.getName());
client.addGroup(traccarGroups, groupId, tenantId); client.addGroup(traccarGroups, groupId, tenantId);
} }
@Override
public void updateGroup(DeviceGroup group, int groupId, int tenantId) throws public void updateGroup(DeviceGroup group, int groupId, int tenantId) throws
TrackerManagementDAOException, TrackerAlreadyExistException, ExecutionException, InterruptedException { TrackerManagementDAOException, TrackerAlreadyExistException, ExecutionException, InterruptedException {
TraccarGroups traccarGroups = new TraccarGroups(group.getName()); TraccarGroups traccarGroups = new TraccarGroups(group.getName());
client.updateGroup(traccarGroups, groupId, tenantId); client.updateGroup(traccarGroups, groupId, tenantId);
} }
@Override
public void deleteGroup(int groupId, int tenantId) throws public void deleteGroup(int groupId, int tenantId) throws
TrackerManagementDAOException, ExecutionException, InterruptedException { TrackerManagementDAOException, ExecutionException, InterruptedException {
client.deleteGroup(groupId, tenantId); client.deleteGroup(groupId, tenantId);
} }
public static String fetchUserInfo(String userName) throws ExecutionException, InterruptedException { @Override
public String returnUser(String userName) {
TraccarClientFactory client = TraccarClientFactory.getInstance(); 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 { TrackerManagementDAOException {
TraccarClientFactory client = TraccarClientFactory.getInstance(); TraccarClientFactory client = TraccarClientFactory.getInstance();
return client.getTrackerDevice(deviceId, tenantId); return client.getTrackerDevice(deviceId, tenantId);
} }
public static boolean getUserIdofPermissionByDeviceIdNUserId(int deviceId, int userId) throws @Override
public boolean getUserIdofPermissionByDeviceIdNUserId(int deviceId, int userId) throws
TrackerManagementDAOException { TrackerManagementDAOException {
TraccarClientFactory client = TraccarClientFactory.getInstance(); TraccarClientFactory client = TraccarClientFactory.getInstance();
return client.getUserIdofPermissionByDeviceIdNUserId(deviceId, userId); 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(); TraccarClientFactory client = TraccarClientFactory.getInstance();
return client.createUser(traccarUser); client.setPermission(userId, deviceId);
} }
public static String updateUser(TraccarUser traccarUser, int userId) throws @Override
ExecutionException, InterruptedException { public List<TrackerPermissionInfo> getUserIdofPermissionByUserIdNIdList(int userId, List<Integer> NotInDeviceIdList) throws
TrackerManagementDAOException {
TraccarClientFactory client = TraccarClientFactory.getInstance(); 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(); TraccarClientFactory client = TraccarClientFactory.getInstance();
try { client.removePermission(userId, deviceId, removeType);
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 void addTrackerUserDevicePermission(int userId, int deviceId) throws public static String fetchUserInfo(String userName) throws ExecutionException, InterruptedException {
TrackerManagementDAOException, ExecutionException, InterruptedException {
TraccarClientFactory client = TraccarClientFactory.getInstance(); TraccarClientFactory client = TraccarClientFactory.getInstance();
client.setPermission(userId, deviceId); return client.fetchUserInfo(userName);
} }
public static void removeTrackerUserDevicePermission(int userId, int deviceId, int removeType) throws public static String createUser(TraccarUser traccarUser) throws ExecutionException, InterruptedException {
TrackerManagementDAOException, ExecutionException, InterruptedException {
TraccarClientFactory client = TraccarClientFactory.getInstance(); TraccarClientFactory client = TraccarClientFactory.getInstance();
client.removePermission(userId, deviceId, removeType); return client.createUser(traccarUser);
} }
public static List<TrackerPermissionInfo> getUserIdofPermissionByUserIdNIdList(int userId, List<Integer> NotInDeviceIdList) throws public static String updateUser(TraccarUser traccarUser, int userId) throws
TrackerManagementDAOException { ExecutionException, InterruptedException {
TraccarClientFactory client = TraccarClientFactory.getInstance(); TraccarClientFactory client = TraccarClientFactory.getInstance();
return client.getUserIdofPermissionByUserIdNIdList(userId, NotInDeviceIdList); return client.updateUser(traccarUser, userId);
} }
public static String generateRandomString(int len) { public static String generateRandomString(int len) {

Loading…
Cancel
Save