diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java index a949ee7f99f..bd0d7cc86a2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java @@ -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() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractApplicationDAOImpl.java similarity index 85% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractApplicationDAOImpl.java index 805775d0e4a..3594cb2b31e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractApplicationDAOImpl.java @@ -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 addApplications(List applications, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs; - List 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 removeApplications(List apps, int tenantId) throws DeviceManagementDAOException { Connection conn = null; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericApplicationDAOImpl.java new file mode 100644 index 00000000000..367f71f612e --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericApplicationDAOImpl.java @@ -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 addApplications(List applications, + int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs; + List 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(); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/PostgreSQLApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/PostgreSQLApplicationDAOImpl.java new file mode 100644 index 00000000000..a6a50783235 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/PostgreSQLApplicationDAOImpl.java @@ -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 addApplications(List applications, + int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs; + List 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(); + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceTests.java index 6bbeb5393ad..732adf791d9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceTests.java @@ -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(); } }