From e79c9b4919c16f045084b7073bd67f9636047a48 Mon Sep 17 00:00:00 2001 From: megala21 Date: Mon, 31 Jul 2017 15:34:02 +0530 Subject: [PATCH] Fixing minor issues with database queries --- .../impl/platform/GenericPlatformDAOImpl.java | 24 +++++--- .../mgt/core/deployer/PlatformDeployer.java | 19 +++--- .../dbscripts/cdm/application-mgt/h2.sql | 58 ++++++++++--------- .../dbscripts/cdm/application-mgt/mysql.sql | 34 +++++++---- 4 files changed, 79 insertions(+), 56 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/GenericPlatformDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/GenericPlatformDAOImpl.java index ae51b716d2..b7cee5755b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/GenericPlatformDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/GenericPlatformDAOImpl.java @@ -249,7 +249,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD try { ConnectionManagerUtil.beginTransaction(); for (String platformIdentifier : platformIdentifiers) { - if (getTenantPlatformMapping(tenantId, platformIdentifier) != -1) { + if (getTenantPlatformMapping(tenantId, platformIdentifier) == -1) { int platformId = getPlatformId(tenantId, platformIdentifier); Connection connection = ConnectionManagerUtil.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(insertMapping); @@ -285,8 +285,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD private int getTenantPlatformMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { - String getMapping = "SELECT MAPPING.ID as ID FROM (SELECT ID FROM APPM_PLATFORM_TENANT_MAPPING WHERE " - + "TENANT_ID=?) MAPPING JOIN (SELECT ID FROM APPM_PLATFORM WHERE APPM_PLATFORM.IDENTIFIER=?) " + String getMapping = "SELECT MAPPING.ID as ID FROM (SELECT ID, PLATFORM_ID FROM APPM_PLATFORM_TENANT_MAPPING " + + "WHERE TENANT_ID=?) MAPPING JOIN (SELECT ID FROM APPM_PLATFORM WHERE APPM_PLATFORM.IDENTIFIER=?) " + "PLATFORM ON MAPPING.PLATFORM_ID=PLATFORM.ID"; try { Connection connection = ConnectionManagerUtil.getConnection(); @@ -300,7 +300,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD return -1; } catch (DBConnectionException e) { throw new PlatformManagementDAOException( - "Error occured while obtaining the connection to get the existing " + "Tenant - Platform Mapping.", + "Error occurred while obtaining the connection to get the existing " + "Tenant - Platform Mapping.", e); } catch (SQLException e) { throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + getMapping, e); @@ -462,21 +462,27 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD try { conn = this.getConnection(); - sql += "SELECT * "; - sql += "FROM APPM_PLATFORM "; - sql += "WHERE IDENTIFIER = ? "; + sql += "SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, identifier); + stmt.setInt(2, tenantId); rs = stmt.executeQuery(); Platform platform = null; if (rs.next()) { platform = new Platform(); - platform.setId(rs.getInt("ID")); - platform.setName(rs.getString("NAME")); + platform.setFileBased(rs.getBoolean("FILE_BASED")); + platform.setIdentifier(rs.getString("IDENTIFIER")); + if (!platform.isFileBased()) { + platform.setId(rs.getInt("ID")); + platform.setName(rs.getString("NAME")); + platform.setDescription(rs.getString("DESCRIPTION")); + platform.setIconName(rs.getString("ICON_NAME")); + platform.setShared(rs.getBoolean("IS_SHARED")); + } } return platform; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/PlatformDeployer.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/PlatformDeployer.java index abc043918a..51fb6bef4d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/PlatformDeployer.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/PlatformDeployer.java @@ -59,7 +59,7 @@ public class PlatformDeployer extends AbstractDeployer { JAXBContext jaxbContext = JAXBContext.newInstance(Platform.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Platform platformConf = (Platform) unmarshaller.unmarshal(deploymentFile); - if (platformConf.getName().contentEquals(getPlatformID(deploymentFile.getName()))) { + if (platformConf.getId().contentEquals(getPlatformID(deploymentFile.getName()))) { org.wso2.carbon.device.application.mgt.common.Platform platform = convert(platformConf); DataHolder.getInstance().getPlatformManager() .register(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platform); @@ -105,13 +105,16 @@ public class PlatformDeployer extends AbstractDeployer { platform.setDefaultTenantMapping(platformConfig.isTenantMapping()); platform.setEnabled(false); List properties = new ArrayList<>(); - for (Property propertyConfig : platformConfig.getProperties()) { - org.wso2.carbon.device.application.mgt.common.Platform.Property property = - new org.wso2.carbon.device.application.mgt.common.Platform.Property(); - property.setName(propertyConfig.getName()); - property.setDefaultValue(propertyConfig.getDefaultValue()); - property.setOptional(propertyConfig.isOptional()); - properties.add(property); + + if (platformConfig.getProperties() != null) { + for (Property propertyConfig : platformConfig.getProperties()) { + org.wso2.carbon.device.application.mgt.common.Platform.Property property = + new org.wso2.carbon.device.application.mgt.common.Platform.Property(); + property.setName(propertyConfig.getName()); + property.setDefaultValue(propertyConfig.getDefaultValue()); + property.setOptional(propertyConfig.isOptional()); + properties.add(property); + } } platform.setProperties(properties); return platform; diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql index 6f19489dec..06c22b4750 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql @@ -6,18 +6,37 @@ -- Table APPM_PLATFORM -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS APPM_PLATFORM ( - ID INT NOT NULL, - NAME VARCHAR(100) NOT NULL, - DESCRIPTION TEXT NULL, - IDENTIFIER VARCHAR(100) NOT NULL, - ICON_NAME VARCHAR(100) NULL, - DESCRIPTOR LONGTEXT NOT NULL, - PUBLISHED TINYINT NULL, - IS_SHARED BOOLEAN DEFAULT FALSE, - FILE_BASED BOOLEAN DEFAULT FALSE, - TENANT_ID INT, - PRIMARY KEY (ID), - UNIQUE INDEX IDENTIFIER_UNIQUE (IDENTIFIER ASC)); +ID INT NOT NULL AUTO_INCREMENT UNIQUE, +IDENTIFIER VARCHAR (100) NOT NULL, +TENANT_ID INT NOT NULL , +NAME VARCHAR (255), +FILE_BASED BOOLEAN, +DESCRIPTION LONGVARCHAR, +IS_SHARED BOOLEAN, +ICON_NAME VARCHAR (100), +PRIMARY KEY (IDENTIFIER, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS APPM_PLATFORM_PROPERTIES ( +ID INT NOT NULL AUTO_INCREMENT, +PLATFORM_ID VARCHAR (100) NOT NULL, +PROP_NAME VARCHAR (100) NOT NULL, +OPTIONAL BOOLEAN, +DEFAUL_VALUE VARCHAR (255), +TENANT_ID INT NOT NULL , +FOREIGN KEY(PLATFORM_ID, TENANT_ID) REFERENCES APPM_PLATFORM(IDENTIFIER, TENANT_ID) ON DELETE CASCADE, +PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS APPM_PLATFORM_TENANT_MAPPING ( +ID INT NOT NULL AUTO_INCREMENT, +TENANT_ID INT NOT NULL , +PLATFORM_ID VARCHAR (100) NOT NULL, +FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(IDENTIFIER) ON DELETE CASCADE, +PRIMARY KEY (ID, TENANT_ID, PLATFORM_ID) +); + +CREATE INDEX FK_PLATFROM_TENANT_MAPPING_PLATFORM ON APPM_PLATFORM_TENANT_MAPPING(PLATFORM_ID ASC); -- ----------------------------------------------------- -- Table APPM_APPLICATION_CATEGORY @@ -29,21 +48,6 @@ CREATE TABLE IF NOT EXISTS APPM_APPLICATION_CATEGORY ( PUBLISHED TINYINT NULL, PRIMARY KEY (ID)); --- ----------------------------------------------------- --- Table APPM_PLATFORM_TENANT_MAPPING --- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS APPM_PLATFORM_TENANT_MAPPING ( - ID INT NOT NULL AUTO_INCREMENT, - PLATFORM_ID INT NOT NULL, - TENANT_ID INT NOT NULL, - PRIMARY KEY (ID, PLATFORM_ID), - CONSTRAINT fk_APPM_PLATFORM_TENANT_MAPPING_APPM_SUPPORTED_PLATFORM1 - FOREIGN KEY (PLATFORM_ID) - REFERENCES APPM_PLATFORM (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION); - -CREATE INDEX FK_PLATFROM_TENANT_MAPPING_PLATFORM ON APPM_PLATFORM_TENANT_MAPPING(PLATFORM_ID ASC); -- ----------------------------------------------------- diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql index a316f290e6..3512f095c0 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql @@ -14,18 +14,28 @@ SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES'; -- ----------------------------------------------------- -- Table `APPM_PLATFORM` -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `APPM_PLATFORM` ( - `ID` INT NOT NULL, - `NAME` VARCHAR(100) NOT NULL, - `DESCRIPTION` TEXT NULL, - `IDENTIFIER` VARCHAR(100) NOT NULL, - `DESCRIPTOR` LONGTEXT NOT NULL, - IS_SHARED BOOLEAN DEFAULT FALSE, - FILE_BASED BOOLEAN DEFAULT FALSE, - PRIMARY KEY (`ID`), - UNIQUE INDEX `IDENTIFIER_UNIQUE` (`IDENTIFIER` ASC)) - ENGINE = InnoDB - COMMENT = 'This table contains the data related to the application platform'; +CREATE TABLE IF NOT EXISTS APPM_PLATFORM ( +ID INT NOT NULL AUTO_INCREMENT UNIQUE, +IDENTIFIER VARCHAR (100) NOT NULL, +TENANT_ID INT NOT NULL , +NAME VARCHAR (255), +FILE_BASED BOOLEAN, +DESCRIPTION VARCHAR(2048), +IS_SHARED BOOLEAN, +ICON_NAME VARCHAR (100), +PRIMARY KEY (IDENTIFIER, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS APPM_PLATFORM_PROPERTIES ( +ID INT NOT NULL AUTO_INCREMENT UNIQUE, +PLATFORM_ID VARCHAR (100) NOT NULL, +TENANT_ID INT NOT NULL , +PROP_NAME VARCHAR (100) NOT NULL, +OPTIONAL BOOLEAN, +DEFAUL_VALUE VARCHAR (255), +PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME, TENANT_ID), +FOREIGN KEY(PLATFORM_ID, TENANT_ID) REFERENCES APPM_PLATFORM(IDENTIFIER, TENANT_ID) ON DELETE CASCADE +); -- -----------------------------------------------------