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

org.isda.cdm.validation.ParentValidator Maven / Gradle / Ivy

There is a newer version: 11.25.1
Show newest version
package org.isda.cdm.validation;

import com.google.common.collect.Lists;
import com.rosetta.model.lib.expression.ComparisonResult;
import com.rosetta.model.lib.path.RosettaPath;
import com.rosetta.model.lib.validation.ValidationResult;
import com.rosetta.model.lib.validation.ValidationResult.ValidationType;
import com.rosetta.model.lib.validation.Validator;
import com.rosetta.model.metafields.ReferenceWithMetaString;
import java.util.List;
import org.isda.cdm.AnotherMiddle;
import org.isda.cdm.Flat;
import org.isda.cdm.Middle;
import org.isda.cdm.Parent;

import static com.google.common.base.Strings.isNullOrEmpty;
import static com.rosetta.model.lib.expression.ExpressionOperators.checkCardinality;
import static com.rosetta.model.lib.validation.ValidationResult.failure;
import static com.rosetta.model.lib.validation.ValidationResult.success;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

public class ParentValidator implements Validator {

	private List getComparisonResults(Parent o) {
		return Lists.newArrayList(
				checkCardinality("parentFieldOne", (Flat) o.getParentFieldOne() != null ? 1 : 0, 1, 1), 
				checkCardinality("parentFieldTwo", (List) o.getParentFieldTwo() == null ? 0 : ((List) o.getParentFieldTwo()).size(), 1, 0), 
				checkCardinality("parentFieldThree", (String) o.getParentFieldThree() != null ? 1 : 0, 1, 1), 
				checkCardinality("parentFieldFour", (List) o.getParentFieldFour() == null ? 0 : ((List) o.getParentFieldFour()).size(), 1, 0), 
				checkCardinality("parentFieldFiveReference", (ReferenceWithMetaString) o.getParentFieldFiveReference() != null ? 1 : 0, 1, 1), 
				checkCardinality("parentFieldSix", (Flat) o.getParentFieldSix() != null ? 1 : 0, 1, 1), 
				checkCardinality("repeatedField", (List) o.getRepeatedField() == null ? 0 : ((List) o.getRepeatedField()).size(), 1, 0), 
				checkCardinality("parentFieldSeven", (Middle) o.getParentFieldSeven() != null ? 1 : 0, 1, 1), 
				checkCardinality("parentFieldEight", (AnotherMiddle) o.getParentFieldEight() != null ? 1 : 0, 1, 1)
			);
	}

	@Override
	public ValidationResult validate(RosettaPath path, Parent o) {
		String error = getComparisonResults(o)
			.stream()
			.filter(res -> !res.get())
			.map(res -> res.getError())
			.collect(joining("; "));

		if (!isNullOrEmpty(error)) {
			return failure("Parent", ValidationType.CARDINALITY, "Parent", path, "", error);
		}
		return success("Parent", ValidationType.CARDINALITY, "Parent", path, "");
	}

	@Override
	public List> getValidationResults(RosettaPath path, Parent o) {
		return getComparisonResults(o)
			.stream()
			.map(res -> {
				if (!isNullOrEmpty(res.getError())) {
					return failure("Parent", ValidationType.CARDINALITY, "Parent", path, "", res.getError());
				}
				return success("Parent", ValidationType.CARDINALITY, "Parent", path, "");
			})
			.collect(toList());
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy