From 671ef77b63a235f6fc6830b4a7552429c60a34f2 Mon Sep 17 00:00:00 2001 From: harshanl Date: Fri, 9 Sep 2016 18:25:28 +0530 Subject: [PATCH] Fixed EMM-1107 issue --- .../core/dao/PolicyManagementDAOFactory.java | 31 ++++- .../AbstractFeatureDAO.java} | 108 ++++++------------ .../impl/feature/GenericFeatureDAOImpl.java | 93 +++++++++++++++ .../impl/feature/SQLServerFeatureDAOImpl.java | 94 +++++++++++++++ 4 files changed, 248 insertions(+), 78 deletions(-) rename components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/{FeatureDAOImpl.java => feature/AbstractFeatureDAO.java} (79%) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/GenericFeatureDAOImpl.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/SQLServerFeatureDAOImpl.java diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java index 0b00f90b14..026335ccf7 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java @@ -20,13 +20,16 @@ package org.wso2.carbon.policy.mgt.core.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException; +import org.wso2.carbon.device.mgt.common.UnsupportedDatabaseEngineException; import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.policy.mgt.core.config.datasource.JNDILookupDefinition; -import org.wso2.carbon.policy.mgt.core.dao.impl.FeatureDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.MonitoringDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl; +import org.wso2.carbon.policy.mgt.core.dao.impl.feature.GenericFeatureDAOImpl; +import org.wso2.carbon.policy.mgt.core.dao.impl.feature.SQLServerFeatureDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; import javax.sql.DataSource; @@ -38,15 +41,26 @@ import java.util.List; public class PolicyManagementDAOFactory { private static DataSource dataSource; + private static String databaseEngine; private static final Log log = LogFactory.getLog(PolicyManagementDAOFactory.class); private static ThreadLocal currentConnection = new ThreadLocal<>(); public static void init(DataSourceConfig config) { dataSource = resolveDataSource(config); + try { + databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName(); + } catch (SQLException e) { + log.error("Error occurred while retrieving config.datasource connection", e); + } } public static void init(DataSource dtSource) { dataSource = dtSource; + try { + databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName(); + } catch (SQLException e) { + log.error("Error occurred while retrieving config.datasource connection", e); + } } public static PolicyDAO getPolicyDAO() { @@ -58,7 +72,20 @@ public class PolicyManagementDAOFactory { } public static FeatureDAO getFeatureDAO() { - return new FeatureDAOImpl(); + if (databaseEngine != null) { + switch (databaseEngine) { + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL: + return new SQLServerFeatureDAOImpl(); + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE: + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL: + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2: + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL: + return new GenericFeatureDAOImpl(); + default: + throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); + } + } + throw new IllegalStateException("Database engine has not initialized properly."); } public static MonitoringDAO getMonitoringDAO() { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/AbstractFeatureDAO.java similarity index 79% rename from components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java rename to components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/AbstractFeatureDAO.java index 80e56af140..c94cf48b2a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/AbstractFeatureDAO.java @@ -1,22 +1,22 @@ /* -* 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.policy.mgt.core.dao.impl; + * Copyright (c) 2016, 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.policy.mgt.core.dao.impl.feature; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -40,9 +40,12 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -public class FeatureDAOImpl implements FeatureDAO { +/** + * Abstract implementation of FeatureDAO which holds generic SQL queries. + */ +public abstract class AbstractFeatureDAO implements FeatureDAO { - private static final Log log = LogFactory.getLog(FeatureDAOImpl.class); + private static final Log log = LogFactory.getLog(AbstractFeatureDAO.class); @Override public ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException { @@ -51,59 +54,13 @@ public class FeatureDAOImpl implements FeatureDAO { @Override public ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws - FeatureManagerDAOException { + FeatureManagerDAOException { return null; } - @Override - public List addProfileFeatures(List features, int profileId) throws - FeatureManagerDAOException { - - Connection conn; - PreparedStatement stmt = null; - ResultSet generatedKeys = null; - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - - try { - conn = this.getConnection(); - String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE, CONTENT, " + - "TENANT_ID) VALUES (?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(query, new String[] {"id"}); - - for (ProfileFeature feature : features) { - stmt.setInt(1, profileId); - stmt.setString(2, feature.getFeatureCode()); - stmt.setString(3, feature.getDeviceType()); - // if (conn.getMetaData().getDriverName().contains("H2")) { - // stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); - // } else { - stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); - //} - stmt.setInt(5, tenantId); - stmt.addBatch(); - //Not adding the logic to check the size of the stmt and execute if the size records added is over 1000 - } - stmt.executeBatch(); - - generatedKeys = stmt.getGeneratedKeys(); - int i = 0; - - while (generatedKeys.next()) { - features.get(i).setId(generatedKeys.getInt(1)); - i++; - } - - } catch (SQLException | IOException e) { - throw new FeatureManagerDAOException("Error occurred while adding the feature list to the database.", e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); - } - return features; - } - @Override public List updateProfileFeatures(List features, int profileId) throws - FeatureManagerDAOException { + FeatureManagerDAOException { Connection conn; PreparedStatement stmt = null; @@ -112,7 +69,7 @@ public class FeatureDAOImpl implements FeatureDAO { try { conn = this.getConnection(); String query = "UPDATE DM_PROFILE_FEATURES SET CONTENT = ? WHERE PROFILE_ID = ? AND FEATURE_CODE = ? AND" + - " TENANT_ID = ?"; + " TENANT_ID = ?"; stmt = conn.prepareStatement(query); for (ProfileFeature feature : features) { @@ -209,7 +166,7 @@ public class FeatureDAOImpl implements FeatureDAO { try { conn = this.getConnection(); String query = "SELECT ID, PROFILE_ID, FEATURE_CODE, DEVICE_TYPE, CONTENT FROM DM_PROFILE_FEATURES " + - "WHERE TENANT_ID = ?"; + "WHERE TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); @@ -270,8 +227,8 @@ public class FeatureDAOImpl implements FeatureDAO { try { conn = this.getConnection(); String query = "SELECT f.ID ID, f.NAME NAME, f.CODE CODE, f.DEVICE_TYPE DEVICE_TYPE," + - " f.EVALUATION_RULE EVALUATION_RULE FROM DM_FEATURES f INNER JOIN DM_DEVICE_TYPE d " + - "ON d.ID=f.DEVICE_TYPE WHERE d.NAME = ?"; + " f.EVALUATION_RULE EVALUATION_RULE FROM DM_FEATURES f INNER JOIN DM_DEVICE_TYPE d " + + "ON d.ID=f.DEVICE_TYPE WHERE d.NAME = ?"; stmt = conn.prepareStatement(query); stmt.setString(1, deviceType); resultSet = stmt.executeQuery(); @@ -286,7 +243,7 @@ public class FeatureDAOImpl implements FeatureDAO { } } catch (SQLException e) { throw new FeatureManagerDAOException("Unable to get the list of the features related device type " + - "from database.", e); + "from database.", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); } @@ -304,7 +261,7 @@ public class FeatureDAOImpl implements FeatureDAO { try { conn = this.getConnection(); String query = "SELECT ID, FEATURE_CODE, DEVICE_TYPE, CONTENT FROM DM_PROFILE_FEATURES " + - "WHERE PROFILE_ID = ? AND TENANT_ID = ?"; + "WHERE PROFILE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, profileId); stmt.setInt(2, tenantId); @@ -372,7 +329,7 @@ public class FeatureDAOImpl implements FeatureDAO { return false; } catch (SQLException e) { throw new FeatureManagerDAOException("Unable to delete the feature " + featureId + " (Feature ID) " + - "from database.", e); + "from database.", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -381,5 +338,4 @@ public class FeatureDAOImpl implements FeatureDAO { private Connection getConnection() throws FeatureManagerDAOException { return PolicyManagementDAOFactory.getConnection(); } - } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/GenericFeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/GenericFeatureDAOImpl.java new file mode 100644 index 0000000000..520e5da8a1 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/GenericFeatureDAOImpl.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2016, 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.policy.mgt.core.dao.impl.feature; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.policy.mgt.common.ProfileFeature; +import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; +import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +/** + * FeatureDAO implementation for DB engines with ANSI SQL support. + */ +public final class GenericFeatureDAOImpl extends AbstractFeatureDAO { + + private static final Log log = LogFactory.getLog(GenericFeatureDAOImpl.class); + + @Override + public List addProfileFeatures(List features, int profileId) throws + FeatureManagerDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE, CONTENT, " + + "TENANT_ID) VALUES (?, ?, ?, ?, ?)"; + stmt = conn.prepareStatement(query, new String[] {"id"}); + + for (ProfileFeature feature : features) { + stmt.setInt(1, profileId); + stmt.setString(2, feature.getFeatureCode()); + stmt.setString(3, feature.getDeviceType()); + // if (conn.getMetaData().getDriverName().contains("H2")) { + // stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); + // } else { + stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); + //} + stmt.setInt(5, tenantId); + stmt.addBatch(); + //Not adding the logic to check the size of the stmt and execute if the size records added is over 1000 + } + stmt.executeBatch(); + + generatedKeys = stmt.getGeneratedKeys(); + int i = 0; + + while (generatedKeys.next()) { + features.get(i).setId(generatedKeys.getInt(1)); + i++; + } + + } catch (SQLException | IOException e) { + throw new FeatureManagerDAOException("Error occurred while adding the feature list to the database.", e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); + } + return features; + } + + private Connection getConnection() throws FeatureManagerDAOException { + return PolicyManagementDAOFactory.getConnection(); + } +} \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/SQLServerFeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/SQLServerFeatureDAOImpl.java new file mode 100644 index 0000000000..ea5c538003 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/feature/SQLServerFeatureDAOImpl.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2016, 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.policy.mgt.core.dao.impl.feature; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.policy.mgt.common.ProfileFeature; +import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; +import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +/** + * FeatureDAO implementation for MSSQL DB engine. + */ +public final class SQLServerFeatureDAOImpl extends AbstractFeatureDAO { + + private static final Log log = LogFactory.getLog(SQLServerFeatureDAOImpl.class); + + @Override + public List addProfileFeatures(List features, int profileId) throws + FeatureManagerDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE, CONTENT, " + + "TENANT_ID) VALUES (?, ?, ?, ?, ?)"; + stmt = conn.prepareStatement(query, new String[] {"id"}); + + for (ProfileFeature feature : features) { + stmt.setInt(1, profileId); + stmt.setString(2, feature.getFeatureCode()); + stmt.setString(3, feature.getDeviceType()); + // if (conn.getMetaData().getDriverName().contains("H2")) { + // stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); + // } else { + stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); + //} + stmt.setInt(5, tenantId); + stmt.addBatch(); + //Not adding the logic to check the size of the stmt and execute if the size records added is over 1000 + } + stmt.executeBatch(); +// This logic has been commented out due to getGeneratedKeys method is not supported in MSSQL. +// generatedKeys = stmt.getGeneratedKeys(); +// int i = 0; +// +// while (generatedKeys.next()) { +// features.get(i).setId(generatedKeys.getInt(1)); +// i++; +// } + + } catch (SQLException | IOException e) { + throw new FeatureManagerDAOException("Error occurred while adding the feature list to the database.", e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); + } + return features; + } + + private Connection getConnection() throws FeatureManagerDAOException { + return PolicyManagementDAOFactory.getConnection(); + } + +}