/**
   * This creates a new database connector for use. It establishes a database connection immediately
   * ready for use.
   */
  public ZabbixDirectDbDataSourceAdaptor() {
    HISTORY_TABLES.add("history");
    HISTORY_TABLES.add("history_str");
    HISTORY_TABLES.add("history_uint");
    HISTORY_TABLES.add("history_text");

    try {
      PropertiesConfiguration config;
      if (new File(CONFIG_FILE).exists()) {
        config = new PropertiesConfiguration(CONFIG_FILE);
      } else {
        config = new PropertiesConfiguration();
        config.setFile(new File(CONFIG_FILE));
      }
      config.setAutoSave(
          true); // This will save the configuration file back to disk. In case the defaults need
                 // setting.
      databaseURL = config.getString("iaas.energy.modeller.zabbix.db.url", databaseURL);
      config.setProperty("iaas.energy.modeller.zabbix.db.url", databaseURL);
      databaseDriver = config.getString("iaas.energy.modeller.zabbix.db.driver", databaseDriver);
      config.setProperty("iaas.energy.modeller.zabbix.db.driver", databaseDriver);
      databasePassword =
          config.getString("iaas.energy.modeller.zabbix.db.password", databasePassword);
      config.setProperty("iaas.energy.modeller.zabbix.db.password", databasePassword);
      databaseUser = config.getString("iaas.energy.modeller.zabbix.db.user", databaseUser);
      config.setProperty("iaas.energy.modeller.zabbix.db.user", databaseUser);
      begins = config.getString("iaas.energy.modeller.filter.begins", begins);
      config.setProperty("iaas.energy.modeller.filter.begins", begins);
      isHost = config.getBoolean("iaas.energy.modeller.filter.isHost", isHost);
      config.setProperty("iaas.energy.modeller.filter.isHost", isHost);

    } catch (ConfigurationException ex) {
      DB_LOGGER.log(Level.SEVERE, "Error loading the configuration of the IaaS energy modeller");
    }
    try {
      connection = getConnection();
    } catch (IOException | SQLException | ClassNotFoundException ex) {
      DB_LOGGER.log(Level.SEVERE, "Failed to establish the connection to the Zabbix DB", ex);
    }
  }
  static {
    try {
      solenoidValvesOfInterest.add(Double.valueOf(0.0));
      solenoidValvesOfInterest.add(Double.valueOf(1.0));
      solenoidValvesOfInterest.add(Double.valueOf(2.0));
      solenoidValvesOfInterest.add(Double.valueOf(4.0));
      solenoidValvesOfInterest.add(Double.valueOf(8.0));
      solenoidValvesOfInterest.add(Double.valueOf(16.0));

      allSolenoidValves.add(Double.valueOf(0.0));
      allSolenoidValves.add(Double.valueOf(1.0));
      allSolenoidValves.add(Double.valueOf(2.0));
      allSolenoidValves.add(Double.valueOf(4.0));
      allSolenoidValves.add(Double.valueOf(8.0));
      allSolenoidValves.add(Double.valueOf(16.0));

      configuration = new PropertiesConfiguration(FileConfiguration.getConfigurationFile());
      configuration.setAutoSave(true);

      if (!JarUtil.isRunningOnWindows7()) {
        inputFolder = new File(configuration.getString(FileConfiguration.INPUT_FOLDER, "input"));
        outputFolder = new File(configuration.getString(FileConfiguration.OUTPUT_FOLDER, "output"));
        temperatureInputFolder =
            new File(configuration.getString(FileConfiguration.TEMPERATURE_FOLDER, "input/temp"));
      } else {
        File userDir = JarUtil.getPlantEvaluationUserHomeDir();
        inputFolder =
            new File(
                configuration.getString(
                    FileConfiguration.INPUT_FOLDER, userDir.getAbsolutePath() + "/input"));
        outputFolder =
            new File(
                configuration.getString(
                    FileConfiguration.OUTPUT_FOLDER, userDir.getAbsolutePath() + "/output"));
        temperatureInputFolder =
            new File(
                configuration.getString(
                    FileConfiguration.TEMPERATURE_FOLDER,
                    userDir.getAbsolutePath() + "/input/temp"));
      }

      if (!inputFolder.exists()) inputFolder.mkdir();
      if (!outputFolder.exists()) outputFolder.mkdir();
      if (!temperatureInputFolder.exists()) temperatureInputFolder.mkdir();

      TypeAEvaluationOptions.shiftByOneHour =
          configuration.getBoolean(TypeAEvaluationOptions.OPTION_SHIFT_BY_ONE_HOUR, false);
      TypeAEvaluationOptions.recordReferenceValve =
          configuration.getBoolean(TypeAEvaluationOptions.OPTIONS_RECORD_REFERENCE_VALVE, false);
      TypeAEvaluationOptions.sampleRate =
          configuration.getDouble(TypeAEvaluationOptions.OPTIONS_SAMPLE_RATE, 10.0);

      solenoidValvesOfInterest =
          configuration.getList(
              Options.OPTIONS_SOLENOID_VALVES_OF_INTEREST, solenoidValvesOfInterest);

      checkSolenoidValves();

      // options for co2 absolute only evaluation
      CO2AbsoluteOnlyEvaluationOptions.isAutoScaleCO2Absolute =
          configuration.getBoolean(
              CO2AbsoluteOnlyEvaluationOptions.OPTIONS_CO2_ABSOLUTE_IS_CO2_ABSOLUTE_AUTOSCALE,
              true);
      CO2AbsoluteOnlyEvaluationOptions.isAutoScaleDeltaFiveMinutes =
          configuration.getBoolean(
              CO2AbsoluteOnlyEvaluationOptions.OPTIONS_CO2_ABSOLUTE_IS_DELTA_FIVE_MINUTES_AUTOSCALE,
              true);
      CO2AbsoluteOnlyEvaluationOptions.co2AbsoluteDatasetMinimum =
          configuration.getDouble(
              CO2AbsoluteOnlyEvaluationOptions.OPTIONS_CO2_ABSOLUTE_SCALE_MINIMUM_CO2_ABSOLUTE,
              Integer.MIN_VALUE);
      CO2AbsoluteOnlyEvaluationOptions.co2AbsoluteDatasetMaximum =
          configuration.getDouble(
              CO2AbsoluteOnlyEvaluationOptions.OPTIONS_CO2_ABSOLUTE_SCALE_MAXIMUM_CO2_ABSOLUTE,
              Integer.MAX_VALUE);
      CO2AbsoluteOnlyEvaluationOptions.deltaFiveMinutesMinimum =
          configuration.getDouble(
              CO2AbsoluteOnlyEvaluationOptions
                  .OPTIONS_CO2_ABSOLUTE_SCALE_MINIMUM_DELTA_FIVE_MINUTES,
              Integer.MIN_VALUE);
      CO2AbsoluteOnlyEvaluationOptions.deltaFiveMinutesMaximum =
          configuration.getDouble(
              CO2AbsoluteOnlyEvaluationOptions
                  .OPTIONS_CO2_ABSOLUTE_SCALE_MAXIMUM_DELTA_FIVE_MINUTES,
              Integer.MAX_VALUE);

      // options for type B (= Ingo's evaluation)
      TypeBEvaluationOptions.density =
          configuration.getInt(TypeBEvaluationOptions.OPTIONS_TYPE_B_DENSITY, 60);
      TypeBEvaluationOptions.isCo2AbsoluteAutoscale =
          configuration.getBoolean(
              TypeBEvaluationOptions.OPTIONS_TYPE_B_IS_CO2_ABSOLUTE_AUTOSCALE, true);
      TypeBEvaluationOptions.isDeltaRawAutoscale =
          configuration.getBoolean(
              TypeBEvaluationOptions.OPTIONS_TYPE_B_IS_DELTA_RAW_AUTOSCALE, true);
      TypeBEvaluationOptions.co2AbsoluteDatasetMinimum =
          configuration.getDouble(
              TypeBEvaluationOptions.OPTIONS_TYPE_B_CO2_ABSOLUTE_SCALE_MINIMUM, Integer.MIN_VALUE);
      TypeBEvaluationOptions.co2AbsoluteDatasetMaximum =
          configuration.getDouble(
              TypeBEvaluationOptions.OPTIONS_TYPE_B_CO2_ABSOLUTE_SCALE_MAXIMUM, Integer.MAX_VALUE);
      TypeBEvaluationOptions.deltaRawDatasetMinimum =
          configuration.getDouble(
              TypeBEvaluationOptions.OPTIONS_TYPE_B_DELTA_RAW_SCALE_MINIMUM, Integer.MIN_VALUE);
      TypeBEvaluationOptions.deltaRawDatasetMaximum =
          configuration.getDouble(
              TypeBEvaluationOptions.OPTIONS_TYPE_B_DELTA_RAW_SCALE_MAXIMUM, Integer.MAX_VALUE);
    } catch (ConfigurationException ce) {
      log.error("Could not update configuration.", ce);
    }
  }