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

cdm.base.staticdata.asset.common.validation.datarule.CommodityOrdinalExists Maven / Gradle / Ivy

There is a newer version: 6.0.0-dev.89
Show newest version
package cdm.base.staticdata.asset.common.validation.datarule;

import cdm.base.staticdata.asset.common.Commodity;
import cdm.base.staticdata.asset.common.Taxonomy;
import cdm.base.staticdata.asset.common.TaxonomyClassification;
import cdm.base.staticdata.asset.common.TaxonomyValue;
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 static com.rosetta.model.lib.expression.ExpressionOperators.*;

/**
 * @version 6.0.0-dev.82
 */
@RosettaDataRule("CommodityOrdinalExists")
@ImplementedBy(CommodityOrdinalExists.Default.class)
public interface CommodityOrdinalExists extends Validator {
	
	String NAME = "CommodityOrdinalExists";
	String DEFINITION = "if taxonomy -> value -> classification count > 1 then taxonomy -> value -> classification -> ordinal exists";
	
	ValidationResult validate(RosettaPath path, Commodity commodity);
	
	class Default implements CommodityOrdinalExists {
	
		@Override
		public ValidationResult validate(RosettaPath path, Commodity commodity) {
			ComparisonResult result = executeDataRule(commodity);
			if (result.get()) {
				return ValidationResult.success(NAME, ValidationResult.ValidationType.DATA_RULE, "Commodity", path, DEFINITION);
			}
			
			String failureMessage = result.getError();
			if (failureMessage == null || failureMessage.contains("Null") || failureMessage == "") {
				failureMessage = "Condition has failed.";
			}
			return ValidationResult.failure(NAME, ValidationType.DATA_RULE, "Commodity", path, DEFINITION, failureMessage);
		}
		
		private ComparisonResult executeDataRule(Commodity commodity) {
			try {
				if (greaterThan(MapperS.of(MapperS.of(commodity).mapC("getTaxonomy", _commodity -> _commodity.getTaxonomy()).map("getValue", taxonomy -> taxonomy.getValue()).mapC("getClassification", taxonomyValue -> taxonomyValue.getClassification()).resultCount()), MapperS.of(1), CardinalityOperator.All).getOrDefault(false)) {
					return exists(MapperS.of(commodity).mapC("getTaxonomy", _commodity -> _commodity.getTaxonomy()).map("getValue", taxonomy -> taxonomy.getValue()).mapC("getClassification", taxonomyValue -> taxonomyValue.getClassification()).map("getOrdinal", taxonomyClassification -> taxonomyClassification.getOrdinal()));
				}
				return ComparisonResult.successEmptyOperand("");
			}
			catch (Exception ex) {
				return ComparisonResult.failure(ex.getMessage());
			}
		}
	}
	
	@SuppressWarnings("unused")
	class NoOp implements CommodityOrdinalExists {
	
		@Override
		public ValidationResult validate(RosettaPath path, Commodity commodity) {
			return ValidationResult.success(NAME, ValidationResult.ValidationType.DATA_RULE, "Commodity", path, DEFINITION);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy