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

org.isda.cdm.validation.datarule.StringRepForChildClass Maven / Gradle / Ivy

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

import com.google.inject.ImplementedBy;
import com.rosetta.model.lib.annotations.RosettaDataRule;
import com.rosetta.model.lib.expression.CardinalityOperator;
import com.rosetta.model.lib.expression.ComparisonResult;
import com.rosetta.model.lib.mapper.MapperS;
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 org.isda.cdm.StringRep;

import static com.rosetta.model.lib.expression.ExpressionOperators.*;

/**
 * @version test
 */
@RosettaDataRule("StringRepForChildClass")
@ImplementedBy(StringRepForChildClass.Default.class)
public interface StringRepForChildClass extends Validator {
	
	String NAME = "StringRepForChildClass";
	String DEFINITION = "val <> ''";
	
	ValidationResult validate(RosettaPath path, StringRep stringRep);
	
	class Default implements StringRepForChildClass {
	
		@Override
		public ValidationResult validate(RosettaPath path, StringRep stringRep) {
			ComparisonResult result = executeDataRule(stringRep);
			if (result.get()) {
				return ValidationResult.success(NAME, ValidationResult.ValidationType.DATA_RULE, "StringRep", path, DEFINITION);
			}
			
			String failureMessage = result.getError();
			if (failureMessage == null || failureMessage.contains("Null") || failureMessage == "") {
				failureMessage = "Condition has failed.";
			}
			return ValidationResult.failure(NAME, ValidationType.DATA_RULE, "StringRep", path, DEFINITION, failureMessage);
		}
		
		private ComparisonResult executeDataRule(StringRep stringRep) {
			try {
				return notEqual(MapperS.of(stringRep).map("getVal", _stringRep -> _stringRep.getVal()), MapperS.of(""), CardinalityOperator.Any);
			}
			catch (Exception ex) {
				return ComparisonResult.failure(ex.getMessage());
			}
		}
	}
	
	@SuppressWarnings("unused")
	class NoOp implements StringRepForChildClass {
	
		@Override
		public ValidationResult validate(RosettaPath path, StringRep stringRep) {
			return ValidationResult.success(NAME, ValidationResult.ValidationType.DATA_RULE, "StringRep", path, DEFINITION);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy