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

com.supwisdom.spreadsheet.mapper.validation.DefaultSheetValidationJob Maven / Gradle / Ivy

There is a newer version: 1.0.9
Show newest version
package com.supwisdom.spreadsheet.mapper.validation;

import com.supwisdom.spreadsheet.mapper.model.core.Row;
import com.supwisdom.spreadsheet.mapper.model.core.Sheet;
import com.supwisdom.spreadsheet.mapper.model.meta.FieldMeta;
import com.supwisdom.spreadsheet.mapper.model.meta.SheetMeta;
import com.supwisdom.spreadsheet.mapper.model.msg.Message;
import com.supwisdom.spreadsheet.mapper.model.msg.MessageBean;
import com.supwisdom.spreadsheet.mapper.m2f.excel.ExcelMessageWriteStrategies;
import com.supwisdom.spreadsheet.mapper.validation.engine.CellGroupValidationEngine;
import com.supwisdom.spreadsheet.mapper.validation.validator.Dependant;
import com.supwisdom.spreadsheet.mapper.validation.validator.cell.CellValidator;
import com.supwisdom.spreadsheet.mapper.validation.validator.row.RowValidator;
import com.supwisdom.spreadsheet.mapper.validation.validator.sheet.SheetValidator;
import com.supwisdom.spreadsheet.mapper.validation.validator.unioncell.UnionCellValidator;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by hanwen on 15-12-16.
 */
public class DefaultSheetValidationJob implements SheetValidationJob {

  private List sheetValidators = new ArrayList<>();

  private List rowValidators = new ArrayList<>();

  private List cellValidators = new ArrayList<>();

  private List errorMessages = new ArrayList<>();

  private transient CellGroupValidationEngine cellGroupValidationEngine;

  @Override
  public DefaultSheetValidationJob addValidator(SheetValidator sheetValidator) {
    if (sheetValidator == null) {
      throw new IllegalArgumentException("sheet validator can not be null");
    }
    sheetValidators.add(sheetValidator);
    return this;
  }

  @Override
  public DefaultSheetValidationJob addValidator(RowValidator rowValidator) {
    if (rowValidator == null) {
      throw new IllegalArgumentException("row validator can not be null");
    }
    rowValidators.add(rowValidator);
    return this;
  }

  @Override
  public DefaultSheetValidationJob addValidator(CellValidator cellValidator) {

    if (cellValidator == null) {
      throw new IllegalArgumentException("Null cellValidator");
    }

    String group = cellValidator.getGroup();
    if (StringUtils.isBlank(group)) {
      throw new WorkbookValidateException("CellValidator[" + cellValidator.getClass().getName() + "]'s group is null");
    }

    this.cellValidators.add(cellValidator);
    return this;

  }

  @Override
  public DefaultSheetValidationJob addValidator(UnionCellValidator unionCellValidator) {

    if (unionCellValidator == null) {
      throw new IllegalArgumentException("Null unionCellValidator");
    }

    String group = unionCellValidator.getGroup();
    if (StringUtils.isBlank(group)) {
      throw new WorkbookValidateException(
          "UnionCellValidator[" + unionCellValidator.getClass().getName() + "]'s group is null");
    }

    this.cellValidators.add(unionCellValidator);
    return this;

  }

  /**
   * 
    *
  • {@link SheetValidator}校验失败的消息是{@link ExcelMessageWriteStrategies#TEXT_BOX}
  • *
  • {@link RowValidator}校验失败的消息是{@link ExcelMessageWriteStrategies#COMMENT},在每个{@link RowValidator#getErrorFields()}上
  • *
  • {@link CellValidator}校验失败的消息是{@link ExcelMessageWriteStrategies#COMMENT},在失败的field上
  • *
  • {@link UnionCellValidator}校验失败的消息是{@link ExcelMessageWriteStrategies#COMMENT},在失败的field上
  • *
* */ @Override public List getErrorMessages() { return errorMessages; } @Override public boolean validate(Sheet sheet, SheetMeta sheetMeta) { assertNoDuplicatedFieldMeta(sheetMeta); if (!executeSheetValidators(sheet, sheetMeta)) { return false; } cellGroupValidationEngine = new CellGroupValidationEngine(sheetMeta, cellValidators); cellGroupValidationEngine.initialize(); boolean result = true; for (Row row : sheet.getRows()) { if (!executeRowValidators(row, sheetMeta)) { result = false; continue; } if (row.getIndex() >= sheetMeta.getDataStartRowIndex()) { if (!executeCellValidators(row, sheetMeta)) { result = false; } } } return result; } /** * 检查不存在相同name的FieldMeta * * @param sheetMeta */ private void assertNoDuplicatedFieldMeta(SheetMeta sheetMeta) { List fieldMetas = sheetMeta.getFieldMetas(); Set fieldNames = new HashSet<>(fieldMetas.size()); for (FieldMeta fieldMeta : fieldMetas) { if (!fieldNames.add(fieldMeta.getName())) { throw new WorkbookValidateException("SheetMeta contains duplicate FieldMeta [" + fieldMeta.getName() + "]"); } } } private boolean executeSheetValidators(Sheet sheet, SheetMeta sheetMeta) { boolean result = true; for (SheetValidator validator : sheetValidators) { if (!validator.validate(sheet, sheetMeta)) { result = false; String errorMessage = validator.getErrorMessage(); if (StringUtils.isNotBlank(errorMessage)) { errorMessages.add(new MessageBean(ExcelMessageWriteStrategies.TEXT_BOX, errorMessage, sheet.getIndex())); } } } return result; } private boolean executeRowValidators(Row row, SheetMeta sheetMeta) { boolean result = true; for (RowValidator validator : rowValidators) { if (!validator.validate(row, sheetMeta)) { result = false; String errorMessage = validator.getErrorMessage(); Set errorFields = validator.getErrorFields(); if (StringUtils.isBlank(errorMessage) || CollectionUtils.isEmpty(errorFields)) { continue; } for (String messageOnField : errorFields) { FieldMeta fieldMeta = sheetMeta.getUniqueFieldMeta(messageOnField); errorMessages.add( new MessageBean(ExcelMessageWriteStrategies.COMMENT, StringUtils.defaultIfBlank(errorMessage, "Invalid"), row.getSheet().getIndex(), row.getIndex(), fieldMeta.getColumnIndex())); } } } return result; } private boolean executeCellValidators(Row row, SheetMeta sheetMeta) { boolean result = cellGroupValidationEngine.validate(row, sheetMeta); errorMessages.addAll(cellGroupValidationEngine.getErrorMessages()); cellGroupValidationEngine.clearResults(); return result; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy