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

cdm.event.common.functions.Qualify_Cancellation Maven / Gradle / Ivy

There is a newer version: 6.0.0-dev.89
Show newest version
package cdm.event.common.functions;

import cdm.base.datetime.AdjustableDate;
import cdm.base.datetime.AdjustableOrRelativeDate;
import cdm.event.common.BusinessEvent;
import cdm.event.common.Instruction;
import cdm.event.common.Trade;
import cdm.event.common.TradeState;
import cdm.event.common.metafields.ReferenceWithMetaTradeState;
import cdm.product.template.ContractualProduct;
import cdm.product.template.EconomicTerms;
import cdm.product.template.Product;
import cdm.product.template.TradableProduct;
import com.google.inject.ImplementedBy;
import com.rosetta.model.lib.expression.CardinalityOperator;
import com.rosetta.model.lib.functions.IQualifyFunctionExtension;
import com.rosetta.model.lib.functions.RosettaFunction;
import com.rosetta.model.lib.mapper.Mapper;
import com.rosetta.model.lib.mapper.MapperC;
import com.rosetta.model.lib.mapper.MapperS;
import com.rosetta.model.lib.mapper.MapperUtils;
import com.rosetta.model.lib.records.Date;
import javax.inject.Inject;

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

@ImplementedBy(Qualify_Cancellation.Qualify_CancellationDefault.class)
public abstract class Qualify_Cancellation implements RosettaFunction,IQualifyFunctionExtension {
	
	// RosettaFunction dependencies
	//
	@Inject protected FilterClosedTradeStates filterClosedTradeStates;
	@Inject protected FilterOpenTradeStates filterOpenTradeStates;

	/**
	* @param businessEvent 
	* @return is_event 
	*/
	@Override
	public Boolean evaluate(BusinessEvent businessEvent) {
		Boolean is_event = doEvaluate(businessEvent);
		
		return is_event;
	}

	protected abstract Boolean doEvaluate(BusinessEvent businessEvent);

	protected abstract Mapper closedEconomicTerms(BusinessEvent businessEvent);

	protected abstract Mapper openEconomicTerms(BusinessEvent businessEvent);

	public static class Qualify_CancellationDefault extends Qualify_Cancellation {
		@Override
		protected Boolean doEvaluate(BusinessEvent businessEvent) {
			Boolean is_event = null;
			return assignOutput(is_event, businessEvent);
		}
		
		protected Boolean assignOutput(Boolean is_event, BusinessEvent businessEvent) {
			is_event = areEqual(MapperS.of(MapperS.of(businessEvent).mapC("getInstruction", eventInstruction -> eventInstruction.getInstruction()).map("getBefore", instruction -> instruction.getBefore()).map("getValue", _f->_f.getValue()).resultCount()), MapperS.of(Integer.valueOf(1)), CardinalityOperator.All).and(exists(MapperS.of(closedEconomicTerms(businessEvent).get()))).and(exists(MapperS.of(openEconomicTerms(businessEvent).get()))).and(MapperUtils.toComparisonResult(MapperUtils.runSingle(() -> {
				if (exists(MapperS.of(openEconomicTerms(businessEvent).get()).map("getTerminationDate", economicTerms -> economicTerms.getTerminationDate())).and(exists(MapperS.of(closedEconomicTerms(businessEvent).get()).map("getTerminationDate", economicTerms -> economicTerms.getTerminationDate()))).getOrDefault(false)) {
					return lessThan(MapperS.of(openEconomicTerms(businessEvent).get()).map("getTerminationDate", economicTerms -> economicTerms.getTerminationDate()).map("getAdjustableDate", adjustableOrRelativeDate -> adjustableOrRelativeDate.getAdjustableDate()).map("getUnadjustedDate", adjustableDate -> adjustableDate.getUnadjustedDate()), MapperS.of(closedEconomicTerms(businessEvent).get()).map("getTerminationDate", economicTerms -> economicTerms.getTerminationDate()).map("getAdjustableDate", adjustableOrRelativeDate -> adjustableOrRelativeDate.getAdjustableDate()).map("getUnadjustedDate", adjustableDate -> adjustableDate.getUnadjustedDate()), CardinalityOperator.All);
				}
				else {
					return exists(MapperS.of(openEconomicTerms(businessEvent).get()).map("getTerminationDate", economicTerms -> economicTerms.getTerminationDate()));
				}
			}))).get();
			
			return is_event;
		}
		
		@Override
		protected Mapper closedEconomicTerms(BusinessEvent businessEvent) {
			return MapperS.of(MapperC.of(filterClosedTradeStates.evaluate(MapperS.of(businessEvent).mapC("getAfter", _businessEvent -> _businessEvent.getAfter()).getMulti())).get()).map("getTrade", tradeState -> tradeState.getTrade()).map("getTradableProduct", trade -> trade.getTradableProduct()).map("getProduct", tradableProduct -> tradableProduct.getProduct()).map("getContractualProduct", product -> product.getContractualProduct()).map("getEconomicTerms", contractualProduct -> contractualProduct.getEconomicTerms());
		}
		
		@Override
		protected Mapper openEconomicTerms(BusinessEvent businessEvent) {
			return MapperS.of(MapperC.of(filterOpenTradeStates.evaluate(MapperS.of(businessEvent).mapC("getAfter", _businessEvent -> _businessEvent.getAfter()).getMulti())).get()).map("getTrade", tradeState -> tradeState.getTrade()).map("getTradableProduct", trade -> trade.getTradableProduct()).map("getProduct", tradableProduct -> tradableProduct.getProduct()).map("getContractualProduct", product -> product.getContractualProduct()).map("getEconomicTerms", contractualProduct -> contractualProduct.getEconomicTerms());
		}
	}
		
		@Override
		public String getNamePrefix() {
			return "Qualify";
		}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy