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

org.isda.cdm.validation.ChildValidator 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.Child;
import org.isda.cdm.Flat;
import org.isda.cdm.Middle;

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 ChildValidator implements Validator {

	private List getComparisonResults(Child 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), 
				checkCardinality("childFieldOne", (Flat) o.getChildFieldOne() != null ? 1 : 0, 1, 1), 
				checkCardinality("childFieldTwo", (List) o.getChildFieldTwo() == null ? 0 : ((List) o.getChildFieldTwo()).size(), 1, 0), 
				checkCardinality("childFieldThree", (String) o.getChildFieldThree() != null ? 1 : 0, 1, 1), 
				checkCardinality("childFieldFour", (List) o.getChildFieldFour() == null ? 0 : ((List) o.getChildFieldFour()).size(), 1, 0)
			);
	}

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

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

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy