Merge pull request #1184 from ruwany/master

Fixed https://github.com/wso2/carbon-device-mgt/issues/1175
merge-requests/1/head
Ruwan 7 years ago committed by GitHub
commit 40ae5a09a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -26,10 +26,12 @@ import org.wso2.carbon.device.mgt.common.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.UnsupportedDatabaseEngineException;
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.device.mgt.core.dao.impl.ApplicationDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.AbstractApplicationDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.ApplicationMappingDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.DeviceTypeDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.EnrollmentDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.GenericApplicationDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.PostgreSQLApplicationDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.device.GenericDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.device.OracleDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.device.PostgreSQLDeviceDAOImpl;
@ -125,7 +127,20 @@ public class DeviceManagementDAOFactory {
}
public static ApplicationDAO getApplicationDAO() {
return new ApplicationDAOImpl();
if (databaseEngine != null) {
switch (databaseEngine) {
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL:
return new PostgreSQLApplicationDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE:
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL:
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2:
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL:
return new GenericApplicationDAOImpl();
default:
throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine);
}
}
throw new IllegalStateException("Database engine has not initialized properly.");
}
public static ApplicationMappingDAO getApplicationMappingDAO() {

@ -40,9 +40,9 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class ApplicationDAOImpl implements ApplicationDAO {
public abstract class AbstractApplicationDAOImpl implements ApplicationDAO {
private static final Log log = LogFactory.getLog(ApplicationDAOImpl.class);
private static final Log log = LogFactory.getLog(AbstractApplicationDAOImpl.class);
@Override
public int addApplication(Application application, int tenantId) throws DeviceManagementDAOException {
@ -106,55 +106,6 @@ public class ApplicationDAOImpl implements ApplicationDAO {
}
}
@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);
}
}
@Override
public List<Integer> removeApplications(List<Application> apps, int tenantId) throws DeviceManagementDAOException {
Connection conn = null;

@ -0,0 +1,91 @@
/*
* 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();
}
}

@ -0,0 +1,90 @@
/*
* 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();
}
}

@ -32,7 +32,7 @@ import java.sql.SQLException;
public class ApplicationPersistenceTests extends BaseDeviceManagementTest {
private static final Log log = LogFactory.getLog(ApplicationPersistenceTests.class);
private ApplicationDAO applicationDAO = DeviceManagementDAOFactory.getApplicationDAO();
private ApplicationDAO applicationDAO = null;
@Test
public void testAddApplication() {
@ -79,6 +79,7 @@ public class ApplicationPersistenceTests extends BaseDeviceManagementTest {
@Override
public void init() throws Exception {
this.initDataSource();
applicationDAO = DeviceManagementDAOFactory.getApplicationDAO();
}
}

Loading…
Cancel
Save