Refactored pagination code

revert-70aa11f8
harshanl 9 years ago
parent 67e1bfb012
commit a61fe6171a

@ -27,10 +27,15 @@ public class PaginationRequest {
private int rowCount;
private String owner;
private String status;
private String type;
private String deviceType;
private String deviceName;
private String ownership;
public PaginationRequest(int start, int rowCount) {
this.startIndex = start;
this.rowCount = rowCount;
}
public int getStartIndex() {
return startIndex;
}
@ -63,12 +68,12 @@ public class PaginationRequest {
this.status = status;
}
public String getType() {
return type;
public String getDeviceType() {
return deviceType;
}
public void setType(String type) {
this.type = type;
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public String getDeviceName() {

@ -38,7 +38,7 @@ public interface DeviceDAO {
* @return returns the device count of given type.
* @throws DeviceManagementDAOException
*/
int getDeviceCount(String type, int tenantId) throws DeviceManagementDAOException;
int getDeviceCountByType(String type, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to get the device count by user.
@ -68,7 +68,7 @@ public interface DeviceDAO {
* @return returns the device count of given status.
* @throws DeviceManagementDAOException
*/
int getDeviceCount(EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException;
int getDeviceCountByStatus(String status, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to get the device count by ownership.
@ -78,7 +78,7 @@ public interface DeviceDAO {
* @return returns the device count of given ownership.
* @throws DeviceManagementDAOException
*/
int getDeviceCountByOwnership(EnrolmentInfo.OwnerShip ownerShip, int tenantId) throws DeviceManagementDAOException;
int getDeviceCountByOwnership(String ownerShip, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to add a device.
@ -174,13 +174,12 @@ public interface DeviceDAO {
/**
* This method is used to retrieve the devices of a given tenant and type as a paginated result.
*
* @param type device type.
* @param request PaginationRequest object holding the data for pagination
* @param request PaginationRequest object holding the data for pagination and search.
* @param tenantId tenant id.
* @return returns paginated list of devices of provided type.
* @throws DeviceManagementDAOException
*/
List<Device> getDevices(String type, PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
List<Device> getDevicesByType(PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve all the devices of a given tenant and device type.
@ -203,15 +202,14 @@ public interface DeviceDAO {
List<Device> getDevicesOfUser(String username, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve devices of a given user.
* This method is used to retrieve devices of a given user as a paginated result.
*
* @param username user name.
* @param request PaginationRequest object holding the data for pagination
* @param request PaginationRequest object holding the data for pagination and search data.
* @param tenantId tenant id.
* @return returns paginated list of devices in which owner matches (search) with given username.
* @throws DeviceManagementDAOException
*/
List<Device> getDevicesOfUser(String username, PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
List<Device> getDevicesOfUser(PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the device count of a given tenant.
@ -251,15 +249,14 @@ public interface DeviceDAO {
List<Device> getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve devices of a given device name.
* This method is used to retrieve devices of a given device name as a paginated result.
*
* @param deviceName device name.
* @param request PaginationRequest object holding the data for pagination
* @param request PaginationRequest object holding the data for pagination and device search info.
* @param tenantId tenant id.
* @return returns paginated list of devices which name matches (search) given device-name.
* @throws DeviceManagementDAOException
*/
List<Device> getDevicesByName(String deviceName, PaginationRequest request, int tenantId)
List<Device> getDevicesByName(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException;
/**
@ -320,27 +317,25 @@ public interface DeviceDAO {
List<Device> getDevicesByStatus(EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve devices of a given ownership.
* This method is used to retrieve devices of a given ownership as a paginated result.
*
* @param ownerShip Ownership of devices.
* @param request PaginationRequest object holding the data for pagination
* @param request PaginationRequest object holding the data for pagination and device search.
* @param tenantId tenant id.
* @return returns list of devices of given ownership.
* @throws DeviceManagementDAOException
*/
List<Device> getDevicesByOwnership(EnrolmentInfo.OwnerShip ownerShip, PaginationRequest request, int tenantId)
List<Device> getDevicesByOwnership(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException;
/**
* This method is used to retrieve devices of a given enrollment status.
* This method is used to retrieve devices of a given enrollment status as a paginated result
*
* @param status enrollment status.
* @param request PaginationRequest object holding the data for pagination
* @param tenantId tenant id.
* @return returns paginated list of devices of given status.
* @throws DeviceManagementDAOException
*/
List<Device> getDevicesByStatus(EnrolmentInfo.Status status, PaginationRequest request, int tenantId)
List<Device> getDevicesByStatus(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException;
/**

@ -368,7 +368,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
String deviceType = request.getType();
String deviceType = request.getDeviceType();
boolean isDeviceTypeProvided = false;
String deviceName = request.getDeviceName();
boolean isDeviceNameProvided = false;
@ -415,7 +415,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
stmt.setInt(1, tenantId);
int paramIdx = 2;
if (isDeviceTypeProvided) {
stmt.setString(paramIdx++, request.getType());
stmt.setString(paramIdx++, request.getDeviceType());
}
if (isDeviceNameProvided) {
stmt.setString(paramIdx++, request.getDeviceName() + "%");
@ -444,7 +444,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
@Override
public int getDeviceCount(String type, int tenantId) throws DeviceManagementDAOException {
public int getDeviceCountByType(String type, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
@ -478,11 +478,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
try {
conn = this.getConnection();
String sql = "SELECT COUNT(e1.DEVICE_ID) AS DEVICE_COUNT FROM DM_DEVICE d, (SELECT e.DEVICE_ID " +
"FROM DM_ENROLMENT e WHERE e.TENANT_ID = ? AND e.OWNER LIKE ?) " +
"FROM DM_ENROLMENT e WHERE e.TENANT_ID = ? AND e.OWNER = ?) " +
"e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID AND t.ID = d.DEVICE_TYPE_ID";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, username + "%");
stmt.setString(2, username);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
@ -526,7 +526,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
@Override
public int getDeviceCountByOwnership(EnrolmentInfo.OwnerShip ownerShip, int tenantId) throws DeviceManagementDAOException {
public int getDeviceCountByOwnership(String ownerShip, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
int deviceCount = 0;
@ -537,7 +537,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
"DM_DEVICE_TYPE t WHERE d.ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, ownerShip.toString());
stmt.setString(2, ownerShip);
stmt.setInt(3, tenantId);
ResultSet rs = stmt.executeQuery();
@ -554,7 +554,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
@Override
public int getDeviceCount(Status status, int tenantId) throws DeviceManagementDAOException {
public int getDeviceCountByStatus(String status, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
int deviceCount = 0;
@ -565,7 +565,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
"DM_DEVICE_TYPE t WHERE d.ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setString(2, status);
stmt.setInt(3, tenantId);
ResultSet rs = stmt.executeQuery();

@ -19,7 +19,6 @@
package org.wso2.carbon.device.mgt.core.dao.impl.device;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
@ -45,7 +44,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Device> devices = null;
String deviceType = request.getType();
String deviceType = request.getDeviceType();
boolean isDeviceTypeProvided = false;
String deviceName = request.getDeviceName();
boolean isDeviceNameProvided = false;
@ -98,7 +97,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
stmt.setInt(1, tenantId);
int paramIdx = 2;
if (isDeviceTypeProvided) {
stmt.setString(paramIdx++, request.getType());
stmt.setString(paramIdx++, request.getDeviceType());
}
if (isDeviceNameProvided) {
stmt.setString(paramIdx++, request.getDeviceName() + "%");
@ -131,7 +130,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevices(String type, PaginationRequest request, int tenantId)
public List<Device> getDevicesByType(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
@ -146,7 +145,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
"DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " +
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, type);
stmt.setString(1, request.getDeviceType());
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
@ -158,7 +157,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + request.getDeviceType() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -166,7 +165,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesOfUser(String username, PaginationRequest request, int tenantId)
public List<Device> getDevicesOfUser(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
@ -177,11 +176,11 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
" e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DEVICE_IDENTIFICATION, t.NAME " +
"AS DEVICE_TYPE FROM DM_DEVICE d, (SELECT e.OWNER, e.OWNERSHIP, e.ID AS ENROLMENT_ID, " +
"e.DEVICE_ID, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE " +
"e.TENANT_ID = ? AND e.OWNER LIKE ?) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " +
"e.TENANT_ID = ? AND e.OWNER = ?) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " +
"AND t.ID = d.DEVICE_TYPE_ID LIMIT ?,?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, username + "%");
stmt.setString(2, request.getOwner());
stmt.setInt(3, request.getStartIndex());
stmt.setInt(4, request.getRowCount());
ResultSet rs = stmt.executeQuery();
@ -192,7 +191,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices belongs to '" +
username + "'", e);
request.getOwner() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
@ -200,7 +199,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesByName(String deviceName, PaginationRequest request, int tenantId)
public List<Device> getDevicesByName(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
@ -214,7 +213,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " +
"WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceName + "%");
stmt.setString(1, request.getDeviceName() + "%");
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
@ -227,7 +226,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " +
"'" + deviceName + "'", e);
"'" + request.getDeviceName() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
@ -235,8 +234,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesByOwnership(EnrolmentInfo.OwnerShip ownerShip, PaginationRequest request,
int tenantId) throws DeviceManagementDAOException {
public List<Device> getDevicesByOwnership(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
@ -250,7 +249,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
"WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? LIMIT ?,?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, ownerShip.toString());
stmt.setString(2, request.getOwnership());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
@ -262,7 +261,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to ownership " +
"'" + ownerShip + "'", e);
"'" + request.getOwnership() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
@ -270,8 +269,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status, PaginationRequest request,
int tenantId) throws DeviceManagementDAOException {
public List<Device> getDevicesByStatus(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
@ -285,7 +284,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
"WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? LIMIT ?,?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setString(2, request.getStatus());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
@ -297,7 +296,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " +
"'" + status + "'", e);
"'" + request.getStatus() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}

@ -19,7 +19,6 @@
package org.wso2.carbon.device.mgt.core.dao.impl.device;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
@ -46,7 +45,7 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Device> devices = null;
String deviceType = request.getType();
String deviceType = request.getDeviceType();
boolean isDeviceTypeProvided = false;
String deviceName = request.getDeviceName();
boolean isDeviceNameProvided = false;
@ -99,7 +98,7 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
stmt.setInt(1, tenantId);
int paramIdx = 2;
if (isDeviceTypeProvided) {
stmt.setString(paramIdx++, request.getType());
stmt.setString(paramIdx++, request.getDeviceType());
}
if (isDeviceNameProvided) {
stmt.setString(paramIdx++, request.getDeviceName() + "%");
@ -132,7 +131,7 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevices(String type, PaginationRequest request, int tenantId)
public List<Device> getDevicesByType(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
@ -147,7 +146,7 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
"DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " +
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setString(1, type);
stmt.setString(1, request.getDeviceType());
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
@ -159,7 +158,7 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + request.getDeviceType() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -167,7 +166,7 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesOfUser(String username, PaginationRequest request, int tenantId)
public List<Device> getDevicesOfUser(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
@ -178,11 +177,11 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
" e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DEVICE_IDENTIFICATION, t.NAME " +
"AS DEVICE_TYPE FROM DM_DEVICE d, (SELECT e.OWNER, e.OWNERSHIP, e.ID AS ENROLMENT_ID, " +
"e.DEVICE_ID, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE " +
"e.TENANT_ID = ? AND e.OWNER LIKE ?) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " +
"e.TENANT_ID = ? AND e.OWNER = ?) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " +
"AND t.ID = d.DEVICE_TYPE_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, username + "%");
stmt.setString(2, request.getOwner());
stmt.setInt(3, request.getStartIndex());
stmt.setInt(4, request.getRowCount());
ResultSet rs = stmt.executeQuery();
@ -193,7 +192,7 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices belongs to '" +
username + "'", e);
request.getOwner() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
@ -201,23 +200,22 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status, PaginationRequest request,
int tenantId) throws DeviceManagementDAOException {
public List<Device> getDevicesByName(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " +
"d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " +
"e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e " +
"WHERE TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? OFFSET ? ROWS" +
" FETCH NEXT ? ROWS ONLY";
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, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " +
"WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setString(1, request.getDeviceName() + "%");
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
@ -228,8 +226,8 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " +
"'" + status + "'", e);
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " +
"'" + request.getDeviceName() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
@ -237,22 +235,23 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesByName(String deviceName, PaginationRequest request, int tenantId)
public List<Device> getDevicesByOwnership(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
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, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " +
"WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " +
"d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " +
"e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e " +
"WHERE TENANT_ID = ? AND OWNERSHIP = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? OFFSET ? " +
"ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceName + "%");
stmt.setInt(2, tenantId);
stmt.setInt(1, tenantId);
stmt.setString(2, request.getOwnership());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
@ -263,8 +262,8 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " +
"'" + deviceName + "'", e);
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to ownership " +
"'" + request.getOwnership() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
@ -272,8 +271,8 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesByOwnership(EnrolmentInfo.OwnerShip ownerShip, PaginationRequest request,
int tenantId) throws DeviceManagementDAOException {
public List<Device> getDevicesByStatus(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
@ -283,12 +282,12 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
"d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " +
"e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e " +
"WHERE TENANT_ID = ? AND OWNERSHIP = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? OFFSET ? " +
"ROWS FETCH NEXT ? ROWS ONLY";
"WHERE TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? OFFSET ? ROWS" +
" FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, ownerShip.toString());
stmt.setString(2, request.getStatus());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
@ -299,8 +298,8 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to ownership " +
"'" + ownerShip + "'", e);
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " +
"'" + request.getStatus() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}

@ -19,7 +19,6 @@
package org.wso2.carbon.device.mgt.core.dao.impl.device;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
@ -45,7 +44,7 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Device> devices = null;
String deviceType = request.getType();
String deviceType = request.getDeviceType();
boolean isDeviceTypeProvided = false;
String deviceName = request.getDeviceName();
boolean isDeviceNameProvided = false;
@ -98,7 +97,7 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
stmt.setInt(1, tenantId);
int paramIdx = 2;
if (isDeviceTypeProvided) {
stmt.setString(paramIdx++, request.getType());
stmt.setString(paramIdx++, request.getDeviceType());
}
if (isDeviceNameProvided) {
stmt.setString(paramIdx++, request.getDeviceName() + "%");
@ -113,8 +112,8 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
if (isStatusProvided) {
stmt.setString(paramIdx++, request.getStatus());
}
stmt.setInt(paramIdx, request.getRowCount());
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx++, request.getRowCount());
stmt.setInt(paramIdx, request.getStartIndex());
rs = stmt.executeQuery();
devices = new ArrayList<>();
while (rs.next()) {
@ -131,7 +130,7 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevices(String type, PaginationRequest request, int tenantId)
public List<Device> getDevicesByType(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
@ -146,7 +145,7 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
"DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " +
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, type);
stmt.setString(1, request.getDeviceType());
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getRowCount());
@ -158,7 +157,7 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + request.getDeviceType() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -166,7 +165,7 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesOfUser(String username, PaginationRequest request, int tenantId)
public List<Device> getDevicesOfUser(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
@ -177,11 +176,11 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
" e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DEVICE_IDENTIFICATION, t.NAME " +
"AS DEVICE_TYPE FROM DM_DEVICE d, (SELECT e.OWNER, e.OWNERSHIP, e.ID AS ENROLMENT_ID, " +
"e.DEVICE_ID, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE " +
"e.TENANT_ID = ? AND e.OWNER LIKE ?) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " +
"e.TENANT_ID = ? AND e.OWNER = ?) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " +
"AND t.ID = d.DEVICE_TYPE_ID LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, username + "%");
stmt.setString(2, request.getOwner());
stmt.setInt(3, request.getRowCount());
stmt.setInt(4, request.getStartIndex());
ResultSet rs = stmt.executeQuery();
@ -192,7 +191,7 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices belongs to '" +
username + "'", e);
request.getOwner() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
@ -200,22 +199,22 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status, PaginationRequest request,
int tenantId) throws DeviceManagementDAOException {
public List<Device> getDevicesByName(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " +
"d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " +
"e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e " +
"WHERE TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? LIMIT ? OFFSET ?";
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, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " +
"WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setString(1, request.getDeviceName() + "%");
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getRowCount());
stmt.setInt(5, request.getStartIndex());
@ -226,8 +225,8 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " +
"'" + status + "'", e);
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " +
"'" + request.getDeviceName() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
@ -235,22 +234,22 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesByName(String deviceName, PaginationRequest request, int tenantId)
public List<Device> getDevicesByOwnership(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
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, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " +
"WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? OFFSET ?";
String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " +
"d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " +
"e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e " +
"WHERE TENANT_ID = ? AND OWNERSHIP = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceName + "%");
stmt.setInt(2, tenantId);
stmt.setInt(1, tenantId);
stmt.setString(2, request.getOwnership());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getRowCount());
stmt.setInt(5, request.getStartIndex());
@ -261,8 +260,8 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " +
"'" + deviceName + "'", e);
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to ownership " +
"'" + request.getOwnership() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
@ -270,8 +269,8 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesByOwnership(EnrolmentInfo.OwnerShip ownerShip, PaginationRequest request,
int tenantId) throws DeviceManagementDAOException {
public List<Device> getDevicesByStatus(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
@ -281,11 +280,11 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
"d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " +
"e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e " +
"WHERE TENANT_ID = ? AND OWNERSHIP = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, ownerShip.toString());
stmt.setString(2, request.getStatus());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getRowCount());
stmt.setInt(5, request.getStartIndex());
@ -296,8 +295,8 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to ownership " +
"'" + ownerShip + "'", e);
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " +
"'" + request.getStatus() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}

@ -19,7 +19,6 @@
package org.wso2.carbon.device.mgt.core.dao.impl.device;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
@ -45,7 +44,7 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Device> devices = null;
String deviceType = request.getType();
String deviceType = request.getDeviceType();
boolean isDeviceTypeProvided = false;
String deviceName = request.getDeviceName();
boolean isDeviceNameProvided = false;
@ -98,7 +97,7 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
stmt.setInt(1, tenantId);
int paramIdx = 2;
if (isDeviceTypeProvided) {
stmt.setString(paramIdx++, request.getType());
stmt.setString(paramIdx++, request.getDeviceType());
}
if (isDeviceNameProvided) {
stmt.setString(paramIdx++, request.getDeviceName() + "%");
@ -131,7 +130,7 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevices(String type, PaginationRequest request, int tenantId)
public List<Device> getDevicesByType(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
@ -146,7 +145,7 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
"DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " +
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setString(1, type);
stmt.setString(1, request.getDeviceType());
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
@ -158,7 +157,7 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + request.getDeviceType() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -166,7 +165,7 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesOfUser(String username, PaginationRequest request, int tenantId)
public List<Device> getDevicesOfUser(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
@ -177,11 +176,11 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
" e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DEVICE_IDENTIFICATION, t.NAME " +
"AS DEVICE_TYPE FROM DM_DEVICE d, (SELECT e.OWNER, e.OWNERSHIP, e.ID AS ENROLMENT_ID, " +
"e.DEVICE_ID, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE " +
"e.TENANT_ID = ? AND e.OWNER LIKE ?) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " +
"e.TENANT_ID = ? AND e.OWNER = ?) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " +
"AND t.ID = d.DEVICE_TYPE_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, username + "%");
stmt.setString(2, request.getOwner());
stmt.setInt(3, request.getStartIndex());
stmt.setInt(4, request.getRowCount());
ResultSet rs = stmt.executeQuery();
@ -192,7 +191,7 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices belongs to '" +
username + "'", e);
request.getOwner() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
@ -200,23 +199,22 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status, PaginationRequest request,
int tenantId) throws DeviceManagementDAOException {
public List<Device> getDevicesByName(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " +
"d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " +
"e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e " +
"WHERE TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? OFFSET ? ROWS" +
" FETCH NEXT ? ROWS ONLY";
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, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " +
"WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setString(1, request.getDeviceName() + "%");
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
@ -227,8 +225,8 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " +
"'" + status + "'", e);
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " +
"'" + request.getDeviceName() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
@ -236,22 +234,23 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesByName(String deviceName, PaginationRequest request, int tenantId)
public List<Device> getDevicesByOwnership(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
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, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " +
"WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, t.NAME AS DEVICE_TYPE, " +
"d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " +
"e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e " +
"WHERE TENANT_ID = ? AND OWNERSHIP = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? OFFSET ? " +
"ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceName + "%");
stmt.setInt(2, tenantId);
stmt.setInt(1, tenantId);
stmt.setString(2, request.getOwnership());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
@ -262,8 +261,8 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " +
"'" + deviceName + "'", e);
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to ownership " +
"'" + request.getOwnership() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
@ -271,8 +270,8 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
}
@Override
public List<Device> getDevicesByOwnership(EnrolmentInfo.OwnerShip ownerShip, PaginationRequest request,
int tenantId) throws DeviceManagementDAOException {
public List<Device> getDevicesByStatus(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
@ -282,12 +281,12 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
"d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " +
"e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e " +
"WHERE TENANT_ID = ? AND OWNERSHIP = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? OFFSET ? " +
"ROWS FETCH NEXT ? ROWS ONLY";
"WHERE TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE DEVICE_ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? OFFSET ? ROWS" +
" FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, ownerShip.toString());
stmt.setString(2, request.getStatus());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
@ -298,8 +297,8 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to ownership " +
"'" + ownerShip + "'", e);
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " +
"'" + request.getStatus() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}

@ -42,13 +42,12 @@ public interface DeviceManagementProviderService extends OperationManager {
/**
* Method to retrieve all the devices with pagination support.
*
* @param deviceType Device platform
* @param request PaginationRequest object holding the data for pagination
* @return PaginationResult - Result including the required parameters necessary to do pagination.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the
* devices.
*/
PaginationResult getAllDevices(String deviceType, PaginationRequest request) throws DeviceManagementException;
PaginationResult getDevicesByType(PaginationRequest request) throws DeviceManagementException;
/**
* Method to retrieve all the devices with pagination support.
@ -79,25 +78,22 @@ public interface DeviceManagementProviderService extends OperationManager {
/**
* Method to get the list of devices owned by an user with paging information.
*
* @param userName Username of the user
* @param request PaginationRequest object holding the data for pagination
* @return List of devices owned by a particular user along with the required parameters necessary to do pagination.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the
* device list
*/
PaginationResult getDevicesOfUser(String userName, PaginationRequest request) throws DeviceManagementException;
PaginationResult getDevicesOfUser(PaginationRequest request) throws DeviceManagementException;
/**
* Method to get the list of devices filtered by the ownership with paging information.
*
* @param ownerShip Ownership type of devices
* @param request PaginationRequest object holding the data for pagination
* @return List of devices owned by a particular user along with the required parameters necessary to do pagination.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the
* device list
*/
PaginationResult getDevicesByOwnership(EnrolmentInfo.OwnerShip ownerShip, PaginationRequest request)
throws DeviceManagementException;
PaginationResult getDevicesByOwnership(PaginationRequest request) throws DeviceManagementException;
/**
* Method to get the list of devices owned by an user.
@ -140,13 +136,12 @@ public interface DeviceManagementProviderService extends OperationManager {
/**
* This method is used to retrieve list of devices that matches with the given device name with paging information.
*
* @param deviceName name of the device
* @param request PaginationRequest object holding the data for pagination
* @return List of devices in given status along with the required parameters necessary to do pagination.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the
* device list
*/
PaginationResult getDevicesByName(String deviceName, PaginationRequest request) throws DeviceManagementException;
PaginationResult getDevicesByName(PaginationRequest request) throws DeviceManagementException;
void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status active) throws DeviceManagementException;
@ -162,13 +157,12 @@ public interface DeviceManagementProviderService extends OperationManager {
/**
* This method is used to retrieve list of devices based on the device status with paging information.
*
* @param status Device status
* @param request PaginationRequest object holding the data for pagination
* @return List of devices in given status along with the required parameters necessary to do pagination.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the
* device list
*/
PaginationResult getDevicesByStatus(EnrolmentInfo.Status status, PaginationRequest request) throws DeviceManagementException;
PaginationResult getDevicesByStatus(PaginationRequest request) throws DeviceManagementException;
License getLicense(String deviceType, String languageCode) throws DeviceManagementException;

@ -21,7 +21,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.*;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration;
import org.wso2.carbon.device.mgt.common.license.mgt.License;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException;
@ -394,19 +393,20 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public PaginationResult getAllDevices(String deviceType, PaginationRequest request) throws DeviceManagementException {
public PaginationResult getDevicesByType(PaginationRequest request) throws DeviceManagementException {
PaginationResult paginationResult = new PaginationResult();
List<Device> devices = new ArrayList<>();
List<Device> allDevices = new ArrayList<>();
int count = 0;
int tenantId = this.getTenantId();
String deviceType = request.getDeviceType();
try {
DeviceManagementDAOFactory.openConnection();
allDevices = deviceDAO.getDevices(deviceType, request, tenantId);
count = deviceDAO.getDeviceCount(deviceType, tenantId);
allDevices = deviceDAO.getDevices(request, tenantId);
count = deviceDAO.getDeviceCountByType(deviceType, tenantId);
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " +
"the current tenant", e);
"the current tenant of type " + deviceType, e);
} catch (SQLException e) {
throw new DeviceManagementException("Error occurred while opening a connection to the data source", e);
} finally {
@ -962,16 +962,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public PaginationResult getDevicesOfUser(String username, PaginationRequest request)
public PaginationResult getDevicesOfUser(PaginationRequest request)
throws DeviceManagementException {
PaginationResult result = new PaginationResult();
int deviceCount = 0;
int tenantId = this.getTenantId();
String username = request.getOwner();
List<Device> devices = new ArrayList<>();
List<Device> userDevices = new ArrayList<>();
try {
DeviceManagementDAOFactory.openConnection();
userDevices = deviceDAO.getDevicesOfUser(username, request, tenantId);
userDevices = deviceDAO.getDevicesOfUser(request, tenantId);
deviceCount = deviceDAO.getDeviceCountByUser(username, tenantId);
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while retrieving the list of devices that " +
@ -1007,17 +1008,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public PaginationResult getDevicesByOwnership(EnrolmentInfo.OwnerShip ownerShip,
PaginationRequest request)
public PaginationResult getDevicesByOwnership(PaginationRequest request)
throws DeviceManagementException {
PaginationResult result = new PaginationResult();
List<Device> devices = new ArrayList<>();
List<Device> allDevices;
int deviceCount = 0;
int tenantId = this.getTenantId();
String ownerShip = request.getOwnership();
try {
DeviceManagementDAOFactory.openConnection();
allDevices = deviceDAO.getDevicesByOwnership(ownerShip, request, tenantId);
allDevices = deviceDAO.getDevicesByOwnership(request, tenantId);
deviceCount = deviceDAO.getDeviceCountByOwnership(ownerShip, tenantId);
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException(
@ -1121,15 +1122,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public PaginationResult getDevicesByName(String deviceName, PaginationRequest request)
public PaginationResult getDevicesByName(PaginationRequest request)
throws DeviceManagementException {
PaginationResult result = new PaginationResult();
int tenantId = this.getTenantId();
List<Device> devices = new ArrayList<>();
List<Device> allDevices = new ArrayList<>();
String deviceName = request.getDeviceName();
try {
DeviceManagementDAOFactory.openConnection();
allDevices = deviceDAO.getDevicesByName(deviceName, request, tenantId);
allDevices = deviceDAO.getDevicesByName(request, tenantId);
int deviceCount = deviceDAO.getDeviceCountByName(deviceName, tenantId);
result.setRecordsTotal(deviceCount);
result.setRecordsFiltered(deviceCount);
@ -1221,16 +1223,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public PaginationResult getDevicesByStatus(EnrolmentInfo.Status status, PaginationRequest request)
public PaginationResult getDevicesByStatus(PaginationRequest request)
throws DeviceManagementException {
PaginationResult result = new PaginationResult();
List<Device> devices = new ArrayList<>();
List<Device> allDevices = new ArrayList<>();
int tenantId = this.getTenantId();
String status = request.getStatus();
try {
DeviceManagementDAOFactory.openConnection();
allDevices = deviceDAO.getDevicesByStatus(status, request, tenantId);
int deviceCount = deviceDAO.getDeviceCount(status, tenantId);
allDevices = deviceDAO.getDevicesByStatus(request, tenantId);
int deviceCount = deviceDAO.getDeviceCountByStatus(status, tenantId);
result.setRecordsTotal(deviceCount);
result.setRecordsFiltered(deviceCount);
} catch (DeviceManagementDAOException e) {

Loading…
Cancel
Save