From 6c196d5317775290c5b72a2eeeafaddde9de9150 Mon Sep 17 00:00:00 2001 From: harshanl Date: Fri, 16 Oct 2015 23:40:13 +0530 Subject: [PATCH] Added pagination support --- .../wso2/carbon/device/mgt/common/Device.java | 2 - .../mgt/common/DeviceManagementConstants.java | 12 ++ .../device/mgt/common/PaginationResult.java | 67 ++++++++++ .../carbon/device/mgt/core/dao/DeviceDAO.java | 36 ++++- .../core/dao/DeviceManagementDAOFactory.java | 29 +++- ...AOImpl.java => AbstractDeviceDAOImpl.java} | 69 +++++----- .../core/dao/impl/GenericDeviceDAOImpl.java | 126 ++++++++++++++++++ .../core/dao/impl/OracleDeviceDAOImpl.java | 122 +++++++++++++++++ .../dao/impl/PostgreSQLDeviceDAOImpl.java | 122 +++++++++++++++++ .../core/dao/impl/SQLServerDeviceDAOImpl.java | 122 +++++++++++++++++ .../dao/util/DeviceManagementDAOUtil.java | 24 ++++ .../internal/SCEPManagerServiceComponent.java | 17 +++ .../DeviceManagementProviderService.java | 4 + .../DeviceManagementProviderServiceImpl.java | 78 +++++++++++ 14 files changed, 793 insertions(+), 37 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationResult.java rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/{DeviceDAOImpl.java => AbstractDeviceDAOImpl.java} (93%) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/OracleDeviceDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/PostgreSQLDeviceDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/SQLServerDeviceDAOImpl.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java index 07842fbc6b..be6f7bf951 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java @@ -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; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java index 69f95923e8..3b9035ec13 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java @@ -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"; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationResult.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationResult.java new file mode 100644 index 0000000000..0125b0fd90 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationResult.java @@ -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; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 12c8014d7b..6b593e85c6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -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 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 diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java index 670935f3bb..173688058c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java @@ -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 currentConnection = new ThreadLocal(); + 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 { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java similarity index 93% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index e38a3e6df6..a2e4f4d12c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -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 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) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java new file mode 100644 index 0000000000..efd0d39da5 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java @@ -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 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 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(); + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/OracleDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/OracleDeviceDAOImpl.java new file mode 100644 index 0000000000..2524945f59 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/OracleDeviceDAOImpl.java @@ -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 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 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(); + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/PostgreSQLDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/PostgreSQLDeviceDAOImpl.java new file mode 100644 index 0000000000..5a65c807c2 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/PostgreSQLDeviceDAOImpl.java @@ -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 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 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(); + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/SQLServerDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/SQLServerDeviceDAOImpl.java new file mode 100644 index 0000000000..224c7ee280 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/SQLServerDeviceDAOImpl.java @@ -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 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 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(); + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java index 225e27d83a..6e640802eb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java @@ -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; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/SCEPManagerServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/SCEPManagerServiceComponent.java index 9c3b20a7a8..f59c04586a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/SCEPManagerServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/SCEPManagerServiceComponent.java @@ -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 + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index bd0223e962..aead0bda52 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -36,6 +36,10 @@ public interface DeviceManagementProviderService extends OperationManager { List 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; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 494b03da1d..f28c7f8bcf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -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 devices = new ArrayList<>(); + List 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) 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 devices = new ArrayList<>(); + List 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) 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 getAllDevices(String deviceType) throws DeviceManagementException { List devices = new ArrayList<>();