From ae80bd8ba8a1ad94de736596becd9077fad25721 Mon Sep 17 00:00:00 2001 From: amalhub Date: Mon, 11 Sep 2017 15:01:36 +0530 Subject: [PATCH] App-mgt store mapping validation --- .../GenericSubscriptionDAOImpl.java | 34 +++++++++++++------ .../dbscripts/cdm/application-mgt/mysql.sql | 5 +-- 2 files changed, 27 insertions(+), 12 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/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index b00b0499af..b2d2b520d1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -30,26 +30,40 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc private static Log log = LogFactory.getLog(GenericSubscriptionDAOImpl.class); @Override - public int addDeviceApplicationMapping(String deviceIdentifier, String applicationUUID, boolean installed) throws ApplicationManagementException { + public int addDeviceApplicationMapping(String deviceIdentifier, String applicationUUID, boolean installed) throws + ApplicationManagementException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; int mappingId = -1; try { conn = this.getDBConnection(); - String sql = "INSERT INTO APPM_DEVICE_APPLICATION_MAPPING (DEVICE_IDENTIFIER, APPLICATION_UUID, " + - "INSTALLED) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); + String sql = "SELECT ID FROM APPM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_IDENTIFIER = ? AND " + + "APPLICATION_UUID = ?"; + stmt = conn.prepareStatement(sql); stmt.setString(1, deviceIdentifier); stmt.setString(2, applicationUUID); - stmt.setBoolean(3, installed); - stmt.executeUpdate(); + rs = stmt.executeQuery(); - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - mappingId = rs.getInt(1); + if (!rs.next()) { + sql = "INSERT INTO APPM_DEVICE_APPLICATION_MAPPING (DEVICE_IDENTIFIER, APPLICATION_UUID, " + + "INSTALLED) VALUES (?, ?, ?)"; + stmt = conn.prepareStatement(sql, new String[]{"id"}); + stmt.setString(1, deviceIdentifier); + stmt.setString(2, applicationUUID); + stmt.setBoolean(3, installed); + stmt.executeUpdate(); + + rs = stmt.getGeneratedKeys(); + if (rs.next()) { + mappingId = rs.getInt(1); + } + return mappingId; + } else { + log.warn("Device[" + deviceIdentifier + "] application[" + applicationUUID + "] mapping already " + + "exists in the DB"); + return -1; } - return mappingId; } catch (SQLException e) { throw new ApplicationManagementException("Error occurred while adding device application mapping to DB", e); } finally { 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 1d840f1e2a..a5f5bbd5a5 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 @@ -400,8 +400,9 @@ CREATE TABLE IF NOT EXISTS `APPM_DEVICE_APPLICATION_MAPPING` ( `INSTALLED` BOOLEAN NOT NULL, `SENT_AT` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (ID), - CONSTRAINT fk_appm_application FOREIGN KEY (`APPLICATION_UUID`) REFERENCES - APPM_APPLICATION (`UUID`) ON DELETE NO ACTION ON UPDATE NO ACTION + CONSTRAINT `fk_appm_application` FOREIGN KEY (`APPLICATION_UUID`) REFERENCES + APPM_APPLICATION (`UUID`) ON DELETE NO ACTION ON UPDATE NO ACTION, + UNIQUE KEY `device_app_mapping` (`DEVICE_IDENTIFIER`, `APPLICATION_UUID`) ) ENGINE = InnoDB; SET SQL_MODE=@OLD_SQL_MODE;