From 81cb39a4a6a1876fd1633d37fb0749599e34f186 Mon Sep 17 00:00:00 2001 From: Ace Date: Wed, 14 Feb 2018 21:27:39 +0530 Subject: [PATCH 1/4] Fixed https://github.com/wso2/carbon-device-mgt/issues/1175 --- .../core/dao/DeviceManagementDAOFactory.java | 19 +++- ...l.java => AbstractApplicationDAOImpl.java} | 53 +---------- .../dao/impl/GenericApplicationDAOImpl.java | 88 +++++++++++++++++++ .../impl/PostgreSQLApplicationDAOImpl.java | 87 ++++++++++++++++++ 4 files changed, 194 insertions(+), 53 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/{ApplicationDAOImpl.java => AbstractApplicationDAOImpl.java} (85%) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericApplicationDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/PostgreSQLApplicationDAOImpl.java 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 a949ee7f99..bd0d7cc86a 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 805775d0e4..3594cb2b31 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 0000000000..caa61709c2 --- /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,88 @@ +/* +* 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; + +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 0000000000..5fbce2b18e --- /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,87 @@ +/* +* 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; + +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(); + } +} From bba4b092842ac38be134975fcdc7e79557dd8f17 Mon Sep 17 00:00:00 2001 From: Ace Date: Thu, 15 Feb 2018 08:30:34 +0530 Subject: [PATCH 2/4] Adding comments --- .../device/mgt/core/dao/impl/GenericApplicationDAOImpl.java | 3 +++ .../device/mgt/core/dao/impl/PostgreSQLApplicationDAOImpl.java | 3 +++ 2 files changed, 6 insertions(+) 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 index caa61709c2..d3362041c3 100644 --- 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 @@ -30,6 +30,9 @@ 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 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 index 5fbce2b18e..c2411337c1 100644 --- 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 @@ -30,6 +30,9 @@ 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 From e604532b28385ec14332d1df9d24cb2eb7ecf557 Mon Sep 17 00:00:00 2001 From: Ace Date: Thu, 15 Feb 2018 08:50:16 +0530 Subject: [PATCH 3/4] Fixing fomatting issues --- .../device/mgt/core/dao/impl/GenericApplicationDAOImpl.java | 6 +++--- .../mgt/core/dao/impl/PostgreSQLApplicationDAOImpl.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) 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 index d3362041c3..367f71f612 100644 --- 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 @@ -44,11 +44,11 @@ public class GenericApplicationDAOImpl extends AbstractApplicationDAOImpl{ 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) " + + 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()); 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 index c2411337c1..a6a5078323 100644 --- 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 @@ -44,11 +44,11 @@ public class PostgreSQLApplicationDAOImpl extends AbstractApplicationDAOImpl{ 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) " + + 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()); From 251696715933342304688dea0f23e738ede72873 Mon Sep 17 00:00:00 2001 From: Ace Date: Thu, 15 Feb 2018 13:31:34 +0530 Subject: [PATCH 4/4] Fixing test failure --- .../device/mgt/core/dao/ApplicationPersistenceTests.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 6bbeb5393a..732adf791d 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(); } }