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

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

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

import cdm.base.math.ArithmeticOperationEnum;
import cdm.base.math.FinancialUnitEnum;
import cdm.base.math.NonNegativeQuantity;
import cdm.base.math.NonNegativeQuantitySchedule;
import cdm.base.math.Quantity;
import cdm.base.math.QuantitySchedule;
import cdm.base.math.UnitType;
import cdm.base.math.functions.FilterQuantityByFinancialUnit;
import cdm.base.math.metafields.FieldWithMetaNonNegativeQuantitySchedule;
import cdm.base.staticdata.asset.common.Asset;
import cdm.base.staticdata.asset.common.AssetIdentifier;
import cdm.base.staticdata.asset.common.util.AssetDeepPathUtil;
import cdm.base.staticdata.party.Counterparty;
import cdm.base.staticdata.party.CounterpartyRoleEnum;
import cdm.base.staticdata.party.Party;
import cdm.base.staticdata.party.PayerReceiver;
import cdm.base.staticdata.party.functions.ExtractCounterpartyByRole;
import cdm.base.staticdata.party.metafields.ReferenceWithMetaParty;
import cdm.event.common.CalculateTransferInstruction;
import cdm.event.common.Trade;
import cdm.event.common.TradeState;
import cdm.event.common.Transfer;
import cdm.event.common.Transfer.TransferBuilder;
import cdm.observable.asset.PriceQuantity;
import cdm.observable.asset.PriceSchedule;
import cdm.observable.asset.PriceTypeEnum;
import cdm.observable.asset.functions.FilterPrice;
import cdm.observable.asset.metafields.FieldWithMetaPriceSchedule;
import cdm.product.template.AssetPayout;
import cdm.product.template.EconomicTerms;
import cdm.product.template.NonTransferableProduct;
import cdm.product.template.Payout;
import cdm.product.template.TradeLot;
import cdm.product.template.metafields.ReferenceWithMetaAssetPayout;
import com.google.inject.ImplementedBy;
import com.rosetta.model.lib.expression.CardinalityOperator;
import com.rosetta.model.lib.expression.ComparisonResult;
import com.rosetta.model.lib.functions.ConditionValidator;
import com.rosetta.model.lib.functions.ModelObjectValidator;
import com.rosetta.model.lib.functions.RosettaFunction;
import com.rosetta.model.lib.mapper.MapperC;
import com.rosetta.model.lib.mapper.MapperS;
import com.rosetta.model.lib.records.Date;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Optional;
import javax.inject.Inject;

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

@ImplementedBy(Create_AssetTransfer.Create_AssetTransferDefault.class)
public abstract class Create_AssetTransfer implements RosettaFunction {
	
	@Inject protected ConditionValidator conditionValidator;
	
	@Inject protected ModelObjectValidator objectValidator;
	
	// RosettaFunction dependencies
	//
	@Inject protected AssetDeepPathUtil assetDeepPathUtil;
	@Inject protected ExtractCounterpartyByRole extractCounterpartyByRole;
	@Inject protected FilterPrice filterPrice;
	@Inject protected FilterQuantityByFinancialUnit filterQuantityByFinancialUnit;

	/**
	* @param instruction 
	* @return transfer 
	*/
	public Transfer evaluate(CalculateTransferInstruction instruction) {
		// pre-conditions
		conditionValidator.validate(() -> {
			if (exists(MapperS.of(instruction).map("getQuantity", calculateTransferInstruction -> calculateTransferInstruction.getQuantity())).getOrDefault(false)) {
				return areEqual(MapperS.of(instruction).map("getQuantity", calculateTransferInstruction -> calculateTransferInstruction.getQuantity()).map("getUnit", quantity -> quantity.getUnit()).map("getFinancialUnit", unitType -> unitType.getFinancialUnit()), MapperS.of(FinancialUnitEnum.SHARE), CardinalityOperator.All);
			}
			return ComparisonResult.successEmptyOperand("");
		},
			"");
		
		Transfer.TransferBuilder transferBuilder = doEvaluate(instruction);
		
		final Transfer transfer;
		if (transferBuilder == null) {
			transfer = null;
		} else {
			transfer = transferBuilder.build();
			objectValidator.validate(Transfer.class, transfer);
		}
		
		return transfer;
	}

	protected abstract Transfer.TransferBuilder doEvaluate(CalculateTransferInstruction instruction);

	protected abstract MapperS assetPayout(CalculateTransferInstruction instruction);

	protected abstract MapperS tradeQuantity(CalculateTransferInstruction instruction);

	protected abstract MapperS securityQuantity(CalculateTransferInstruction instruction);

	protected abstract MapperS securityPrice(CalculateTransferInstruction instruction);

	public static class Create_AssetTransferDefault extends Create_AssetTransfer {
		@Override
		protected Transfer.TransferBuilder doEvaluate(CalculateTransferInstruction instruction) {
			Transfer.TransferBuilder transfer = Transfer.builder();
			return assignOutput(transfer, instruction);
		}
		
		protected Transfer.TransferBuilder assignOutput(Transfer.TransferBuilder transfer, CalculateTransferInstruction instruction) {
			transfer
				.setQuantity(NonNegativeQuantity.builder()
					.setValue(securityQuantity(instruction).map("getValue", quantity -> quantity.getValue()).get())
					.setUnit(securityQuantity(instruction).map("getUnit", quantity -> quantity.getUnit()).get())
					.build());
			
			transfer
				.getOrCreateAsset()
				.getOrCreateInstrument()
				.getOrCreateSecurity()
				.addIdentifier(assetPayout(instruction).map("getUnderlier", _assetPayout -> _assetPayout.getUnderlier()).mapC("chooseIdentifier", asset -> assetDeepPathUtil.chooseIdentifier(asset)).getMulti());
			
			final Party ifThenElseResult0;
			if (exists(MapperS.of(instruction).map("getPayerReceiver", calculateTransferInstruction -> calculateTransferInstruction.getPayerReceiver()).map("getPayer", payerReceiver -> payerReceiver.getPayer())).getOrDefault(false)) {
				ifThenElseResult0 = MapperS.of(extractCounterpartyByRole.evaluate(MapperS.of(instruction).map("getTradeState", calculateTransferInstruction -> calculateTransferInstruction.getTradeState()).map("getTrade", tradeState -> tradeState.getTrade()).mapC("getCounterparty", trade -> trade.getCounterparty()).getMulti(), MapperS.of(instruction).map("getPayerReceiver", calculateTransferInstruction -> calculateTransferInstruction.getPayerReceiver()).map("getPayer", payerReceiver -> payerReceiver.getPayer()).get())).map("getPartyReference", counterparty -> counterparty.getPartyReference()).map("getValue", _f->_f.getValue()).get();
			} else if (exists(assetPayout(instruction).map("getPayerReceiver", _assetPayout -> _assetPayout.getPayerReceiver()).map("getPayer", payerReceiver -> payerReceiver.getPayer())).getOrDefault(false)) {
				ifThenElseResult0 = MapperS.of(extractCounterpartyByRole.evaluate(MapperS.of(instruction).map("getTradeState", calculateTransferInstruction -> calculateTransferInstruction.getTradeState()).map("getTrade", tradeState -> tradeState.getTrade()).mapC("getCounterparty", trade -> trade.getCounterparty()).getMulti(), assetPayout(instruction).map("getPayerReceiver", _assetPayout -> _assetPayout.getPayerReceiver()).map("getPayer", payerReceiver -> payerReceiver.getPayer()).get())).map("getPartyReference", counterparty -> counterparty.getPartyReference()).map("getValue", _f->_f.getValue()).get();
			} else {
				ifThenElseResult0 = null;
			}
			transfer
				.getOrCreatePayerReceiver()
				.setPayerPartyReferenceValue(ifThenElseResult0);
			
			final Party ifThenElseResult1;
			if (exists(MapperS.of(instruction).map("getPayerReceiver", calculateTransferInstruction -> calculateTransferInstruction.getPayerReceiver()).map("getPayer", payerReceiver -> payerReceiver.getPayer())).getOrDefault(false)) {
				ifThenElseResult1 = MapperS.of(extractCounterpartyByRole.evaluate(MapperS.of(instruction).map("getTradeState", calculateTransferInstruction -> calculateTransferInstruction.getTradeState()).map("getTrade", tradeState -> tradeState.getTrade()).mapC("getCounterparty", trade -> trade.getCounterparty()).getMulti(), MapperS.of(instruction).map("getPayerReceiver", calculateTransferInstruction -> calculateTransferInstruction.getPayerReceiver()).map("getReceiver", payerReceiver -> payerReceiver.getReceiver()).get())).map("getPartyReference", counterparty -> counterparty.getPartyReference()).map("getValue", _f->_f.getValue()).get();
			} else if (exists(assetPayout(instruction).map("getPayerReceiver", _assetPayout -> _assetPayout.getPayerReceiver()).map("getReceiver", payerReceiver -> payerReceiver.getReceiver())).getOrDefault(false)) {
				ifThenElseResult1 = MapperS.of(extractCounterpartyByRole.evaluate(MapperS.of(instruction).map("getTradeState", calculateTransferInstruction -> calculateTransferInstruction.getTradeState()).map("getTrade", tradeState -> tradeState.getTrade()).mapC("getCounterparty", trade -> trade.getCounterparty()).getMulti(), assetPayout(instruction).map("getPayerReceiver", _assetPayout -> _assetPayout.getPayerReceiver()).map("getReceiver", payerReceiver -> payerReceiver.getReceiver()).get())).map("getPartyReference", counterparty -> counterparty.getPartyReference()).map("getValue", _f->_f.getValue()).get();
			} else {
				ifThenElseResult1 = null;
			}
			transfer
				.getOrCreatePayerReceiver()
				.setReceiverPartyReferenceValue(ifThenElseResult1);
			
			transfer
				.getOrCreateSettlementDate()
				.setAdjustedDateValue(MapperS.of(instruction).map("getDate", calculateTransferInstruction -> calculateTransferInstruction.getDate()).get());
			
			AssetPayout transferSettlementOriginAssetPayout = null;
			if (exists(MapperS.of(instruction).map("getTradeState", calculateTransferInstruction -> calculateTransferInstruction.getTradeState()).map("getTrade", tradeState -> tradeState.getTrade()).map("getProduct", trade -> trade.getProduct()).map("getEconomicTerms", nonTransferableProduct -> nonTransferableProduct.getEconomicTerms()).map("getPayout", economicTerms -> economicTerms.getPayout()).mapC("getAssetPayout", payout -> payout.getAssetPayout())).getOrDefault(false)) {
				transferSettlementOriginAssetPayout = MapperS.of(instruction).map("getTradeState", calculateTransferInstruction -> calculateTransferInstruction.getTradeState()).map("getTrade", tradeState -> tradeState.getTrade()).map("getProduct", trade -> trade.getProduct()).map("getEconomicTerms", nonTransferableProduct -> nonTransferableProduct.getEconomicTerms()).map("getPayout", economicTerms -> economicTerms.getPayout()).mapC("getAssetPayout", payout -> payout.getAssetPayout()).get();
			}
			transfer
				.getOrCreateSettlementOrigin()
				.setAssetPayout(ReferenceWithMetaAssetPayout.builder()
					.setGlobalReference(Optional.ofNullable(transferSettlementOriginAssetPayout)
						.map(r -> r.getMeta())
						.map(m -> m.getGlobalKey())
						.orElse(null))
					.setExternalReference(Optional.ofNullable(transferSettlementOriginAssetPayout)
						.map(r -> r.getMeta())
						.map(m -> m.getExternalKey())
						.orElse(null))
					.build()
				);
			
			return Optional.ofNullable(transfer)
				.map(o -> o.prune())
				.orElse(null);
		}
		
