|
|
|
@ -1,19 +1,37 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
|
|
|
|
* Copyright (c) 2016, 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
|
|
|
|
|
* 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
|
|
|
|
|
* 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.
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2019, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Entgra (pvt) Ltd. 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;
|
|
|
|
@ -55,7 +73,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
String sql = "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, " +
|
|
|
|
|
"LAST_UPDATED_TIMESTAMP, TENANT_ID) " +
|
|
|
|
|
"VALUES (?, ?, ?, ?, ?, ?)";
|
|
|
|
|
stmt = conn.prepareStatement(sql, new String[] {"id"});
|
|
|
|
|
stmt = conn.prepareStatement(sql, new String[]{"id"});
|
|
|
|
|
stmt.setString(1, device.getDescription());
|
|
|
|
|
stmt.setString(2, device.getName());
|
|
|
|
|
stmt.setInt(3, typeId);
|
|
|
|
@ -87,7 +105,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
String sql = "UPDATE DM_DEVICE SET NAME = ?, DESCRIPTION = ?, LAST_UPDATED_TIMESTAMP = ? " +
|
|
|
|
|
"WHERE DEVICE_TYPE_ID = (SELECT ID FROM DM_DEVICE_TYPE WHERE NAME = ? AND (PROVIDER_TENANT_ID = ? OR SHARED_WITH_ALL_TENANTS = ?)) " +
|
|
|
|
|
"AND DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?";
|
|
|
|
|
stmt = conn.prepareStatement(sql, new String[] {"id"});
|
|
|
|
|
stmt = conn.prepareStatement(sql, new String[]{"id"});
|
|
|
|
|
stmt.setString(1, device.getName());
|
|
|
|
|
stmt.setString(2, device.getDescription());
|
|
|
|
|
stmt.setTimestamp(3, new Timestamp(new Date().getTime()));
|
|
|
|
@ -145,6 +163,53 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
return device;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Device getDevice(String deviceIdentifier, int tenantId) throws DeviceManagementDAOException {
|
|
|
|
|
Connection conn;
|
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
|
ResultSet rs = null;
|
|
|
|
|
Device device = 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, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION " +
|
|
|
|
|
"FROM " +
|
|
|
|
|
"DM_DEVICE d, DM_DEVICE_TYPE t " +
|
|
|
|
|
"WHERE " +
|
|
|
|
|
"t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 " +
|
|
|
|
|
"WHERE " +
|
|
|
|
|
"d1.ID = e.DEVICE_ID " +
|
|
|
|
|
"AND TENANT_ID = ? " +
|
|
|
|
|
"ORDER BY e.DATE_OF_LAST_UPDATE DESC, e.STATUS ASC";
|
|
|
|
|
// Status adeed as an orderby clause to fix a bug : when an existing device is
|
|
|
|
|
// re-enrolled, earlier enrollment is marked as removed and a new enrollment is added.
|
|
|
|
|
// However, both enrollments share the same time stamp. When retrieving the device
|
|
|
|
|
// due to same timestamp, enrollment information is incorrect, intermittently. Hence
|
|
|
|
|
// status also should be taken into consideration when ordering. This should not present a
|
|
|
|
|
// problem for other status transitions, as there would be an intermediary removed
|
|
|
|
|
// state in between.
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
stmt.setString(1, deviceIdentifier);
|
|
|
|
|
stmt.setInt(2, tenantId);
|
|
|
|
|
stmt.setInt(3, tenantId);
|
|
|
|
|
rs = stmt.executeQuery();
|
|
|
|
|
if (rs.next()) {
|
|
|
|
|
device = DeviceManagementDAOUtil.loadMatchingDevice(rs, false);
|
|
|
|
|
}
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new DeviceManagementDAOException("Error occurred while listing device " +
|
|
|
|
|
"'" + deviceIdentifier + "'", e);
|
|
|
|
|
} finally {
|
|
|
|
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
|
|
|
|
}
|
|
|
|
|
return device;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Device getDevice(DeviceIdentifier deviceIdentifier, String owner, int tenantId)
|
|
|
|
|
throws DeviceManagementDAOException {
|
|
|
|
@ -181,7 +246,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Device getDevice(DeviceIdentifier deviceIdentifier, Date since, int tenantId)
|
|
|
|
|
throws DeviceManagementDAOException {
|
|
|
|
|
throws DeviceManagementDAOException {
|
|
|
|
|
Connection conn;
|
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
|
ResultSet rs = null;
|
|
|
|
@ -189,11 +254,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
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, " +
|
|
|
|
|
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_DETAIL dt " +
|
|
|
|
|
"WHERE t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? AND dt.DEVICE_ID = d.ID " +
|
|
|
|
|
"AND dt.UPDATE_TIMESTAMP > ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC" ;
|
|
|
|
|
"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, " +
|
|
|
|
|
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_DETAIL dt " +
|
|
|
|
|
"WHERE t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? AND dt.DEVICE_ID = d.ID " +
|
|
|
|
|
"AND dt.UPDATE_TIMESTAMP > ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
int paramIdx = 1;
|
|
|
|
|
stmt.setString(paramIdx++, deviceIdentifier.getType());
|
|
|
|
@ -207,7 +272,52 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
}
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new DeviceManagementDAOException("Error occurred while listing device for type " +
|
|
|
|
|
"'" + deviceIdentifier.getType() + "'", e);
|
|
|
|
|
"'" + deviceIdentifier.getType() + "'", e);
|
|
|
|
|
} finally {
|
|
|
|
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
|
|
|
|
}
|
|
|
|
|
return device;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Device getDevice(String deviceIdentifier, Date since, int tenantId)
|
|
|
|
|
throws DeviceManagementDAOException {
|
|
|
|
|
Connection conn;
|
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
|
ResultSet rs = null;
|
|
|
|
|
Device device = 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, " +
|
|
|
|
|
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION " +
|
|
|
|
|
"FROM" +
|
|
|
|
|
" DM_DEVICE d, DM_DEVICE_TYPE t," +
|
|
|
|
|
" DM_DEVICE_DETAIL dt " +
|
|
|
|
|
"WHERE " +
|
|
|
|
|
"t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? AND" +
|
|
|
|
|
" dt.DEVICE_ID = d.ID AND dt.UPDATE_TIMESTAMP > ?) d1 " +
|
|
|
|
|
"WHERE" +
|
|
|
|
|
" d1.ID = e.DEVICE_ID AND TENANT_ID = ? " +
|
|
|
|
|
"ORDER BY " +
|
|
|
|
|
"e.DATE_OF_LAST_UPDATE DESC";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
int paramIdx = 1;
|
|
|
|
|
stmt.setString(paramIdx++, deviceIdentifier);
|
|
|
|
|
stmt.setInt(paramIdx++, tenantId);
|
|
|
|
|
stmt.setLong(paramIdx++, since.getTime());
|
|
|
|
|
stmt.setInt(paramIdx, tenantId);
|
|
|
|
|
rs = stmt.executeQuery();
|
|
|
|
|
if (rs.next()) {
|
|
|
|
|
device = DeviceManagementDAOUtil.loadMatchingDevice(rs, false);
|
|
|
|
|
}
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new DeviceManagementDAOException("Error occurred while listing device for id " +
|
|
|
|
|
"'" + deviceIdentifier + "'", e);
|
|
|
|
|
} finally {
|
|
|
|
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
|
|
|
|
}
|
|
|
|
@ -228,7 +338,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " +
|
|
|
|
|
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_DETAIL dt " +
|
|
|
|
|
"WHERE t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? AND dt.DEVICE_ID = d.ID " +
|
|
|
|
|
"AND dt.UPDATE_TIMESTAMP > ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? AND e.OWNER = ?" ;
|
|
|
|
|
"AND dt.UPDATE_TIMESTAMP > ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? AND e.OWNER = ?";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
stmt.setString(1, deviceIdentifier.getType());
|
|
|
|
|
stmt.setString(2, deviceIdentifier.getId());
|
|
|
|
@ -250,8 +360,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Device getDevice(DeviceIdentifier deviceIdentifier, EnrolmentInfo.Status status, int tenantId) throws
|
|
|
|
|
DeviceManagementDAOException {
|
|
|
|
|
public Device getDevice(DeviceIdentifier deviceIdentifier, EnrolmentInfo.Status status, int tenantId)
|
|
|
|
|
throws DeviceManagementDAOException {
|
|
|
|
|
Connection conn;
|
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
|
ResultSet rs = null;
|
|
|
|
@ -259,11 +369,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
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, " +
|
|
|
|
|
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " +
|
|
|
|
|
"t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " +
|
|
|
|
|
"AND TENANT_ID = ? AND e.STATUS = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC";
|
|
|
|
|
"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, " +
|
|
|
|
|
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " +
|
|
|
|
|
"t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " +
|
|
|
|
|
"AND TENANT_ID = ? AND e.STATUS = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
stmt.setString(1, deviceIdentifier.getType());
|
|
|
|
|
stmt.setString(2, deviceIdentifier.getId());
|
|
|
|
@ -276,7 +386,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
}
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new DeviceManagementDAOException("Error occurred while listing devices for type " +
|
|
|
|
|
"'" + deviceIdentifier.getType() + "'", e);
|
|
|
|
|
"'" + deviceIdentifier.getType() + "'", e);
|
|
|
|
|
} finally {
|
|
|
|
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
|
|
|
|
}
|
|
|
|
@ -293,13 +403,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
try {
|
|
|
|
|
conn = this.getConnection();
|
|
|
|
|
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, e.TENANT_ID, " +
|
|
|
|
|
"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, " +
|
|
|
|
|
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " +
|
|
|
|
|
"t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? ) d1 WHERE d1.ID = e.DEVICE_ID ORDER BY e.DATE_OF_LAST_UPDATE DESC";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
stmt.setString(1, deviceIdentifier.getType());
|
|
|
|
|
stmt.setString(2, deviceIdentifier.getId());
|
|
|
|
|
"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, " +
|
|
|
|
|
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " +
|
|
|
|
|
"t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? ) d1 WHERE d1.ID = e.DEVICE_ID ORDER BY e.DATE_OF_LAST_UPDATE DESC";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
stmt.setString(1, deviceIdentifier.getType());
|
|
|
|
|
stmt.setString(2, deviceIdentifier.getId());
|
|
|
|
|
rs = stmt.executeQuery();
|
|
|
|
|
if (rs.next()) {
|
|
|
|
|
device = DeviceManagementDAOUtil.loadDevice(rs);
|
|
|
|
@ -323,11 +433,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
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, " +
|
|
|
|
|
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " +
|
|
|
|
|
"d.ID = ? AND t.ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " +
|
|
|
|
|
"AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC";
|
|
|
|
|
"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, " +
|
|
|
|
|
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " +
|
|
|
|
|
"d.ID = ? AND t.ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID " +
|
|
|
|
|
"AND TENANT_ID = ? ORDER BY e.DATE_OF_LAST_UPDATE DESC";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
stmt.setInt(1, deviceId);
|
|
|
|
|
stmt.setInt(2, tenantId);
|
|
|
|
@ -338,7 +448,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
}
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new DeviceManagementDAOException("Error occurred while retrieving device for id " +
|
|
|
|
|
"'" + deviceId + "'", e);
|
|
|
|
|
"'" + deviceId + "'", e);
|
|
|
|
|
} finally {
|
|
|
|
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
|
|
|
|
}
|
|
|
|
@ -421,11 +531,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
try {
|
|
|
|
|
conn = this.getConnection();
|
|
|
|
|
String sql = "SELECT e1.OWNER, e1.OWNERSHIP, e1.ENROLMENT_ID, e1.DEVICE_ID, e1.STATUS, e1.DATE_OF_LAST_UPDATE," +
|
|
|
|
|
" e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DEVICE_IDENTIFICATION, t.NAME " +
|
|
|
|
|
"AS DEVICE_TYPE FROM DM_DEVICE d, (SELECT e.OWNER, e.OWNERSHIP, e.ID AS ENROLMENT_ID, " +
|
|
|
|
|
"e.DEVICE_ID, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE " +
|
|
|
|
|
"e.TENANT_ID = ? AND LOWER(e.OWNER) = LOWER(?) ORDER BY e.DATE_OF_LAST_UPDATE DESC) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " +
|
|
|
|
|
"AND t.ID = d.DEVICE_TYPE_ID";
|
|
|
|
|
" e1.DATE_OF_ENROLMENT, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DEVICE_IDENTIFICATION, t.NAME " +
|
|
|
|
|
"AS DEVICE_TYPE FROM DM_DEVICE d, (SELECT e.OWNER, e.OWNERSHIP, e.ID AS ENROLMENT_ID, " +
|
|
|
|
|
"e.DEVICE_ID, e.STATUS, e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT FROM DM_ENROLMENT e WHERE " +
|
|
|
|
|
"e.TENANT_ID = ? AND LOWER(e.OWNER) = LOWER(?) ORDER BY e.DATE_OF_LAST_UPDATE DESC) e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID " +
|
|
|
|
|
"AND t.ID = d.DEVICE_TYPE_ID";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
stmt.setInt(1, tenantId);
|
|
|
|
|
stmt.setString(2, username);
|
|
|
|
@ -497,8 +607,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
try {
|
|
|
|
|
conn = this.getConnection();
|
|
|
|
|
String sql = "SELECT COUNT(d1.DEVICE_ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID FROM " +
|
|
|
|
|
"DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 WHERE " +
|
|
|
|
|
"d1.DEVICE_ID = e.DEVICE_ID AND LOWER(e.OWNER) = LOWER(?) AND TENANT_ID = ?";
|
|
|
|
|
"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 LOWER(e.OWNER) = LOWER(?) AND TENANT_ID = ?";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
stmt.setInt(1, tenantId);
|
|
|
|
|
stmt.setString(2, username);
|
|
|
|
@ -580,7 +690,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean setEnrolmentStatusInBulk(String deviceType, String status,
|
|
|
|
|
int tenantId, List<String> devices) throws DeviceManagementDAOException {
|
|
|
|
|
int tenantId, List<String> devices) throws DeviceManagementDAOException {
|
|
|
|
|
Connection conn;
|
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
|
try {
|
|
|
|
@ -625,8 +735,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
try {
|
|
|
|
|
conn = this.getConnection();
|
|
|
|
|
String sql = "SELECT COUNT(d1.DEVICE_ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID FROM " +
|
|
|
|
|
"DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 WHERE " +
|
|
|
|
|
"d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?";
|
|
|
|
|
"DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?) d1 WHERE " +
|
|
|
|
|
"d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
stmt.setInt(1, tenantId);
|
|
|
|
|
stmt.setInt(2, tenantId);
|
|
|
|
@ -665,7 +775,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
try {
|
|
|
|
|
conn = this.getConnection();
|
|
|
|
|
String sql = "SELECT COUNT(d1.ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, d.DEVICE_IDENTIFICATION, " +
|
|
|
|
|
"t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t";
|
|
|
|
|
"t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, DM_DEVICE_TYPE t";
|
|
|
|
|
|
|
|
|
|
//Add query for last updated timestamp
|
|
|
|
|
if (since != null) {
|
|
|
|
@ -739,7 +849,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
}
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new DeviceManagementDAOException("Error occurred while retrieving information of all " +
|
|
|
|
|
"registered devices", e);
|
|
|
|
|
"registered devices", e);
|
|
|
|
|
} finally {
|
|
|
|
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
|
|
|
|
}
|
|
|
|
@ -755,8 +865,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
try {
|
|
|
|
|
conn = this.getConnection();
|
|
|
|
|
String sql = "SELECT COUNT(d1.ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d, " +
|
|
|
|
|
"DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " +
|
|
|
|
|
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?";
|
|
|
|
|
"DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " +
|
|
|
|
|
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
stmt.setString(1, type);
|
|
|
|
|
stmt.setInt(2, tenantId);
|
|
|
|
@ -781,8 +891,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
try {
|
|
|
|
|
conn = this.getConnection();
|
|
|
|
|
String sql = "SELECT COUNT(e1.DEVICE_ID) AS DEVICE_COUNT FROM DM_DEVICE d, (SELECT e.DEVICE_ID " +
|
|
|
|
|
"FROM DM_ENROLMENT e WHERE e.TENANT_ID = ? AND LOWER(e.OWNER) = LOWER(?)) " +
|
|
|
|
|
"e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID AND t.ID = d.DEVICE_TYPE_ID";
|
|
|
|
|
"FROM DM_ENROLMENT e WHERE e.TENANT_ID = ? AND LOWER(e.OWNER) = LOWER(?)) " +
|
|
|
|
|
"e1, DM_DEVICE_TYPE t WHERE d.ID = e1.DEVICE_ID AND t.ID = d.DEVICE_TYPE_ID";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
stmt.setInt(1, tenantId);
|
|
|
|
|
stmt.setString(2, username);
|
|
|
|
@ -793,7 +903,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
}
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices belongs to '" +
|
|
|
|
|
username + "'", e);
|
|
|
|
|
username + "'", e);
|
|
|
|
|
} finally {
|
|
|
|
|
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
|
|
|
|
}
|
|
|
|
@ -808,8 +918,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
try {
|
|
|
|
|
conn = this.getConnection();
|
|
|
|
|
String sql = "SELECT COUNT(d1.ID) AS DEVICE_COUNT FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d, " +
|
|
|
|
|
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " +
|
|
|
|
|
"WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?";
|
|
|
|
|
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " +
|
|
|
|
|
"WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
stmt.setString(1, deviceName + "%");
|
|
|
|
|
stmt.setInt(2, tenantId);
|
|
|
|
@ -821,7 +931,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
}
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new DeviceManagementDAOException("Error occurred while fetching the device count that matches " +
|
|
|
|
|
"'" + deviceName + "'", e);
|
|
|
|
|
"'" + deviceName + "'", e);
|
|
|
|
|
} finally {
|
|
|
|
|
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
|
|
|
|
}
|
|
|
|
@ -836,8 +946,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
try {
|
|
|
|
|
conn = this.getConnection();
|
|
|
|
|
String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " +
|
|
|
|
|
"TENANT_ID = ? AND OWNERSHIP = ?) e, DM_DEVICE d, " +
|
|
|
|
|
"DM_DEVICE_TYPE t WHERE d.ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?";
|
|
|
|
|
"TENANT_ID = ? AND OWNERSHIP = ?) e, DM_DEVICE d, " +
|
|
|
|
|
"DM_DEVICE_TYPE t WHERE d.ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
stmt.setInt(1, tenantId);
|
|
|
|
|
stmt.setString(2, ownerShip);
|
|
|
|
@ -849,7 +959,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
}
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to ownership " +
|
|
|
|
|
"'" + ownerShip + "'", e);
|
|
|
|
|
"'" + ownerShip + "'", e);
|
|
|
|
|
} finally {
|
|
|
|
|
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
|
|
|
|
}
|
|
|
|
@ -923,7 +1033,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
conn = this.getConnection();
|
|
|
|
|
String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS,DATE_OF_ENROLMENT, " +
|
|
|
|
|
"DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)";
|
|
|
|
|
stmt = conn.prepareStatement(sql, new String[] {"id"});
|
|
|
|
|
stmt = conn.prepareStatement(sql, new String[]{"id"});
|
|
|
|
|
stmt.setInt(1, device.getId());
|
|
|
|
|
stmt.setString(2, device.getEnrolmentInfo().getOwner());
|
|
|
|
|
stmt.setString(3, device.getEnrolmentInfo().getOwnership().toString());
|
|
|
|
@ -1044,10 +1154,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
try {
|
|
|
|
|
conn = this.getConnection();
|
|
|
|
|
String sql = "SELECT ID AS ENROLMENT_ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, DATE_OF_ENROLMENT, " +
|
|
|
|
|
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID " +
|
|
|
|
|
"FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " +
|
|
|
|
|
"AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " +
|
|
|
|
|
"AND TENANT_ID = ? AND STATUS in ('ACTIVE','UNREACHABLE','INACTIVE')";
|
|
|
|
|
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID " +
|
|
|
|
|
"FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " +
|
|
|
|
|
"AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " +
|
|
|
|
|
"AND TENANT_ID = ? AND STATUS in ('ACTIVE','UNREACHABLE','INACTIVE')";
|
|
|
|
|
stmt = conn.prepareStatement(sql);
|
|
|
|
|
stmt.setString(1, deviceId.getId());
|
|
|
|
|
stmt.setString(2, deviceId.getType());
|
|
|
|
@ -1060,8 +1170,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
return enrolmentInfo;
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
|
|
|
|
|
"information of device '" + deviceId.getId() + "' of type : "
|
|
|
|
|
+ deviceId.getType(), e);
|
|
|
|
|
"information of device '" + deviceId.getId() + "' of type : "
|
|
|
|
|
+ deviceId.getType(), e);
|
|
|
|
|
} finally {
|
|
|
|
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
|
|
|
|
}
|
|
|
|
@ -1130,7 +1240,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
*
|
|
|
|
|
* @param timestamp Timestamp in long, after which the devices have been updated.
|
|
|
|
|
* @param tenantId Tenant id of the currently logged in user.
|
|
|
|
|
* @return A collection of devices that have been updated after the provided timestamp
|
|
|
|
|
* @return A collection of devices that have been updated after the provided timestamp
|
|
|
|
|
* @throws DeviceManagementDAOException
|
|
|
|
|
*/
|
|
|
|
|
public List<Device> getDevices(long timestamp, int tenantId) throws DeviceManagementDAOException {
|
|
|
|
@ -1194,7 +1304,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
List<GeoCluster> geoClusters = new ArrayList<>();
|
|
|
|
|
try {
|
|
|
|
|
conn = this.getConnection();
|
|
|
|
|
String sql ="SELECT AVG(DEVICE_LOCATION.LATITUDE) AS LATITUDE,AVG(DEVICE_LOCATION.LONGITUDE) AS LONGITUDE," +
|
|
|
|
|
String sql = "SELECT AVG(DEVICE_LOCATION.LATITUDE) AS LATITUDE,AVG(DEVICE_LOCATION.LONGITUDE) AS LONGITUDE," +
|
|
|
|
|
" MIN(DEVICE_LOCATION.LATITUDE) AS MIN_LATITUDE, MAX(DEVICE_LOCATION.LATITUDE) AS MAX_LATITUDE," +
|
|
|
|
|
" MIN(DEVICE_LOCATION.LONGITUDE) AS MIN_LONGITUDE," +
|
|
|
|
|
" MAX(DEVICE_LOCATION.LONGITUDE) AS MAX_LONGITUDE," +
|
|
|
|
@ -1217,7 +1327,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
stmt.setDouble(3, northEast.getLatitude());
|
|
|
|
|
stmt.setDouble(4, southWest.getLongitude());
|
|
|
|
|
stmt.setDouble(5, northEast.getLongitude());
|
|
|
|
|
stmt.setDouble(6,tenantId);
|
|
|
|
|
stmt.setDouble(6, tenantId);
|
|
|
|
|
if (deviceType != null && !deviceType.isEmpty()) {
|
|
|
|
|
stmt.setString(7, deviceType);
|
|
|
|
|
}
|
|
|
|
@ -1230,13 +1340,13 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
double min_longitude = rs.getDouble("MIN_LONGITUDE");
|
|
|
|
|
double max_longitude = rs.getDouble("MAX_LONGITUDE");
|
|
|
|
|
String device_identification = rs.getString("DEVICE_IDENTIFICATION");
|
|
|
|
|
String device_type=rs.getString("TYPE");
|
|
|
|
|
String device_type = rs.getString("TYPE");
|
|
|
|
|
String last_seen = rs.getString("LAST_UPDATED_TIMESTAMP");
|
|
|
|
|
long count = rs.getLong("COUNT");
|
|
|
|
|
String geohashPrefix = rs.getString("GEOHASH_PREFIX");
|
|
|
|
|
geoClusters.add(new GeoCluster(new GeoCoordinate(latitude, longitude),
|
|
|
|
|
new GeoCoordinate(min_latitude,min_longitude), new GeoCoordinate(max_latitude,max_longitude),
|
|
|
|
|
count, geohashPrefix,device_identification,device_type,last_seen));
|
|
|
|
|
new GeoCoordinate(min_latitude, min_longitude), new GeoCoordinate(max_latitude, max_longitude),
|
|
|
|
|
count, geohashPrefix, device_identification, device_type, last_seen));
|
|
|
|
|
}
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new DeviceManagementDAOException("Error occurred while retrieving information of " +
|
|
|
|
|