forked from community/device-mgt-core
commit
129d49e897
@ -0,0 +1,555 @@
|
||||
/*
|
||||
* Copyright (c) 2023, 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.enrolment;
|
||||
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
||||
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||
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.impl.AbstractEnrollmentDAOImpl;
|
||||
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class GenericEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl {
|
||||
|
||||
@Override
|
||||
public EnrolmentInfo addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
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"});
|
||||
Timestamp enrollmentTime = new Timestamp(new Date().getTime());
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setString(2, enrolmentInfo.getOwner());
|
||||
stmt.setString(3, enrolmentInfo.getOwnership().toString());
|
||||
stmt.setString(4, enrolmentInfo.getStatus().toString());
|
||||
stmt.setTimestamp(5, enrollmentTime);
|
||||
stmt.setTimestamp(6, enrollmentTime);
|
||||
stmt.setInt(7, tenantId);
|
||||
stmt.execute();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
int enrolmentId = rs.getInt(1);
|
||||
enrolmentInfo.setId(enrolmentId);
|
||||
enrolmentInfo.setDateOfEnrolment(enrollmentTime.getTime());
|
||||
enrolmentInfo.setDateOfLastUpdate(enrollmentTime.getTime());
|
||||
addDeviceStatus(enrolmentId, enrolmentInfo.getStatus());
|
||||
return enrolmentInfo;
|
||||
}
|
||||
return null;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
throw new DeviceManagementDAOException("Error occurred while adding enrolment configuration", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updateEnrollment(EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, DATE_OF_LAST_UPDATE = ? " +
|
||||
"WHERE ID = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, enrolmentInfo.getOwnership().toString());
|
||||
stmt.setString(2, enrolmentInfo.getStatus().toString());
|
||||
stmt.setTimestamp(3, new Timestamp(new Date().getTime()));
|
||||
stmt.setInt(4, enrolmentInfo.getId());
|
||||
stmt.setInt(5, tenantId);
|
||||
int updatedCount = stmt.executeUpdate();
|
||||
if (updatedCount == 1){
|
||||
addDeviceStatus(enrolmentInfo.getId(), enrolmentInfo.getStatus());
|
||||
}
|
||||
return updatedCount;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateEnrollmentStatus(List<EnrolmentInfo> enrolmentInfos) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
boolean status = false;
|
||||
int updateStatus = -1;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
if (conn.getMetaData().supportsBatchUpdates()) {
|
||||
for (EnrolmentInfo enrolmentInfo : enrolmentInfos) {
|
||||
stmt.setString(1, enrolmentInfo.getStatus().toString());
|
||||
stmt.setInt(2, enrolmentInfo.getId());
|
||||
stmt.addBatch();
|
||||
}
|
||||
updateStatus = stmt.executeBatch().length;
|
||||
} else {
|
||||
for (EnrolmentInfo enrolmentInfo : enrolmentInfos) {
|
||||
stmt.setString(1, enrolmentInfo.getStatus().toString());
|
||||
stmt.setInt(2, enrolmentInfo.getId());
|
||||
updateStatus = stmt.executeUpdate();
|
||||
}
|
||||
}
|
||||
if (updateStatus > 0) {
|
||||
status = true;
|
||||
for (EnrolmentInfo enrolmentInfo : enrolmentInfos) {
|
||||
addDeviceStatus(enrolmentInfo);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while updating enrolment status of given device-list.", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int removeEnrollment(int deviceId, String currentOwner,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
int status = -1;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql, new String[] {"id"});
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setString(2, currentOwner);
|
||||
stmt.setInt(3, tenantId);
|
||||
stmt.executeUpdate();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
status = 1;
|
||||
}
|
||||
return status;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while removing device enrolment", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
private int getCountOfDevicesOfOwner(String owner, int tenantID) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
int count = 0;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String checkQuery = "SELECT COUNT(ID) AS COUNT FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(checkQuery);
|
||||
stmt.setString(1, owner);
|
||||
stmt.setInt(2, tenantID);
|
||||
rs = stmt.executeQuery();
|
||||
if(rs.next()){
|
||||
count = rs.getInt("COUNT");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while trying to get device " +
|
||||
"count of Owner : "+owner, e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setStatus(String currentOwner, EnrolmentInfo.Status status,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
return setStatusAllDevices(currentOwner, status, tenantId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setStatusAllDevices(String currentOwner, EnrolmentInfo.Status status, int tenantId)
|
||||
throws DeviceManagementDAOException{
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
Timestamp updateTime = new Timestamp(new Date().getTime());
|
||||
if(getCountOfDevicesOfOwner(currentOwner, tenantId) > 0){
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, status.toString());
|
||||
stmt.setTimestamp(2, updateTime);
|
||||
stmt.setString(3, currentOwner);
|
||||
stmt.setInt(4, tenantId);
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||
}
|
||||
return addDeviceStatus(currentOwner, status, tenantId);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setStatus(int enrolmentID, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
Timestamp updateTime = new Timestamp(new Date().getTime());
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE ID = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, status.toString());
|
||||
stmt.setTimestamp(2, updateTime);
|
||||
stmt.setInt(3, enrolmentID);
|
||||
stmt.setInt(4, tenantId);
|
||||
int updatedRowCount = stmt.executeUpdate();
|
||||
if (updatedRowCount != 1){
|
||||
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: "+
|
||||
updatedRowCount + " rows were updated instead of one row!!!");
|
||||
}
|
||||
// save the device status history
|
||||
addDeviceStatus(enrolmentID, status);
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException {
|
||||
return addDeviceStatus(config.getId(), config.getStatus());
|
||||
}
|
||||
|
||||
private boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
|
||||
if (changedBy == null){
|
||||
changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER;
|
||||
}
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
List<int[]> enrolmentInfoList = new ArrayList<>();
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
|
||||
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, currentOwner);
|
||||
stmt.setInt(2, tenantId);
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
int enrolmentId = rs.getInt("ID");
|
||||
int deviceId = rs.getInt("DEVICE_ID");
|
||||
enrolmentInfoList.add(new int[]{enrolmentId, deviceId});
|
||||
}
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
Timestamp updateTime = new Timestamp(new Date().getTime());
|
||||
sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)";
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
if (conn.getMetaData().supportsBatchUpdates()) {
|
||||
for(int[] info: enrolmentInfoList){
|
||||
ps.setInt(1, info[0]);
|
||||
ps.setInt(2, info[1]);
|
||||
ps.setString(3, status.toString());
|
||||
ps.setTimestamp(4, updateTime);
|
||||
ps.setString(5, changedBy);
|
||||
ps.addBatch();
|
||||
}
|
||||
int[] batchResult = ps.executeBatch();
|
||||
for (int i : batchResult) {
|
||||
if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for(int[] info: enrolmentInfoList){
|
||||
ps.setInt(1, info[0]);
|
||||
ps.setInt(2, info[1]);
|
||||
ps.setString(3, status.toString());
|
||||
ps.setTimestamp(4, updateTime);
|
||||
ps.setString(5, changedBy);
|
||||
ps.execute();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " +
|
||||
"information of owner '" + currentOwner + "'", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
|
||||
if (changedBy == null){
|
||||
changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER;
|
||||
}
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
// get the device id and last udpated status from the device status table
|
||||
String sql = "SELECT DEVICE_ID, STATUS FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? ORDER BY UPDATE_TIME DESC LIMIT 1";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, enrolmentId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
int deviceId = -1;
|
||||
EnrolmentInfo.Status previousStatus = null;
|
||||
if (rs.next()) {
|
||||
// if there is a record corresponding to the enrolment we save the status and the device id
|
||||
previousStatus = EnrolmentInfo.Status.valueOf(rs.getString("STATUS"));
|
||||
deviceId = rs.getInt("DEVICE_ID");
|
||||
}
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||
// if there was no record for the enrolment or the previous status is not the same as the current status
|
||||
// we'll add a record
|
||||
if (previousStatus == null || previousStatus != status){
|
||||
if (deviceId == -1) {
|
||||
// we need the device id in order to add a new record, therefore we get it from the enrolment table
|
||||
sql = "SELECT DEVICE_ID FROM DM_ENROLMENT WHERE ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, enrolmentId);
|
||||
rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
deviceId = rs.getInt("DEVICE_ID");
|
||||
} else {
|
||||
// if there were no records corresponding to the enrolment id this is a problem. i.e. enrolment
|
||||
// id is invalid
|
||||
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: no record for enrolment id " + enrolmentId);
|
||||
}
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||
}
|
||||
|
||||
sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
Timestamp updateTime = new Timestamp(new Date().getTime());
|
||||
stmt.setInt(1, enrolmentId);
|
||||
stmt.setInt(2, deviceId);
|
||||
stmt.setString(3, status.toString());
|
||||
stmt.setTimestamp(4, updateTime);
|
||||
stmt.setString(5, changedBy);
|
||||
stmt.execute();
|
||||
} else {
|
||||
// no need to update status since the last recorded status is the same as the current status
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while setting the status of device", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public EnrolmentInfo.Status getStatus(int deviceId, String currentOwner,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
EnrolmentInfo.Status status = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setString(2, currentOwner);
|
||||
stmt.setInt(3, tenantId);
|
||||
rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
status = EnrolmentInfo.Status.valueOf(rs.getString("STATUS"));
|
||||
}
|
||||
return status;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnrolmentInfo getEnrollment(int deviceId, String currentOwner,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
EnrolmentInfo enrolmentInfo = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
|
||||
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setString(2, currentOwner);
|
||||
stmt.setInt(3, tenantId);
|
||||
rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
enrolmentInfo = this.loadEnrolment(rs);
|
||||
}
|
||||
return enrolmentInfo;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
|
||||
"information of user '" + currentOwner + "' upon device '" + deviceId + "'", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnrolmentInfo getEnrollment(int deviceId, int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
EnrolmentInfo enrolmentInfo = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
|
||||
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ? " +
|
||||
"ORDER BY DATE_OF_LAST_UPDATE DESC";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setInt(2, tenantId);
|
||||
rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
enrolmentInfo = this.loadEnrolment(rs);
|
||||
}
|
||||
return enrolmentInfo;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
|
||||
"information of device '" + deviceId + "'", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<EnrolmentInfo> getEnrollmentsOfUser(int deviceId, String user, int tenantId)
|
||||
throws DeviceManagementDAOException {
|
||||
List<EnrolmentInfo> enrolmentInfos = new ArrayList<>();
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
EnrolmentInfo enrolmentInfo = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
|
||||
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setString(2, user);
|
||||
stmt.setInt(3, tenantId);
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
enrolmentInfo = this.loadEnrolment(rs);
|
||||
enrolmentInfos.add(enrolmentInfo);
|
||||
}
|
||||
return enrolmentInfos;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " +
|
||||
"information of user '" + user + "' upon device '" + deviceId + "'", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateOwnerOfEnrollment(List<Device> devices, String owner, int tenantId)
|
||||
throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
boolean updateStatus = true;
|
||||
String sql = "UPDATE DM_ENROLMENT "
|
||||
+ "SET OWNER = ?, IS_TRANSFERRED = ?, DATE_OF_LAST_UPDATE = ? "
|
||||
+ "WHERE ID = ? AND TENANT_ID = ?";
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
if (conn.getMetaData().supportsBatchUpdates()) {
|
||||
for (Device device : devices) {
|
||||
ps.setString(1, owner);
|
||||
ps.setBoolean(2, device.getEnrolmentInfo().isTransferred());
|
||||
ps.setTimestamp(3, new Timestamp(new Date().getTime()));
|
||||
ps.setInt(4, device.getEnrolmentInfo().getId());
|
||||
ps.setInt(5, tenantId);
|
||||
ps.addBatch();
|
||||
}
|
||||
int[] batchResult = ps.executeBatch();
|
||||
for (int i : batchResult) {
|
||||
if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) {
|
||||
updateStatus = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (Device device : devices) {
|
||||
ps.setString(1, owner);
|
||||
ps.setBoolean(2, device.getEnrolmentInfo().isTransferred());
|
||||
ps.setInt(3, device.getId());
|
||||
ps.setInt(4, tenantId);
|
||||
if (ps.executeUpdate() == 0) {
|
||||
updateStatus = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return updateStatus;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while obtaining the DB connection to update the "
|
||||
+ "owner of the device enrollment.", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
|
||||
private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException {
|
||||
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
||||
enrolmentInfo.setOwner(rs.getString("OWNER"));
|
||||
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP")));
|
||||
enrolmentInfo.setTransferred(rs.getBoolean("IS_TRANSFERRED"));
|
||||
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")));
|
||||
enrolmentInfo.setId(rs.getInt("ID"));
|
||||
return enrolmentInfo;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,554 @@
|
||||
/*
|
||||
* Copyright (c) 2023, 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.enrolment;
|
||||
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
||||
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||
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.impl.AbstractEnrollmentDAOImpl;
|
||||
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class SQLServerEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl {
|
||||
|
||||
@Override
|
||||
public EnrolmentInfo addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
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"});
|
||||
Timestamp enrollmentTime = new Timestamp(new Date().getTime());
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setString(2, enrolmentInfo.getOwner());
|
||||
stmt.setString(3, enrolmentInfo.getOwnership().toString());
|
||||
stmt.setString(4, enrolmentInfo.getStatus().toString());
|
||||
stmt.setTimestamp(5, enrollmentTime);
|
||||
stmt.setTimestamp(6, enrollmentTime);
|
||||
stmt.setInt(7, tenantId);
|
||||
stmt.execute();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
int enrolmentId = rs.getInt(1);
|
||||
enrolmentInfo.setId(enrolmentId);
|
||||
enrolmentInfo.setDateOfEnrolment(enrollmentTime.getTime());
|
||||
enrolmentInfo.setDateOfLastUpdate(enrollmentTime.getTime());
|
||||
addDeviceStatus(enrolmentId, enrolmentInfo.getStatus());
|
||||
return enrolmentInfo;
|
||||
}
|
||||
return null;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
throw new DeviceManagementDAOException("Error occurred while adding enrolment configuration", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updateEnrollment(EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, DATE_OF_LAST_UPDATE = ? " +
|
||||
"WHERE ID = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, enrolmentInfo.getOwnership().toString());
|
||||
stmt.setString(2, enrolmentInfo.getStatus().toString());
|
||||
stmt.setTimestamp(3, new Timestamp(new Date().getTime()));
|
||||
stmt.setInt(4, enrolmentInfo.getId());
|
||||
stmt.setInt(5, tenantId);
|
||||
int updatedCount = stmt.executeUpdate();
|
||||
if (updatedCount == 1){
|
||||
addDeviceStatus(enrolmentInfo.getId(), enrolmentInfo.getStatus());
|
||||
}
|
||||
return updatedCount;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateEnrollmentStatus(List<EnrolmentInfo> enrolmentInfos) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
boolean status = false;
|
||||
int updateStatus = -1;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
if (conn.getMetaData().supportsBatchUpdates()) {
|
||||
for (EnrolmentInfo enrolmentInfo : enrolmentInfos) {
|
||||
stmt.setString(1, enrolmentInfo.getStatus().toString());
|
||||
stmt.setInt(2, enrolmentInfo.getId());
|
||||
stmt.addBatch();
|
||||
}
|
||||
updateStatus = stmt.executeBatch().length;
|
||||
} else {
|
||||
for (EnrolmentInfo enrolmentInfo : enrolmentInfos) {
|
||||
stmt.setString(1, enrolmentInfo.getStatus().toString());
|
||||
stmt.setInt(2, enrolmentInfo.getId());
|
||||
updateStatus = stmt.executeUpdate();
|
||||
}
|
||||
}
|
||||
if (updateStatus > 0) {
|
||||
status = true;
|
||||
for (EnrolmentInfo enrolmentInfo : enrolmentInfos) {
|
||||
addDeviceStatus(enrolmentInfo);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while updating enrolment status of given device-list.", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int removeEnrollment(int deviceId, String currentOwner,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
int status = -1;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql, new String[] {"id"});
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setString(2, currentOwner);
|
||||
stmt.setInt(3, tenantId);
|
||||
stmt.executeUpdate();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
status = 1;
|
||||
}
|
||||
return status;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while removing device enrolment", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
private int getCountOfDevicesOfOwner(String owner, int tenantID) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
int count = 0;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String checkQuery = "SELECT COUNT(ID) AS COUNT FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(checkQuery);
|
||||
stmt.setString(1, owner);
|
||||
stmt.setInt(2, tenantID);
|
||||
rs = stmt.executeQuery();
|
||||
if(rs.next()){
|
||||
count = rs.getInt("COUNT");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while trying to get device " +
|
||||
"count of Owner : "+owner, e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setStatus(String currentOwner, EnrolmentInfo.Status status,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
return setStatusAllDevices(currentOwner, status, tenantId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setStatusAllDevices(String currentOwner, EnrolmentInfo.Status status, int tenantId)
|
||||
throws DeviceManagementDAOException{
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
Timestamp updateTime = new Timestamp(new Date().getTime());
|
||||
if(getCountOfDevicesOfOwner(currentOwner, tenantId) > 0){
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, status.toString());
|
||||
stmt.setTimestamp(2, updateTime);
|
||||
stmt.setString(3, currentOwner);
|
||||
stmt.setInt(4, tenantId);
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||
}
|
||||
return addDeviceStatus(currentOwner, status, tenantId);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setStatus(int enrolmentID, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
Timestamp updateTime = new Timestamp(new Date().getTime());
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE ID = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, status.toString());
|
||||
stmt.setTimestamp(2, updateTime);
|
||||
stmt.setInt(3, enrolmentID);
|
||||
stmt.setInt(4, tenantId);
|
||||
int updatedRowCount = stmt.executeUpdate();
|
||||
if (updatedRowCount != 1){
|
||||
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: "+
|
||||
updatedRowCount + " rows were updated instead of one row!!!");
|
||||
}
|
||||
// save the device status history
|
||||
addDeviceStatus(enrolmentID, status);
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException {
|
||||
return addDeviceStatus(config.getId(), config.getStatus());
|
||||
}
|
||||
|
||||
private boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
|
||||
if (changedBy == null){
|
||||
changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER;
|
||||
}
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
List<int[]> enrolmentInfoList = new ArrayList<>();
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
|
||||
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, currentOwner);
|
||||
stmt.setInt(2, tenantId);
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
int enrolmentId = rs.getInt("ID");
|
||||
int deviceId = rs.getInt("DEVICE_ID");
|
||||
enrolmentInfoList.add(new int[]{enrolmentId, deviceId});
|
||||
}
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
Timestamp updateTime = new Timestamp(new Date().getTime());
|
||||
sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)";
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
if (conn.getMetaData().supportsBatchUpdates()) {
|
||||
for(int[] info: enrolmentInfoList){
|
||||
ps.setInt(1, info[0]);
|
||||
ps.setInt(2, info[1]);
|
||||
ps.setString(3, status.toString());
|
||||
ps.setTimestamp(4, updateTime);
|
||||
ps.setString(5, changedBy);
|
||||
ps.addBatch();
|
||||
}
|
||||
int[] batchResult = ps.executeBatch();
|
||||
for (int i : batchResult) {
|
||||
if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for(int[] info: enrolmentInfoList){
|
||||
ps.setInt(1, info[0]);
|
||||
ps.setInt(2, info[1]);
|
||||
ps.setString(3, status.toString());
|
||||
ps.setTimestamp(4, updateTime);
|
||||
ps.setString(5, changedBy);
|
||||
ps.execute();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " +
|
||||
"information of owner '" + currentOwner + "'", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
|
||||
if (changedBy == null){
|
||||
changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER;
|
||||
}
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
// get the device id and last udpated status from the device status table
|
||||
String sql = "SELECT TOP 1 DEVICE_ID, STATUS FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? ORDER BY UPDATE_TIME DESC";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, enrolmentId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
int deviceId = -1;
|
||||
EnrolmentInfo.Status previousStatus = null;
|
||||
if (rs.next()) {
|
||||
// if there is a record corresponding to the enrolment we save the status and the device id
|
||||
previousStatus = EnrolmentInfo.Status.valueOf(rs.getString("STATUS"));
|
||||
deviceId = rs.getInt("DEVICE_ID");
|
||||
}
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||
// if there was no record for the enrolment or the previous status is not the same as the current status
|
||||
// we'll add a record
|
||||
if (previousStatus == null || previousStatus != status){
|
||||
if (deviceId == -1) {
|
||||
// we need the device id in order to add a new record, therefore we get it from the enrolment table
|
||||
sql = "SELECT DEVICE_ID FROM DM_ENROLMENT WHERE ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, enrolmentId);
|
||||
rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
deviceId = rs.getInt("DEVICE_ID");
|
||||
} else {
|
||||
// if there were no records corresponding to the enrolment id this is a problem. i.e. enrolment
|
||||
// id is invalid
|
||||
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: no record for enrolment id " + enrolmentId);
|
||||
}
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||
}
|
||||
|
||||
sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
Timestamp updateTime = new Timestamp(new Date().getTime());
|
||||
stmt.setInt(1, enrolmentId);
|
||||
stmt.setInt(2, deviceId);
|
||||
stmt.setString(3, status.toString());
|
||||
stmt.setTimestamp(4, updateTime);
|
||||
stmt.setString(5, changedBy);
|
||||
stmt.execute();
|
||||
} else {
|
||||
// no need to update status since the last recorded status is the same as the current status
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while setting the status of device", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public EnrolmentInfo.Status getStatus(int deviceId, String currentOwner,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
EnrolmentInfo.Status status = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setString(2, currentOwner);
|
||||
stmt.setInt(3, tenantId);
|
||||
rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
status = EnrolmentInfo.Status.valueOf(rs.getString("STATUS"));
|
||||
}
|
||||
return status;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnrolmentInfo getEnrollment(int deviceId, String currentOwner,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
EnrolmentInfo enrolmentInfo = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
|
||||
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setString(2, currentOwner);
|
||||
stmt.setInt(3, tenantId);
|
||||
rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
enrolmentInfo = this.loadEnrolment(rs);
|
||||
}
|
||||
return enrolmentInfo;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
|
||||
"information of user '" + currentOwner + "' upon device '" + deviceId + "'", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnrolmentInfo getEnrollment(int deviceId, int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
EnrolmentInfo enrolmentInfo = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
|
||||
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ? " +
|
||||
"ORDER BY DATE_OF_LAST_UPDATE DESC";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setInt(2, tenantId);
|
||||
rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
enrolmentInfo = this.loadEnrolment(rs);
|
||||
}
|
||||
return enrolmentInfo;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
|
||||
"information of device '" + deviceId + "'", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<EnrolmentInfo> getEnrollmentsOfUser(int deviceId, String user, int tenantId)
|
||||
throws DeviceManagementDAOException {
|
||||
List<EnrolmentInfo> enrolmentInfos = new ArrayList<>();
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
EnrolmentInfo enrolmentInfo = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
|
||||
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setString(2, user);
|
||||
stmt.setInt(3, tenantId);
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
enrolmentInfo = this.loadEnrolment(rs);
|
||||
enrolmentInfos.add(enrolmentInfo);
|
||||
}
|
||||
return enrolmentInfos;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " +
|
||||
"information of user '" + user + "' upon device '" + deviceId + "'", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateOwnerOfEnrollment(List<Device> devices, String owner, int tenantId)
|
||||
throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
boolean updateStatus = true;
|
||||
String sql = "UPDATE DM_ENROLMENT "
|
||||
+ "SET OWNER = ?, IS_TRANSFERRED = ?, DATE_OF_LAST_UPDATE = ? "
|
||||
+ "WHERE ID = ? AND TENANT_ID = ?";
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
if (conn.getMetaData().supportsBatchUpdates()) {
|
||||
for (Device device : devices) {
|
||||
ps.setString(1, owner);
|
||||
ps.setBoolean(2, device.getEnrolmentInfo().isTransferred());
|
||||
ps.setTimestamp(3, new Timestamp(new Date().getTime()));
|
||||
ps.setInt(4, device.getEnrolmentInfo().getId());
|
||||
ps.setInt(5, tenantId);
|
||||
ps.addBatch();
|
||||
}
|
||||
int[] batchResult = ps.executeBatch();
|
||||
for (int i : batchResult) {
|
||||
if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) {
|
||||
updateStatus = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (Device device : devices) {
|
||||
ps.setString(1, owner);
|
||||
ps.setBoolean(2, device.getEnrolmentInfo().isTransferred());
|
||||
ps.setInt(3, device.getId());
|
||||
ps.setInt(4, tenantId);
|
||||
if (ps.executeUpdate() == 0) {
|
||||
updateStatus = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return updateStatus;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while obtaining the DB connection to update the "
|
||||
+ "owner of the device enrollment.", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
|
||||
private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException {
|
||||
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
||||
enrolmentInfo.setOwner(rs.getString("OWNER"));
|
||||
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP")));
|
||||
enrolmentInfo.setTransferred(rs.getBoolean("IS_TRANSFERRED"));
|
||||
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")));
|
||||
enrolmentInfo.setId(rs.getInt("ID"));
|
||||
return enrolmentInfo;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,637 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.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.geofence;
|
||||
|
||||
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.PaginationRequest;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
|
||||
import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EventManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.dao.GeofenceDAO;
|
||||
import org.wso2.carbon.device.mgt.core.dao.impl.AbstractGeofenceDAOImpl;
|
||||
import org.wso2.carbon.device.mgt.core.dto.event.config.GeoFenceGroupMap;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Date;
|
||||
import java.util.*;
|
||||
|
||||
public class GenericGeofenceDAOImpl extends AbstractGeofenceDAOImpl {
|
||||
private static final Log log = LogFactory.getLog(GenericGeofenceDAOImpl.class);
|
||||
@Override
|
||||
public GeofenceData saveGeofence(GeofenceData geofenceData) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "INSERT INTO DM_GEOFENCE(" +
|
||||
"FENCE_NAME, " +
|
||||
"DESCRIPTION, " +
|
||||
"LATITUDE, " +
|
||||
"LONGITUDE, " +
|
||||
"RADIUS, " +
|
||||
"GEO_JSON, " +
|
||||
"FENCE_SHAPE, " +
|
||||
"CREATED_TIMESTAMP, " +
|
||||
"OWNER, " +
|
||||
"TENANT_ID) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setString(1, geofenceData.getFenceName());
|
||||
stmt.setString(2, geofenceData.getDescription());
|
||||
stmt.setDouble(3, geofenceData.getLatitude());
|
||||
stmt.setDouble(4, geofenceData.getLongitude());
|
||||
stmt.setFloat(5, geofenceData.getRadius());
|
||||
stmt.setString(6, geofenceData.getGeoJson());
|
||||
stmt.setString(7, geofenceData.getFenceShape());
|
||||
stmt.setTimestamp(8, new Timestamp(new Date().getTime()));
|
||||
stmt.setString(9, geofenceData.getOwner());
|
||||
stmt.setInt(10, geofenceData.getTenantId());
|
||||
if (stmt.executeUpdate() > 0) {
|
||||
ResultSet generatedKeys = stmt.getGeneratedKeys();
|
||||
if (generatedKeys.next()) {
|
||||
geofenceData.setId(generatedKeys.getInt(1));
|
||||
}
|
||||
}
|
||||
return geofenceData;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while creating Geofence for the tenant id "+geofenceData.getTenantId();
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeofenceData getGeofence(int fenceId) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
GeofenceData geofenceData = null;
|
||||
String sql = "SELECT " +
|
||||
"ID, " +
|
||||
"FENCE_NAME, " +
|
||||
"DESCRIPTION, " +
|
||||
"LATITUDE, " +
|
||||
"LONGITUDE, " +
|
||||
"RADIUS, " +
|
||||
"GEO_JSON, " +
|
||||
"FENCE_SHAPE, " +
|
||||
"OWNER, " +
|
||||
"TENANT_ID " +
|
||||
"FROM DM_GEOFENCE " +
|
||||
"WHERE ID = ?";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(1, fenceId);
|
||||
try (ResultSet rst = stmt.executeQuery()) {
|
||||
List<GeofenceData> geofenceDataList = extractGeofenceData(rst);
|
||||
if (!geofenceDataList.isEmpty()) {
|
||||
geofenceData = geofenceDataList.get(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
return geofenceData;
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while retrieving Geofence with id "+fenceId;
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GeofenceData> getGeoFencesOfTenant(PaginationRequest request, int tenantId)
|
||||
throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
boolean isNameProvided = false;
|
||||
List<GeofenceData> geofenceData;
|
||||
String sql = "SELECT " +
|
||||
"ID, " +
|
||||
"FENCE_NAME, " +
|
||||
"DESCRIPTION, " +
|
||||
"LATITUDE, " +
|
||||
"LONGITUDE, " +
|
||||
"RADIUS, " +
|
||||
"GEO_JSON, " +
|
||||
"FENCE_SHAPE, " +
|
||||
"OWNER, " +
|
||||
"TENANT_ID " +
|
||||
"FROM DM_GEOFENCE " +
|
||||
"WHERE TENANT_ID = ? ";
|
||||
|
||||
if (request.getProperty(DeviceManagementConstants.GeoServices.FENCE_NAME) != null) {
|
||||
sql += "AND FENCE_NAME LIKE ?";
|
||||
isNameProvided = true;
|
||||
}
|
||||
sql += "LIMIT ? OFFSET ?";
|
||||
int index = 1;
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(index++, tenantId);
|
||||
if (isNameProvided) {
|
||||
stmt.setString(index++, request.getProperty(DeviceManagementConstants.GeoServices.FENCE_NAME).toString() + "%");
|
||||
}
|
||||
stmt.setInt(index++, request.getRowCount());
|
||||
stmt.setInt(index, request.getStartIndex());
|
||||
try (ResultSet rst = stmt.executeQuery()) {
|
||||
geofenceData = extractGeofenceData(rst);
|
||||
}
|
||||
}
|
||||
return geofenceData;
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while retrieving Geofence of the tenant " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GeofenceData> getGeoFencesOfTenant(String fenceName, int tenantId)
|
||||
throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
List<GeofenceData> geofenceData;
|
||||
String sql = "SELECT " +
|
||||
"ID, " +
|
||||
"FENCE_NAME, " +
|
||||
"DESCRIPTION, " +
|
||||
"LATITUDE, " +
|
||||
"LONGITUDE, " +
|
||||
"RADIUS, " +
|
||||
"GEO_JSON, " +
|
||||
"FENCE_SHAPE, " +
|
||||
"OWNER, " +
|
||||
"TENANT_ID " +
|
||||
"FROM DM_GEOFENCE " +
|
||||
"WHERE FENCE_NAME LIKE ?" +
|
||||
"AND TENANT_ID = ? ";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, fenceName + "%");
|
||||
stmt.setInt(2, tenantId);
|
||||
try (ResultSet rst = stmt.executeQuery()) {
|
||||
geofenceData = extractGeofenceData(rst);
|
||||
}
|
||||
}
|
||||
return geofenceData;
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while retrieving Geofence of the tenant " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GeofenceData> getGeoFencesOfTenant(int tenantId)
|
||||
throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
List<GeofenceData> geofenceData;
|
||||
String sql = "SELECT " +
|
||||
"ID, " +
|
||||
"FENCE_NAME, " +
|
||||
"DESCRIPTION, " +
|
||||
"LATITUDE, " +
|
||||
"LONGITUDE, " +
|
||||
"RADIUS, " +
|
||||
"GEO_JSON, " +
|
||||
"FENCE_SHAPE, " +
|
||||
"OWNER, " +
|
||||
"TENANT_ID " +
|
||||
"FROM DM_GEOFENCE " +
|
||||
"WHERE TENANT_ID = ? ";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(1, tenantId);
|
||||
try (ResultSet rst = stmt.executeQuery()) {
|
||||
geofenceData = extractGeofenceData(rst);
|
||||
}
|
||||
}
|
||||
return geofenceData;
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while retrieving Geofence of the tenant " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int deleteGeofenceById(int fenceId) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "DELETE FROM DM_GEOFENCE WHERE ID = ?";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(1, fenceId);
|
||||
return stmt.executeUpdate();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while deleting Geofence with ID " + fenceId;
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updateGeofence(GeofenceData geofenceData, int fenceId) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "UPDATE DM_GEOFENCE SET " +
|
||||
"FENCE_NAME = ?, " +
|
||||
"DESCRIPTION = ?, " +
|
||||
"LATITUDE = ?, " +
|
||||
"LONGITUDE = ?, " +
|
||||
"RADIUS = ?, " +
|
||||
"GEO_JSON = ?, " +
|
||||
"FENCE_SHAPE = ? " +
|
||||
"WHERE ID = ?";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, geofenceData.getFenceName());
|
||||
stmt.setString(2, geofenceData.getDescription());
|
||||
stmt.setDouble(3, geofenceData.getLatitude());
|
||||
stmt.setDouble(4, geofenceData.getLongitude());
|
||||
stmt.setFloat(5, geofenceData.getRadius());
|
||||
stmt.setString(6, geofenceData.getGeoJson());
|
||||
stmt.setString(7, geofenceData.getFenceShape());
|
||||
stmt.setInt(8, fenceId);
|
||||
return stmt.executeUpdate();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while updating Geofence record with id " + fenceId;
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean createGeofenceGroupMapping(GeofenceData geofenceData, List<Integer> groupIds) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "INSERT INTO DM_GEOFENCE_GROUP_MAPPING(" +
|
||||
"FENCE_ID, " +
|
||||
"GROUP_ID) " +
|
||||
"VALUES (?, ?)";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
for (Integer groupId : groupIds) {
|
||||
stmt.setInt(1, geofenceData.getId());
|
||||
stmt.setInt(2, groupId);
|
||||
stmt.addBatch();
|
||||
}
|
||||
return stmt.executeBatch().length > 0;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while creating geofence group mapping records";
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return EventManagementDAOFactory.getConnection();
|
||||
}
|
||||
|
||||
private List<GeofenceData> extractGeofenceData(ResultSet rst) throws SQLException {
|
||||
List <GeofenceData> geofenceDataList = new ArrayList<>();
|
||||
while (rst.next()) {
|
||||
GeofenceData geofenceData = new GeofenceData();
|
||||
geofenceData.setId(rst.getInt("ID"));
|
||||
geofenceData.setFenceName(rst.getString("FENCE_NAME"));
|
||||
geofenceData.setDescription(rst.getString("DESCRIPTION"));
|
||||
geofenceData.setLatitude(rst.getDouble("LATITUDE"));
|
||||
geofenceData.setLongitude(rst.getDouble("LONGITUDE"));
|
||||
geofenceData.setRadius(rst.getFloat("RADIUS"));
|
||||
geofenceData.setGeoJson(rst.getString("GEO_JSON"));
|
||||
geofenceData.setFenceShape(rst.getString("FENCE_SHAPE"));
|
||||
geofenceData.setOwner(rst.getString("OWNER"));
|
||||
geofenceData.setTenantId(rst.getInt("TENANT_ID"));
|
||||
geofenceDataList.add(geofenceData);
|
||||
}
|
||||
return geofenceDataList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getGroupIdsOfGeoFence(int fenceId) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "SELECT " +
|
||||
"GROUP_ID " +
|
||||
"FROM DM_GEOFENCE_GROUP_MAPPING " +
|
||||
"WHERE FENCE_ID = ? ";
|
||||
List<Integer> groupIds = new ArrayList<>();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(1, fenceId);
|
||||
try (ResultSet rst = stmt.executeQuery()) {
|
||||
while (rst.next()) {
|
||||
groupIds.add(rst.getInt(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
return groupIds;
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while fetching group IDs of the fence " + fenceId;
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteGeofenceGroupMapping(List<Integer> groupIdsToDelete, int fenceId) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "DELETE FROM DM_GEOFENCE_GROUP_MAPPING WHERE GROUP_ID = ? AND FENCE_ID = ?";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
for (Integer groupId : groupIdsToDelete) {
|
||||
stmt.setInt(1, groupId);
|
||||
stmt.setInt(2, fenceId);
|
||||
stmt.addBatch();
|
||||
}
|
||||
stmt.executeBatch();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while deleting Geofence group mapping records";
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createGeofenceEventMapping(int fenceId, List<Integer> eventIds) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "INSERT INTO DM_GEOFENCE_EVENT_MAPPING(" +
|
||||
"FENCE_ID, "+
|
||||
"EVENT_ID) " +
|
||||
"VALUES (?, ?)";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
for (Integer createdEventId : eventIds) {
|
||||
stmt.setInt(1, fenceId);
|
||||
stmt.setInt(2, createdEventId);
|
||||
stmt.addBatch();
|
||||
}
|
||||
stmt.executeBatch();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while creating geofence event group mapping records";
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteGeofenceEventMapping(List<Integer> removedEventIdList) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "DELETE FROM DM_GEOFENCE_EVENT_MAPPING WHERE EVENT_ID = ?";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
for (Integer eventId : removedEventIdList) {
|
||||
stmt.setInt(1, eventId);
|
||||
stmt.addBatch();
|
||||
}
|
||||
stmt.executeBatch();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while deleting Geofence event mapping records";
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Integer, List<EventConfig>> getEventsOfGeoFences(List<Integer> geofenceIds) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Map<Integer, List<EventConfig>> geoFenceEventMap = new HashMap<>();
|
||||
if (geofenceIds.isEmpty()) {
|
||||
return geoFenceEventMap;
|
||||
}
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "SELECT " +
|
||||
"E.ID AS EVENT_ID, " +
|
||||
"M.FENCE_ID AS FENCE_ID, " +
|
||||
"EVENT_SOURCE, " +
|
||||
"EVENT_LOGIC, " +
|
||||
"ACTIONS " +
|
||||
"FROM DM_DEVICE_EVENT E, DM_GEOFENCE_EVENT_MAPPING M " +
|
||||
"WHERE E.ID = M.EVENT_ID " +
|
||||
"AND M.FENCE_ID IN (%s)";
|
||||
String inClause = String.join(", ", Collections.nCopies(geofenceIds.size(), "?"));
|
||||
sql = String.format(sql, inClause);
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
int index = 1;
|
||||
for (Integer geofenceId : geofenceIds) {
|
||||
stmt.setInt(index++, geofenceId);
|
||||
}
|
||||
ResultSet resultSet = stmt.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
int fenceId = resultSet.getInt("FENCE_ID");
|
||||
List<EventConfig> eventConfigList = geoFenceEventMap.get(fenceId);
|
||||
if (eventConfigList == null) {
|
||||
eventConfigList = new ArrayList<>();
|
||||
}
|
||||
EventConfig event = new EventConfig();
|
||||
event.setEventId(resultSet.getInt("EVENT_ID"));
|
||||
event.setEventSource(resultSet.getString("EVENT_SOURCE"));
|
||||
event.setEventLogic(resultSet.getString("EVENT_LOGIC"));
|
||||
event.setActions(resultSet.getString("ACTIONS"));
|
||||
eventConfigList.add(event);
|
||||
geoFenceEventMap.put(fenceId, eventConfigList);
|
||||
}
|
||||
return geoFenceEventMap;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while updating Geofence record with id ";
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<EventConfig> getEventsOfGeoFence(int geofenceId) throws DeviceManagementDAOException {
|
||||
try {
|
||||
List<EventConfig> eventList = new ArrayList<>();
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "SELECT " +
|
||||
"E.ID AS EVENT_ID, " +
|
||||
"EVENT_SOURCE, " +
|
||||
"EVENT_LOGIC, " +
|
||||
"ACTIONS " +
|
||||
"FROM DM_DEVICE_EVENT E, DM_GEOFENCE_EVENT_MAPPING G " +
|
||||
"WHERE E.ID = G.EVENT_ID " +
|
||||
"AND G.FENCE_ID = ?";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(1, geofenceId);
|
||||
return getEventConfigs(stmt);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while updating Geofence record with id ";
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<GeoFenceGroupMap> getGroupIdsOfGeoFences(List<Integer> fenceIds) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Set<GeoFenceGroupMap> geoFenceGroupSet = new HashSet<>();
|
||||
if (fenceIds.isEmpty()) {
|
||||
return geoFenceGroupSet;
|
||||
}
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "SELECT " +
|
||||
"FENCE_ID, " +
|
||||
"M.GROUP_ID, " +
|
||||
"G.GROUP_NAME " +
|
||||
"FROM DM_GEOFENCE_GROUP_MAPPING M, DM_GROUP G " +
|
||||
"WHERE M.GROUP_ID = G.ID " +
|
||||
"AND FENCE_ID IN (%s)";
|
||||
String inClause = String.join(", ", Collections.nCopies(fenceIds.size(), "?"));
|
||||
sql = String.format(sql, inClause);
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
int index = 1;
|
||||
for (Integer fenceId : fenceIds) {
|
||||
stmt.setInt(index++, fenceId);
|
||||
}
|
||||
ResultSet rst = stmt.executeQuery();
|
||||
while (rst.next()) {
|
||||
GeoFenceGroupMap geoFenceGroupMap = new GeoFenceGroupMap();
|
||||
geoFenceGroupMap.setFenceId(rst.getInt("FENCE_ID"));
|
||||
geoFenceGroupMap.setGroupId(rst.getInt("GROUP_ID"));
|
||||
geoFenceGroupMap.setGroupName(rst.getString("GROUP_NAME"));
|
||||
geoFenceGroupSet.add(geoFenceGroupMap);
|
||||
}
|
||||
}
|
||||
return geoFenceGroupSet;
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while fetching group IDs of the fences";
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the geofence event extracted from the DB
|
||||
* @param stmt prepared statement to retrieve data from the DB
|
||||
* @return Retrieved Event list from the DB
|
||||
* @throws SQLException for the errors occur while accessing the DB
|
||||
*/
|
||||
private List<EventConfig> getEventConfigs(PreparedStatement stmt) throws SQLException {
|
||||
List<EventConfig> eventList = new ArrayList<>();
|
||||
ResultSet resultSet = stmt.executeQuery();
|
||||
EventConfig event;
|
||||
while (resultSet.next()) {
|
||||
event = new EventConfig();
|
||||
event.setEventId(resultSet.getInt("EVENT_ID"));
|
||||
event.setEventSource(resultSet.getString("EVENT_SOURCE"));
|
||||
event.setEventLogic(resultSet.getString("EVENT_LOGIC"));
|
||||
event.setActions(resultSet.getString("ACTIONS"));
|
||||
eventList.add(event);
|
||||
}
|
||||
return eventList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GeofenceData> getGeoFences(int groupId, int tenantId) throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "SELECT " +
|
||||
"G.ID AS FENCE_ID, " +
|
||||
"FENCE_NAME, " +
|
||||
"DESCRIPTION, " +
|
||||
"LATITUDE," +
|
||||
"LONGITUDE, " +
|
||||
"RADIUS, " +
|
||||
"GEO_JSON, " +
|
||||
"FENCE_SHAPE " +
|
||||
"FROM DM_GEOFENCE G, DM_GEOFENCE_GROUP_MAPPING M " +
|
||||
"WHERE M.GROUP_ID = ? AND TENANT_ID = ? " +
|
||||
"GROUP BY G.ID";
|
||||
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(1, groupId);
|
||||
stmt.setInt(2, tenantId);
|
||||
ResultSet rst = stmt.executeQuery();
|
||||
List <GeofenceData> geofenceDataList = new ArrayList<>();
|
||||
while (rst.next()) {
|
||||
GeofenceData geofenceData = new GeofenceData();
|
||||
geofenceData.setId(rst.getInt("FENCE_ID"));
|
||||
geofenceData.setFenceName(rst.getString("FENCE_NAME"));
|
||||
geofenceData.setDescription(rst.getString("DESCRIPTION"));
|
||||
geofenceData.setLatitude(rst.getDouble("LATITUDE"));
|
||||
geofenceData.setLongitude(rst.getDouble("LONGITUDE"));
|
||||
geofenceData.setRadius(rst.getFloat("RADIUS"));
|
||||
geofenceData.setGeoJson(rst.getString("GEO_JSON"));
|
||||
geofenceData.setFenceShape(rst.getString("FENCE_SHAPE"));
|
||||
geofenceDataList.add(geofenceData);
|
||||
}
|
||||
return geofenceDataList;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while retrieving Geo fences of group " + groupId
|
||||
+ " and tenant " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeofenceData getGeofence(int fenceId, boolean requireGroupData) throws DeviceManagementDAOException {
|
||||
if (!requireGroupData) {
|
||||
return getGeofence(fenceId);
|
||||
}
|
||||
|
||||
try {
|
||||
Connection con = this.getConnection();
|
||||
String sql = "SELECT " +
|
||||
"G.ID AS FENCE_ID, " +
|
||||
"FENCE_NAME, " +
|
||||
"G.DESCRIPTION, " +
|
||||
"LATITUDE, " +
|
||||
"LONGITUDE, " +
|
||||
"RADIUS, " +
|
||||
"GEO_JSON, " +
|
||||
"FENCE_SHAPE, " +
|
||||
"M.GROUP_ID AS GROUP_ID, " +
|
||||
"GR.GROUP_NAME " +
|
||||
"FROM DM_GEOFENCE G, DM_GEOFENCE_GROUP_MAPPING M, DM_GROUP GR " +
|
||||
"WHERE G.ID = M.FENCE_ID " +
|
||||
"AND M.GROUP_ID = GR.ID " +
|
||||
"AND G.ID = ?";
|
||||
try (PreparedStatement stmt = con.prepareStatement(sql)){
|
||||
stmt.setInt(1, fenceId);
|
||||
ResultSet rst = stmt.executeQuery();
|
||||
Map<Integer, String> groupMap = new HashMap<>();
|
||||
GeofenceData geofenceData = null;
|
||||
while (rst.next()) {
|
||||
groupMap.put(rst.getInt("GROUP_ID"), rst.getString("GROUP_NAME"));
|
||||
if (rst.isLast()) {
|
||||
geofenceData = new GeofenceData();
|
||||
geofenceData.setId(rst.getInt("FENCE_ID"));
|
||||
geofenceData.setFenceName(rst.getString("FENCE_NAME"));
|
||||
geofenceData.setDescription(rst.getString("DESCRIPTION"));
|
||||
geofenceData.setLatitude(rst.getDouble("LATITUDE"));
|
||||
geofenceData.setLongitude(rst.getDouble("LONGITUDE"));
|
||||
geofenceData.setRadius(rst.getFloat("RADIUS"));
|
||||
geofenceData.setGeoJson(rst.getString("GEO_JSON"));
|
||||
geofenceData.setFenceShape(rst.getString("FENCE_SHAPE"));
|
||||
geofenceData.setGroupData(groupMap);
|
||||
}
|
||||
}
|
||||
return geofenceData;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while retrieving Geo fence data " + fenceId;
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.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.geofence;
|
||||
|
||||
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.PaginationRequest;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
|
||||
import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EventManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.dao.GeofenceDAO;
|
||||
import org.wso2.carbon.device.mgt.core.dao.impl.AbstractGeofenceDAOImpl;
|
||||
import org.wso2.carbon.device.mgt.core.dto.event.config.GeoFenceGroupMap;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Date;
|
||||
import java.util.*;
|
||||
|
||||
public class SQLServerGeofenceDAOImpl extends AbstractGeofenceDAOImpl {
|
||||
private static final Log log = LogFactory.getLog(SQLServerGeofenceDAOImpl.class);
|
||||
|
||||
@Override
|
||||
public List<GeofenceData> getGeoFencesOfTenant(PaginationRequest request, int tenantId)
|
||||
throws DeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
boolean isNameProvided = false;
|
||||
List<GeofenceData> geofenceData;
|
||||
String sql = "SELECT " +
|
||||
"ID, " +
|
||||
"FENCE_NAME, " +
|
||||
"DESCRIPTION, " +
|
||||
"LATITUDE, " +
|
||||
"LONGITUDE, " +
|
||||
"RADIUS, " +
|
||||
"GEO_JSON, " +
|
||||
"FENCE_SHAPE, " +
|
||||
"OWNER, " +
|
||||
"TENANT_ID " +
|
||||
"FROM DM_GEOFENCE " +
|
||||
"WHERE TENANT_ID = ? ";
|
||||
|
||||
if (request.getProperty(DeviceManagementConstants.GeoServices.FENCE_NAME) != null) {
|
||||
sql += "AND FENCE_NAME LIKE ?";
|
||||
isNameProvided = true;
|
||||
}
|
||||
sql += "ORDER BY FENCE_NAME ";
|
||||
sql += "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
|
||||
int index = 1;
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(index++, tenantId);
|
||||
if (isNameProvided) {
|
||||
stmt.setString(index++, request.getProperty(DeviceManagementConstants.GeoServices.FENCE_NAME).toString() + "%");
|
||||
}
|
||||
stmt.setInt(index++, request.getStartIndex());
|
||||
stmt.setInt(index, request.getRowCount());
|
||||
try (ResultSet rst = stmt.executeQuery()) {
|
||||
geofenceData = extractGeofenceData(rst);
|
||||
}
|
||||
}
|
||||
return geofenceData;
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while retrieving Geofence of the tenant " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return EventManagementDAOFactory.getConnection();
|
||||
}
|
||||
|
||||
private List<GeofenceData> extractGeofenceData(ResultSet rst) throws SQLException {
|
||||
List <GeofenceData> geofenceDataList = new ArrayList<>();
|
||||
while (rst.next()) {
|
||||
GeofenceData geofenceData = new GeofenceData();
|
||||
geofenceData.setId(rst.getInt("ID"));
|
||||
geofenceData.setFenceName(rst.getString("FENCE_NAME"));
|
||||
geofenceData.setDescription(rst.getString("DESCRIPTION"));
|
||||
geofenceData.setLatitude(rst.getDouble("LATITUDE"));
|
||||
geofenceData.setLongitude(rst.getDouble("LONGITUDE"));
|
||||
geofenceData.setRadius(rst.getFloat("RADIUS"));
|
||||
geofenceData.setGeoJson(rst.getString("GEO_JSON"));
|
||||
geofenceData.setFenceShape(rst.getString("FENCE_SHAPE"));
|
||||
geofenceData.setOwner(rst.getString("OWNER"));
|
||||
geofenceData.setTenantId(rst.getInt("TENANT_ID"));
|
||||
geofenceDataList.add(geofenceData);
|
||||
}
|
||||
return geofenceDataList;
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.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.
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>tenant-mgt</artifactId>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<version>5.0.25-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>io.entgra.tenant.mgt.common</artifactId>
|
||||
<name>Entgra IoT - Tenant Manager Common</name>
|
||||
<description>Entgra IoT - Tenant Manager Common</description>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<instructions>
|
||||
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
|
||||
<Bundle-Description>Tenant Management Common Bundle</Bundle-Description>
|
||||
<Import-Package>
|
||||
org.apache.commons.logging,
|
||||
org.wso2.carbon.stratos.common.beans
|
||||
</Import-Package>
|
||||
<Export-Package>io.entgra.tenant.mgt.common.*</Export-Package>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.commons</groupId>
|
||||
<artifactId>org.wso2.carbon.tenant.common</artifactId>
|
||||
<version>${carbon.commons.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) 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 io.entgra.tenant.mgt.common.exception;
|
||||
|
||||
public class TenantMgtException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 4304028531230841553L;
|
||||
|
||||
public TenantMgtException(String msg, Throwable ex) {
|
||||
super(msg, ex);
|
||||
}
|
||||
|
||||
public TenantMgtException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) 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 io.entgra.tenant.mgt.common.spi;
|
||||
|
||||
import io.entgra.tenant.mgt.common.exception.TenantMgtException;
|
||||
import org.wso2.carbon.stratos.common.beans.TenantInfoBean;
|
||||
|
||||
public interface TenantManagerService {
|
||||
void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException;
|
||||
|
||||
void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException;
|
||||
}
|
@ -0,0 +1,136 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.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.
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>tenant-mgt</artifactId>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<version>5.0.25-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>io.entgra.tenant.mgt.core</artifactId>
|
||||
<name>Entgra IoT - Tenant Manager Core</name>
|
||||
<description>Entgra IoT - Tenant Manager Core</description>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-scr-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<instructions>
|
||||
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
|
||||
<Bundle-Description>Tenant Management Core Bundle</Bundle-Description>
|
||||
<Private-Package>io.entgra.tenant.mgt.core.internal</Private-Package>
|
||||
<Import-Package>
|
||||
org.osgi.framework.*;version="${imp.package.version.osgi.framework}",
|
||||
org.osgi.service.*;version="${imp.package.version.osgi.service}",
|
||||
org.apache.commons.logging,
|
||||
io.entgra.application.mgt.common.*,
|
||||
io.entgra.application.mgt.core.config,
|
||||
org.wso2.carbon.device.mgt.core,
|
||||
org.wso2.carbon.user.api,
|
||||
org.wso2.carbon.registry.core.exceptions,
|
||||
io.entgra.tenant.mgt.common.*,
|
||||
org.wso2.carbon.stratos.common.beans,
|
||||
org.wso2.carbon.stratos.common.exception,
|
||||
org.wso2.carbon.stratos.common.listeners,
|
||||
org.wso2.carbon.device.mgt.common.metadata.mgt,
|
||||
org.wso2.carbon.device.mgt.common.exceptions,
|
||||
org.wso2.carbon.device.mgt.common.permission.mgt,
|
||||
org.wso2.carbon.device.mgt.common.roles.config,
|
||||
org.wso2.carbon.device.mgt.core.metadata.mgt,
|
||||
org.wso2.carbon.device.mgt.core.config,
|
||||
org.wso2.carbon.user.core.service,
|
||||
org.wso2.carbon.context
|
||||
</Import-Package>
|
||||
<Export-Package>
|
||||
!io.entgra.tenant.mgt.core.internal,
|
||||
io.entgra.tenant.mgt.core.*
|
||||
</Export-Package>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.osgi</groupId>
|
||||
<artifactId>org.eclipse.osgi</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.osgi</groupId>
|
||||
<artifactId>org.eclipse.osgi.services</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.commons</groupId>
|
||||
<artifactId>org.wso2.carbon.tenant.common</artifactId>
|
||||
<version>${carbon.commons.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon</groupId>
|
||||
<artifactId>org.wso2.carbon.registry.core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon</groupId>
|
||||
<artifactId>org.wso2.carbon.user.core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon</groupId>
|
||||
<artifactId>org.wso2.carbon.user.api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>org.wso2.carbon.device.mgt.core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>io.entgra.application.mgt.core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>io.entgra.application.mgt.common</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>io.entgra.tenant.mgt.common</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon</groupId>
|
||||
<artifactId>org.wso2.carbon.utils</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) 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 io.entgra.tenant.mgt.core;
|
||||
|
||||
import io.entgra.tenant.mgt.common.exception.TenantMgtException;
|
||||
import org.wso2.carbon.stratos.common.beans.TenantInfoBean;
|
||||
|
||||
public interface TenantManager {
|
||||
|
||||
/**
|
||||
* Add default roles to a tenant described by the tenant info bean
|
||||
* @param tenantInfoBean The info bean that provides tenant info
|
||||
* @throws TenantMgtException Throws when error occurred while adding
|
||||
* a role into user store or adding default white label theme to created tenant
|
||||
*/
|
||||
void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException;
|
||||
|
||||
/**
|
||||
* Add default application categories to a tenant described by the tenant info bean
|
||||
* @param tenantInfoBean The info bean that provides tenant info
|
||||
* @throws TenantMgtException Throws when error occurred while adding default application categories
|
||||
*/
|
||||
void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException;
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) 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 io.entgra.tenant.mgt.core.impl;
|
||||
|
||||
import io.entgra.tenant.mgt.common.spi.TenantManagerService;
|
||||
import io.entgra.tenant.mgt.common.exception.TenantMgtException;
|
||||
import io.entgra.tenant.mgt.core.internal.TenantMgtDataHolder;
|
||||
import org.wso2.carbon.stratos.common.beans.TenantInfoBean;
|
||||
|
||||
public class TenantManagerServiceImpl implements TenantManagerService {
|
||||
|
||||
@Override
|
||||
public void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException {
|
||||
TenantMgtDataHolder.getInstance().getTenantManager().addDefaultRoles(tenantInfoBean);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException {
|
||||
TenantMgtDataHolder.getInstance().getTenantManager().addDefaultAppCategories(tenantInfoBean);
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) 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 io.entgra.tenant.mgt.core.internal;
|
||||
|
||||
import io.entgra.application.mgt.common.services.ApplicationManager;
|
||||
import io.entgra.tenant.mgt.core.TenantManager;
|
||||
import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelManagementService;
|
||||
import org.wso2.carbon.user.core.service.RealmService;
|
||||
|
||||
public class TenantMgtDataHolder {
|
||||
private static final TenantMgtDataHolder instance = new TenantMgtDataHolder();
|
||||
private TenantManager tenantManager;
|
||||
|
||||
private ApplicationManager applicationManager;
|
||||
|
||||
private WhiteLabelManagementService whiteLabelManagementService;
|
||||
|
||||
private RealmService realmService;
|
||||
|
||||
public RealmService getRealmService() {
|
||||
return realmService;
|
||||
}
|
||||
|
||||
public void setRealmService(RealmService realmService) {
|
||||
this.realmService = realmService;
|
||||
}
|
||||
|
||||
public ApplicationManager getApplicationManager() {
|
||||
return applicationManager;
|
||||
}
|
||||
|
||||
public void setApplicationManager(ApplicationManager applicationManager) {
|
||||
this.applicationManager = applicationManager;
|
||||
}
|
||||
|
||||
public WhiteLabelManagementService getWhiteLabelManagementService() {
|
||||
return whiteLabelManagementService;
|
||||
}
|
||||
|
||||
public void setWhiteLabelManagementService(WhiteLabelManagementService whiteLabelManagementService) {
|
||||
this.whiteLabelManagementService = whiteLabelManagementService;
|
||||
}
|
||||
|
||||
public TenantManager getTenantManager() {
|
||||
return tenantManager;
|
||||
}
|
||||
|
||||
public void setTenantManager(TenantManager tenantManager) {
|
||||
this.tenantManager = tenantManager;
|
||||
}
|
||||
|
||||
public static TenantMgtDataHolder getInstance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) 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 io.entgra.tenant.mgt.core.internal;
|
||||
|
||||
import io.entgra.application.mgt.common.services.ApplicationManager;
|
||||
import io.entgra.tenant.mgt.common.spi.TenantManagerService;
|
||||
import io.entgra.tenant.mgt.core.TenantManager;
|
||||
import io.entgra.tenant.mgt.core.impl.TenantManagerImpl;
|
||||
import io.entgra.tenant.mgt.core.impl.TenantManagerServiceImpl;
|
||||
import io.entgra.tenant.mgt.core.listener.DeviceMgtTenantListener;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.osgi.service.component.ComponentContext;
|
||||
import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelManagementService;
|
||||
import org.wso2.carbon.device.mgt.core.metadata.mgt.WhiteLabelManagementServiceImpl;
|
||||
import org.wso2.carbon.stratos.common.listeners.TenantMgtListener;
|
||||
import org.wso2.carbon.user.core.service.RealmService;
|
||||
|
||||
/**
|
||||
* @scr.component name="org.wso2.carbon.devicemgt.tenant.manager" immediate="true"
|
||||
* @scr.reference name="org.wso2.carbon.application.mgt.service"
|
||||
* interface="io.entgra.application.mgt.common.services.ApplicationManager"
|
||||
* cardinality="1..1"
|
||||
* policy="dynamic"
|
||||
* bind="setApplicationManager"
|
||||
* unbind="unsetApplicationManager"
|
||||
* @scr.reference name="user.realmservice.default"
|
||||
* interface="org.wso2.carbon.user.core.service.RealmService"
|
||||
* cardinality="1..1"
|
||||
* policy="dynamic"
|
||||
* bind="setRealmService"
|
||||
* unbind="unsetRealmService"
|
||||
*/
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class TenantMgtServiceComponent {
|
||||
|
||||
private static final Log log = LogFactory.getLog(TenantManagerService.class);
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
protected void activate(ComponentContext componentContext) {
|
||||
try {
|
||||
TenantManagerService tenantManagerService = new TenantManagerServiceImpl();
|
||||
componentContext.getBundleContext().
|
||||
registerService(TenantManagerServiceImpl.class.getName(), tenantManagerService, null);
|
||||
TenantManager tenantManager = new TenantManagerImpl();
|
||||
TenantMgtDataHolder.getInstance().setTenantManager(tenantManager);
|
||||
WhiteLabelManagementService whiteLabelManagementService = new WhiteLabelManagementServiceImpl();
|
||||
componentContext.getBundleContext().registerService(WhiteLabelManagementServiceImpl.class.getName(),
|
||||
whiteLabelManagementService, null);
|
||||
TenantMgtDataHolder.getInstance().setWhiteLabelManagementService(whiteLabelManagementService);
|
||||
DeviceMgtTenantListener deviceMgtTenantListener = new DeviceMgtTenantListener();
|
||||
if(log.isDebugEnabled()) {
|
||||
log.info("Tenant management listener is registering");
|
||||
}
|
||||
componentContext.getBundleContext().
|
||||
registerService(TenantMgtListener.class.getName(), deviceMgtTenantListener, null);
|
||||
if(log.isDebugEnabled()) {
|
||||
log.info("Tenant management service activated");
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
String msg = "Error occurred while activating tenant management service";
|
||||
log.error(msg, t);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
protected void deactivate(ComponentContext componentContext) {
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
protected void setApplicationManager(ApplicationManager applicationManager) {
|
||||
if(log.isDebugEnabled()) {
|
||||
log.info("Application manager service is binding");
|
||||
}
|
||||
TenantMgtDataHolder.getInstance().setApplicationManager(applicationManager);
|
||||
}
|
||||
|
||||
protected void unsetApplicationManager(ApplicationManager applicationManager) {
|
||||
if(log.isDebugEnabled()) {
|
||||
log.info("Application manager service is unbinding");
|
||||
}
|
||||
TenantMgtDataHolder.getInstance().setApplicationManager(null);
|
||||
}
|
||||
|
||||
protected void setRealmService(RealmService realmService) {
|
||||
if(log.isDebugEnabled()) {
|
||||
log.info("Realm Service service is binding");
|
||||
}
|
||||
TenantMgtDataHolder.getInstance().setRealmService(realmService);
|
||||
}
|
||||
|
||||
protected void unsetRealmService(RealmService realmService) {
|
||||
if(log.isDebugEnabled()) {
|
||||
log.info("Realm Service service is unbinding");
|
||||
}
|
||||
TenantMgtDataHolder.getInstance().setRealmService(null);
|
||||
}
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) 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 io.entgra.tenant.mgt.core.listener;
|
||||
|
||||
import io.entgra.tenant.mgt.core.TenantManager;
|
||||
import io.entgra.tenant.mgt.common.exception.TenantMgtException;
|
||||
import io.entgra.tenant.mgt.core.internal.TenantMgtDataHolder;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.stratos.common.beans.TenantInfoBean;
|
||||
import org.wso2.carbon.stratos.common.exception.StratosException;
|
||||
import org.wso2.carbon.stratos.common.listeners.TenantMgtListener;
|
||||
|
||||
public class DeviceMgtTenantListener implements TenantMgtListener {
|
||||
|
||||
private static final Log log = LogFactory.getLog(DeviceMgtTenantListener.class);
|
||||
public static final int LISTENER_EXECUTION_ORDER = 10;
|
||||
|
||||
@Override
|
||||
public void onTenantCreate(TenantInfoBean tenantInfoBean) {
|
||||
// Any work to be performed after a tenant creation
|
||||
TenantManager tenantManager = TenantMgtDataHolder.getInstance().getTenantManager();
|
||||
try {
|
||||
tenantManager.addDefaultRoles(tenantInfoBean);
|
||||
tenantManager.addDefaultAppCategories(tenantInfoBean);
|
||||
} catch (TenantMgtException e) {
|
||||
String msg = "Error occurred while executing tenant creation flow";
|
||||
log.error(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTenantUpdate(TenantInfoBean tenantInfoBean) throws StratosException {
|
||||
// Any work to be performed after a tenant information update happens
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTenantDelete(int i) {
|
||||
// Any work to be performed after a tenant deletion
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTenantRename(int i, String s, String s1) throws StratosException {
|
||||
// Any work to be performed after a tenant rename happens
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTenantInitialActivation(int i) throws StratosException {
|
||||
// Any work to be performed after a tenant's initial activation happens
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTenantActivation(int i) throws StratosException {
|
||||
// Any work to be performed after a tenant activation
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTenantDeactivation(int i) throws StratosException {
|
||||
// Any work to be performed after a tenant deactivation
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscriptionPlanChange(int i, String s, String s1) throws StratosException {
|
||||
// Any work to be performed after subscription plan change
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getListenerOrder() {
|
||||
return LISTENER_EXECUTION_ORDER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPreDelete(int i) throws StratosException {
|
||||
// Any work to be performed before a tenant is deleted
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.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.
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>carbon-devicemgt</artifactId>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<version>5.0.25-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>tenant-mgt</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<name>Entgra IoT - Tenant Management Component</name>
|
||||
<description>Entgra IoT - Tenant Management Component</description>
|
||||
|
||||
<modules>
|
||||
<module>io.entgra.tenant.mgt.core</module>
|
||||
<module>io.entgra.tenant.mgt.common</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
@ -0,0 +1,111 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright (c) 2014, 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.
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>tenant-mgt-feature</artifactId>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<version>5.0.25-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>io.entgra.tenant.mgt.server.feature</artifactId>
|
||||
<name>Entgra IoT - Tenant Management Server Feature</name>
|
||||
<description>Entgra IoT - Tenant Management Server Feature</description>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>io.entgra.tenant.mgt.core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>io.entgra.tenant.mgt.common</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-resources</id>
|
||||
<phase>generate-resources</phase>
|
||||
<goals>
|
||||
<goal>copy-resources</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>src/main/resources</outputDirectory>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>resources</directory>
|
||||
<includes>
|
||||
<include>build.properties</include>
|
||||
<include>p2.inf</include>
|
||||
</includes>
|
||||
</resource>
|
||||
</resources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.wso2.maven</groupId>
|
||||
<artifactId>carbon-p2-plugin</artifactId>
|
||||
<version>${carbon.p2.plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>p2-feature-generation</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>p2-feature-gen</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<id>io.entgra.tenant.mgt.server</id>
|
||||
<propertiesFile>../../etc/feature.properties</propertiesFile>
|
||||
<adviceFile>
|
||||
<properties>
|
||||
<propertyDef>org.wso2.carbon.p2.category.type:server
|
||||
</propertyDef>
|
||||
<propertyDef>org.eclipse.equinox.p2.type.group:true
|
||||
</propertyDef>
|
||||
</properties>
|
||||
</adviceFile>
|
||||
<bundles>
|
||||
<!--<bundleDef>{groupId}:{artifactId}:{version}</bundleDef>-->
|
||||
<bundleDef>
|
||||
org.wso2.carbon.devicemgt:io.entgra.tenant.mgt.core:${carbon.device.mgt.version}
|
||||
</bundleDef>
|
||||
<bundleDef>
|
||||
org.wso2.carbon.devicemgt:io.entgra.tenant.mgt.common:${carbon.device.mgt.version}
|
||||
</bundleDef>
|
||||
</bundles>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1 @@
|
||||
custom=true
|
@ -0,0 +1 @@
|
||||
instructions.configure = \
|
@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright (c) 2014, 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.
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<parent>
|
||||
<artifactId>carbon-devicemgt</artifactId>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<version>5.0.25-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>tenant-mgt-feature</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<name>Entgra IoT - Tenant Management Feature</name>
|
||||
|
||||
<modules>
|
||||
<module>io.entgra.tenant.mgt.server.feature</module>
|
||||
</modules>
|
||||
</project>
|
Loading…
Reference in new issue