		@Override
		protected MapperS assetPayout(CalculateTransferInstruction instruction) {
			return MapperS.of(MapperS.of(instruction).map("getTradeState", calculateTransferInstruction -> calculateTransferInstruction.getTradeState()).map("getTrade", tradeState -> tradeState.getTrade()).map("getProduct", trade -> trade.getProduct()).map("getEconomicTerms", nonTransferableProduct -> nonTransferableProduct.getEconomicTerms()).map("getPayout", economicTerms -> economicTerms.getPayout()).mapC("getAssetPayout", payout -> payout.getAssetPayout()).get());
		}
		
		@Override
		protected MapperS tradeQuantity(CalculateTransferInstruction instruction) {
			return MapperS.of(MapperC.of(filterQuantityByFinancialUnit.evaluate(MapperS.of(instruction).map("getTradeState", calculateTransferInstruction -> calculateTransferInstruction.getTradeState()).map("getTrade", tradeState -> tradeState.getTrade()).mapC("getTradeLot", trade -> trade.getTradeLot()).mapC("getPriceQuantity", tradeLot -> tradeLot.getPriceQuantity()).mapC("getQuantity", priceQuantity -> priceQuantity.getQuantity()).map("getValue", _f->_f.getValue()).getMulti(), FinancialUnitEnum.SHARE)).get());
		}
		
		@Override
		protected MapperS securityQuantity(CalculateTransferInstruction instruction) {
			if (exists(MapperS.of(instruction).map("getQuantity", calculateTransferInstruction -> calculateTransferInstruction.getQuantity())).getOrDefault(false)) {
				return MapperS.of(instruction).map("getQuantity", calculateTransferInstruction -> calculateTransferInstruction.getQuantity());
			}
			return MapperS.of(NonNegativeQuantity.builder()
				.setValue(tradeQuantity(instruction).map("getValue", quantitySchedule -> quantitySchedule.getValue()).get())
				.setUnit(tradeQuantity(instruction).map("getUnit", quantitySchedule -> quantitySchedule.getUnit()).get())
				.build());
		}
		
		@Override
		protected MapperS securityPrice(CalculateTransferInstruction instruction) {
			return MapperS.of(filterPrice.evaluate(MapperS.of(instruction).map("getTradeState", calculateTransferInstruction -> calculateTransferInstruction.getTradeState()).map("getTrade", tradeState -> tradeState.getTrade()).mapC("getTradeLot", trade -> trade.getTradeLot()).mapC("getPriceQuantity", tradeLot -> tradeLot.getPriceQuantity()).mapC("getPrice", priceQuantity -> priceQuantity.getPrice()).map("getValue", _f->_f.getValue()).getMulti(), PriceTypeEnum.ASSET_PRICE, Collections.emptyList(), null));
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy