All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.digidoc4j.impl.AbstractValidationResult Maven / Gradle / Ivy

Go to download

DigiDoc4j is a Java library for digitally signing documents and creating digital signature containers of signed documents

There is a newer version: 6.0.1
Show newest version
package org.digidoc4j.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.collections4.CollectionUtils;
import org.digidoc4j.Configuration;
import org.digidoc4j.ValidationResult;
import org.digidoc4j.exceptions.DigiDoc4JException;
import org.digidoc4j.utils.Helper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Janar Rahumeel (CGI Estonia)
 */
public abstract class AbstractValidationResult implements ValidationResult {

  private static final Logger LOGGER = LoggerFactory.getLogger(AbstractValidationResult.class);
  protected List errors = new ArrayList<>();
  protected List warnings = new ArrayList<>();

  @Override
  public boolean isValid() {
    return errors.isEmpty();
  }

  @Override
  public boolean hasWarnings() {
    return CollectionUtils.isNotEmpty(this.warnings);
  }

  /**
   * @param configuration configuration context
   */
  public void print(Configuration configuration) {
    if (configuration.getPrintValidationReport()) {
      boolean hasWarningsOnly = CollectionUtils.isNotEmpty(this.warnings) && this.isValid();
      if (hasWarningsOnly || CollectionUtils.isNotEmpty(this.errors)) {
        if (hasWarningsOnly) {
          Helper.printWarningSection(LOGGER, String.format("Start of <%s> validation result", this.getResultName
              ()));
        } else {
          Helper.printErrorSection(LOGGER, String.format("Start of <%s> validation result", this.getResultName()));
        }
        if (CollectionUtils.isNotEmpty(this.errors)) {
          for (DigiDoc4JException error : this.errors) {
            LOGGER.error(error.toString());
          }
        }
        if (CollectionUtils.isNotEmpty(this.warnings)) {
          for (DigiDoc4JException warning : this.warnings) {
            LOGGER.warn(warning.toString());
          }
        }
        if (hasWarningsOnly) {
          Helper.printWarningSection(LOGGER, String.format("End of <%s> validation result", this
              .getResultName()));
        } else {
          Helper.printErrorSection(LOGGER, String.format("End of <%s> validation result", this.getResultName()));
        }
      }
    }
  }

  /*
   * RESTRICTED METHODS
   */

  protected abstract String getResultName();

  /*
   * ACCESSORS
   */

  @Override
  public List getErrors() {
    return errors;
  }

  public void addErrors(List errors) {
    this.errors = concatenate(this.errors, errors);
  }

  public void setErrors(List errors) {
    this.errors = errors;
  }

  @Override
  public List getWarnings() {
    return warnings;
  }

  public void addWarnings(List warnings) {
    this.warnings = concatenate(this.warnings, warnings);
  }

  public void setWarnings(List warnings) {
    this.warnings = warnings;
  }

  protected static List concatenate(List first, List second) {
    return Stream.concat(
            Optional.ofNullable(first).map(List::stream).orElseGet(Stream::empty),
            Optional.ofNullable(second).map(List::stream).orElseGet(Stream::empty)
    ).collect(Collectors.toList());
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy