/*
   * Continuously updates the database with the riskValue set by
   * setRiskValue()
   *
   * (non-Javadoc)
   *
   * @see java.lang.Thread#run()
   */
  public void run() {

    if (!servicePhase.equals(SERVICEPHASE_OPERATION)) {
      LOGGER.error(
          "RiskPropagator: Service phase is not operation, will not continiously update database. Revise you intialisation of this class.");
      return;
    }

    executing = true;
    LOGGER.debug("RiskPropagator: Continuous update mode actived");

    try {
      while (executing) {
        propagateRiskValue();
        Thread.sleep(THREAD_SLEEP_SHORT);
      }
      LOGGER.debug("RiskPropagator: Kill received");
    } catch (InterruptedException e) {
      LOGGER.error("RiskPropagator: Thread interrupted", e);
    }

    // Clean up the database connection what ever happens
    cleanUp();

    LOGGER.debug("RiskPropagator: Continious update mode deactivated");
  }
  /** Commits the current riskValue to the database, deployment only */
  public void addRiskValue() {
    if (!servicePhase.equals(SERVICEPHASE_DEPLOYMENT)) {
      LOGGER.error(
          "RiskPropagator: Service phase is not deployment, does not make sense to add single value to database in operation phase. Revise you intialisation of this class");
      return;
    }

    LOGGER.debug("RiskPropagator: addRiskValue() called for servicePhase: deployment");

    // Propagate the single risk value to the database
    propagateRiskValue();

    // Clean up the database connection
    cleanUp();
  }
  /**
   * Generic initialisation code used in both constructors
   *
   * @param providerType the type of provider, valid arguments are "sp", "ip"
   * @param servicePhase the phase in which a service is running, valid arguments are "deployment",
   *     "operation"
   * @param providerId the human readable providerId or name, e.g. "Atos"
   * @param serviceId the not so human readable serviceId hash value, e.g.
   *     "76c44bda-4f5a-4f97-806d-011d174bea44"
   * @param graphType the graphType the data will be rendered to in the UI
   * @return true on success, false on an error
   */
  private boolean initialise(
      String providerType,
      String servicePhase,
      String providerId,
      String serviceId,
      int graphType) {

    // Validate input parameters
    if (!validateConstructorInput(providerType, servicePhase, providerId, serviceId, graphType)) {
      // There was an error so we return
      return false;
    }

    // Select an appropriate table given the input parameters
    if (!tableSelect(graphType)) {
      // There was an error so we return
      return false;
    }

    // Initialise the connection to the database
    try {
      LOGGER.debug("RiskPropagator: Connecting to database");
      Driver myDriver = new com.mysql.jdbc.Driver();
      DriverManager.registerDriver(myDriver);
      conn = DriverManager.getConnection(url, username, password);
      stmt = conn.createStatement();
      LOGGER.debug("RiskPropagator: Connected to database");
    } catch (SQLException e) {
      LOGGER.error("RiskPropagator: Database connection failed", e);
      cleanUp();
      return false;
    }

    // Resolve the human readable providerId to that stored in the database
    // or add new ID's to the Database if they do not exist
    if (!resolveProviderId(providerId)) {
      // There was an error so we return
      return false;
    }
    if (!resolveServiceId(serviceId)) {
      // There was an error so we return
      return false;
    }

    return true;
  }