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

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

package cdm.event.common.functions;

import cdm.base.staticdata.identifier.IdentifiedList;
import cdm.base.staticdata.party.AncillaryParty;
import cdm.base.staticdata.party.Counterparty;
import cdm.event.common.BusinessEvent;
import cdm.event.common.ExecutionDetails;
import cdm.event.common.Instruction;
import cdm.event.common.PrimitiveInstruction;
import cdm.event.common.Trade;
import cdm.event.common.TradeState;
import cdm.event.common.metafields.ReferenceWithMetaTradeState;
import cdm.product.common.NotionalAdjustmentEnum;
import cdm.product.template.NonTransferableProduct;
import cdm.product.template.TradeLot;
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.MapperC;
import com.rosetta.model.lib.mapper.MapperS;
import javax.inject.Inject;

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

@ImplementedBy(Qualify_PairOff.Qualify_PairOffDefault.class)
public abstract class Qualify_PairOff implements RosettaFunction,IQualifyFunctionExtension {
	
	// RosettaFunction dependencies
	//
	@Inject protected FilterOpenTradeStates filterOpenTradeStates;
	@Inject protected NewTradeInstructionOnlyExists newTradeInstructionOnlyExists;

	/**
	* @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 MapperC openTradeState(BusinessEvent businessEvent);

	protected abstract MapperC newTradeInstruction(BusinessEvent businessEvent);

	protected abstract MapperC packageRef(BusinessEvent businessEvent);

	public static class Qualify_PairOffDefault extends Qualify_PairOff {
		@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(newTradeInstruction(businessEvent).resultCount()), MapperS.of(MapperS.of(businessEvent).mapC("getInstruction", _businessEvent -> _businessEvent.getInstruction()).resultCount()), CardinalityOperator.All).and(areEqual(openTradeState(businessEvent).map("getTrade", tradeState -> tradeState.getTrade()).map("getProduct", trade -> trade.getProduct()), newTradeInstruction(businessEvent).map("getBefore", instruction -> instruction.getBefore()).map("Type coercion", referenceWithMetaTradeState -> referenceWithMetaTradeState.getValue()).map("getTrade", tradeState -> tradeState.getTrade()).map("getProduct", trade -> trade.getProduct()), CardinalityOperator.All)).and(areEqual(openTradeState(businessEvent).map("getTrade", tradeState -> tradeState.getTrade()).mapC("getTradeLot", trade -> trade.getTradeLot()), newTradeInstruction(businessEvent).map("getBefore", instruction -> instruction.getBefore()).map("Type coercion", referenceWithMetaTradeState -> referenceWithMetaTradeState.getValue()).map("getTrade", tradeState -> tradeState.getTrade()).mapC("getTradeLot", trade -> trade.getTradeLot()), CardinalityOperator.All)).and(areEqual(openTradeState(businessEvent).map("getTrade", tradeState -> tradeState.getTrade()).mapC("getCounterparty", trade -> trade.getCounterparty()), newTradeInstruction(businessEvent).map("getBefore", instruction -> instruction.getBefore()).map("Type coercion", referenceWithMetaTradeState -> referenceWithMetaTradeState.getValue()).map("getTrade", tradeState -> tradeState.getTrade()).mapC("getCounterparty", trade -> trade.getCounterparty()), CardinalityOperator.All)).and(areEqual(openTradeState(businessEvent).map("getTrade", tradeState -> tradeState.getTrade()).mapC("getAncillaryParty", trade -> trade.getAncillaryParty()), newTradeInstruction(businessEvent).map("getBefore", instruction -> instruction.getBefore()).map("Type coercion", referenceWithMetaTradeState -> referenceWithMetaTradeState.getValue()).map("getTrade", tradeState -> tradeState.getTrade()).mapC("getAncillaryParty", trade -> trade.getAncillaryParty()), CardinalityOperator.All)).and(areEqual(openTradeState(businessEvent).map("getTrade", tradeState -> tradeState.getTrade()).map("getAdjustment", trade -> trade.getAdjustment()), newTradeInstruction(businessEvent).map("getBefore", instruction -> instruction.getBefore()).map("Type coercion", referenceWithMetaTradeState -> referenceWithMetaTradeState.getValue()).map("getTrade", tradeState -> tradeState.getTrade()).map("getAdjustment", trade -> trade.getAdjustment()), CardinalityOperator.All)).and(areEqual(MapperS.of(packageRef(businessEvent).resultCount()), MapperS.of(openTradeState(businessEvent).resultCount()), CardinalityOperator.All)).and(areEqual(MapperS.of(distinct(packageRef(businessEvent)).resultCount()), MapperS.of(1), CardinalityOperator.All)).get();
			
			return is_event;
		}
		
		@Override
		protected MapperC openTradeState(BusinessEvent businessEvent) {
			return MapperC.of(filterOpenTradeStates.evaluate(MapperS.of(businessEvent).mapC("getAfter", _businessEvent -> _businessEvent.getAfter()).getMulti()));
		}
		
		@Override
		protected MapperC newTradeInstruction(BusinessEvent businessEvent) {
			return MapperS.of(businessEvent).mapC("getInstruction", _businessEvent -> _businessEvent.getInstruction())
				.filterItemNullSafe(item -> newTradeInstructionOnlyExists.evaluate(item.map("getPrimitiveInstruction", instruction -> instruction.getPrimitiveInstruction()).get()));
		}
		
		@Override
		protected MapperC packageRef(BusinessEvent businessEvent) {
			return openTradeState(businessEvent).map("getTrade", tradeState -> tradeState.getTrade()).map("getExecutionDetails", trade -> trade.getExecutionDetails()).map("getPackageReference", executionDetails -> executionDetails.getPackageReference());
		}
	}
		
		@Override
		public String getNamePrefix() {
			return "Qualify";
		}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy