<commit_before>/**
 * 
 */
package org.openforis.collect.model.validation;

import static org.openforis.collect.model.FieldSymbol.CONFIRMED;

import java.util.List;

import org.openforis.collect.manager.RecordManager;
import org.openforis.collect.model.CollectRecord;
import org.openforis.collect.model.FieldSymbol;
import org.openforis.collect.model.CollectRecord.Step;
import org.openforis.idm.metamodel.KeyAttributeDefinition;
import org.openforis.idm.metamodel.validation.ValidationResult;
import org.openforis.idm.metamodel.validation.ValidationResultFlag;
import org.openforis.idm.metamodel.validation.ValidationResults;
import org.openforis.idm.metamodel.validation.Validator;
import org.openforis.idm.model.Attribute;
import org.openforis.idm.model.Field;
import org.openforis.idm.model.Record;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author <NAME>
 * 
 */
public class CollectValidator extends Validator {

	@Autowired
	private RecordManager recordManager;

	@Override
	public ValidationResults validate(Attribute<?, ?> attribute) {
		ValidationResults results = new ValidationResults();

		SpecifiedValidator specifiedValidator = new SpecifiedValidator();
		ValidationResultFlag specifiedResultFlag = specifiedValidator.evaluate(attribute);
		results.addResult(specifiedValidator, specifiedResultFlag);

		if (!specifiedResultFlag.isError()) {
			if (isRootEntityKey(attribute)) {
				validateRootEntityKey(attribute, results);
			}

			CollectRecord record = (CollectRecord) attribute.getRecord();
			Step step = record.getStep();

			ValidationResults idmResults = internalValidate(attribute);

			boolean confirmed = (step == Step.ENTRY) ? isValueConfirmed(attribute) : false;
			List<ValidationResult> errors = idmResults.getErrors();
			for (ValidationResult error : errors) {
				ValidationResultFlag newFlag = confirmed ? ValidationResultFlag.WARNING : ValidationResultFlag.ERROR;
				results.addResult(error.getValidator(), newFlag);
			}
			results.addResults(idmResults.getWarnings());
		}
		return results;

	}

	private ValidationResults internalValidate(Attribute<?, ?> attribute) {
		ValidationResults results = new ValidationResults();
		if (!(attribute.isEmpty() || isReasonBlankSpecified(attribute))) {
			validateAttributeValue(attribute, results);
			if (!results.hasErrors()) {
				validateAttributeChecks(attribute, results);
			}
		}
		return results;
	}

	private void validateRootEntityKey(Attribute<?, ?> attribute, ValidationResults results) {
		RecordKeyUniquenessValidator keyValidator = new RecordKeyUniquenessValidator(recordManager);
		ValidationResultFlag res = keyValidator.evaluate(attribute);
		if (res == ValidationResultFlag.ERROR) {
			results.addResult(keyValidator, ValidationResultFlag.ERROR);
		}
	}

	private boolean isRootEntityKey(Attribute<?, ?> attribute) {
		Record record = attribute.getRecord();
		return attribute.getDefinition() instanceof KeyAttributeDefinition && record.getRootEntity().equals(attribute.getParent());
	}

	static boolean isValueConfirmed(Attribute<?, ?> attribute) {
		int fieldCount = attribute.getFieldCount();
		for (int i = 0; i < fieldCount; i++) {
			Field<?> field = attribute.getField(i);
			Character symbol = field.getSymbol();

			if (!CONFIRMED.getSymbol().equals(symbol)) {
				return false;
			}
		}
		return true;<commit_msg>OFC-206 Complete logic in CollectValidator.validate() OFC-205 Override MinCountValidator in Collect OFC-103 Collect custom validation logic in CollectValidator<commit_after>/**
 * 
 */
package org.openforis.collect.model.validation;

import static org.openforis.collect.model.FieldSymbol.CONFIRMED;

import java.util.List;

import org.openforis.collect.manager.RecordManager;
import org.openforis.collect.model.CollectRecord;
import org.openforis.collect.model.FieldSymbol;
import org.openforis.collect.model.CollectRecord.Step;
import org.openforis.idm.metamodel.KeyAttributeDefinition;
import org.openforis.idm.metamodel.validation.ValidationResult;
import org.openforis.idm.metamodel.validation.ValidationResultFlag;
import org.openforis.idm.metamodel.validation.ValidationResults;
import org.openforis.idm.metamodel.validation.Validator;
import org.openforis.idm.model.Attribute;
import org.openforis.idm.model.Field;
import org.openforis.idm.model.Record;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author <NAME>
 * 
 */
public class CollectValidator extends Validator {

	@Autowired
	private RecordManager recordManager;

	@Override
	public ValidationResults validate(Attribute<?, ?> attribute) {
		ValidationResults results = new ValidationResults();

		// check if attribute has been specified
		SpecifiedValidator specifiedValidator = new SpecifiedValidator();
		ValidationResultFlag specifiedResultFlag = specifiedValidator.evaluate(attribute);
		results.addResult(specifiedValidator, specifiedResultFlag);

		if ( !specifiedResultFlag.isError() ) {
			CollectRecord record = (CollectRecord) attribute.getRecord();
			Step step = record.getStep();

			// validate root entity keys
			if ( isRootEntityKey(attribute) ) {
				validateRootEntityKey(attribute, results);
			}

			/*
			 * if the attribute is not empty and 'reason blank' has not been specified than validate it
			 */
			if (!(attribute.isEmpty() || isReasonBlankSpecified(attribute))) {
				validateAttributeValue(attribute, results);
				if (!results.hasErrors()) {
					validateAttributeChecks(attribute, results);
				}
			}

			if (step == Step.ENTRY) {
				results = adjustErrorsForEntryPhase(results, attribute);
			}
		}
		return results;
	}

	private ValidationResults adjustErrorsForEntryPhase(ValidationResults results, Attribute<?, ?> attribute) {
		boolean confirmed = isValueConfirmed(attribute);
		
		ValidationResults phaseEntryResults = new ValidationResults();
		List<ValidationResult> errors = results.getErrors();
		for (ValidationResult error : errors) {
			ValidationResultFlag newFlag = confirmed ? ValidationResultFlag.WARNING : ValidationResultFlag.ERROR;
			phaseEntryResults.addResult(error.getValidator(), newFlag);
		}
		
		phaseEntryResults.addResults(results.getWarnings());
		return phaseEntryResults;
	}

	private void validateRootEntityKey(Attribute<?, ?> attribute, ValidationResults results) {
		RecordKeyUniquenessValidator keyValidator = new RecordKeyUniquenessValidator(recordManager);
		ValidationResultFlag res = keyValidator.evaluate(attribute);
		if (res == ValidationResultFlag.ERROR) {
			results.addResult(keyValidator, ValidationResultFlag.ERROR);
		}
	}

	private boolean isRootEntityKey(Attribute<?, ?> attribute) {
		Record record = attribute.getRecord();
		return attribute.getDefinition() instanceof KeyAttributeDefinition && record.getRootEntity().equals(attribute.getParent());
	}

	static boolean isValueConfirmed(Attribute<?, ?> attribute) {
		int fieldCount = attribute.getFieldCount();
		for (int i = 0; i < fieldCount; i++) {
			Field<?> field = attribute.getField(i);
			Character symbol = field.getSymbol();

			if (!CONFIRMED.getSymbol().equals(symbol)) {
				return false;
			}
		}
		return true;
{'fraction_non_alphanumeric': 0.10612876019117233, 'fraction_numerical': 0.0015462468372223785, 'mean_word_length': 5.594068582020389, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1895228', 'n_tokens_mistral': 2155, 'n_tokens_neox': 1925, 'n_words': 397}