module MxML where

import Text.XML.Light
import Control.Monad ((>=>))
import XML

-- common fields

_genericIdentifier, _genericBaseIdentifier, _genericVersion :: Element -> [String]
_genericIdentifier		= gP "businessObjectId/identifier"
_genericBaseIdentifier	= gP "businessObjectId/baseIdentifier"
_genericVersion 		= gP "businessObjectId/versionIdentifier/versionRevision/versionNumber"

_genericUdfs :: Element -> [(String, String)]
_genericUdfs = gC "userDefinedField" >=> \udf -> zip (gP "fieldLabel" udf) (gP "fieldValue" udf)

-- properties

mxmlPropertiesDate, mxmlPropertiesTime, mxmlPropertiesUser, mxmlPropertiesGroup :: Element -> [String]
mxmlPropertiesDate	= gP "documentProperties/systemDate"
mxmlPropertiesTime	= gP "documentProperties/computerTime"
mxmlPropertiesUser	= gP "documentProperties/producedBy/user/name"
mxmlPropertiesGroup	= gP "documentProperties/producedBy/user/group"

-- events

mxmlEventsAction, mxmlEventsId, mxmlEventsNature :: Element -> [String]
mxmlEventsAction	= gP "events/mainEvent/action"
mxmlEventsId		= gP "events/mainEvent/object/objectId"
mxmlEventsNature	= gP "events/mainEvent/object/objectNature"

-- flows

_mxmlFlows :: Element -> [Element]
_mxmlFlows = gPe "deliverableFlows/deliverableFlow"

mxmlFlowsIdentifier :: Element -> [String]
mxmlFlowsIdentifier = _mxmlFlows >=> _genericIdentifier

mxmlFlowsBaseIdentifier :: Element -> [String]
mxmlFlowsBaseIdentifier = _mxmlFlows >=> _genericBaseIdentifier

mxmlFlowsVersion :: Element -> [String]
mxmlFlowsVersion = _mxmlFlows >=> _genericVersion

_mxmlFlowsSource :: Element -> [Element]
_mxmlFlowsSource = _mxmlFlows >=> gPe "deliverableFlowHeader/flowSource"

_mxmlFlowsFinancialKey :: Element -> [Element]
_mxmlFlowsFinancialKey = _mxmlFlowsSource >=> gC "flowFinancialKey"

mxmlFlowsContractInternalId :: Element -> [String]
mxmlFlowsContractInternalId = _mxmlFlowsFinancialKey >=> gP "producerId/internalId"

mxmlFlowsContractVersion :: Element -> [String]
mxmlFlowsContractVersion = _mxmlFlowsFinancialKey >=> gP "producerId/version"

mxmlFlowsTradeInternalId :: Element -> [String]
mxmlFlowsTradeInternalId = _mxmlFlowsFinancialKey >=> gP "tradeInternalId"

mxmlFlowsTradeLeg :: Element -> [String]
mxmlFlowsTradeLeg = _mxmlFlowsFinancialKey >=> gP "leg"

mxmlFlowsEventType :: Element -> [String]
mxmlFlowsEventType = _mxmlFlowsSource >=> gP "eventId/@mefClass"

mxmlFlowsEventIdentifier :: Element -> [String]
mxmlFlowsEventIdentifier = _mxmlFlowsSource >=> gC "eventId" >=> _genericIdentifier

mxmlFlowsIssuingEventType :: Element -> [String]
mxmlFlowsIssuingEventType = _mxmlFlowsSource >=> gP "issuingEventBody/@mefClass"

mxmlFlowsNettingContributorsId :: Element -> [String]
mxmlFlowsNettingContributorsId = _mxmlFlowsSource >=> gPe "issuingEventBody/eventNetting/deliverableFlowContributors/deliverableFlow" >=> _genericIdentifier

mxmlFlowsTypologies :: Element -> [String]
mxmlFlowsTypologies = _mxmlFlows >=> gP "deliverableFlowHeader/flowCategories/flowTypologies/flowTypology/value"

mxmlFlowsTradeTypology :: Element -> [String]
mxmlFlowsTradeTypology = _mxmlFlows >=> gP "deliverableFlowHeader/flowCategories/tradeTypology"

_mxmlFlowsInputCondition :: Element -> [Element]
_mxmlFlowsInputCondition = _mxmlFlows >=> gC "deliverableFlowInputCondition"

mxmlFlowsValueDate :: Element -> [String]
mxmlFlowsValueDate = _mxmlFlowsInputCondition >=> gP "valueDate"

mxmlFlowsSettlementDate :: Element -> [String]
mxmlFlowsSettlementDate = _mxmlFlowsInputCondition >=> gP "settlementDate"

mxmlFlowsOriginatingParty :: Element -> [String]
mxmlFlowsOriginatingParty = _mxmlFlowsInputCondition >=> gP "originatingParty/partyName"

mxmlFlowsBeneficiaryParty :: Element -> [String]
mxmlFlowsBeneficiaryParty = _mxmlFlowsInputCondition >=> gP "beneficiaryParty/partyName"

mxmlFlowsTradeParty :: Element -> [String]
mxmlFlowsTradeParty = _mxmlFlowsInputCondition >=> gP "tradeParty/partyName"

mxmlFlowsEntity :: Element -> [String]
mxmlFlowsEntity = _mxmlFlowsInputCondition >=> gP "entity"

_mxmlFlowsCash :: Element -> [Element]
_mxmlFlowsCash = _mxmlFlows >=> gPe "deliverableFlowBody/flowCash"

mxmlFlowsPortfolio :: Element -> [String]
mxmlFlowsPortfolio = _mxmlFlowsCash >=> gP "portfolio/businessObjectId/displayLabel"

mxmlFlowsDirection :: Element -> [String]
mxmlFlowsDirection = _mxmlFlowsCash >=> gP "flowSendReceive"

mxmlFlowsAmount :: Element -> [String]
mxmlFlowsAmount = _mxmlFlowsCash >=> gP "flowAmount"

_mxmlFlowsAccountingData :: Element -> [Element]
_mxmlFlowsAccountingData = _mxmlFlowsCash >=> gC "flowAccountingData"

mxmlFlowsTradeFamily :: Element -> [String]
mxmlFlowsTradeFamily = _mxmlFlowsAccountingData >=> gP "tradeFamily"

mxmlFlowsTradeGroup :: Element -> [String]
mxmlFlowsTradeGroup = _mxmlFlowsAccountingData >=> gP "tradeGroup"

mxmlFlowsTradeInstrument :: Element -> [String]
mxmlFlowsTradeInstrument = _mxmlFlowsAccountingData >=> gP "instrument"

_mxmlFlowsSettlements :: Element -> [Element]
_mxmlFlowsSettlements = _mxmlFlowsCash >=> gC "flowSettlements"

_mxmlFlowsNostro :: Element -> [Element]
_mxmlFlowsNostro = _mxmlFlowsSettlements >=> gC "nostroSettlement"

_mxmlFlowsVostro :: Element -> [Element]
_mxmlFlowsVostro = _mxmlFlowsSettlements >=> gC "vostroSettlement"

mxmlFlowsNostroReference :: Element -> [String]
mxmlFlowsNostroReference = _mxmlFlowsNostro >=> gP "settlementReference"

mxmlFlowsVostroReference :: Element -> [String]
mxmlFlowsVostroReference = _mxmlFlowsVostro >=> gP "settlementReference"

mxmlFlowsNostroUdfs :: Element -> [(String, String)]
mxmlFlowsNostroUdfs = _mxmlFlowsNostro >=> gC "userDefinedFields" >=> _genericUdfs

mxmlFlowsVostroUdfs :: Element -> [(String, String)]
mxmlFlowsVostroUdfs = _mxmlFlowsVostro >=> gC "userDefinedFields" >=> _genericUdfs

mxmlFlowsCurrency = _mxmlFlowsCash >=> gP "flowCurrency"

-- contract events

_mxmlContractEvents :: Element -> [Element]
_mxmlContractEvents = gPe "contractEvents/contractEvent"

mxmlContractEventsIdentifier :: Element -> [String]
mxmlContractEventsIdentifier = _mxmlContractEvents >=> _genericIdentifier

mxmlContractEventsEventType :: Element -> [String]
mxmlContractEventsEventType = _mxmlContractEvents >:> gA "mefClass"

_mxmlContractEventsInputBy :: Element -> [Element]
_mxmlContractEventsInputBy = _mxmlContractEvents >=> gPe "inputConditions/inputBy"

mxmlContractEventsUser :: Element -> [String]
mxmlContractEventsUser = _mxmlContractEventsInputBy >=> gP "userName"

mxmlContractEventsGroup :: Element -> [String]
mxmlContractEventsGroup = _mxmlContractEventsInputBy >=> gP "group"

_mxmlContractEventsTargetReference :: Element -> [Element]
_mxmlContractEventsTargetReference = _mxmlContractEvents >=> gPe "contractEventTargetReferences/contractEventTargetReference/contractReference"

mxmlContractEventsTargetContractIdentifier :: Element -> [String]
mxmlContractEventsTargetContractIdentifier = _mxmlContractEventsTargetReference >=> _genericIdentifier

mxmlContractEventsTargetContractBaseIdentifier :: Element -> [String]
mxmlContractEventsTargetContractBaseIdentifier = _mxmlContractEventsTargetReference >=> _genericBaseIdentifier

mxmlContractEventsTargetContractVersion :: Element -> [String]
mxmlContractEventsTargetContractVersion = _mxmlContractEventsTargetReference >=> _genericVersion

_mxmlContractEventsHeader :: Element -> [Element]
_mxmlContractEventsHeader = _mxmlContractEvents >=> gC "contractEventHeader"

mxmlContractEventsBaseIdentifier :: Element -> [String]
mxmlContractEventsBaseIdentifier = _mxmlContractEventsHeader >=> gC "contractEventId" >=> _genericBaseIdentifier

mxmlContractEventsVersion :: Element -> [String]
mxmlContractEventsVersion = _mxmlContractEventsHeader >=> gC "contractEventId" >=> _genericVersion

mxmlContractEventsDate :: Element -> [String]
mxmlContractEventsDate = _mxmlContractEventsHeader >=> gP "contractEventDate"

_mxmlContractEventsSource :: Element -> [Element]
_mxmlContractEventsSource = _mxmlContractEventsHeader >=> gC "contractEventSource"

mxmlContractEventsSourceModule :: Element -> [String]
mxmlContractEventsSourceModule = _mxmlContractEventsSource >=> gP "sourceModule"

_mxmlContractEventsImpacts :: Element -> [Element]
_mxmlContractEventsImpacts = _mxmlContractEvents >=> gPe "contractEventImpacts/contractEventImpact"

mxmlContractEventsImpactsType :: Element -> [String]
mxmlContractEventsImpactsType = _mxmlContractEventsImpacts >:> gA "mefClass"

mxmlContractEventsImpactsIdentifier :: Element -> [String]
mxmlContractEventsImpactsIdentifier = _mxmlContractEventsImpacts >=> gP "contractEventImpactHeader/contractEventImpactId/internalId"

mxmlContractEventsTargetTradeIdentifier :: Element -> [String]
mxmlContractEventsTargetTradeIdentifier = _mxmlContractEventsImpacts >=> gP "tradeReference/tradeId/tradeInternalId"

-- contracts

_mxmlContracts :: Element -> [Element]
_mxmlContracts = gPe "contracts/contract"

mxmlContractsIdentifier :: Element -> [String]
mxmlContractsIdentifier = _mxmlContracts >=> _genericIdentifier

mxmlContractsBaseIdentifier :: Element -> [String]
mxmlContractsBaseIdentifier = _mxmlContracts >=> _genericBaseIdentifier

mxmlContractsVersion :: Element -> [String]
mxmlContractsVersion =  _mxmlContracts >=> _genericVersion

mxmlContractsTypology :: Element -> [String]
mxmlContractsTypology = _mxmlContracts >=> gP "contractHeader/contractCategory/typology"

_mxmlContractsSource :: Element -> [Element]
_mxmlContractsSource = _mxmlContracts >=> gPe "contractHeader/contractSource"

mxmlContractsSourceModule :: Element -> [String]
mxmlContractsSourceModule = _mxmlContractsSource >=> gP "sourceModule"

mxmlContractsLastEventType :: Element -> [String]
mxmlContractsLastEventType = _mxmlContractsSource >=> gP "lastContractEventReference/@mefClass"

mxmlContractsLastEventReference :: Element -> [String]
mxmlContractsLastEventReference = _mxmlContractsSource >=> gP "lastContractEventReference/contractEventId/internalId"

_mxmlContractsLastActor :: Element -> [Element]
_mxmlContractsLastActor = _mxmlContractsSource >=> gPe "workflowParties/wopLastActor"

mxmlContractsLastActorUser :: Element -> [String]
mxmlContractsLastActorUser = _mxmlContractsLastActor >=> gP "name"

mxmlContractsLastActorGroup :: Element -> [String]
mxmlContractsLastActorGroup = _mxmlContractsLastActor >=> gP "group"

-- trades

_mxmlTrades :: Element -> [Element]
_mxmlTrades = gPe "trades/trade"

mxmlTradesIdentifier :: Element -> [String]
mxmlTradesIdentifier = _mxmlTrades >=> _genericIdentifier

mxmlTradesBaseIdentifier :: Element -> [String]
mxmlTradesBaseIdentifier = _mxmlTrades >=> _genericBaseIdentifier

mxmlTradesVersion :: Element -> [String]
mxmlTradesVersion = _mxmlTrades >=> _genericVersion

mxmlTradesParties :: Element -> [String]
mxmlTradesParties = _mxmlTrades >=> gP "parties/party/partyName"

mxmlTradesPortfolios :: Element -> [String]
mxmlTradesPortfolios = _mxmlTrades >=> gP "portfolios/portfolio/portfolioLabel"

_mxmlTradesHeader :: Element -> [Element]
_mxmlTradesHeader = _mxmlTrades >=> gC "tradeHeader"

mxmlTradesDate :: Element -> [String]
mxmlTradesDate = _mxmlTradesHeader >=> gP "tradeDate"

_mxmlTradesCategory :: Element -> [Element]
_mxmlTradesCategory = _mxmlTradesHeader >=> gC "tradeCategory"

mxmlTradesDestination :: Element -> [String]
mxmlTradesDestination = _mxmlTradesCategory >=> gP "tradeDestination"

mxmlTradesTypology :: Element -> [String]
mxmlTradesTypology = _mxmlTradesCategory >=> gP "typology"

mxmlTradesFamily :: Element -> [String]
mxmlTradesFamily = _mxmlTradesCategory >=> gP "tradeFamily"

mxmlTradesGroup :: Element -> [String]
mxmlTradesGroup = _mxmlTradesCategory >=> gP "tradeGroup"

mxmlTradesType :: Element -> [String]
mxmlTradesType = _mxmlTradesCategory >=> gP "tradeType"

mxmlTradesUdfs :: Element -> [(String, String)]
mxmlTradesUdfs = _mxmlTradesHeader >=> gC "tradeUserDefinedFields" >=> _genericUdfs

mxmlTradesGlobalId :: Element -> [String]
mxmlTradesGlobalId = _mxmlTradesHeader >=> gP "tradeViews/tradeView/tradeId/tradeGlobalId"

_mxmlTradesSettlementFlows :: Element -> [Element]
_mxmlTradesSettlementFlows = _mxmlTrades >=> gPe "tradeBody/*/settlement/settlementFlow/flow"

mxmlTradesSettlementFlowsDate :: Element -> [String]
mxmlTradesSettlementFlowsDate = _mxmlTradesSettlementFlows >=> gP "date"

mxmlTradesSettlementFlowsAmount :: Element -> [String]
mxmlTradesSettlementFlowsAmount = _mxmlTradesSettlementFlows >=> gP "amount"

mxmlTradesSettlementFlowsCurrency :: Element -> [String]
mxmlTradesSettlementFlowsCurrency = _mxmlTradesSettlementFlows >=> gP "currency"

_mxmlTradesStreams :: Element -> [Element]
_mxmlTradesStreams = _mxmlTrades >=> gPe "tradeBody/*/stream"

_mxmlTradesStreamsTemplate :: Element -> [Element]
_mxmlTradesStreamsTemplate = _mxmlTradesStreams >=> gC "streamTemplate"

mxmlTradesStreamsFixFloat :: Element -> [String]
mxmlTradesStreamsFixFloat = _mxmlTradesStreamsTemplate >=> gP "payoutType"

mxmlTradesStreamsCurrency :: Element -> [String]
mxmlTradesStreamsCurrency = _mxmlTradesStreamsTemplate >=> gP "paymentCurrency"

mxmlTradesStreamsSettlement :: Element -> [String]
mxmlTradesStreamsSettlement = _mxmlTradesStreamsTemplate >=> gP "settlementNature"

mxmlTradesStreamsPayReceive :: Element -> [String]
mxmlTradesStreamsPayReceive = _mxmlTradesStreams >=> gP "payReceive"

mxmlTradesStreamsEffectiveDate :: Element -> [String]
mxmlTradesStreamsEffectiveDate = _mxmlTradesStreams >=> gP "effectiveDate"

mxmlTradesStreamsMaturity :: Element -> [String]
mxmlTradesStreamsMaturity = _mxmlTradesStreams >=> gP "maturity"

mxmlTradesStreamsCapitalInitial :: Element -> [String]
mxmlTradesStreamsCapitalInitial = _mxmlTradesStreams >=> gP "capital/initialCapitalAmount"

_mxmlTradesStreamsInterestFlows :: Element -> [[Element]]
_mxmlTradesStreamsInterestFlows = _mxmlTradesStreams >=> gPe "cashFlows/interestFlows" >:> gC "interestPaymentPeriod"

mxmlTradesStreamsInterestFlowsCalculationStartDate :: Element -> [[String]]
mxmlTradesStreamsInterestFlowsCalculationStartDate = map ((=<<) (gP "calculationPeriod/calculationStartDate")) . _mxmlTradesStreamsInterestFlows

mxmlTradesStreamsInterestFlowsCalculationEndDate :: Element -> [[String]]
mxmlTradesStreamsInterestFlowsCalculationEndDate = map ((=<<) (gP "calculationPeriod/calculationEndDate")) . _mxmlTradesStreamsInterestFlows

mxmlTradesStreamsInterestFlowsCalculationNotional :: Element -> [[String]]
mxmlTradesStreamsInterestFlowsCalculationNotional = map ((=<<) (gP "calculationPeriod/notionalAmount")) . _mxmlTradesStreamsInterestFlows

mxmlTradesStreamsInterestFlowsPaymentDate :: Element -> [[String]]
mxmlTradesStreamsInterestFlowsPaymentDate = map ((=<<) (gP "paymentDate")) . _mxmlTradesStreamsInterestFlows

mxmlTradesStreamsInterestFlowsAmount :: Element -> [[String]]
mxmlTradesStreamsInterestFlowsAmount = map ((=<<) (gP "interestFlow")) . _mxmlTradesStreamsInterestFlows

mxmlTradesStreamsInterestFlowsCurrency :: Element -> [[String]]
mxmlTradesStreamsInterestFlowsCurrency = map ((=<<) (gP "currency")) . _mxmlTradesStreamsInterestFlows

mxmlTradesStreamsInterestFlowsCoupon :: Element -> [[String]]
mxmlTradesStreamsInterestFlowsCoupon = map ((=<<) (gP "coupon")) . _mxmlTradesStreamsInterestFlows

