private void checkPendingEvent(String eventId) throws UnexpectedResultReturnedFromDatabaseException { try { String eventTypeCode = openContractBusinessTransactionDao.getEventType(eventId); String contractHash; String negotiationId; String negotiationIdFromDatabase; ContractType contractType; BusinessTransactionMetadata businessTransactionMetadata; ContractTransactionStatus contractTransactionStatus; if (eventTypeCode.equals(EventType.INCOMING_BUSINESS_TRANSACTION_CONTRACT_HASH.getCode())) { // Check if contract is created: List<Transaction<BusinessTransactionMetadata>> pendingTransactionList = transactionTransmissionManager.getPendingTransactions(Specialist.UNKNOWN_SPECIALIST); for (Transaction<BusinessTransactionMetadata> record : pendingTransactionList) { businessTransactionMetadata = record.getInformation(); contractHash = businessTransactionMetadata.getContractHash(); if (openContractBusinessTransactionDao.isContractHashExists(contractHash)) { negotiationId = businessTransactionMetadata.getNegotiationId(); negotiationIdFromDatabase = businessTransactionMetadata.getNegotiationId(); if (negotiationId.equals(negotiationIdFromDatabase)) { contractTransactionStatus = ContractTransactionStatus.PENDING_CONFIRMATION; } else { contractTransactionStatus = ContractTransactionStatus.HASH_REJECTED; } openContractBusinessTransactionDao.updateContractTransactionStatus( contractHash, contractTransactionStatus); openContractBusinessTransactionDao.updateEventStatus( contractHash, EventStatus.NOTIFIED); transactionTransmissionManager.confirmReception(record.getTransactionID()); } } } if (eventTypeCode.equals( EventType.INCOMING_CONFIRM_BUSINESS_TRANSACTION_CONTRACT.getCode())) { // Check if contract hash was sent. List<Transaction<BusinessTransactionMetadata>> pendingTransactionList = transactionTransmissionManager.getPendingTransactions(Specialist.UNKNOWN_SPECIALIST); for (Transaction<BusinessTransactionMetadata> record : pendingTransactionList) { businessTransactionMetadata = record.getInformation(); contractHash = businessTransactionMetadata.getContractHash(); if (openContractBusinessTransactionDao.isContractHashSentConfirmation(contractHash)) { openContractBusinessTransactionDao.updateContractTransactionStatus( contractHash, ContractTransactionStatus.PENDING_RESPONSE); openContractBusinessTransactionDao.updateEventStatus( contractHash, EventStatus.NOTIFIED); transactionTransmissionManager.confirmReception(record.getTransactionID()); } } } if (eventTypeCode.equals( EventType.INCOMING_CONFIRM_BUSINESS_TRANSACTION_RESPONSE.getCode())) { // TODO: check if contract hash was sent. List<Transaction<BusinessTransactionMetadata>> pendingTransactionList = transactionTransmissionManager.getPendingTransactions(Specialist.UNKNOWN_SPECIALIST); for (Transaction<BusinessTransactionMetadata> record : pendingTransactionList) { businessTransactionMetadata = record.getInformation(); contractHash = businessTransactionMetadata.getContractHash(); if (openContractBusinessTransactionDao.isContractHashPendingResponse(contractHash)) { openContractBusinessTransactionDao.updateContractTransactionStatus( contractHash, ContractTransactionStatus.CONTRACT_OPENED); openContractBusinessTransactionDao.updateEventStatus( contractHash, EventStatus.NOTIFIED); contractType = openContractBusinessTransactionDao.getContractType(contractHash); switch (contractType) { case PURCHASE: customerBrokerContractPurchaseManager .updateStatusCustomerBrokerPurchaseContractStatus( contractHash, ContractStatus.PENDING_PAYMENT); } transactionTransmissionManager.confirmReception(record.getTransactionID()); raiseNewContractEvent(); } } } // TODO: look a better way to deal with this exceptions } catch (CantDeliverPendingTransactionsException e) { e.printStackTrace(); } catch (CantUpdateRecordException e) { e.printStackTrace(); } catch (CantupdateCustomerBrokerContractPurchaseException e) { e.printStackTrace(); } catch (CantConfirmTransactionException e) { e.printStackTrace(); } }
private void doTheMainTask() throws CannotSendContractHashException, CantUpdateRecordException { try { openContractBusinessTransactionDao = new OpenContractBusinessTransactionDao(pluginDatabaseSystem, pluginId, database); /** Check if exist in database new contracts to send */ List<String> contractPendingToSubmitList = openContractBusinessTransactionDao.getPendingToSubmitContractHash(); String contractXML; ContractPurchaseRecord purchaseContract = new ContractPurchaseRecord(); ContractSaleRecord saleContract = new ContractSaleRecord(); ContractType contractType; UUID transactionId; if (!contractPendingToSubmitList.isEmpty()) { for (String hashToSubmit : contractPendingToSubmitList) { System.out.println("OPEN CONTRACT - Hash to submit:\n" + hashToSubmit); contractXML = openContractBusinessTransactionDao.getContractXML(hashToSubmit); contractType = openContractBusinessTransactionDao.getContractType(hashToSubmit); transactionId = openContractBusinessTransactionDao.getTransactionId(hashToSubmit); switch (contractType) { case PURCHASE: purchaseContract = (ContractPurchaseRecord) XMLParser.parseXML(contractXML, purchaseContract); transactionTransmissionManager.sendContractHashToCryptoBroker( transactionId, purchaseContract.getPublicKeyCustomer(), purchaseContract.getPublicKeyBroker(), hashToSubmit, purchaseContract.getNegotiationId()); break; case SALE: saleContract = (ContractSaleRecord) XMLParser.parseXML(contractXML, saleContract); transactionTransmissionManager.sendContractHashToCryptoCustomer( transactionId, saleContract.getPublicKeyBroker(), saleContract.getPublicKeyCustomer(), hashToSubmit, saleContract.getNegotiationId()); break; } // Update the ContractTransactionStatus openContractBusinessTransactionDao.updateContractTransactionStatus( hashToSubmit, ContractTransactionStatus.CHECKING_HASH); } } /** Check if pending contract to confirm */ List<String> contractPendingToConfirmList = openContractBusinessTransactionDao.getPendingToConfirmContractHash(); if (!contractPendingToConfirmList.isEmpty()) { for (String hashToSubmit : contractPendingToSubmitList) { System.out.println("OPEN CONTRACT - Hash to confirm:\n" + hashToSubmit); contractXML = openContractBusinessTransactionDao.getContractXML(hashToSubmit); contractType = openContractBusinessTransactionDao.getContractType(hashToSubmit); switch (contractType) { case PURCHASE: purchaseContract = (ContractPurchaseRecord) XMLParser.parseXML(contractXML, purchaseContract); transactionTransmissionManager.sendTransactionNewStatusNotification( purchaseContract.getPublicKeyCustomer(), purchaseContract.getPublicKeyBroker(), hashToSubmit, ContractTransactionStatus.CONTRACT_CONFIRMED); break; case SALE: saleContract = (ContractSaleRecord) XMLParser.parseXML(contractXML, saleContract); transactionTransmissionManager.sendTransactionNewStatusNotification( purchaseContract.getPublicKeyBroker(), purchaseContract.getPublicKeyCustomer(), hashToSubmit, ContractTransactionStatus.CONTRACT_CONFIRMED); break; } // Update the ContractTransactionStatus openContractBusinessTransactionDao.updateContractTransactionStatus( hashToSubmit, ContractTransactionStatus.CONTRACT_CONFIRMED); } } /** Check if pending events */ List<String> pendingEventsIdList = openContractBusinessTransactionDao.getPendingEvents(); for (String eventId : pendingEventsIdList) { checkPendingEvent(eventId); } } catch (CantGetContractListException e) { throw new CannotSendContractHashException( e, "Sending contract hash", "Cannot get the contract list from database"); } catch (UnexpectedResultReturnedFromDatabaseException e) { throw new CannotSendContractHashException( e, "Sending contract hash", "Unexpected result in database"); } catch (CantSendBusinessTransactionHashException e) { throw new CannotSendContractHashException( e, "Sending contract hash", "Error in Transaction Transmission Network Service"); } }