revert-70aa11f8
pasinduj 9 years ago
commit 08387e178d

@ -0,0 +1,89 @@
/*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.common;
/**
* This class holds required parameters for a querying a paginated response.
*/
public class PaginationRequest {
private int startIndex;
private int rowCount;
private String owner;
private String status;
private String type;
private String deviceName;
private String ownership;
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public String getOwnership() {
return ownership;
}
public void setOwnership(String ownership) {
this.ownership = ownership;
}
}

@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.common.operation.mgt;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationResult;
import java.util.List; import java.util.List;
@ -52,13 +53,12 @@ public interface OperationManager {
* Method to retrieve all the operations applied to a device with pagination support. * Method to retrieve all the operations applied to a device with pagination support.
* *
* @param deviceId DeviceIdentifier of the device * @param deviceId DeviceIdentifier of the device
* @param index Starting row number * @param request PaginationRequest object holding the data for pagination
* @param limit No of rows to fetch
* @return PaginationResult - Result including the required parameters necessary to do pagination. * @return PaginationResult - Result including the required parameters necessary to do pagination.
* @throws OperationManagementException If some unusual behaviour is observed while fetching the * @throws OperationManagementException If some unusual behaviour is observed while fetching the
* operation list. * operation list.
*/ */
PaginationResult getOperations(DeviceIdentifier deviceId, int index, int limit) throws OperationManagementException; PaginationResult getOperations(DeviceIdentifier deviceId, PaginationRequest request) throws OperationManagementException;
/** /**
* Method to retrieve the list of available operations to a device. * Method to retrieve the list of available operations to a device.

@ -18,11 +18,8 @@
package org.wso2.carbon.device.mgt.core.dao; package org.wso2.carbon.device.mgt.core.dao;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.*;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import java.util.HashMap; import java.util.HashMap;
@ -34,7 +31,7 @@ import java.util.List;
public interface DeviceDAO { public interface DeviceDAO {
/** /**
* This method is used to add a device. * This method is used to get the device count by device-type.
* *
* @param type device type. * @param type device type.
* @param tenantId tenant id. * @param tenantId tenant id.
@ -43,6 +40,46 @@ public interface DeviceDAO {
*/ */
int getDeviceCount(String type, int tenantId) throws DeviceManagementDAOException; int getDeviceCount(String type, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to get the device count by user.
*
* @param username username of the user.
* @param tenantId tenant id.
* @return returns the device count of given user.
* @throws DeviceManagementDAOException
*/
int getDeviceCountByUser(String username, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to get the device count by device name (pattern).
*
* @param deviceName name of the device.
* @param tenantId tenant id.
* @return returns the device count of given user.
* @throws DeviceManagementDAOException
*/
int getDeviceCountByName(String deviceName, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to get the device count by status.
*
* @param status enrollment status.
* @param tenantId tenant id.
* @return returns the device count of given status.
* @throws DeviceManagementDAOException
*/
int getDeviceCount(EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to get the device count by ownership.
*
* @param ownerShip Ownership of devices.
* @param tenantId tenant id.
* @return returns the device count of given ownership.
* @throws DeviceManagementDAOException
*/
int getDeviceCountByOwnership(EnrolmentInfo.OwnerShip ownerShip, int tenantId) throws DeviceManagementDAOException;
/** /**
* This method is used to add a device. * This method is used to add a device.
* *
@ -127,32 +164,30 @@ public interface DeviceDAO {
/** /**
* This method is used to retrieve the devices of a given tenant as a paginated result. * This method is used to retrieve the devices of a given tenant as a paginated result.
* *
* @param index start index of result set. * @param request PaginationRequest object holding the data for pagination
* @param limit number of records to be returned.
* @param tenantId tenant id. * @param tenantId tenant id.
* @return returns a PaginationResult including the requested data. * @return returns paginated list of devices.
* @throws DeviceManagementDAOException * @throws DeviceManagementDAOException
*/ */
PaginationResult getDevices(int index, int limit, int tenantId) throws DeviceManagementDAOException; List<Device> getDevices(PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
/** /**
* This method is used to retrieve the devices of a given tenant and type as a paginated result. * This method is used to retrieve the devices of a given tenant and type as a paginated result.
* *
* @param type device type. * @param type device type.
* @param index start index of result set. * @param request PaginationRequest object holding the data for pagination
* @param limit number of records to be returned.
* @param tenantId tenant id. * @param tenantId tenant id.
* @return returns a PaginationResult including the requested data. * @return returns paginated list of devices of provided type.
* @throws DeviceManagementDAOException * @throws DeviceManagementDAOException
*/ */
PaginationResult getDevices(String type, int index, int limit, int tenantId) throws DeviceManagementDAOException; List<Device> getDevices(String type, PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
/** /**
* This method is used to retrieve all the devices of a given tenant and device type. * This method is used to retrieve all the devices of a given tenant and device type.
* *
* @param type device type. * @param type device type.
* @param tenantId tenant id. * @param tenantId tenant id.
* @return returns list of devices. * @return returns list of devices of provided type.
* @throws DeviceManagementDAOException * @throws DeviceManagementDAOException
*/ */
List<Device> getDevices(String type, int tenantId) throws DeviceManagementDAOException; List<Device> getDevices(String type, int tenantId) throws DeviceManagementDAOException;
@ -162,11 +197,22 @@ public interface DeviceDAO {
* *
* @param username user name. * @param username user name.
* @param tenantId tenant id. * @param tenantId tenant id.
* @return returns list of devices. * @return returns list of devices of given user.
* @throws DeviceManagementDAOException * @throws DeviceManagementDAOException
*/ */
List<Device> getDevicesOfUser(String username, int tenantId) throws DeviceManagementDAOException; List<Device> getDevicesOfUser(String username, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve devices of a given user.
*
* @param username user name.
* @param request PaginationRequest object holding the data for pagination
* @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;
/** /**
* This method is used to retrieve the device count of a given tenant. * This method is used to retrieve the device count of a given tenant.
* *
@ -176,6 +222,16 @@ public interface DeviceDAO {
*/ */
int getDeviceCount(int tenantId) throws DeviceManagementDAOException; int getDeviceCount(int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the device count of a given tenant for the given search terms.
*
* @param request paginated request used to search devices.
* @param tenantId tenant id.
* @return returns the device count.
* @throws DeviceManagementDAOException
*/
int getDeviceCount(PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
/** /**
* This method is used to retrieve the available device types of a given tenant. * This method is used to retrieve the available device types of a given tenant.
* *
@ -194,6 +250,18 @@ public interface DeviceDAO {
*/ */
List<Device> getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException; List<Device> getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve devices of a given device name.
*
* @param deviceName device name.
* @param request PaginationRequest object holding the data for pagination
* @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)
throws DeviceManagementDAOException;
/** /**
* This method is used to add an enrollment information of a given device. * This method is used to add an enrollment information of a given device.
* *
@ -246,11 +314,35 @@ public interface DeviceDAO {
* *
* @param status enrollment status. * @param status enrollment status.
* @param tenantId tenant id. * @param tenantId tenant id.
* @return returns list of devices. * @return returns list of devices of given status.
* @throws DeviceManagementDAOException * @throws DeviceManagementDAOException
*/ */
List<Device> getDevicesByStatus(EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException; List<Device> getDevicesByStatus(EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve devices of a given ownership.
*
* @param ownerShip Ownership of devices.
* @param request PaginationRequest object holding the data for pagination
* @param tenantId tenant id.
* @return returns list of devices of given ownership.
* @throws DeviceManagementDAOException
*/
List<Device> getDevicesByOwnership(EnrolmentInfo.OwnerShip ownerShip, PaginationRequest request, int tenantId)
throws DeviceManagementDAOException;
/**
* This method is used to retrieve devices of a given enrollment status.
*
* @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)
throws DeviceManagementDAOException;
/** /**
* This method is used to retrieve the enrollment id of a given device and status. * This method is used to retrieve the enrollment id of a given device and status.
* *

@ -22,6 +22,7 @@ import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
@ -343,9 +344,12 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
int deviceCount = 0; int deviceCount = 0;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_DEVICE WHERE TENANT_ID = ? "; String sql = "SELECT COUNT(d1.DEVICE_ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID FROM " +
"DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 WHERE " +
"d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
stmt.setInt(2, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT"); deviceCount = rs.getInt("DEVICE_COUNT");
@ -358,6 +362,87 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
return deviceCount; return deviceCount;
} }
@Override
public int getDeviceCount(PaginationRequest request, int tenantId) throws DeviceManagementDAOException {
int deviceCount = 0;
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
String deviceType = request.getType();
boolean isDeviceTypeProvided = false;
String deviceName = request.getDeviceName();
boolean isDeviceNameProvided = false;
String owner = request.getOwner();
boolean isOwnerProvided = false;
String ownership = request.getOwnership();
boolean isOwnershipProvided = false;
String status = request.getStatus();
boolean isStatusProvided = false;
try {
conn = this.getConnection();
String sql = "SELECT COUNT(d1.ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, d.DEVICE_IDENTIFICATION, " +
"t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID " +
"AND d.TENANT_ID = ?";
if (deviceType != null && !deviceType.isEmpty()) {
sql = sql + " AND t.NAME = ?";
isDeviceTypeProvided = true;
}
if (deviceName != null && !deviceName.isEmpty()) {
sql = sql + " AND d.NAME LIKE ?";
isDeviceNameProvided = true;
}
sql = sql + ") d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?";
if (ownership != null && !ownership.isEmpty()) {
sql = sql + " AND e.OWNERSHIP = ?";
isOwnershipProvided = true;
}
if (owner != null && !owner.isEmpty()) {
sql = sql + " AND e.OWNER LIKE ?";
isOwnerProvided = true;
}
if (status != null && !status.isEmpty()) {
sql = sql + " AND e.STATUS = ?";
isStatusProvided = true;
}
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
int paramIdx = 2;
if (isDeviceTypeProvided) {
stmt.setString(paramIdx++, request.getType());
}
if (isDeviceNameProvided) {
stmt.setString(paramIdx++, request.getDeviceName() + "%");
}
stmt.setInt(paramIdx++, tenantId);
if (isOwnershipProvided) {
stmt.setString(paramIdx++, request.getOwnership());
}
if (isOwnerProvided) {
stmt.setString(paramIdx++, request.getOwner() + "%");
}
if (isStatusProvided) {
stmt.setString(paramIdx++, request.getStatus());
}
rs = stmt.executeQuery();
if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving information of all " +
"registered devices", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return deviceCount;
}
@Override @Override
public int getDeviceCount(String type, int tenantId) throws DeviceManagementDAOException { public int getDeviceCount(String type, int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
@ -366,11 +451,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
int deviceCount = 0; int deviceCount = 0;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM DM_DEVICE d, (SELECT t.ID AS TYPE_ID FROM DM_DEVICE_TYPE t " + String sql = "SELECT COUNT(d1.ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d, " +
"WHERE t.NAME = ?) d1 WHERE TYPE_ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ?"; "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 = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, type); stmt.setString(1, type);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT"); deviceCount = rs.getInt("DEVICE_COUNT");
@ -383,6 +470,117 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
return deviceCount; return deviceCount;
} }
@Override
public int getDeviceCountByUser(String username, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
int deviceCount = 0;
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 ?) " +
"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 + "%");
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices belongs to '" +
username + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return deviceCount;
}
@Override
public int getDeviceCountByName(String deviceName, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
int deviceCount = 0;
try {
conn = this.getConnection();
String sql = "SELECT COUNT(d1.ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID 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 d1.ID = e.DEVICE_ID AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceName + "%");
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the device count that matches " +
"'" + deviceName + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return deviceCount;
}
@Override
public int getDeviceCountByOwnership(EnrolmentInfo.OwnerShip ownerShip, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
int deviceCount = 0;
try {
conn = this.getConnection();
String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " +
"TENANT_ID = ? AND OWNERSHIP = ?) e, DM_DEVICE d, " +
"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.setInt(3, tenantId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to ownership " +
"'" + ownerShip + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return deviceCount;
}
@Override
public int getDeviceCount(Status status, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
int deviceCount = 0;
try {
conn = this.getConnection();
String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " +
"TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, " +
"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.setInt(3, tenantId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " +
"'" + status + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return deviceCount;
}
/** /**
* Get the list of devices that matches with the given device name. * Get the list of devices that matches with the given device name.
* *

@ -26,9 +26,7 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import java.io.ByteArrayInputStream; import java.io.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.*; import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -43,6 +41,8 @@ public class ApplicationDAOImpl implements ApplicationDAO {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null;
int applicationId = -1; int applicationId = -1;
try { try {
conn = this.getConnection(); conn = this.getConnection();
@ -58,7 +58,12 @@ public class ApplicationDAOImpl implements ApplicationDAO {
stmt.setString(6, application.getLocationUrl()); stmt.setString(6, application.getLocationUrl());
stmt.setString(7, application.getImageUrl()); stmt.setString(7, application.getImageUrl());
stmt.setInt(8, tenantId); stmt.setInt(8, tenantId);
stmt.setObject(9, application.getAppProperties());
bao = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bao);
oos.writeObject(application.getAppProperties());
stmt.setBytes(9, bao.toByteArray());
stmt.setString(10, application.getApplicationIdentifier()); stmt.setString(10, application.getApplicationIdentifier());
stmt.execute(); stmt.execute();
@ -70,7 +75,23 @@ public class ApplicationDAOImpl implements ApplicationDAO {
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while adding application '" + throw new DeviceManagementDAOException("Error occurred while adding application '" +
application.getName() + "'", e); application.getName() + "'", e);
} catch (IOException e) {
throw new DeviceManagementDAOException("Error occurred while serializing application properties object", e);
} finally { } finally {
if (bao != null) {
try {
bao.close();
} catch (IOException e) {
log.warn("Error occurred while closing ByteArrayOutputStream", e);
}
}
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
log.warn("Error occurred while closing ObjectOutputStream", e);
}
}
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
} }
@ -81,6 +102,8 @@ public class ApplicationDAOImpl implements ApplicationDAO {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs; ResultSet rs;
ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null;
List<Integer> applicationIds = new ArrayList<>(); List<Integer> applicationIds = new ArrayList<>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
@ -99,7 +122,12 @@ public class ApplicationDAOImpl implements ApplicationDAO {
stmt.setString(6, application.getLocationUrl()); stmt.setString(6, application.getLocationUrl());
stmt.setString(7, application.getImageUrl()); stmt.setString(7, application.getImageUrl());
stmt.setInt(8, tenantId); stmt.setInt(8, tenantId);
stmt.setObject(9, application.getAppProperties());
bao = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bao);
oos.writeObject(application.getAppProperties());
stmt.setBytes(9, bao.toByteArray());
stmt.setString(10, application.getApplicationIdentifier()); stmt.setString(10, application.getApplicationIdentifier());
stmt.executeUpdate(); stmt.executeUpdate();
@ -111,7 +139,23 @@ public class ApplicationDAOImpl implements ApplicationDAO {
return applicationIds; return applicationIds;
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while adding bulk application list", e); throw new DeviceManagementDAOException("Error occurred while adding bulk application list", e);
} catch (IOException e) {
throw new DeviceManagementDAOException("Error occurred while serializing application properties object", e);
} finally { } finally {
if (bao != null) {
try {
bao.close();
} catch (IOException e) {
log.warn("Error occurred while closing ByteArrayOutputStream", e);
}
}
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
log.warn("Error occurred while closing ObjectOutputStream", e);
}
}
DeviceManagementDAOUtil.cleanupResources(stmt, null); DeviceManagementDAOUtil.cleanupResources(stmt, null);
} }
} }

@ -71,7 +71,6 @@ public class EnrollmentDAOImpl implements EnrollmentDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
int status = -1; int status = -1;
int rows;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, " + String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, " +
@ -86,12 +85,12 @@ public class EnrollmentDAOImpl implements EnrollmentDAO {
stmt.setString(6, enrolmentInfo.getOwner()); stmt.setString(6, enrolmentInfo.getOwner());
stmt.setInt(7, tenantId); stmt.setInt(7, tenantId);
stmt.setInt(8, enrolmentInfo.getId()); stmt.setInt(8, enrolmentInfo.getId());
rows = stmt.executeUpdate(); stmt.executeUpdate();
if (rows > 0) { rs = stmt.getGeneratedKeys();
if (rs.next()) {
status = 1; status = 1;
} }
return status; return status;
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e); throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e);

@ -20,12 +20,11 @@ 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.Device;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.PaginationResult; 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.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -40,27 +39,82 @@ import java.util.List;
public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
@Override @Override
public PaginationResult getDevices(int index, int limit, int tenantId) public List<Device> getDevices(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
PaginationResult result = new PaginationResult();
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
List<Device> devices = null; List<Device> devices = null;
String deviceType = request.getType();
boolean isDeviceTypeProvided = false;
String deviceName = request.getDeviceName();
boolean isDeviceNameProvided = false;
String owner = request.getOwner();
boolean isOwnerProvided = false;
String ownership = request.getOwnership();
boolean isOwnershipProvided = false;
String status = request.getStatus();
boolean isStatusProvided = false;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + 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, " + "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 AS DEVICE_ID, " + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " +
"d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t " +
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " + "WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?";
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?";
// String sql = "SELECT * FROM DM_DEVICE WHERE TENANT_ID = ? LIMIT ?,?"; //Add the query for device-type
if (deviceType != null && !deviceType.isEmpty()) {
sql = sql + " AND t.NAME = ?";
isDeviceTypeProvided = true;
}
//Add the query for device-name
if (deviceName != null && !deviceName.isEmpty()) {
sql = sql + " AND d.NAME LIKE ?";
isDeviceNameProvided = true;
}
sql = sql + ") d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?";
//Add the query for ownership
if (ownership != null && !ownership.isEmpty()) {
sql = sql + " AND e.OWNERSHIP = ?";
isOwnershipProvided = true;
}
//Add the query for owner
if (owner != null && !owner.isEmpty()) {
sql = sql + " AND e.OWNER LIKE ?";
isOwnerProvided = true;
}
//Add the query for status
if (status != null && !status.isEmpty()) {
sql = sql + " AND e.STATUS = ?";
isStatusProvided = true;
}
sql = sql + " LIMIT ?,?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
stmt.setInt(2, tenantId); int paramIdx = 2;
stmt.setInt(3, index); if (isDeviceTypeProvided) {
stmt.setInt(4, limit); stmt.setString(paramIdx++, request.getType());
}
if (isDeviceNameProvided) {
stmt.setString(paramIdx++, request.getDeviceName() + "%");
}
stmt.setInt(paramIdx++, tenantId);
if (isOwnershipProvided) {
stmt.setString(paramIdx++, request.getOwnership());
}
if (isOwnerProvided) {
stmt.setString(paramIdx++, request.getOwner() + "%");
}
if (isStatusProvided) {
stmt.setString(paramIdx++, request.getStatus());
}
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx, request.getRowCount());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<>(); devices = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
@ -73,17 +127,12 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
int count = this.getDeviceCount(tenantId); return devices;
result.setData(devices);
result.setRecordsFiltered(count);
result.setRecordsTotal(count);
return result;
} }
@Override @Override
public PaginationResult getDevices(String type, int index, int limit, int tenantId) public List<Device> getDevices(String type, PaginationRequest request, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
PaginationResult result = new PaginationResult();
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
@ -96,14 +145,12 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
"d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + "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 ?,?"; "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?";
// String sql = "SELECT * FROM DM_DEVICE d, (SELECT t.ID AS TYPE_ID FROM DM_DEVICE_TYPE t WHERE t.NAME = ?)" +
// " d1 WHERE TYPE_ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ? LIMIT ?,?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, type); stmt.setString(1, type);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
stmt.setInt(4, index); stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, limit); stmt.setInt(5, request.getRowCount());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<>(); devices = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
@ -115,11 +162,146 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
int count = this.getDeviceCount(type, tenantId); return devices;
result.setData(devices); }
result.setRecordsFiltered(count);
result.setRecordsTotal(count); @Override
return result; public List<Device> getDevicesOfUser(String username, PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.DATE_OF_LAST_UPDATE," +
" 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 " +
"AND t.ID = d.DEVICE_TYPE_ID LIMIT ?,?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, username + "%");
stmt.setInt(3, request.getStartIndex());
stmt.setInt(4, request.getRowCount());
ResultSet 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 belongs to '" +
username + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return devices;
}
@Override
public List<Device> getDevicesByName(String deviceName, 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 ?,?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceName + "%");
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
ResultSet 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, null);
}
return devices;
}
@Override
public List<Device> getDevicesByOwnership(EnrolmentInfo.OwnerShip ownerShip, 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 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 ?,?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, ownerShip.toString());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
ResultSet 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 to ownership " +
"'" + ownerShip + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return devices;
}
@Override
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status, 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 ?,?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
ResultSet 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 to status " +
"'" + status + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return devices;
} }
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {

@ -19,12 +19,13 @@
package org.wso2.carbon.device.mgt.core.dao.impl.device; 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.Device;
import org.wso2.carbon.device.mgt.common.PaginationResult; 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.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -39,26 +40,82 @@ import java.util.List;
public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
@Override @Override
public PaginationResult getDevices(int index, int limit, int tenantId) public List<Device> getDevices(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
PaginationResult result = new PaginationResult();
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
List<Device> devices = null; List<Device> devices = null;
String deviceType = request.getType();
boolean isDeviceTypeProvided = false;
String deviceName = request.getDeviceName();
boolean isDeviceNameProvided = false;
String owner = request.getOwner();
boolean isOwnerProvided = false;
String ownership = request.getOwnership();
boolean isOwnershipProvided = false;
String status = request.getStatus();
boolean isStatusProvided = false;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + 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, " + "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 AS DEVICE_ID, " + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " +
"d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t " +
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " + "WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?";
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
//Add the query for device-type
if (deviceType != null && !deviceType.isEmpty()) {
sql = sql + " AND t.NAME = ?";
isDeviceTypeProvided = true;
}
//Add the query for device-name
if (deviceName != null && !deviceName.isEmpty()) {
sql = sql + " AND d.NAME LIKE ?";
isDeviceNameProvided = true;
}
sql = sql + ") d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?";
//Add the query for ownership
if (ownership != null && !ownership.isEmpty()) {
sql = sql + " AND e.OWNERSHIP = ?";
isOwnershipProvided = true;
}
//Add the query for owner
if (owner != null && !owner.isEmpty()) {
sql = sql + " AND e.OWNER LIKE ?";
isOwnerProvided = true;
}
//Add the query for status
if (status != null && !status.isEmpty()) {
sql = sql + " AND e.STATUS = ?";
isStatusProvided = true;
}
sql = sql + " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
stmt.setInt(2, tenantId); int paramIdx = 2;
stmt.setInt(3, index); if (isDeviceTypeProvided) {
stmt.setInt(4, limit); stmt.setString(paramIdx++, request.getType());
}
if (isDeviceNameProvided) {
stmt.setString(paramIdx++, request.getDeviceName() + "%");
}
stmt.setInt(paramIdx++, tenantId);
if (isOwnershipProvided) {
stmt.setString(paramIdx++, request.getOwnership());
}
if (isOwnerProvided) {
stmt.setString(paramIdx++, request.getOwner() + "%");
}
if (isStatusProvided) {
stmt.setString(paramIdx++, request.getStatus());
}
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx, request.getRowCount());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<>(); devices = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
@ -71,17 +128,12 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
int count = this.getDeviceCount(tenantId); return devices;
result.setData(devices);
result.setRecordsFiltered(count);
result.setRecordsTotal(count);
return result;
} }
@Override @Override
public PaginationResult getDevices(String type, int index, int limit, int tenantId) public List<Device> getDevices(String type, PaginationRequest request, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
PaginationResult result = new PaginationResult();
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
@ -98,8 +150,8 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
stmt.setString(1, type); stmt.setString(1, type);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
stmt.setInt(4, index); stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, limit); stmt.setInt(5, request.getRowCount());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<>(); devices = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
@ -111,11 +163,148 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
int count = this.getDeviceCount(type, tenantId); return devices;
result.setData(devices); }
result.setRecordsFiltered(count);
result.setRecordsTotal(count); @Override
return result; public List<Device> getDevicesOfUser(String username, PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.DATE_OF_LAST_UPDATE," +
" 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 " +
"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.setInt(3, request.getStartIndex());
stmt.setInt(4, request.getRowCount());
ResultSet 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 belongs to '" +
username + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return devices;
}
@Override
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status, 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";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
ResultSet 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 to status " +
"'" + status + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return devices;
}
@Override
public List<Device> getDevicesByName(String deviceName, 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";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceName + "%");
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
ResultSet 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, null);
}
return devices;
}
@Override
public List<Device> getDevicesByOwnership(EnrolmentInfo.OwnerShip ownerShip, 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 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.setInt(1, tenantId);
stmt.setString(2, ownerShip.toString());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
ResultSet 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 to ownership " +
"'" + ownerShip + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return devices;
} }
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {

@ -19,12 +19,12 @@
package org.wso2.carbon.device.mgt.core.dao.impl.device; 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.Device;
import org.wso2.carbon.device.mgt.common.PaginationResult; 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.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -39,26 +39,82 @@ import java.util.List;
public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
@Override @Override
public PaginationResult getDevices(int index, int limit, int tenantId) public List<Device> getDevices(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
PaginationResult result = new PaginationResult();
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
List<Device> devices = null; List<Device> devices = null;
String deviceType = request.getType();
boolean isDeviceTypeProvided = false;
String deviceName = request.getDeviceName();
boolean isDeviceNameProvided = false;
String owner = request.getOwner();
boolean isOwnerProvided = false;
String ownership = request.getOwnership();
boolean isOwnershipProvided = false;
String status = request.getStatus();
boolean isStatusProvided = false;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + 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, " + "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 AS DEVICE_ID, " + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " +
"d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t " +
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " + "WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?";
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? OFFSET ?";
//Add the query for device-type
if (deviceType != null && !deviceType.isEmpty()) {
sql = sql + " AND t.NAME = ?";
isDeviceTypeProvided = true;
}
//Add the query for device-name
if (deviceName != null && !deviceName.isEmpty()) {
sql = sql + " AND d.NAME LIKE ?";
isDeviceNameProvided = true;
}
sql = sql + ") d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?";
//Add the query for ownership
if (ownership != null && !ownership.isEmpty()) {
sql = sql + " AND e.OWNERSHIP = ?";
isOwnershipProvided = true;
}
//Add the query for owner
if (owner != null && !owner.isEmpty()) {
sql = sql + " AND e.OWNER LIKE ?";
isOwnerProvided = true;
}
//Add the query for status
if (status != null && !status.isEmpty()) {
sql = sql + " AND e.STATUS = ?";
isStatusProvided = true;
}
sql = sql + " LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
stmt.setInt(2, tenantId); int paramIdx = 2;
stmt.setInt(3, limit); if (isDeviceTypeProvided) {
stmt.setInt(4, index); stmt.setString(paramIdx++, request.getType());
}
if (isDeviceNameProvided) {
stmt.setString(paramIdx++, request.getDeviceName() + "%");
}
stmt.setInt(paramIdx++, tenantId);
if (isOwnershipProvided) {
stmt.setString(paramIdx++, request.getOwnership());
}
if (isOwnerProvided) {
stmt.setString(paramIdx++, request.getOwner() + "%");
}
if (isStatusProvided) {
stmt.setString(paramIdx++, request.getStatus());
}
stmt.setInt(paramIdx, request.getRowCount());
stmt.setInt(paramIdx++, request.getStartIndex());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<>(); devices = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
@ -71,17 +127,12 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
int count = this.getDeviceCount(tenantId); return devices;
result.setData(devices);
result.setRecordsFiltered(count);
result.setRecordsTotal(count);
return result;
} }
@Override @Override
public PaginationResult getDevices(String type, int index, int limit, int tenantId) public List<Device> getDevices(String type, PaginationRequest request, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
PaginationResult result = new PaginationResult();
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
@ -98,8 +149,8 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
stmt.setString(1, type); stmt.setString(1, type);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
stmt.setInt(4, limit); stmt.setInt(4, request.getRowCount());
stmt.setInt(5, index); stmt.setInt(5, request.getStartIndex());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<>(); devices = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
@ -111,11 +162,146 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
int count = this.getDeviceCount(type, tenantId); return devices;
result.setData(devices); }
result.setRecordsFiltered(count);
result.setRecordsTotal(count); @Override
return result; public List<Device> getDevicesOfUser(String username, PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.DATE_OF_LAST_UPDATE," +
" 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 " +
"AND t.ID = d.DEVICE_TYPE_ID LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, username + "%");
stmt.setInt(3, request.getRowCount());
stmt.setInt(4, request.getStartIndex());
ResultSet 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 belongs to '" +
username + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return devices;
}
@Override
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status, 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 ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getRowCount());
stmt.setInt(5, request.getStartIndex());
ResultSet 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 to status " +
"'" + status + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return devices;
}
@Override
public List<Device> getDevicesByName(String deviceName, 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 ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceName + "%");
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getRowCount());
stmt.setInt(5, request.getStartIndex());
ResultSet 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, null);
}
return devices;
}
@Override
public List<Device> getDevicesByOwnership(EnrolmentInfo.OwnerShip ownerShip, 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 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.setInt(1, tenantId);
stmt.setString(2, ownerShip.toString());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getRowCount());
stmt.setInt(5, request.getStartIndex());
ResultSet 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 to ownership " +
"'" + ownerShip + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return devices;
} }
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {

@ -19,12 +19,12 @@
package org.wso2.carbon.device.mgt.core.dao.impl.device; 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.Device;
import org.wso2.carbon.device.mgt.common.PaginationResult; 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.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -39,26 +39,82 @@ import java.util.List;
public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
@Override @Override
public PaginationResult getDevices(int index, int limit, int tenantId) public List<Device> getDevices(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
PaginationResult result = new PaginationResult();
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
List<Device> devices = null; List<Device> devices = null;
String deviceType = request.getType();
boolean isDeviceTypeProvided = false;
String deviceName = request.getDeviceName();
boolean isDeviceNameProvided = false;
String owner = request.getOwner();
boolean isOwnerProvided = false;
String ownership = request.getOwnership();
boolean isOwnershipProvided = false;
String status = request.getStatus();
boolean isStatusProvided = false;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + 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, " + "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 AS DEVICE_ID, " + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " +
"d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t " +
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " + "WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?";
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
//Add the query for device-type
if (deviceType != null && !deviceType.isEmpty()) {
sql = sql + " AND t.NAME = ?";
isDeviceTypeProvided = true;
}
//Add the query for device-name
if (deviceName != null && !deviceName.isEmpty()) {
sql = sql + " AND d.NAME LIKE ?";
isDeviceNameProvided = true;
}
sql = sql + ") d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?";
//Add the query for ownership
if (ownership != null && !ownership.isEmpty()) {
sql = sql + " AND e.OWNERSHIP = ?";
isOwnershipProvided = true;
}
//Add the query for owner
if (owner != null && !owner.isEmpty()) {
sql = sql + " AND e.OWNER LIKE ?";
isOwnerProvided = true;
}
//Add the query for status
if (status != null && !status.isEmpty()) {
sql = sql + " AND e.STATUS = ?";
isStatusProvided = true;
}
sql = sql + " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
stmt.setInt(2, tenantId); int paramIdx = 2;
stmt.setInt(3, index); if (isDeviceTypeProvided) {
stmt.setInt(4, limit); stmt.setString(paramIdx++, request.getType());
}
if (isDeviceNameProvided) {
stmt.setString(paramIdx++, request.getDeviceName() + "%");
}
stmt.setInt(paramIdx++, tenantId);
if (isOwnershipProvided) {
stmt.setString(paramIdx++, request.getOwnership());
}
if (isOwnerProvided) {
stmt.setString(paramIdx++, request.getOwner() + "%");
}
if (isStatusProvided) {
stmt.setString(paramIdx++, request.getStatus());
}
stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx, request.getRowCount());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<>(); devices = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
@ -71,17 +127,12 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
int count = this.getDeviceCount(tenantId); return devices;
result.setData(devices);
result.setRecordsFiltered(count);
result.setRecordsTotal(count);
return result;
} }
@Override @Override
public PaginationResult getDevices(String type, int index, int limit, int tenantId) public List<Device> getDevices(String type, PaginationRequest request, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
PaginationResult result = new PaginationResult();
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
@ -98,8 +149,8 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
stmt.setString(1, type); stmt.setString(1, type);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
stmt.setInt(4, index); stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, limit); stmt.setInt(5, request.getRowCount());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<>(); devices = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
@ -111,11 +162,148 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
int count = this.getDeviceCount(type, tenantId); return devices;
result.setData(devices); }
result.setRecordsFiltered(count);
result.setRecordsTotal(count); @Override
return result; public List<Device> getDevicesOfUser(String username, PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.DATE_OF_LAST_UPDATE," +
" 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 " +
"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.setInt(3, request.getStartIndex());
stmt.setInt(4, request.getRowCount());
ResultSet 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 belongs to '" +
username + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return devices;
}
@Override
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status, 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";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
ResultSet 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 to status " +
"'" + status + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return devices;
}
@Override
public List<Device> getDevicesByName(String deviceName, 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";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceName + "%");
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
ResultSet 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, null);
}
return devices;
}
@Override
public List<Device> getDevicesByOwnership(EnrolmentInfo.OwnerShip ownerShip, 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 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.setInt(1, tenantId);
stmt.setString(2, ownerShip.toString());
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
ResultSet 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 to ownership " +
"'" + ownerShip + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return devices;
} }
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {

@ -21,10 +21,7 @@ package org.wso2.carbon.device.mgt.core.operation.mgt;
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.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.*;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
@ -190,7 +187,7 @@ public class OperationManagerImpl implements OperationManager {
} }
@Override @Override
public PaginationResult getOperations(DeviceIdentifier deviceId, int index, int limit) public PaginationResult getOperations(DeviceIdentifier deviceId, PaginationRequest request)
throws OperationManagementException { throws OperationManagementException {
PaginationResult paginationResult = null; PaginationResult paginationResult = null;
int enrolmentId; int enrolmentId;
@ -215,7 +212,7 @@ public class OperationManagerImpl implements OperationManager {
deviceId.getType()); deviceId.getType());
} }
List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> operationList = List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> operationList =
operationDAO.getOperationsForDevice(enrolmentId, index, limit); operationDAO.getOperationsForDevice(enrolmentId, request);
for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) {
Operation operation = OperationDAOUtil.convertOperation(dtoOperation); Operation operation = OperationDAOUtil.convertOperation(dtoOperation);
operations.add(operation); operations.add(operation);
@ -224,7 +221,7 @@ public class OperationManagerImpl implements OperationManager {
int count = operationDAO.getOperationCountForDevice(enrolmentId); int count = operationDAO.getOperationCountForDevice(enrolmentId);
paginationResult.setData(operations); paginationResult.setData(operations);
paginationResult.setRecordsTotal(count); paginationResult.setRecordsTotal(count);
paginationResult.setRecordsFiltered(operations.size()); paginationResult.setRecordsFiltered(count);
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " + throw new OperationManagementException("Error occurred while retrieving the list of " +
"operations assigned for '" + deviceId.getType() + "operations assigned for '" + deviceId.getType() +

@ -18,6 +18,7 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt.dao; package org.wso2.carbon.device.mgt.core.operation.mgt.dao;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import java.util.List; import java.util.List;
@ -37,14 +38,14 @@ public interface OperationDAO {
List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status) List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status)
throws OperationManagementDAOException; throws OperationManagementDAOException;
List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, int index, int limit, Operation.Status status) List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, PaginationRequest request, Operation.Status status)
throws OperationManagementDAOException; throws OperationManagementDAOException;
List<? extends Operation> getOperationsForDevice(int enrolmentId) throws OperationManagementDAOException; List<? extends Operation> getOperationsForDevice(int enrolmentId) throws OperationManagementDAOException;
int getOperationCountForDevice(int enrolmentId) throws OperationManagementDAOException; int getOperationCountForDevice(int enrolmentId) throws OperationManagementDAOException;
List<? extends Operation> getOperationsForDevice(int enrolmentId, int index, int limit) throws OperationManagementDAOException; List<? extends Operation> getOperationsForDevice(int enrolmentId, PaginationRequest request) throws OperationManagementDAOException;
Operation getNextOperation(int enrolmentId) throws OperationManagementDAOException; Operation getNextOperation(int enrolmentId) throws OperationManagementDAOException;

@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl;
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.PaginationRequest;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
@ -246,7 +247,7 @@ public class GenericOperationDAOImpl implements OperationDAO {
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " + String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " +
"FROM DM_OPERATION o " + "FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; "WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId); stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString()); stmt.setString(2, status.toString());
@ -276,7 +277,7 @@ public class GenericOperationDAOImpl implements OperationDAO {
} }
@Override @Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, int index, int limit, public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, PaginationRequest request,
Operation.Status status) Operation.Status status)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -289,12 +290,12 @@ public class GenericOperationDAOImpl implements OperationDAO {
"FROM DM_OPERATION o " + "FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " + "WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " +
"o.CREATED_TIMESTAMP ASC LIMIT ?,?"; "o.CREATED_TIMESTAMP DESC LIMIT ?,?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId); stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString()); stmt.setString(2, status.toString());
stmt.setInt(3, index); stmt.setInt(3, request.getStartIndex());
stmt.setInt(4, limit); stmt.setInt(4, request.getRowCount());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
@ -331,7 +332,7 @@ public class GenericOperationDAOImpl implements OperationDAO {
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " +
"OPERATION_CODE, om.STATUS FROM DM_OPERATION o " + "OPERATION_CODE, om.STATUS FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; "WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId); stmt.setInt(1, enrolmentId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
@ -360,7 +361,7 @@ public class GenericOperationDAOImpl implements OperationDAO {
} }
@Override @Override
public List<? extends Operation> getOperationsForDevice(int enrolmentId, int index, int limit) public List<? extends Operation> getOperationsForDevice(int enrolmentId, PaginationRequest request)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
@ -371,11 +372,11 @@ public class GenericOperationDAOImpl implements OperationDAO {
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " +
"OPERATION_CODE, om.STATUS FROM DM_OPERATION o " + "OPERATION_CODE, om.STATUS FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC LIMIT ?,?"; "WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC LIMIT ?,?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId); stmt.setInt(1, enrolmentId);
stmt.setInt(2, index); stmt.setInt(2, request.getStartIndex());
stmt.setInt(3, limit); stmt.setInt(3, request.getRowCount());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {

@ -18,7 +18,7 @@
package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation; package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
@ -38,7 +38,7 @@ import java.util.List;
public class OracleOperationDAOImpl extends GenericOperationDAOImpl { public class OracleOperationDAOImpl extends GenericOperationDAOImpl {
@Override @Override
public List<? extends Operation> getOperationsForDevice(int enrolmentId, int index, int limit) public List<? extends Operation> getOperationsForDevice(int enrolmentId, PaginationRequest request)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
@ -49,12 +49,12 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl {
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " +
"OPERATION_CODE, om.STATUS FROM DM_OPERATION o " + "OPERATION_CODE, om.STATUS FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC " + "WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC " +
"OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId); stmt.setInt(1, enrolmentId);
stmt.setInt(2, index); stmt.setInt(2, request.getStartIndex());
stmt.setInt(3, limit); stmt.setInt(3, request.getRowCount());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
@ -81,7 +81,7 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl {
} }
@Override @Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, int index, int limit, public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, PaginationRequest request,
Operation.Status status) Operation.Status status)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -94,12 +94,12 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl {
"FROM DM_OPERATION o " + "FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " + "WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " +
"o.CREATED_TIMESTAMP ASC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; "o.CREATED_TIMESTAMP DESC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId); stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString()); stmt.setString(2, status.toString());
stmt.setInt(3, index); stmt.setInt(3, request.getStartIndex());
stmt.setInt(4, limit); stmt.setInt(4, request.getRowCount());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {

@ -18,7 +18,7 @@
package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation; package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
@ -38,7 +38,7 @@ import java.util.List;
public class PostgreSQLOperationDAOImpl extends GenericOperationDAOImpl { public class PostgreSQLOperationDAOImpl extends GenericOperationDAOImpl {
@Override @Override
public List<? extends Operation> getOperationsForDevice(int enrolmentId, int index, int limit) public List<? extends Operation> getOperationsForDevice(int enrolmentId, PaginationRequest request)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
@ -49,11 +49,11 @@ public class PostgreSQLOperationDAOImpl extends GenericOperationDAOImpl {
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " +
"OPERATION_CODE, om.STATUS FROM DM_OPERATION o " + "OPERATION_CODE, om.STATUS FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC LIMIT ? OFFSET ?"; "WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId); stmt.setInt(1, enrolmentId);
stmt.setInt(2, limit); stmt.setInt(2, request.getRowCount());
stmt.setInt(3, index); stmt.setInt(3, request.getStartIndex());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
@ -80,7 +80,7 @@ public class PostgreSQLOperationDAOImpl extends GenericOperationDAOImpl {
} }
@Override @Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, int index, int limit, public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, PaginationRequest request,
Operation.Status status) Operation.Status status)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -93,12 +93,12 @@ public class PostgreSQLOperationDAOImpl extends GenericOperationDAOImpl {
"FROM DM_OPERATION o " + "FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " + "WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " +
"o.CREATED_TIMESTAMP ASC LIMIT ? OFFSET ?"; "o.CREATED_TIMESTAMP DESC LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId); stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString()); stmt.setString(2, status.toString());
stmt.setInt(3, limit); stmt.setInt(3, request.getRowCount());
stmt.setInt(4, index); stmt.setInt(4, request.getStartIndex());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {

@ -18,7 +18,7 @@
package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation; package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
@ -38,7 +38,7 @@ import java.util.List;
public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl { public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl {
@Override @Override
public List<? extends Operation> getOperationsForDevice(int enrolmentId, int index, int limit) public List<? extends Operation> getOperationsForDevice(int enrolmentId, PaginationRequest request)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
@ -49,12 +49,12 @@ public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl {
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " +
"OPERATION_CODE, om.STATUS FROM DM_OPERATION o " + "OPERATION_CODE, om.STATUS FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC " + "WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC " +
"OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId); stmt.setInt(1, enrolmentId);
stmt.setInt(2, index); stmt.setInt(2, request.getStartIndex());
stmt.setInt(3, limit); stmt.setInt(3, request.getRowCount());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
@ -81,7 +81,7 @@ public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl {
} }
@Override @Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, int index, int limit, public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, PaginationRequest request,
Operation.Status status) Operation.Status status)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -94,12 +94,12 @@ public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl {
"FROM DM_OPERATION o " + "FROM DM_OPERATION o " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " +
"WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " + "WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY " +
"o.CREATED_TIMESTAMP ASC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; "o.CREATED_TIMESTAMP DESC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId); stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString()); stmt.setString(2, status.toString());
stmt.setInt(3, index); stmt.setInt(3, request.getStartIndex());
stmt.setInt(4, limit); stmt.setInt(4, request.getRowCount());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {

@ -43,24 +43,22 @@ public interface DeviceManagementProviderService extends OperationManager {
* Method to retrieve all the devices with pagination support. * Method to retrieve all the devices with pagination support.
* *
* @param deviceType Device platform * @param deviceType Device platform
* @param index Starting row number * @param request PaginationRequest object holding the data for pagination
* @param limit No of rows to fetch
* @return PaginationResult - Result including the required parameters necessary to do pagination. * @return PaginationResult - Result including the required parameters necessary to do pagination.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the * @throws DeviceManagementException If some unusual behaviour is observed while fetching the
* devices. * devices.
*/ */
PaginationResult getAllDevices(String deviceType, int index, int limit) throws DeviceManagementException; PaginationResult getAllDevices(String deviceType, PaginationRequest request) throws DeviceManagementException;
/** /**
* Method to retrieve all the devices with pagination support. * Method to retrieve all the devices with pagination support.
* *
* @param index Starting row number * @param request PaginationRequest object holding the data for pagination
* @param limit No of rows to fetch
* @return PaginationResult - Result including the required parameters necessary to do pagination. * @return PaginationResult - Result including the required parameters necessary to do pagination.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the * @throws DeviceManagementException If some unusual behaviour is observed while fetching the
* devices. * devices.
*/ */
PaginationResult getAllDevices(int index, int limit) throws DeviceManagementException; PaginationResult getAllDevices(PaginationRequest request) throws DeviceManagementException;
void sendEnrolmentInvitation(EmailMessageProperties config) throws DeviceManagementException; void sendEnrolmentInvitation(EmailMessageProperties config) throws DeviceManagementException;
@ -78,6 +76,29 @@ public interface DeviceManagementProviderService extends OperationManager {
*/ */
TenantConfiguration getConfiguration(String deviceType) throws DeviceManagementException; TenantConfiguration getConfiguration(String deviceType) throws DeviceManagementException;
/**
* 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;
/**
* 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;
/** /**
* Method to get the list of devices owned by an user. * Method to get the list of devices owned by an user.
* *
@ -116,6 +137,17 @@ public interface DeviceManagementProviderService extends OperationManager {
*/ */
List<Device> getDevicesByName(String deviceName) throws DeviceManagementException; List<Device> getDevicesByName(String deviceName) throws DeviceManagementException;
/**
* 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;
void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status active) throws DeviceManagementException; void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status active) throws DeviceManagementException;
/** /**
@ -127,6 +159,17 @@ public interface DeviceManagementProviderService extends OperationManager {
*/ */
List<Device> getDevicesByStatus(EnrolmentInfo.Status status) throws DeviceManagementException; List<Device> getDevicesByStatus(EnrolmentInfo.Status status) throws DeviceManagementException;
/**
* 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;
License getLicense(String deviceType, String languageCode) throws DeviceManagementException; License getLicense(String deviceType, String languageCode) throws DeviceManagementException;
void addLicense(String deviceType, License license) throws DeviceManagementException; void addLicense(String deviceType, License license) throws DeviceManagementException;

@ -288,7 +288,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Device not found for id '" + deviceId.getId() + "'"); log.debug("Device not found for id '" + deviceId.getId() + "'");
} }
throw new DeviceManagementException("Device not found"); return false;
}
if (device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) {
if (log.isDebugEnabled()) {
log.debug("Device has already disenrolled : " + deviceId.getId() + "'");
}
return false;
} }
DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType());
@ -387,13 +394,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @Override
public PaginationResult getAllDevices(String deviceType, int index, int limit) throws DeviceManagementException { public PaginationResult getAllDevices(String deviceType, PaginationRequest request) throws DeviceManagementException {
PaginationResult paginationResult; PaginationResult paginationResult = new PaginationResult();
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
List<Device> allDevices; List<Device> allDevices = new ArrayList<>();
int count = 0;
int tenantId = this.getTenantId();
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
paginationResult = deviceDAO.getDevices(deviceType, index, limit, this.getTenantId()); allDevices = deviceDAO.getDevices(deviceType, request, tenantId);
count = deviceDAO.getDeviceCount(deviceType, tenantId);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " +
"the current tenant", e); "the current tenant", e);
@ -402,7 +412,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} finally { } finally {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
allDevices = (List<Device>) paginationResult.getData();
for (Device device : allDevices) { for (Device device : allDevices) {
DeviceManager deviceManager = this.getDeviceManager(device.getType()); DeviceManager deviceManager = this.getDeviceManager(device.getType());
if (deviceManager == null) { if (deviceManager == null) {
@ -422,17 +431,22 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
devices.add(device); devices.add(device);
} }
paginationResult.setData(devices); paginationResult.setData(devices);
paginationResult.setRecordsFiltered(count);
paginationResult.setRecordsTotal(count);
return paginationResult; return paginationResult;
} }
@Override @Override
public PaginationResult getAllDevices(int index, int limit) throws DeviceManagementException { public PaginationResult getAllDevices(PaginationRequest request) throws DeviceManagementException {
PaginationResult paginationResult; PaginationResult paginationResult = new PaginationResult();
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
List<Device> allDevices; List<Device> allDevices = new ArrayList<>();
int count = 0;
int tenantId = this.getTenantId();
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
paginationResult = deviceDAO.getDevices(index, limit, this.getTenantId()); allDevices = deviceDAO.getDevices(request, tenantId);
count = deviceDAO.getDeviceCount(request, tenantId);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " +
"the current tenant", e); "the current tenant", e);
@ -441,7 +455,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} finally { } finally {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
allDevices = (List<Device>) paginationResult.getData();
for (Device device : allDevices) { for (Device device : allDevices) {
DeviceManager deviceManager = this.getDeviceManager(device.getType()); DeviceManager deviceManager = this.getDeviceManager(device.getType());
if (deviceManager == null) { if (deviceManager == null) {
@ -461,6 +474,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
devices.add(device); devices.add(device);
} }
paginationResult.setData(devices); paginationResult.setData(devices);
paginationResult.setRecordsFiltered(count);
paginationResult.setRecordsTotal(count);
return paginationResult; return paginationResult;
} }
@ -862,9 +877,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @Override
public PaginationResult getOperations(DeviceIdentifier deviceId, int index, int limit) public PaginationResult getOperations(DeviceIdentifier deviceId, PaginationRequest request)
throws OperationManagementException { throws OperationManagementException {
return DeviceManagementDataHolder.getInstance().getOperationManager().getOperations(deviceId, index, limit); return DeviceManagementDataHolder.getInstance().getOperationManager().getOperations(deviceId, request);
} }
@Override @Override
@ -946,6 +961,87 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override
public PaginationResult getDevicesOfUser(String username, PaginationRequest request)
throws DeviceManagementException {
PaginationResult result = new PaginationResult();
int deviceCount = 0;
int tenantId = this.getTenantId();
List<Device> devices = new ArrayList<>();
List<Device> userDevices = new ArrayList<>();
try {
DeviceManagementDAOFactory.openConnection();
userDevices = deviceDAO.getDevicesOfUser(username, request, tenantId);
deviceCount = deviceDAO.getDeviceCountByUser(username, tenantId);
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while retrieving the list of devices that " +
"belong to the user '" + username + "'", e);
} catch (SQLException e) {
throw new DeviceManagementException("Error occurred while opening a connection to the data source", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
for (Device device : userDevices) {
DeviceManager deviceManager = this.getDeviceManager(device.getType());
if (deviceManager == null) {
if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " +
"Therefore, not attempting method 'isEnrolled'");
}
devices.add(device);
continue;
}
Device dmsDevice =
deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
if (dmsDevice != null) {
device.setFeatures(dmsDevice.getFeatures());
device.setProperties(dmsDevice.getProperties());
}
devices.add(device);
}
result.setData(devices);
result.setRecordsTotal(deviceCount);
result.setRecordsFiltered(deviceCount);
return result;
}
@Override
public PaginationResult getDevicesByOwnership(EnrolmentInfo.OwnerShip ownerShip,
PaginationRequest request)
throws DeviceManagementException {
PaginationResult result = new PaginationResult();
List<Device> devices = new ArrayList<>();
List<Device> allDevices;
int deviceCount = 0;
int tenantId = this.getTenantId();
try {
DeviceManagementDAOFactory.openConnection();
allDevices = deviceDAO.getDevicesByOwnership(ownerShip, request, tenantId);
deviceCount = deviceDAO.getDeviceCountByOwnership(ownerShip, tenantId);
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException(
"Error occurred while fetching the list of devices that matches to ownership : '" + ownerShip + "'", e);
} catch (SQLException e) {
throw new DeviceManagementException("Error occurred while opening a connection to the data source", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
for (Device device : allDevices) {
Device dmsDevice = this.getDeviceManager(device.getType()).
getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
if (dmsDevice != null) {
device.setFeatures(dmsDevice.getFeatures());
device.setProperties(dmsDevice.getProperties());
}
devices.add(device);
}
result.setData(devices);
result.setRecordsTotal(deviceCount);
result.setRecordsFiltered(deviceCount);
return result;
}
@Override @Override
public List<Device> getAllDevicesOfRole(String role) throws DeviceManagementException { public List<Device> getAllDevicesOfRole(String role) throws DeviceManagementException {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
@ -1022,7 +1118,40 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
devices.add(device); devices.add(device);
} }
return devices; return devices;
}
@Override
public PaginationResult getDevicesByName(String deviceName, PaginationRequest request)
throws DeviceManagementException {
PaginationResult result = new PaginationResult();
int tenantId = this.getTenantId();
List<Device> devices = new ArrayList<>();
List<Device> allDevices = new ArrayList<>();
try {
DeviceManagementDAOFactory.openConnection();
allDevices = deviceDAO.getDevicesByName(deviceName, request, tenantId);
int deviceCount = deviceDAO.getDeviceCountByName(deviceName, tenantId);
result.setRecordsTotal(deviceCount);
result.setRecordsFiltered(deviceCount);
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while fetching the list of devices that matches to '"
+ deviceName + "'", e);
} catch (SQLException e) {
throw new DeviceManagementException("Error occurred while opening a connection to the data source", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
for (Device device : allDevices) {
Device dmsDevice = this.getDeviceManager(device.getType()).
getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
if (dmsDevice != null) {
device.setFeatures(dmsDevice.getFeatures());
device.setProperties(dmsDevice.getProperties());
}
devices.add(device);
}
result.setData(devices);
return result;
} }
@Override @Override
@ -1091,6 +1220,40 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return devices; return devices;
} }
@Override
public PaginationResult getDevicesByStatus(EnrolmentInfo.Status status, PaginationRequest request)
throws DeviceManagementException {
PaginationResult result = new PaginationResult();
List<Device> devices = new ArrayList<>();
List<Device> allDevices = new ArrayList<>();
int tenantId = this.getTenantId();
try {
DeviceManagementDAOFactory.openConnection();
allDevices = deviceDAO.getDevicesByStatus(status, request, tenantId);
int deviceCount = deviceDAO.getDeviceCount(status, tenantId);
result.setRecordsTotal(deviceCount);
result.setRecordsFiltered(deviceCount);
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException(
"Error occurred while fetching the list of devices that matches to status: '" + status + "'", e);
} catch (SQLException e) {
throw new DeviceManagementException("Error occurred while opening a connection to the data source", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
for (Device device : allDevices) {
Device dmsDevice = this.getDeviceManager(device.getType()).
getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
if (dmsDevice != null) {
device.setFeatures(dmsDevice.getFeatures());
device.setProperties(dmsDevice.getProperties());
}
devices.add(device);
}
result.setData(devices);
return result;
}
private int getTenantId() { private int getTenantId() {
return CarbonContext.getThreadLocalCarbonContext().getTenantId(); return CarbonContext.getThreadLocalCarbonContext().getTenantId();
} }

@ -59,6 +59,7 @@ public class OauthAuthenticator implements CarbonServerAuthenticator {
*/ */
public boolean isHandle(MessageContext messageContext) { public boolean isHandle(MessageContext messageContext) {
HttpServletRequest httpServletRequest = getHttpRequest(messageContext); HttpServletRequest httpServletRequest = getHttpRequest(messageContext);
if (httpServletRequest != null) {
String headerValue = httpServletRequest.getHeader(HTTPConstants.HEADER_AUTHORIZATION); String headerValue = httpServletRequest.getHeader(HTTPConstants.HEADER_AUTHORIZATION);
if (headerValue != null && !headerValue.trim().isEmpty()) { if (headerValue != null && !headerValue.trim().isEmpty()) {
String[] headerPart = headerValue.trim().split(OauthAuthenticatorConstants.SPLITING_CHARACTOR); String[] headerPart = headerValue.trim().split(OauthAuthenticatorConstants.SPLITING_CHARACTOR);
@ -68,6 +69,7 @@ public class OauthAuthenticator implements CarbonServerAuthenticator {
} else if (httpServletRequest.getParameter(OauthAuthenticatorConstants.BEARER_TOKEN_IDENTIFIER) != null) { } else if (httpServletRequest.getParameter(OauthAuthenticatorConstants.BEARER_TOKEN_IDENTIFIER) != null) {
return true; return true;
} }
}
return false; return false;
} }
@ -134,7 +136,10 @@ public class OauthAuthenticator implements CarbonServerAuthenticator {
* @return boolean true for enable or otherwise for disable status. * @return boolean true for enable or otherwise for disable status.
*/ */
public boolean isDisabled() { public boolean isDisabled() {
return false; AuthenticatorsConfiguration authenticatorsConfiguration = AuthenticatorsConfiguration.getInstance();
AuthenticatorsConfiguration.AuthenticatorConfig authenticatorConfig = authenticatorsConfiguration.
getAuthenticatorConfig(OauthAuthenticatorConstants.AUTHENTICATOR_NAME);
return authenticatorConfig.isDisabled();
} }
/** /**

@ -90,6 +90,7 @@
org.wso2.carbon.user.api, org.wso2.carbon.user.api,
org.wso2.carbon.user.core.service, org.wso2.carbon.user.core.service,
org.wso2.carbon.identity.application.common.model, org.wso2.carbon.identity.application.common.model,
org.wso2.carbon.identity.application.authentication.framework.model,
org.wso2.carbon.user.core.tenant org.wso2.carbon.user.core.tenant
</Import-Package> </Import-Package>
</instructions> </instructions>

@ -73,10 +73,6 @@ public class JWTAuthenticator implements WebappAuthenticator {
authenticationInfo.setStatus(Status.CONTINUE); authenticationInfo.setStatus(Status.CONTINUE);
} }
if (log.isDebugEnabled()) {
log.debug("Authenticating using JWT header.");
}
//Get the filesystem keystore default primary certificate //Get the filesystem keystore default primary certificate
KeyStoreManager keyStoreManager = KeyStoreManager.getInstance(MultitenantConstants.SUPER_TENANT_ID); KeyStoreManager keyStoreManager = KeyStoreManager.getInstance(MultitenantConstants.SUPER_TENANT_ID);
try { try {

@ -141,9 +141,6 @@ public class OAuthAuthenticator implements WebappAuthenticator {
tokenValue = tokenValue.substring(matcher.end()); tokenValue = tokenValue.substring(matcher.end());
} }
} }
if (log.isDebugEnabled()) {
log.debug("Oauth Token : " + tokenValue);
}
return tokenValue; return tokenValue;
} }

@ -86,10 +86,6 @@ public class PermissionAuthorizer {
return WebappAuthenticator.Status.FAILURE; return WebappAuthenticator.Status.FAILURE;
} }
if (log.isDebugEnabled()) {
log.debug("Is user authorized: " + isUserAuthorized);
}
if (isUserAuthorized) { if (isUserAuthorized) {
return WebappAuthenticator.Status.SUCCESS; return WebappAuthenticator.Status.SUCCESS;
} else { } else {

@ -4,10 +4,9 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
); );
CREATE TABLE IF NOT EXISTS DM_DEVICE_CERTIFICATE ( CREATE TABLE IF NOT EXISTS DM_DEVICE_CERTIFICATE (
ID BIGSERIAL PRIMARY KEY, ID BIGSERIAL NOT NULL PRIMARY KEY,
SERIAL_NUMBER VARCHAR(500) DEFAULT NULL, SERIAL_NUMBER VARCHAR(500) DEFAULT NULL,
CERTIFICATE BYTEA DEFAULT NULL, CERTIFICATE BYTEA DEFAULT NULL
PRIMARY KEY (ID)
); );
CREATE TABLE IF NOT EXISTS DM_DEVICE ( CREATE TABLE IF NOT EXISTS DM_DEVICE (

@ -1456,7 +1456,7 @@
<properties> <properties>
<testng.version>6.1.1</testng.version> <testng.version>6.1.1</testng.version>
<carbon.kernel.version>4.4.2</carbon.kernel.version> <carbon.kernel.version>4.4.3</carbon.kernel.version>
<carbon.p2.plugin.version>1.5.4</carbon.p2.plugin.version> <carbon.p2.plugin.version>1.5.4</carbon.p2.plugin.version>
<maven-buildnumber-plugin.version>1.3</maven-buildnumber-plugin.version> <maven-buildnumber-plugin.version>1.3</maven-buildnumber-plugin.version>
@ -1497,7 +1497,7 @@
<carbon.deployment.version>4.6.0</carbon.deployment.version> <carbon.deployment.version>4.6.0</carbon.deployment.version>
<!-- Carbon Identity --> <!-- Carbon Identity -->
<carbon.identity.version>5.0.3</carbon.identity.version> <carbon.identity.version>5.0.5</carbon.identity.version>
<!-- Carbon Multi-tenancy --> <!-- Carbon Multi-tenancy -->
<carbon.multitenancy.version>4.5.0</carbon.multitenancy.version> <carbon.multitenancy.version>4.5.0</carbon.multitenancy.version>

Loading…
Cancel
Save