forked from community/device-mgt-core
parent
65620a7389
commit
8d348a358c
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.core.dao;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ApplicationDAO {
|
||||
|
||||
int addApplication(Application application, int tenantId) throws DeviceManagementDAOException;
|
||||
|
||||
List<Integer> addApplications(List<Application> applications, int tenantId) throws DeviceManagementDAOException;
|
||||
|
||||
int removeApplication(String applicationName, int tenantId) throws DeviceManagementDAOException;
|
||||
|
||||
Application getApplication(String identifier, int tenantId) throws DeviceManagementDAOException;
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.core.dao;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||
import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
|
||||
|
||||
public interface EnrolmentDAO {
|
||||
|
||||
int addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException;
|
||||
|
||||
int updateEnrollment(int deviceId, EnrolmentInfo enrolmentInfo,
|
||||
int tenantId) throws DeviceManagementDAOException;
|
||||
|
||||
int removeEnrollment(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException;
|
||||
|
||||
boolean setStatus(int deviceId, String currentOwner, Status status,
|
||||
int tenantId) throws DeviceManagementDAOException;
|
||||
|
||||
Status getStatus(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException;
|
||||
|
||||
EnrolmentInfo getEnrolment(int deviceId, String currentUser, int tenantId) throws DeviceManagementDAOException;
|
||||
|
||||
}
|
@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.core.dao.impl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||
import org.wso2.carbon.device.mgt.core.dao.ApplicationDAO;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ApplicationDAOImpl implements ApplicationDAO {
|
||||
|
||||
private static final Log log = LogFactory.getLog(ApplicationDAOImpl.class);
|
||||
|
||||
@Override
|
||||
public int addApplication(Application application, int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
int applicationId = -1;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PACKAGE_NAME, PLATFORM, CATEGORY, " +
|
||||
"VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
Statement.RETURN_GENERATED_KEYS);
|
||||
stmt.setString(1, application.getName());
|
||||
stmt.setString(2, application.getPackageName());
|
||||
stmt.setString(3, application.getPlatform());
|
||||
stmt.setString(4, application.getCategory());
|
||||
stmt.setString(5, application.getVersion());
|
||||
stmt.setString(6, application.getType());
|
||||
stmt.setString(7, application.getLocationUrl());
|
||||
stmt.setString(8, application.getImageUrl());
|
||||
stmt.setInt(9, tenantId);
|
||||
stmt.execute();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
applicationId = rs.getInt(1);
|
||||
}
|
||||
return applicationId;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while adding application '" +
|
||||
application.getName() + "'", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> addApplications(List<Application> applications,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs;
|
||||
List<Integer> applicationIds = new ArrayList<Integer>();
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PACKAGE_NAME, PLATFORM, CATEGORY, " +
|
||||
"VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
for (Application application : applications) {
|
||||
stmt.setString(1, application.getName());
|
||||
stmt.setString(2, application.getPackageName());
|
||||
stmt.setString(3, application.getPlatform());
|
||||
stmt.setString(4, application.getCategory());
|
||||
stmt.setString(5, application.getVersion());
|
||||
stmt.setString(6, application.getType());
|
||||
stmt.setString(7, application.getLocationUrl());
|
||||
stmt.setString(8, application.getImageUrl());
|
||||
stmt.setInt(9, tenantId);
|
||||
stmt.addBatch();
|
||||
}
|
||||
stmt.executeBatch();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
while (rs.next()) {
|
||||
applicationIds.add(rs.getInt(1));
|
||||
}
|
||||
return applicationIds;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while adding bulk application list", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int removeApplication(String applicationName, int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
int applicationId = -1;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
conn.setAutoCommit(false);
|
||||
stmt = conn.prepareStatement("DELETE DM_APPLICATION WHERE NAME = ? AND TENANT_ID = ?");
|
||||
stmt.setString(1, applicationName);
|
||||
stmt.setInt(2, tenantId);
|
||||
stmt.execute();
|
||||
conn.commit();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
applicationId = rs.getInt(1);
|
||||
}
|
||||
return applicationId;
|
||||
} catch (SQLException e) {
|
||||
try {
|
||||
conn.rollback();
|
||||
} catch (SQLException e1) {
|
||||
log.warn("Error occurred while roll-backing the transaction", e);
|
||||
}
|
||||
throw new DeviceManagementDAOException("Error occurred while removing application '" +
|
||||
applicationName + "'", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Application getApplication(String identifier, int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
Application application = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
stmt = conn.prepareStatement("SELECT ID, NAME, PACKAGE_NAME, CATEGORY, PLATFORM, TYPE, VERSION, IMAGE_URL, " +
|
||||
"LOCATION_URL FROM DM_APPLICATION WHERE PACKAGE_NAME = ? AND TENANT_ID = ?");
|
||||
stmt.setString(1, identifier);
|
||||
stmt.setInt(2, tenantId);
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
application = this.loadApplication(rs);
|
||||
}
|
||||
return application;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while retrieving application application '" +
|
||||
identifier + "'", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws DeviceManagementDAOException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
|
||||
private Application loadApplication(ResultSet rs) throws SQLException {
|
||||
Application application = new Application();
|
||||
application.setId(rs.getInt("ID"));
|
||||
application.setName(rs.getString("NAME"));
|
||||
application.setPackageName(rs.getString("PACKAGE_NAME"));
|
||||
application.setCategory(rs.getString("CATEGORY"));
|
||||
application.setType(rs.getString("TYPE"));
|
||||
application.setVersion(rs.getString("VERSION"));
|
||||
application.setImageUrl(rs.getString("IMAGE_URL"));
|
||||
application.setLocationUrl(rs.getString("LOCATION_URL"));
|
||||
application.setPlatform(rs.getString("PLATFORM"));
|
||||
return application;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.core.dao.impl;
|
||||
|
||||
import org.wso2.carbon.device.mgt.core.dao.ApplicationMappingDAO;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ApplicationMappingDAOImpl implements ApplicationMappingDAO {
|
||||
|
||||
@Override
|
||||
public int addApplicationMapping(int deviceId, int applicationId,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
int mappingId = -1;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " +
|
||||
"TENANT_ID) VALUES (?, ?, ?)";
|
||||
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setInt(2, applicationId);
|
||||
stmt.setInt(3, tenantId);
|
||||
stmt.execute();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
mappingId = rs.getInt(1);
|
||||
}
|
||||
return mappingId;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while adding device application mapping", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> addApplicationMappings(int deviceId, List<Integer> applicationIds,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
List<Integer> mappingIds = new ArrayList<Integer>();
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " +
|
||||
"TENANT_ID) VALUES (?, ?, ?)";
|
||||
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
|
||||
for (int applicationId : applicationIds) {
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setInt(2, applicationId);
|
||||
stmt.setInt(3, tenantId);
|
||||
stmt.addBatch();
|
||||
}
|
||||
stmt.executeBatch();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
while (rs.next()) {
|
||||
mappingIds.add(rs.getInt(1));
|
||||
}
|
||||
return mappingIds;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while adding device application mappings", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int removeApplicationMapping(int deviceId, int applicationId,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
ResultSet rs;
|
||||
int mappingId = -1;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "DELETE DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " +
|
||||
"APPLICATION_ID = ? AND TENANT_ID = ?";
|
||||
PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setInt(2, applicationId);
|
||||
stmt.setInt(3, tenantId);
|
||||
stmt.execute();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
mappingId = rs.getInt(1);
|
||||
}
|
||||
return mappingId;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while adding device application mapping", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws DeviceManagementDAOException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.core.dao.impl;
|
||||
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EnrollmentDAO;
|
||||
|
||||
public class EnrollmentDAOImpl implements EnrollmentDAO {
|
||||
|
||||
@Override
|
||||
public boolean addEnrollment() throws DeviceManagementDAOException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateEnrollment() throws DeviceManagementDAOException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeEnrollment() throws DeviceManagementDAOException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setStatus(int deviceId, String currentOwner, String status) throws DeviceManagementDAOException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getStatus() throws DeviceManagementDAOException {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,216 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.core.dao.impl;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.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.EnrolmentDAO;
|
||||
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Date;
|
||||
|
||||
public class EnrolmentDAOImpl implements EnrolmentDAO {
|
||||
|
||||
@Override
|
||||
public int addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
int enrolmentId = -1;
|
||||
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, Statement.RETURN_GENERATED_KEYS);
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setString(2, enrolmentInfo.getOwner());
|
||||
stmt.setString(3, enrolmentInfo.getOwnership().toString());
|
||||
stmt.setString(4, enrolmentInfo.getStatus().toString());
|
||||
stmt.setTimestamp(5, new Timestamp(new Date().getTime()));
|
||||
stmt.setTimestamp(6, new Timestamp(new Date().getTime()));
|
||||
stmt.setInt(7, tenantId);
|
||||
stmt.execute();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
enrolmentId = rs.getInt(1);
|
||||
}
|
||||
return enrolmentId;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while adding enrolment configuration", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updateEnrollment(int deviceId, EnrolmentInfo enrolmentInfo,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
int enrolmentId = -1;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, " +
|
||||
"DATE_OF_ENROLMENT = ?, DATE_OF_LAST_UPDATE = ? WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
stmt.setString(1, enrolmentInfo.getOwnership().toString());
|
||||
stmt.setString(2, enrolmentInfo.getStatus().toString());
|
||||
stmt.setTimestamp(3, new Timestamp(enrolmentInfo.getDateOfEnrolment()));
|
||||
stmt.setTimestamp(4, new Timestamp(enrolmentInfo.getDateOfLastUpdate()));
|
||||
stmt.setInt(5, deviceId);
|
||||
stmt.setString(6, enrolmentInfo.getOwner());
|
||||
stmt.setInt(7, tenantId);
|
||||
stmt.executeUpdate();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
enrolmentId = rs.getInt(1);
|
||||
}
|
||||
return enrolmentId;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int removeEnrollment(int deviceId, String currentOwner,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
int enrolmentId = -1;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "DELETE DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setString(2, currentOwner);
|
||||
stmt.setInt(3, tenantId);
|
||||
stmt.executeUpdate();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
enrolmentId = rs.getInt(1);
|
||||
}
|
||||
return enrolmentId;
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while removing device enrolment", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setStatus(int deviceId, String currentOwner, EnrolmentInfo.Status status,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, status.toString());
|
||||
stmt.setInt(2, deviceId);
|
||||
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 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(2, deviceId);
|
||||
stmt.setString(3, currentOwner);
|
||||
stmt.setInt(4, 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 getEnrolment(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, 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);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws DeviceManagementDAOException {
|
||||
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.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime());
|
||||
enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime());
|
||||
enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS")));
|
||||
return enrolmentInfo;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.core.dao;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||
|
||||
public class ApplicationPersistenceDAOTests extends BaseDeviceManagementDAOTest {
|
||||
|
||||
private static final Log log = LogFactory.getLog(ApplicationPersistenceDAOTests.class);
|
||||
private ApplicationDAO applicationDAO = DeviceManagementDAOFactory.getApplicationDAO();
|
||||
|
||||
@Test
|
||||
public void testAddApplication() {
|
||||
/* Initializing source application bean to be tested */
|
||||
Application source = new Application();
|
||||
source.setName("SimpleCalculator");
|
||||
source.setCategory("TestCategory");
|
||||
source.setPackageName("com.simple.calculator");
|
||||
source.setType("TestType");
|
||||
source.setVersion("1.0.0");
|
||||
source.setImageUrl("http://test.org/image/");
|
||||
source.setLocationUrl("http://test.org/location/");
|
||||
|
||||
/* Adding dummy application to the application store */
|
||||
try {
|
||||
DeviceManagementDAOFactory.openConnection();
|
||||
applicationDAO.addApplication(source, -1234);
|
||||
} catch (DeviceManagementDAOException e) {
|
||||
log.error("Error occurred while adding application '" + source.getName() + "'", e);
|
||||
} finally {
|
||||
try {
|
||||
DeviceManagementDAOFactory.closeConnection();
|
||||
} catch (DeviceManagementDAOException e) {
|
||||
log.warn("Error occurred while closing the connection", e);
|
||||
}
|
||||
}
|
||||
/* Retrieving the application by its name */
|
||||
Application target = null;
|
||||
try {
|
||||
target = this.getApplication(source.getPackageName(), -1234);
|
||||
} catch (DeviceManagementDAOException e) {
|
||||
String msg = "Error occurred while retrieving application info";
|
||||
log.error(msg, e);
|
||||
Assert.fail(msg, e);
|
||||
}
|
||||
|
||||
Assert.assertEquals(target, source, "Application added is not as same as what's retrieved");
|
||||
}
|
||||
|
||||
private Application getApplication(String packageName, int tenantId) throws DeviceManagementDAOException {
|
||||
try {
|
||||
DeviceManagementDAOFactory.openConnection();
|
||||
return applicationDAO.getApplication(packageName, tenantId);
|
||||
} finally {
|
||||
try {
|
||||
DeviceManagementDAOFactory.closeConnection();
|
||||
} catch (DeviceManagementDAOException e) {
|
||||
log.warn("Error occurred while closing connection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.core.dao;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.tomcat.jdbc.pool.PoolProperties;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.AfterSuite;
|
||||
import org.testng.annotations.BeforeSuite;
|
||||
import org.testng.annotations.Parameters;
|
||||
import org.w3c.dom.Document;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.TestUtils;
|
||||
import org.wso2.carbon.device.mgt.core.common.DBTypes;
|
||||
import org.wso2.carbon.device.mgt.core.common.TestDBConfiguration;
|
||||
import org.wso2.carbon.device.mgt.core.common.TestDBConfigurations;
|
||||
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class BaseDeviceManagementDAOTest {
|
||||
|
||||
private DataSource dataSource;
|
||||
private static final Log log = LogFactory.getLog(BaseDeviceManagementDAOTest.class);
|
||||
|
||||
@BeforeSuite
|
||||
public void setupDataSource() throws Exception {
|
||||
this.dataSource = this.getDataSource(this.readDataSourceConfig());
|
||||
this.initSQLScript();
|
||||
DeviceManagementDAOFactory.init(dataSource);
|
||||
}
|
||||
|
||||
private DataSource getDataSource(TestDBConfiguration config) {
|
||||
PoolProperties properties = new PoolProperties();
|
||||
properties.setUrl(config.getConnectionUrl());
|
||||
properties.setDriverClassName(config.getDriverClass());
|
||||
properties.setUsername(config.getUserName());
|
||||
properties.setPassword(config.getPwd());
|
||||
return new org.apache.tomcat.jdbc.pool.DataSource(properties);
|
||||
}
|
||||
|
||||
private TestDBConfiguration readDataSourceConfig() throws DeviceManagementDAOException,
|
||||
DeviceManagementException {
|
||||
File config = new File("src/test/resources/data-source-config.xml");
|
||||
Document doc;
|
||||
TestDBConfigurations dbConfigs;
|
||||
|
||||
doc = DeviceManagerUtil.convertToDocument(config);
|
||||
JAXBContext testDBContext;
|
||||
|
||||
try {
|
||||
testDBContext = JAXBContext.newInstance(TestDBConfigurations.class);
|
||||
Unmarshaller unmarshaller = testDBContext.createUnmarshaller();
|
||||
dbConfigs = (TestDBConfigurations) unmarshaller.unmarshal(doc);
|
||||
} catch (JAXBException e) {
|
||||
throw new DeviceManagementDAOException("Error parsing test db configurations", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void initSQLScript() throws Exception {
|
||||
Connection conn = null;
|
||||
Statement stmt = null;
|
||||
try {
|
||||
conn = this.getDataSource().getConnection();
|
||||
stmt = conn.createStatement();
|
||||
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/h2.sql'");
|
||||
} finally {
|
||||
TestUtils.cleanupResources(conn, stmt, null);
|
||||
}
|
||||
}
|
||||
|
||||
@AfterSuite
|
||||
public void deleteData() {
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = getDataSource().getConnection();
|
||||
conn.setAutoCommit(false);
|
||||
|
||||
this.cleanupEnrolmentData(conn);
|
||||
this.cleanupDeviceData(conn);
|
||||
this.cleanupDeviceTypeData(conn);
|
||||
|
||||
conn.commit();
|
||||
} catch (SQLException e) {
|
||||
try {
|
||||
if (conn != null) {
|
||||
conn.rollback();
|
||||
}
|
||||
} catch (SQLException e1) {
|
||||
log.error("Error occurred while roll-backing the transaction", e);
|
||||
}
|
||||
String msg = "Error occurred while cleaning up temporary data generated during test execution";
|
||||
log.error(msg, e);
|
||||
Assert.fail(msg, e);
|
||||
} finally {
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing the connection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanupEnrolmentData(Connection conn) throws SQLException {
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
stmt = conn.prepareStatement("DELETE FROM DM_ENROLMENT");
|
||||
stmt.execute();
|
||||
} finally {
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanupDeviceData(Connection conn) throws SQLException {
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
stmt = conn.prepareStatement("DELETE FROM DM_DEVICE");
|
||||
stmt.execute();
|
||||
} finally {
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanupDeviceTypeData(Connection conn) throws SQLException {
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
stmt = conn.prepareStatement("DELETE FROM DM_DEVICE_TYPE");
|
||||
stmt.execute();
|
||||
} finally {
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DataSource getDataSource() {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.core.dao;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class EnrolmentPersistenceDAOTests extends BaseDeviceManagementDAOTest {
|
||||
|
||||
private static final Log log = LogFactory.getLog(EnrolmentPersistenceDAOTests.class);
|
||||
private EnrolmentDAO enrolmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO();
|
||||
|
||||
@Test
|
||||
public void testAddEnrolment() {
|
||||
int deviceId = 1234;
|
||||
String owner = "admin";
|
||||
|
||||
/* Initializing source enrolment configuration bean to be tested */
|
||||
EnrolmentInfo source =
|
||||
new EnrolmentInfo(null, owner, EnrolmentInfo.OwnerShip.BYOD,
|
||||
EnrolmentInfo.Status.CREATED);
|
||||
|
||||
/* Adding dummy enrolment configuration to the device management metadata store */
|
||||
try {
|
||||
DeviceManagementDAOFactory.openConnection();
|
||||
enrolmentDAO.addEnrollment(deviceId, source, -1234);
|
||||
} catch (DeviceManagementDAOException e) {
|
||||
log.error("Error occurred while adding enrollment", e);
|
||||
} finally {
|
||||
try {
|
||||
DeviceManagementDAOFactory.closeConnection();
|
||||
} catch (DeviceManagementDAOException e) {
|
||||
log.warn("Error occurred while closing the connection", e);
|
||||
}
|
||||
}
|
||||
/* Retrieving the enrolment associated with the given deviceId and owner */
|
||||
EnrolmentInfo target = null;
|
||||
try {
|
||||
target = this.getEnrolmentConfig(deviceId, owner, -1234);
|
||||
} catch (DeviceManagementDAOException e) {
|
||||
String msg = "Error occurred while retrieving application info";
|
||||
log.error(msg, e);
|
||||
Assert.fail(msg, e);
|
||||
}
|
||||
|
||||
Assert.assertEquals(target, source, "Enrolment configuration added is not as same as what's retrieved");
|
||||
}
|
||||
|
||||
private EnrolmentInfo getEnrolmentConfig(int deviceId, String currentOwner,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
try {
|
||||
DeviceManagementDAOFactory.openConnection();
|
||||
return enrolmentDAO.getEnrolment(deviceId, currentOwner, tenantId);
|
||||
} finally {
|
||||
try {
|
||||
DeviceManagementDAOFactory.closeConnection();
|
||||
} catch (DeviceManagementDAOException e) {
|
||||
log.warn("Error occurred while closing connection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue