forked from community/device-mgt-core
parent
76d0fff5be
commit
100c58f91f
@ -1,37 +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;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ApplicationMappingDAO {
|
||||
|
||||
int addApplicationMapping(int deviceId, int applicationId, int tenantId) throws DeviceManagementDAOException;
|
||||
|
||||
void addApplicationMappings(int deviceId, List<Integer> applicationIds, int tenantId)
|
||||
throws DeviceManagementDAOException;
|
||||
|
||||
void addApplicationMappingsWithApps(int deviceId, int enrolmentId, List<Application> applications, int tenantId)
|
||||
throws DeviceManagementDAOException;
|
||||
|
||||
void removeApplicationMapping(int deviceId, int enrolmentId, List<Integer> appIdList, int tenantId)
|
||||
throws DeviceManagementDAOException;
|
||||
}
|
@ -1,183 +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.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.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.io.*;
|
||||
import java.sql.*;
|
||||
import java.util.List;
|
||||
|
||||
public class ApplicationMappingDAOImpl implements ApplicationMappingDAO {
|
||||
|
||||
private static final Log log = LogFactory.getLog(ApplicationMappingDAOImpl.class);
|
||||
|
||||
@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, new String[]{"id"});
|
||||
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 void addApplicationMappings(int deviceId, List<Integer> applicationIds,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " +
|
||||
"TENANT_ID) VALUES (?, ?, ?)";
|
||||
|
||||
conn.setAutoCommit(false);
|
||||
stmt = conn.prepareStatement(sql);
|
||||
|
||||
for (int applicationId : applicationIds) {
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setInt(2, applicationId);
|
||||
stmt.setInt(3, tenantId);
|
||||
stmt.addBatch();
|
||||
}
|
||||
stmt.executeBatch();
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while adding device application mappings", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addApplicationMappingsWithApps(int deviceId, int enrolmentId, List<Application> applications, int tenantId)
|
||||
throws DeviceManagementDAOException {
|
||||
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
ByteArrayOutputStream bao = null;
|
||||
ObjectOutputStream oos = null;
|
||||
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, ENROLMENT_ID, APPLICATION_ID, " +
|
||||
"APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
conn.setAutoCommit(false);
|
||||
stmt = conn.prepareStatement(sql);
|
||||
|
||||
for (Application application : applications) {
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setInt(2, enrolmentId);
|
||||
stmt.setInt(3, application.getId());
|
||||
|
||||
bao = new ByteArrayOutputStream();
|
||||
oos = new ObjectOutputStream(bao);
|
||||
oos.writeObject(application.getAppProperties());
|
||||
stmt.setBytes(4, bao.toByteArray());
|
||||
|
||||
stmt.setInt(5, application.getMemoryUsage());
|
||||
stmt.setBoolean(6, application.isActive());
|
||||
|
||||
stmt.setInt(7, tenantId);
|
||||
stmt.addBatch();
|
||||
}
|
||||
stmt.executeBatch();
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while adding device application mappings", e);
|
||||
} catch (IOException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while serializing application properties object", e);
|
||||
} finally {
|
||||
if (bao != null) {
|
||||
try {
|
||||
bao.close();
|
||||
} catch (IOException e) {
|
||||
log.error("Error occurred while closing ByteArrayOutputStream", e);
|
||||
}
|
||||
}
|
||||
if (oos != null) {
|
||||
try {
|
||||
oos.close();
|
||||
} catch (IOException e) {
|
||||
log.error("Error occurred while closing ObjectOutputStream", e);
|
||||
}
|
||||
}
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeApplicationMapping(int deviceId, int enrolmentId, List<Integer> appIdList,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " +
|
||||
"APPLICATION_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?";
|
||||
|
||||
conn = this.getConnection();
|
||||
for (int appId : appIdList) {
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setInt(2, appId);
|
||||
stmt.setInt(3, tenantId);
|
||||
stmt.setInt(4, enrolmentId);
|
||||
stmt.execute();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DeviceManagementDAOException("Error occurred while removing device application mapping", e);
|
||||
} finally {
|
||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 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.app.mgt.Application;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Generic DAO implementation for Application Management Operations
|
||||
*/
|
||||
public class GenericApplicationDAOImpl extends AbstractApplicationDAOImpl{
|
||||
|
||||
@Override
|
||||
public List<Integer> addApplications(List<Application> applications,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs;
|
||||
List<Integer> applicationIds = new ArrayList<>();
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, " +
|
||||
"CATEGORY, VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID,APP_PROPERTIES, " +
|
||||
"APP_IDENTIFIER, MEMORY_USAGE, IS_ACTIVE) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new String[]{"id"});
|
||||
|
||||
for (Application application : applications) {
|
||||
|
||||
stmt.setString(1, application.getName());
|
||||
stmt.setString(2, application.getPlatform());
|
||||
stmt.setString(3, application.getCategory());
|
||||
stmt.setString(4, application.getVersion());
|
||||
stmt.setString(5, application.getType());
|
||||
stmt.setString(6, application.getLocationUrl());
|
||||
stmt.setString(7, application.getImageUrl());
|
||||
stmt.setInt(8, tenantId);
|
||||
|
||||
// Removing the application properties saving from the application table.
|
||||
stmt.setBigDecimal(9, null);
|
||||
|
||||
stmt.setString(10, application.getApplicationIdentifier());
|
||||
|
||||
// Removing the application memory
|
||||
stmt.setInt(11, 0);
|
||||
stmt.setBoolean(12, true);
|
||||
|
||||
stmt.executeUpdate();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (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);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
|
||||
}
|
@ -1,90 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 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.app.mgt.Application;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* PosgreSQL specific DAO implementation for Application Management Operations
|
||||
*/
|
||||
public class PostgreSQLApplicationDAOImpl extends AbstractApplicationDAOImpl{
|
||||
|
||||
@Override
|
||||
public List<Integer> addApplications(List<Application> applications,
|
||||
int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs;
|
||||
List<Integer> applicationIds = new ArrayList<>();
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, " +
|
||||
"CATEGORY, VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID,APP_PROPERTIES, " +
|
||||
"APP_IDENTIFIER, MEMORY_USAGE, IS_ACTIVE) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new String[]{"id"});
|
||||
|
||||
for (Application application : applications) {
|
||||
|
||||
stmt.setString(1, application.getName());
|
||||
stmt.setString(2, application.getPlatform());
|
||||
stmt.setString(3, application.getCategory());
|
||||
stmt.setString(4, application.getVersion());
|
||||
stmt.setString(5, application.getType());
|
||||
stmt.setString(6, application.getLocationUrl());
|
||||
stmt.setString(7, application.getImageUrl());
|
||||
stmt.setInt(8, tenantId);
|
||||
|
||||
// Removing the application properties saving from the application table.
|
||||
stmt.setBytes(9, null);
|
||||
|
||||
stmt.setString(10, application.getApplicationIdentifier());
|
||||
|
||||
// Removing the application memory
|
||||
stmt.setInt(11, 0);
|
||||
stmt.setBoolean(12, true);
|
||||
|
||||
stmt.executeUpdate();
|
||||
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (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);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
}
|
@ -1,85 +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;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.Test;
|
||||
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||
import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest;
|
||||
import org.wso2.carbon.device.mgt.core.common.TestDataHolder;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class ApplicationPersistenceTests extends BaseDeviceManagementTest {
|
||||
|
||||
private static final Log log = LogFactory.getLog(ApplicationPersistenceTests.class);
|
||||
private ApplicationDAO applicationDAO = null;
|
||||
|
||||
@Test
|
||||
public void testAddApplication() {
|
||||
/* Adding dummy application to the application store */
|
||||
String testAppIdentifier = "test sample1";
|
||||
try {
|
||||
DeviceManagementDAOFactory.openConnection();
|
||||
applicationDAO.addApplication(TestDataHolder.generateApplicationDummyData(testAppIdentifier), -1234);
|
||||
} catch (DeviceManagementDAOException | SQLException e) {
|
||||
log.error("Error occurred while adding application test sample1", e);
|
||||
} finally {
|
||||
DeviceManagementDAOFactory.closeConnection();
|
||||
}
|
||||
/* Retrieving the application by its name */
|
||||
Application target = null;
|
||||
try {
|
||||
target = this.getApplication(testAppIdentifier, -1234);
|
||||
} catch (DeviceManagementDAOException e) {
|
||||
String msg = "Error occurred while retrieving application info";
|
||||
log.error(msg, e);
|
||||
Assert.fail(msg, e);
|
||||
}
|
||||
|
||||
if (!isMock()) {
|
||||
Assert.assertEquals(target.getApplicationIdentifier(), testAppIdentifier,
|
||||
"Application added is not as same as what's retrieved");
|
||||
}
|
||||
}
|
||||
|
||||
private Application getApplication(String appIdentifier, int tenantId) throws DeviceManagementDAOException {
|
||||
Application application = null;
|
||||
try {
|
||||
DeviceManagementDAOFactory.openConnection();
|
||||
application = applicationDAO.getApplication(appIdentifier, tenantId);
|
||||
} catch (SQLException e) {
|
||||
log.error("Error occurred while metadata corresponding to the application '" + appIdentifier + "'", e);
|
||||
} finally {
|
||||
DeviceManagementDAOFactory.closeConnection();
|
||||
}
|
||||
return application;
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
@Override
|
||||
public void init() throws Exception {
|
||||
this.initDataSource();
|
||||
applicationDAO = DeviceManagementDAOFactory.getApplicationDAO();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue