class IcapScanFile {

  public static void finalizeOnError(final IcapScanFile icapScanFile)
      throws IOException {
    if (icapScanFile == null) {
      throw new IllegalArgumentException(ARGUMENTS_CANNOT_BE_EMPTY_OR_NULL);
    }
    logger.error("Scan is incorrect: {}",
                 icapScanFile.getResult() == null? "No Result" :
                     icapScanFile.getResult());
    if (icapScanFile.deleteOnError) {
      final File file = new File(icapScanFile.filepath);
      if (!file.delete()) {
        logger.error("File cannot be deleted!");
        throw new IOException("File cannot be deleted!");
      } else {
        logger.warn("File is deleted");
      }
    } else if (icapScanFile.pathMoveError != null) {
      final File file = new File(icapScanFile.filepath);
      final File dir = new File(icapScanFile.pathMoveError);
      if (dir.exists()) {
        if (dir.isDirectory()) {
          try {
            final File to = new File(dir, file.getName());
            FileUtils.copy(file, to, true, false);
            logger.warn("File is moved to " + to.getAbsolutePath());
          } catch (final Reply550Exception e) {
            logger.error("Cannot move to directory", e);
            throw new IOException("Cannot move to directory", e);
          }
        } else {
          logger.error("Move path already exists and is not a directory");
          throw new IOException(
              "Move path already exists and is not a directory");
        }
      } else {
        if (dir.getParentFile().isDirectory()) {
          try {
            FileUtils.copy(file, dir, true, false);
            logger.warn("File is moved to " + dir.getAbsolutePath());
          } catch (final Reply550Exception e) {
            logger.error("Cannot move to file", e);
            logger.error("Cannot move to file", e);
          }
        } else {
          logger.error("Move path is not a directory or existing sub-path");
          throw new IOException(
              "Move path is not a directory or existing sub-path");
        }
      }
    }
  }

  private  IcapScanFile();

}