From c6b12f799f7bfd11ccc585150d60f33c1be6ffed Mon Sep 17 00:00:00 2001 From: Megala Date: Thu, 29 Sep 2016 01:42:11 +0530 Subject: [PATCH] Adding the check on the existence of the database before creating one --- .../plugin/impl/util/AndroidSenseUtils.java | 9 +++++++-- .../arduino/plugin/impl/util/ArduinoUtils.java | 9 +++++++-- .../plugin/impl/util/RaspberrypiUtils.java | 9 +++++++-- .../plugin/impl/util/VirtualFireAlarmUtils.java | 9 +++++++-- .../dao/util/MobileDeviceManagementDAOUtil.java | 17 +++++++++++++++-- 5 files changed, 43 insertions(+), 10 deletions(-) diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/plugin/impl/util/AndroidSenseUtils.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/plugin/impl/util/AndroidSenseUtils.java index 830f375707..e8851df8a1 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/plugin/impl/util/AndroidSenseUtils.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/plugin/impl/util/AndroidSenseUtils.java @@ -92,8 +92,13 @@ public class AndroidSenseUtils { Context ctx = new InitialContext(); DataSource dataSource = (DataSource) ctx.lookup(datasource); DeviceSchemaInitializer initializer = new DeviceSchemaInitializer(dataSource); - log.info("Initializing device management repository database schema"); - initializer.createRegistryDatabase(); + String checkSql = "select * from ANDROID_SENSE_DEVICE"; + if (!initializer.isDatabaseStructureCreated(checkSql)) { + log.info("Initializing device management repository database schema"); + initializer.createRegistryDatabase(); + } else { + log.info("Device management repository database already exists. Not creating a new database."); + } } catch (NamingException e) { log.error("Error while looking up the data source: " + datasource, e); diff --git a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/plugin/impl/util/ArduinoUtils.java b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/plugin/impl/util/ArduinoUtils.java index 91a17cf213..f4207d72e0 100644 --- a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/plugin/impl/util/ArduinoUtils.java +++ b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/plugin/impl/util/ArduinoUtils.java @@ -105,8 +105,13 @@ public class ArduinoUtils { Context ctx = new InitialContext(); DataSource dataSource = (DataSource) ctx.lookup(datasource); DeviceSchemaInitializer initializer = new DeviceSchemaInitializer(dataSource); - log.info("Initializing device management repository database schema"); - initializer.createRegistryDatabase(); + String checkSql = "select * from ARDUINO_DEVICE"; + if (!initializer.isDatabaseStructureCreated(checkSql)) { + log.info("Initializing device management repository database schema"); + initializer.createRegistryDatabase(); + } else { + log.info("Device management repository database already exists. Not creating a new database."); + } } catch (NamingException e) { log.error("Error while looking up the data source: " + datasource, e); diff --git a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/plugin/impl/util/RaspberrypiUtils.java b/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/plugin/impl/util/RaspberrypiUtils.java index e2ce1f44a1..8bd1b6e2e9 100644 --- a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/plugin/impl/util/RaspberrypiUtils.java +++ b/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/plugin/impl/util/RaspberrypiUtils.java @@ -94,8 +94,13 @@ public class RaspberrypiUtils { Context ctx = new InitialContext(); DataSource dataSource = (DataSource) ctx.lookup(datasource); DeviceSchemaInitializer initializer = new DeviceSchemaInitializer(dataSource); - log.info("Initializing device management repository database schema"); - initializer.createRegistryDatabase(); + String checkSql = "select * from RASPBERRYPI_DEVICE"; + if (!initializer.isDatabaseStructureCreated(checkSql)) { + log.info("Initializing device management repository database schema"); + initializer.createRegistryDatabase(); + } else { + log.info("Device management repository database already exists. Not creating a new database."); + } } catch (NamingException e) { log.error("Error while looking up the data source: " + datasource, e); } catch (Exception e) { diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/impl/util/VirtualFireAlarmUtils.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/impl/util/VirtualFireAlarmUtils.java index 7377967350..66d5d7747c 100644 --- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/impl/util/VirtualFireAlarmUtils.java +++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/impl/util/VirtualFireAlarmUtils.java @@ -99,8 +99,13 @@ public class VirtualFireAlarmUtils { Context ctx = new InitialContext(); DataSource dataSource = (DataSource) ctx.lookup(datasourceName); DeviceSchemaInitializer initializer = new DeviceSchemaInitializer(dataSource); - log.info("Initializing device management repository database schema"); - initializer.createRegistryDatabase(); + String checkSql = "select * from VIRTUAL_FIREALARM_DEVICE"; + if (!initializer.isDatabaseStructureCreated(checkSql)) { + log.info("Initializing device management repository database schema"); + initializer.createRegistryDatabase(); + } else { + log.info("Device management repository database already exists. Not creating a new database."); + } } catch (NamingException e) { log.error("Error while looking up the data source: " + datasourceName, e); } catch (Exception e) { diff --git a/components/mobile-plugins/mobile-base-plugin/org.wso2.carbon.device.mgt.mobile/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/util/MobileDeviceManagementDAOUtil.java b/components/mobile-plugins/mobile-base-plugin/org.wso2.carbon.device.mgt.mobile/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/util/MobileDeviceManagementDAOUtil.java index 0f72e82e99..bf1c005c18 100644 --- a/components/mobile-plugins/mobile-base-plugin/org.wso2.carbon.device.mgt.mobile/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/util/MobileDeviceManagementDAOUtil.java +++ b/components/mobile-plugins/mobile-base-plugin/org.wso2.carbon.device.mgt.mobile/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/util/MobileDeviceManagementDAOUtil.java @@ -93,9 +93,22 @@ public class MobileDeviceManagementDAOUtil { public static void setupMobileDeviceManagementSchema(DataSource dataSource, String pluginType) throws MobileDeviceMgtPluginException { MobileDeviceManagementSchemaInitializer initializer = new MobileDeviceManagementSchemaInitializer(dataSource, pluginType); - log.info("Initializing mobile device management repository database schema for : " + pluginType); + String checkSql = "select * from "; + if (pluginType.equalsIgnoreCase("android")) { + checkSql += "AD_DEVICE"; + } else if (pluginType.equalsIgnoreCase("windows")) { + checkSql += "WIN_DEVICE"; + } else { + checkSql += "IOS_DEVICE"; + } try { - initializer.createRegistryDatabase(); + if (!initializer.isDatabaseStructureCreated(checkSql)) { + log.info("Initializing mobile device management repository database schema for : " + pluginType); + initializer.createRegistryDatabase(); + } else { + log.info("Mobile device management repository database for " + pluginType + + " already exists. Not creating a new database."); + } } catch (Exception e) { throw new MobileDeviceMgtPluginException("Error occurred while initializing Mobile Device " + "Management database schema", e);