/**
  * Updates this import's status in the database.
  *
  * @param importStatus The status to update to.
  */
 private void updateImportStatus(ImportStatus importStatus) {
   try {
     getDao().updateImportStatus(dbDTO.getId(), importStatus);
   } catch (DAOException e) {
     LOGGER.error("Failed to update import status of database " + dbDTO.getName(), e);
   }
 }
  /**
   * The thread's execution body called by {@link #run()}.
   *
   * @param importLogger Logger that should be used by this database creation (i.e. import) thread.
   * @throws DAOException Thrown if database access error happens.
   * @throws ImportException If any other import error happens.
   */
  private void execute(Logger importLogger) throws DAOException, ImportException {

    updateImportStatus(ImportStatus.STARTED);

    // Create the database.
    DAOFactory.get().getDao(StagingDatabaseDAO.class).createDatabase(dbDTO.getName());

    // Populate the database from the given file.
    // TODO Use a factory mechanism to obtain a particular ImporterIF implementation.
    ImporterIF importer = new MSAccessImporter(importLogger);
    importer.doImport(dbFile, dbDTO.getName());
  }
  /**
   * Creates import logger for the given database-
   *
   * @param dbDTO The given database, as DTO.
   * @return The created import logger.
   */
  private ImportLogger createLogger(StagingDatabaseDTO dbDTO) {

    String loggerName = dbDTO.getName() + "_" + ImportLogger.class.getSimpleName();
    ImportLogger logger = (ImportLogger) Logger.getLogger(loggerName, ImportLoggerFactory.INSTANCE);
    logger.setDbDTO(dbDTO);
    logger.setLevel(Level.TRACE);
    return logger;
  }
  /*
   * (non-Javadoc)
   *
   * @see java.lang.Thread#run()
   */
  @Override
  public void run() {

    LogUtil.debug(
        "Import for database " + dbDTO.getName() + " started from file " + dbFile.getName(),
        importLogger,
        LOGGER);

    try {
      execute(importLogger);
      updateImportStatus(ImportStatus.COMPLETED);
      LogUtil.debug(
          "Import for database " + dbDTO.getName() + " finished from file " + dbFile.getName(),
          importLogger,
          LOGGER);
    } catch (Exception e) {
      updateImportStatus(ImportStatus.ERROR);
      LogUtil.error("Staging database creation failed with error", e, importLogger, LOGGER);
    }
  }
  /**
   * Convenience method that creates an instance of {@link ImportRunner} for the given database from
   * given file, and then starts it.
   *
   * @param dbDTO Will be passed into the private constructor.
   * @param dbFile Will be passed into the private constructor
   * @return The created and started thread.
   */
  public static synchronized ImportRunner start(StagingDatabaseDTO dbDTO, File dbFile) {

    if (dbDTO == null || dbFile == null || !dbFile.exists() || !dbFile.isFile()) {
      throw new IllegalArgumentException("The database DTO and file must be given!");
    }

    String dbName = dbDTO.getName();
    ImportRunner currentRun = CURRENT_RUNS.get(dbName);
    if (currentRun != null) {
      if (currentRun.isAlive()) {
        throw new IllegalStateException(
            "A creator is already running for this staging database: " + dbName);
      } else {
        CURRENT_RUNS.remove(dbName);
      }
    }

    ImportRunner creator = new ImportRunner(dbDTO, dbFile);
    creator.start();
    CURRENT_RUNS.put(dbName, creator);
    return creator;
  }