forked from community/device-mgt-core
parent
2a6db0cdc4
commit
6c196d5317
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds necessary data to represent a paginated result.
|
||||||
|
*/
|
||||||
|
public class PaginationResult implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1998101711L;
|
||||||
|
private int recordsTotal;
|
||||||
|
private int recordsFiltered;
|
||||||
|
private int draw;
|
||||||
|
private List<?> data;
|
||||||
|
|
||||||
|
public int getRecordsTotal() {
|
||||||
|
return recordsTotal;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRecordsFiltered() {
|
||||||
|
return recordsFiltered;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRecordsFiltered(int recordsFiltered) {
|
||||||
|
this.recordsFiltered = recordsFiltered;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRecordsTotal(int recordsTotal) {
|
||||||
|
this.recordsTotal = recordsTotal;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<?> getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setData(List<?> data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDraw() {
|
||||||
|
return draw;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDraw(int draw) {
|
||||||
|
this.draw = draw;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,126 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.dao.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||||
|
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.util.DeviceManagementDAOUtil;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax.
|
||||||
|
*/
|
||||||
|
public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.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 AS DEVICE_ID, " +
|
||||||
|
"d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||||
|
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " +
|
||||||
|
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?";
|
||||||
|
// String sql = "SELECT * FROM DM_DEVICE WHERE TENANT_ID = ? LIMIT ?,?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, tenantId);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, index);
|
||||||
|
stmt.setInt(4, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while retrieving information of all " +
|
||||||
|
"registered devices", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(String type, int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||||
|
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||||
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " +
|
||||||
|
"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 = ? " +
|
||||||
|
"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.setString(1, type);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.setInt(4, index);
|
||||||
|
stmt.setInt(5, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(type, tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection getConnection() throws SQLException {
|
||||||
|
return DeviceManagementDAOFactory.getConnection();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.dao.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||||
|
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.util.DeviceManagementDAOUtil;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax.
|
||||||
|
*/
|
||||||
|
public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.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 AS DEVICE_ID, " +
|
||||||
|
"d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||||
|
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " +
|
||||||
|
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,? ROW_NUMBER <= ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, tenantId);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, index);
|
||||||
|
stmt.setInt(4, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while retrieving information of all " +
|
||||||
|
"registered devices", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(String type, int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||||
|
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||||
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " +
|
||||||
|
"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 = ? " +
|
||||||
|
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, type);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.setInt(4, index);
|
||||||
|
stmt.setInt(5, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(type, tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection getConnection() throws SQLException {
|
||||||
|
return DeviceManagementDAOFactory.getConnection();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.dao.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||||
|
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.util.DeviceManagementDAOUtil;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax.
|
||||||
|
*/
|
||||||
|
public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.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 AS DEVICE_ID, " +
|
||||||
|
"d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||||
|
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " +
|
||||||
|
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? LIMIT ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, tenantId);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, index);
|
||||||
|
stmt.setInt(4, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while retrieving information of all " +
|
||||||
|
"registered devices", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(String type, int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||||
|
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||||
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " +
|
||||||
|
"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 = ? " +
|
||||||
|
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? LIMIT ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, type);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.setInt(4, index);
|
||||||
|
stmt.setInt(5, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(type, tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection getConnection() throws SQLException {
|
||||||
|
return DeviceManagementDAOFactory.getConnection();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.dao.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||||
|
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.util.DeviceManagementDAOUtil;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax.
|
||||||
|
*/
|
||||||
|
public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.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 AS DEVICE_ID, " +
|
||||||
|
"d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
|
||||||
|
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 " +
|
||||||
|
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, tenantId);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, index);
|
||||||
|
stmt.setInt(4, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while retrieving information of all " +
|
||||||
|
"registered devices", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getDevices(String type, int index, int limit, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
|
PaginationResult result = new PaginationResult();
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||||
|
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||||
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, " +
|
||||||
|
"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 = ? " +
|
||||||
|
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, type);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.setInt(4, index);
|
||||||
|
stmt.setInt(5, limit);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
int count = this.getDeviceCount(type, tenantId);
|
||||||
|
result.setData(devices);
|
||||||
|
result.setRecordsFiltered(count);
|
||||||
|
result.setRecordsTotal(count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection getConnection() throws SQLException {
|
||||||
|
return DeviceManagementDAOFactory.getConnection();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue