Added pagination support

merge-requests/7/head
harshanl 9 years ago
parent 2a6db0cdc4
commit 6c196d5317

@ -17,8 +17,6 @@
*/
package org.wso2.carbon.device.mgt.common;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.List;

@ -78,4 +78,16 @@ public final class DeviceManagementConstants {
}
public static final String NOTIFICATION_CONFIG_FILE = "notification-messages.xml";
}
public static final class DataBaseTypes {
private DataBaseTypes() {
throw new AssertionError();
}
public static final String DB_TYPE_MYSQL = "MySQL";
public static final String DB_TYPE_ORACLE = "Oracle";
public static final String DB_TYPE_MSSQL = "MSSQL";
public static final String DB_TYPE_DB2 = "DB2";
public static final String DB_TYPE_H2 = "H2";
public static final String DB_TYPE_POSTGRESQL = "PostgreSQL";
}
}

@ -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;
}
}

@ -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.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import java.util.HashMap;
import java.util.List;
@ -31,6 +32,16 @@ import java.util.List;
*/
public interface DeviceDAO {
/**
* This method is used to add a device.
*
* @param type device type.
* @param tenantId tenant id.
* @return returns the device count of given type.
* @throws DeviceManagementDAOException
*/
int getDeviceCount(String type, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to add a device.
*
@ -100,10 +111,33 @@ public interface DeviceDAO {
*/
List<Device> getDevices(int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the devices of a given tenant as a paginated result.
*
* @param index start index of result set.
* @param limit number of records to be returned.
* @param tenantId tenant id.
* @return returns a PaginationResult including the requested data.
* @throws DeviceManagementDAOException
*/
PaginationResult getDevices(int index, int limit, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the devices of a given tenant and type as a paginated result.
*
* @param type device type.
* @param index start index of result set.
* @param limit number of records to be returned.
* @param tenantId tenant id.
* @return returns a PaginationResult including the requested data.
* @throws DeviceManagementDAOException
*/
PaginationResult getDevices(String type, int index, int limit, int tenantId) throws DeviceManagementDAOException;
/**
* 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.
* @return returns list of devices.
* @throws DeviceManagementDAOException

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException;
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
@ -82,11 +83,27 @@ import java.util.List;
public class DeviceManagementDAOFactory {
private static DataSource dataSource;
private static String databaseEngine;
private static final Log log = LogFactory.getLog(DeviceManagementDAOFactory.class);
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
public static DeviceDAO getDeviceDAO() {
return new DeviceDAOImpl();
if(databaseEngine != null) {
switch (databaseEngine) {
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE:
return new OracleDeviceDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL:
return new SQLServerDeviceDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL:
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2:
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL:
default:
return new GenericDeviceDAOImpl();
}
} else {
return new GenericDeviceDAOImpl();
}
}
public static DeviceTypeDAO getDeviceTypeDAO() {
@ -107,10 +124,20 @@ public class DeviceManagementDAOFactory {
public static void init(DataSourceConfig config) {
dataSource = resolveDataSource(config);
try {
databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName();
} catch (SQLException e) {
log.error("Error occurred while retrieving config.datasource connection", e);
}
}
public static void init(DataSource dtSource) {
dataSource = dtSource;
try {
databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName();
} catch (SQLException e) {
log.error("Error occurred while retrieving config.datasource connection", e);
}
}
public static void beginTransaction() throws TransactionManagementException {

@ -34,7 +34,7 @@ import java.util.Iterator;
import java.util.HashMap;
import java.util.List;
public class DeviceDAOImpl implements DeviceDAO {
public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
@Override
public int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException {
@ -122,7 +122,7 @@ public class DeviceDAOImpl implements DeviceDAO {
stmt.setInt(4, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
device = this.loadDevice(rs);
device = DeviceManagementDAOUtil.loadDevice(rs);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type " +
@ -152,7 +152,7 @@ public class DeviceDAOImpl implements DeviceDAO {
stmt.setString(2, deviceIdentifier.getId());
rs = stmt.executeQuery();
if (rs.next()) {
device = this.loadDevice(rs);
device = DeviceManagementDAOUtil.loadDevice(rs);
deviceHashMap.put(rs.getInt("TENANT_ID"), device);
}
} catch (SQLException e) {
@ -184,7 +184,7 @@ public class DeviceDAOImpl implements DeviceDAO {
stmt.setInt(3, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
device = this.loadDevice(rs);
device = DeviceManagementDAOUtil.loadDevice(rs);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving device for id " +
@ -215,7 +215,7 @@ public class DeviceDAOImpl implements DeviceDAO {
rs = stmt.executeQuery();
devices = new ArrayList<>();
while (rs.next()) {
Device device = this.loadDevice(rs);
Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device);
}
} catch (SQLException e) {
@ -248,7 +248,7 @@ public class DeviceDAOImpl implements DeviceDAO {
rs = stmt.executeQuery();
devices = new ArrayList<>();
while (rs.next()) {
Device device = this.loadDevice(rs);
Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device);
}
} catch (SQLException e) {
@ -279,7 +279,7 @@ public class DeviceDAOImpl implements DeviceDAO {
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Device device = this.loadDevice(rs);
Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device);
}
} catch (SQLException e) {
@ -324,6 +324,31 @@ public class DeviceDAOImpl implements DeviceDAO {
return deviceCount;
}
@Override
public int getDeviceCount(String type, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
int deviceCount = 0;
try {
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 " +
"WHERE t.NAME = ?) d1 WHERE TYPE_ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, type);
stmt.setInt(2, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT");
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while getting the device count", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return deviceCount;
}
/**
* Get the list of devices that matches with the given device name.
*
@ -352,7 +377,7 @@ public class DeviceDAOImpl implements DeviceDAO {
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Device device = this.loadDevice(rs);
Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device);
}
} catch (SQLException e) {
@ -475,7 +500,7 @@ public class DeviceDAOImpl implements DeviceDAO {
stmt.setInt(5, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
enrolmentInfo = this.loadEnrolment(rs);
enrolmentInfo = DeviceManagementDAOUtil.loadEnrolment(rs);
}
return enrolmentInfo;
} catch (SQLException e) {
@ -551,7 +576,7 @@ public class DeviceDAOImpl implements DeviceDAO {
stmt.setInt(index, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
enrolments.add(this.loadEnrolment(rs));
enrolments.add(DeviceManagementDAOUtil.loadEnrolment(rs));
}
return enrolments;
} catch (SQLException e) {
@ -562,28 +587,6 @@ public class DeviceDAOImpl implements DeviceDAO {
}
}
private Device loadDevice(ResultSet rs) throws SQLException {
Device device = new Device();
device.setId(rs.getInt("DEVICE_ID"));
device.setName(rs.getString("DEVICE_NAME"));
device.setDescription(rs.getString("DESCRIPTION"));
device.setType(rs.getString("DEVICE_TYPE"));
device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION"));
device.setEnrolmentInfo(this.loadEnrolment(rs));
return device;
}
private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException {
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
enrolmentInfo.setId(rs.getInt("ENROLMENT_ID"));
enrolmentInfo.setOwner(rs.getString("OWNER"));
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP")));
enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime());
enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime());
enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS")));
return enrolmentInfo;
}
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
@ -604,7 +607,7 @@ public class DeviceDAOImpl implements DeviceDAO {
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Device device = this.loadDevice(rs);
Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device);
}
} catch (SQLException e) {

@ -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();
}
}

@ -20,6 +20,8 @@ package org.wso2.carbon.device.mgt.core.dao.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.user.api.UserStoreException;
@ -120,4 +122,26 @@ public final class DeviceManagementDAOUtil {
}
}
public static Device loadDevice(ResultSet rs) throws SQLException {
Device device = new Device();
device.setId(rs.getInt("DEVICE_ID"));
device.setName(rs.getString("DEVICE_NAME"));
device.setDescription(rs.getString("DESCRIPTION"));
device.setType(rs.getString("DEVICE_TYPE"));
device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION"));
device.setEnrolmentInfo(loadEnrolment(rs));
return device;
}
public static EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException {
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
enrolmentInfo.setId(rs.getInt("ENROLMENT_ID"));
enrolmentInfo.setOwner(rs.getString("OWNER"));
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP")));
enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime());
enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime());
enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS")));
return enrolmentInfo;
}
}

@ -4,11 +4,18 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService;
import org.wso2.carbon.device.mgt.core.scep.SCEPManager;
import org.wso2.carbon.device.mgt.core.scep.SCEPManagerImpl;
/**
* @scr.component name="org.wso2.carbon.device.mgt.core.scep" immediate="true"
* @scr.reference name="app.mgt.service"
* interface="org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService"
* cardinality="1..1"
* policy="dynamic"
* bind="setApplicationManagementProviderService"
* unbind="unsetApplicationManagementProviderService"
*/
public class SCEPManagerServiceComponent {
@ -40,4 +47,14 @@ public class SCEPManagerServiceComponent {
}
}
protected void unsetApplicationManagementProviderService(ApplicationManagementProviderService
applicationManagementProviderService) {
//do nothing
}
protected void setApplicationManagementProviderService(ApplicationManagementProviderService
applicationManagementProviderService) {
//do nothing
}
}

@ -36,6 +36,10 @@ public interface DeviceManagementProviderService extends OperationManager {
List<Device> getAllDevices() throws DeviceManagementException;
PaginationResult getAllDevices(String deviceType, int index, int limit) throws DeviceManagementException;
PaginationResult getAllDevices(int index, int limit) throws DeviceManagementException;
void sendEnrolmentInvitation(EmailMessageProperties config) throws DeviceManagementException;
void sendRegistrationEmail(EmailMessageProperties config) throws DeviceManagementException;

@ -348,6 +348,84 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return devices;
}
@Override
public PaginationResult getAllDevices(String deviceType, int index, int limit) throws DeviceManagementException {
PaginationResult paginationResult;
List<Device> devices = new ArrayList<>();
List<Device> allDevices;
try {
DeviceManagementDAOFactory.openConnection();
paginationResult = deviceDAO.getDevices(deviceType, index, limit, this.getTenantId());
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " +
"the current tenant", e);
} catch (SQLException e) {
throw new DeviceManagementException("Error occurred while opening a connection to the data source", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
allDevices = (List<Device>) paginationResult.getData();
for (Device device : allDevices) {
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);
}
paginationResult.setData(devices);
return paginationResult;
}
@Override
public PaginationResult getAllDevices(int index, int limit) throws DeviceManagementException {
PaginationResult paginationResult;
List<Device> devices = new ArrayList<>();
List<Device> allDevices;
try {
DeviceManagementDAOFactory.openConnection();
paginationResult = deviceDAO.getDevices(index, limit, this.getTenantId());
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " +
"the current tenant", e);
} catch (SQLException e) {
throw new DeviceManagementException("Error occurred while opening a connection to the data source", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
allDevices = (List<Device>) paginationResult.getData();
for (Device device : allDevices) {
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);
}
paginationResult.setData(devices);
return paginationResult;
}
@Override
public List<Device> getAllDevices(String deviceType) throws DeviceManagementException {
List<Device> devices = new ArrayList<>();

Loading…
Cancel
Save