_mxmlTradesSi :: Element -> [Element]
_mxmlTradesSi = _mxmlTrades >=> gPe "settlementInstructions/settlementInstruction"

_mxmlTradesSiKey :: Element -> [Element]
_mxmlTradesSiKey = _mxmlTradesSi >=> gC "settlementInstructionKey"

mxmlTradesSiSign :: Element -> [String]
mxmlTradesSiSign = _mxmlTradesSiKey >=> gP "flowSign"

mxmlTradesSiCurrency :: Element -> [String]
mxmlTradesSiCurrency = _mxmlTradesSiKey >=> gP "flowCurrency"

mxmlTradesSiNature :: Element -> [String]
mxmlTradesSiNature = _mxmlTradesSiKey >=> gP "flowNature"

mxmlTradesSiSettlement :: Element -> [String]
mxmlTradesSiSettlement = _mxmlTradesSiKey >=> gP "settlementType"

_mxmlTradesSiDescription :: Element -> [Element]
_mxmlTradesSiDescription = _mxmlTradesSi >=> gC "settlementInstructionDescription"

mxmlTradesSiNostroReference :: Element -> [String]
mxmlTradesSiNostroReference = _mxmlTradesSiDescription >=> gP "nostroReferenceId"

mxmlTradesSiVostroReference :: Element -> [String]
mxmlTradesSiVostroReference = _mxmlTradesSiDescription >=> gP "vostroReferenceId"

mxmlTradesSiNostroUdfs :: Element -> [(String, String)]
mxmlTradesSiNostroUdfs = _mxmlTradesSi >=> gC "nostroInstructions" >=> _genericUdfs

mxmlTradesSiVostroUdfs :: Element -> [(String, String)]
mxmlTradesSiVostroUdfs = _mxmlTradesSi >=> gC "vostroInstructions" >=> _genericUdfs

_mxmlTradesEvaluationFlows :: Element -> [Element]
_mxmlTradesEvaluationFlows = _mxmlTrades >=> gPe "evaluationFlows/evaluationFlow"

_mxmlTradesEvaluationFlowsFlow :: Element -> [Element]
_mxmlTradesEvaluationFlowsFlow = _mxmlTradesEvaluationFlows >=> gC "flow"

mxmlTradesEvaluationFlowsDate :: Element -> [String]
mxmlTradesEvaluationFlowsDate = _mxmlTradesEvaluationFlowsFlow >=> gP "date"

mxmlTradesEvaluationFlowsAmount :: Element -> [String]
mxmlTradesEvaluationFlowsAmount = _mxmlTradesEvaluationFlowsFlow >=> gP "amount"

mxmlTradesEvaluationFlowsCurrency :: Element -> [String]
mxmlTradesEvaluationFlowsCurrency = _mxmlTradesEvaluationFlowsFlow >=> gP "currency"

_mxmlTradesEvaluationFlowsTransactions :: Element -> [Element]
_mxmlTradesEvaluationFlowsTransactions = _mxmlTradesEvaluationFlows >=> gPe "flowBody/flow_transaction"

mxmlTradesEvaluationFlowsLeg :: Element -> [String]
mxmlTradesEvaluationFlowsLeg = _mxmlTradesEvaluationFlowsTransactions >=> gP "leg"

--

mxmlTradesEvaluationFlowsTypology :: Element -> [[String]]
mxmlTradesEvaluationFlowsTypology = _mxmlTradesEvaluationFlows >:> gP "flowTypologies/flowTypology/value"

_mxmlTradesVersions :: Element -> [Element]
_mxmlTradesVersions = _mxmlTrades >=> gPe "tradeVersions/tradeVersion"

mxmlTradesVersionsIdentifier :: Element -> [String]
mxmlTradesVersionsIdentifier = _mxmlTradesVersions >=> _genericIdentifier

mxmlTradesVersionsEventType :: Element -> [String]
mxmlTradesVersionsEventType = _mxmlTradesVersions >=> gP "contractEventReference/@mefClass"