mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Adding H2/MSSQL/ORACLE/POSTGRESQL support for GET /admin/devices API
This commit is contained in:
parent
07a4a65c7c
commit
0bd2a46690
@ -272,10 +272,11 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
||||
throw new UnexpectedServerErrorException(
|
||||
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
||||
}
|
||||
if (devices == null) {
|
||||
if (devices == null || devices.size() == 0) {
|
||||
return Response.status(Response.Status.NOT_FOUND).entity("It is likely that no device is found upon " +
|
||||
"the provided type and id").build();
|
||||
"the provided search filters").build();
|
||||
}
|
||||
|
||||
return Response.status(Response.Status.OK).entity(devices).build();
|
||||
}
|
||||
|
||||
|
||||
@ -56,30 +56,31 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe
|
||||
int currentTenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||
if (MultitenantConstants.SUPER_TENANT_ID != currentTenantId) {
|
||||
throw new UnauthorizedAccessException(
|
||||
new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage(
|
||||
"Current logged in user is not authorized to perform this operation").build());
|
||||
new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage(
|
||||
"Current logged in user is not authorized to perform this operation").build());
|
||||
}
|
||||
PrivilegedCarbonContext.startTenantFlow();
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain);
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(DeviceMgtAPIUtils.getTenantId(tenantDomain));
|
||||
|
||||
List<Device> devices = DeviceMgtAPIUtils.getDeviceManagementService().
|
||||
getDevicesByNameAndType(name, type, offset, limit);
|
||||
if (devices == null) {
|
||||
getDevicesByNameAndType(name, type, offset, limit);
|
||||
if (devices == null || devices.size() == 0) {
|
||||
return Response.status(Response.Status.NOT_FOUND).entity("No device, which carries the name '" +
|
||||
name + "', is currently enrolled in the system").build();
|
||||
name + "', is currently enrolled in the system").build();
|
||||
}
|
||||
|
||||
// setting up paginated result
|
||||
DeviceList deviceList = new DeviceList();
|
||||
deviceList.setCount(devices.size());
|
||||
deviceList.setList(devices);
|
||||
deviceList.setCount(devices.size());
|
||||
|
||||
return Response.status(Response.Status.OK).entity(deviceList).build();
|
||||
} catch (DeviceManagementException e) {
|
||||
String msg = "Error occurred at server side while fetching device list.";
|
||||
log.error(msg, e);
|
||||
throw new UnexpectedServerErrorException(
|
||||
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
||||
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build());
|
||||
} finally {
|
||||
PrivilegedCarbonContext.endTenantFlow();
|
||||
}
|
||||
|
||||
@ -252,12 +252,13 @@ public class DeviceMgtAPIUtils {
|
||||
RealmService realmService =
|
||||
(RealmService) PrivilegedCarbonContext.getThreadLocalCarbonContext().getOSGiService(RealmService.class, null);
|
||||
if (realmService == null) {
|
||||
throw new IllegalStateException("");
|
||||
throw new IllegalStateException("Realm service has not been initialized.");
|
||||
}
|
||||
try {
|
||||
return realmService.getTenantManager().getTenantId(tenantDomain);
|
||||
} catch (UserStoreException e) {
|
||||
throw new DeviceManagementException("");
|
||||
throw new DeviceManagementException("Error occured while trying to " +
|
||||
"obtain tenant id of currently logged in user");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -259,7 +259,8 @@ public interface DeviceDAO {
|
||||
* @return returns list of devices.
|
||||
* @throws DeviceManagementDAOException
|
||||
*/
|
||||
List<Device> getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit) throws DeviceManagementDAOException;
|
||||
List<Device> getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit)
|
||||
throws DeviceManagementDAOException;
|
||||
|
||||
/**
|
||||
* This method is used to retrieve devices of a given device name as a paginated result.
|
||||
|
||||
@ -619,71 +619,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
||||
return deviceCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of devices that matches with the given device name.
|
||||
*
|
||||
* @param deviceName Name of the device.
|
||||
* @param tenantId Id of the current tenant
|
||||
* @return device list
|
||||
* @throws DeviceManagementDAOException
|
||||
*/
|
||||
@Override
|
||||
public List<Device> getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit)
|
||||
throws DeviceManagementDAOException {
|
||||
|
||||
String filteringString = "";
|
||||
if (deviceName != null && !deviceName.isEmpty()) {
|
||||
filteringString = filteringString + " AND d.NAME LIKE ?";
|
||||
}
|
||||
|
||||
if (type != null && !type.isEmpty()) {
|
||||
filteringString = filteringString + " AND t.NAME = ?";
|
||||
}
|
||||
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
List<Device> devices = new ArrayList<>();
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, " +
|
||||
"d.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" + filteringString +
|
||||
") d1 WHERE d1.ID = e.DEVICE_ID LIMIT ?, ?";
|
||||
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, tenantId);
|
||||
|
||||
int i = 1;
|
||||
|
||||
if (deviceName != null && !deviceName.isEmpty()) {
|
||||
stmt.setString(++i, deviceName + "%");
|
||||
}
|
||||
|
||||
if (type != null && !type.isEmpty()) {
|
||||
stmt.setString(++i, type);
|
||||
}
|
||||
|
||||
stmt.setInt(++i, offset);
|
||||
stmt.setInt(++i, limit);
|
||||
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||
devices.add(device);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " +
|
||||
"'" + deviceName + "'", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int addEnrollment(Device device, int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
|
||||
@ -303,6 +303,71 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
||||
return devices;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of devices that matches with the given device name and (or) device type.
|
||||
*
|
||||
* @param deviceName Name of the device.
|
||||
* @param tenantId Id of the current tenant
|
||||
* @return device list
|
||||
* @throws DeviceManagementDAOException
|
||||
*/
|
||||
@Override
|
||||
public List<Device> getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit)
|
||||
throws DeviceManagementDAOException {
|
||||
|
||||
String filteringString = "";
|
||||
if (deviceName != null && !deviceName.isEmpty()) {
|
||||
filteringString = filteringString + " AND d.NAME LIKE ?";
|
||||
}
|
||||
|
||||
if (type != null && !type.isEmpty()) {
|
||||
filteringString = filteringString + " AND t.NAME = ?";
|
||||
}
|
||||
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
List<Device> devices = new ArrayList<>();
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, " +
|
||||
"d.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" + filteringString +
|
||||
") d1 WHERE d1.ID = e.DEVICE_ID LIMIT ?, ?";
|
||||
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, tenantId);
|
||||
|
||||
int i = 1;
|
||||
|
||||
if (deviceName != null && !deviceName.isEmpty()) {
|
||||
stmt.setString(++i, deviceName + "%");
|
||||
}
|
||||
|
||||
if (type != null && !type.isEmpty()) {
|
||||
stmt.setString(++i, type);
|
||||
}
|
||||
|
||||
stmt.setInt(++i, offset);
|
||||
stmt.setInt(++i, limit);
|
||||
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||
devices.add(device);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices corresponding" +
|
||||
"to the mentioned filtering criteria", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
|
||||
@ -310,6 +310,71 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
||||
return devices;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of devices that matches with the given device name and (or) device type.
|
||||
*
|
||||
* @param deviceName Name of the device.
|
||||
* @param tenantId Id of the current tenant
|
||||
* @return device list
|
||||
* @throws DeviceManagementDAOException
|
||||
*/
|
||||
@Override
|
||||
public List<Device> getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit)
|
||||
throws DeviceManagementDAOException {
|
||||
|
||||
String filteringString = "";
|
||||
if (deviceName != null && !deviceName.isEmpty()) {
|
||||
filteringString = filteringString + " AND d.NAME LIKE ?";
|
||||
}
|
||||
|
||||
if (type != null && !type.isEmpty()) {
|
||||
filteringString = filteringString + " AND t.NAME = ?";
|
||||
}
|
||||
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
List<Device> devices = new ArrayList<>();
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT * FROM (SELECT ROWNUM offset, rs.* FROM (SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, " +
|
||||
"d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, " +
|
||||
"e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " +
|
||||
"(SELECT d.ID, d.NAME, d.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM " +
|
||||
"DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" + filteringString +
|
||||
") d1 WHERE d1.ID = e.DEVICE_ID) rs) WHERE offset >= ? AND ROWNUM <= ?";
|
||||
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, tenantId);
|
||||
|
||||
int i = 1;
|
||||
|
||||
if (deviceName != null && !deviceName.isEmpty()) {
|
||||
stmt.setString(++i, deviceName + "%");
|
||||
}
|
||||
|
||||
if (type != null && !type.isEmpty()) {
|
||||
stmt.setString(++i, type);
|
||||
}
|
||||
|
||||
stmt.setInt(++i, offset);
|
||||
stmt.setInt(++i, limit);
|
||||
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||
devices.add(device);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices corresponding" +
|
||||
"to the mentioned filtering criteria", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
|
||||
@ -303,6 +303,71 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
||||
return devices;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of devices that matches with the given device name and (or) device type.
|
||||
*
|
||||
* @param deviceName Name of the device.
|
||||
* @param tenantId Id of the current tenant
|
||||
* @return device list
|
||||
* @throws DeviceManagementDAOException
|
||||
*/
|
||||
@Override
|
||||
public List<Device> getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit)
|
||||
throws DeviceManagementDAOException {
|
||||
|
||||
String filteringString = "";
|
||||
if (deviceName != null && !deviceName.isEmpty()) {
|
||||
filteringString = filteringString + " AND d.NAME LIKE ?";
|
||||
}
|
||||
|
||||
if (type != null && !type.isEmpty()) {
|
||||
filteringString = filteringString + " AND t.NAME = ?";
|
||||
}
|
||||
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
List<Device> devices = new ArrayList<>();
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, " +
|
||||
"d.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" + filteringString +
|
||||
") d1 WHERE d1.ID = e.DEVICE_ID OFFSET ? LIMIT ?";
|
||||
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, tenantId);
|
||||
|
||||
int i = 1;
|
||||
|
||||
if (deviceName != null && !deviceName.isEmpty()) {
|
||||
stmt.setString(++i, deviceName + "%");
|
||||
}
|
||||
|
||||
if (type != null && !type.isEmpty()) {
|
||||
stmt.setString(++i, type);
|
||||
}
|
||||
|
||||
stmt.setInt(++i, offset);
|
||||
stmt.setInt(++i, limit);
|
||||
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||
devices.add(device);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices corresponding" +
|
||||
"to the mentioned filtering criteria", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
|
||||
@ -305,6 +305,71 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
||||
return devices;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of devices that matches with the given device name and (or) device type.
|
||||
*
|
||||
* @param deviceName Name of the device.
|
||||
* @param tenantId Id of the current tenant
|
||||
* @return device list
|
||||
* @throws DeviceManagementDAOException
|
||||
*/
|
||||
@Override
|
||||
public List<Device> getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit)
|
||||
throws DeviceManagementDAOException {
|
||||
|
||||
String filteringString = "";
|
||||
if (deviceName != null && !deviceName.isEmpty()) {
|
||||
filteringString = filteringString + " AND d.NAME LIKE ?";
|
||||
}
|
||||
|
||||
if (type != null && !type.isEmpty()) {
|
||||
filteringString = filteringString + " AND t.NAME = ?";
|
||||
}
|
||||
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
List<Device> devices = new ArrayList<>();
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, " +
|
||||
"d.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" + filteringString +
|
||||
") d1 WHERE d1.ID = e.DEVICE_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
|
||||
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, tenantId);
|
||||
|
||||
int i = 1;
|
||||
|
||||
if (deviceName != null && !deviceName.isEmpty()) {
|
||||
stmt.setString(++i, deviceName + "%");
|
||||
}
|
||||
|
||||
if (type != null && !type.isEmpty()) {
|
||||
stmt.setString(++i, type);
|
||||
}
|
||||
|
||||
stmt.setInt(++i, offset);
|
||||
stmt.setInt(++i, limit);
|
||||
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||
devices.add(device);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices corresponding" +
|
||||
"to the mentioned filtering criteria", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user