// TODO: mejorar, el plugin de mierda de db es un asco public void markReadTransaction(UUID requestId) { try { DatabaseTable transactionTable = this.database.getTable( OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_TABLE_NAME); transactionTable.addUUIDFilter( OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_TRANSACTION_ID_COLUMN_NAME, requestId, DatabaseFilterType.EQUAL); transactionTable.loadToMemory(); List<DatabaseTableRecord> records = transactionTable.getRecords(); if (!records.isEmpty()) { DatabaseTableRecord record = records.get(0); // set new record values record.setStringValue( OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_TRANSACTION_MARK_COLUMN_NAME, Boolean.TRUE.toString()); transactionTable.updateRecord(record); } } catch (CantLoadTableToMemoryException e) { e.printStackTrace(); } catch (CantUpdateRecordException e) { e.printStackTrace(); } }
public NegotiationTransmission getNotificationById(final UUID transactionId) { NegotiationTransmission negotiationTransmission = null; if (transactionId == null) return null; try { DatabaseTable cryptoPaymentRequestTable = getDatabaseTable(); cryptoPaymentRequestTable.addUUIDFilter( NegotiationTransmissionNetworkServiceDatabaseConstants .OUTGOING_NOTIFICATION_TRANSACTION_ID_COLUMN_NAME, transactionId, DatabaseFilterType.EQUAL); cryptoPaymentRequestTable.loadToMemory(); List<DatabaseTableRecord> records = cryptoPaymentRequestTable.getRecords(); if (!records.isEmpty()) { negotiationTransmission = buildNegotiationTransmission(records.get(0)); } } catch (CantLoadTableToMemoryException e) { e.printStackTrace(); } catch (InvalidParameterException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return negotiationTransmission; }
/* * Given a fiat amount and its equivalent crypto amount this methods calculates the * discount that would be produced if the user debit the same amounts. * The return value represents fiat currency. */ long calculateDiscount(long fiatAmount, long cryptoAmount) throws CalculateDiscountFailedException { long spent = 0; BasicWalletAvailable basicWalletAvailable = new BasicWalletAvailable(this.fiatCurrency, this.cryptoCurrency); basicWalletAvailable.setCryptoIndexManager(this.cryptoIndexManager); basicWalletAvailable.setErrorManager(this.errorManager); basicWalletAvailable.setDatabase(this.database); long available; try { available = basicWalletAvailable.getAvailableAmount(); } catch (CantCalculateAvailableAmountException e) { System.err.println("AvailableFailedException" + e.getMessage()); e.printStackTrace(); throw new CalculateDiscountFailedException(); } if (available < fiatAmount) { System.err.println("Not enough funds"); throw new CalculateDiscountFailedException(); } DatabaseTable valueChunksTable = database.getTable(BasicWalletDatabaseConstants.VALUE_CHUNKS_TABLE_NAME); // We set the filter to get the UNSPENT chunks valueChunksTable.setStringFilter( BasicWalletDatabaseConstants.VALUE_CHUNKS_TABLE_STATUS_COLUMN_NAME, CryptoValueChunkStatus.UNSPENT.getCode(), DatabaseFilterType.EQUAL); // now we apply the filter try { valueChunksTable.loadToMemory(); } catch (CantLoadTableToMemoryException cantLoadTableToMemory) { /** I can not solve this situation. */ System.err.println("CantLoadTableToMemoryException: " + cantLoadTableToMemory.getMessage()); cantLoadTableToMemory.printStackTrace(); throw new CalculateDiscountFailedException(); } /* We first sort the chunks of the list putting the chunks generated * at lower exchange rate at the beginning of the list. The exchange * rate of a chunk (fa,ca,state) is equal to the quotient fa/ca. (see documentation) */ List<DatabaseTableRecord> recordList = valueChunksTable.getRecords(); Collections.sort(recordList, new ChunksComparator()); /* Let's calculate the discount as explained in the * documentation. we will add up the fiat amounts stored * in the chunks that would be used to pay the extraction * equivalent to cryptoAmount */ for (DatabaseTableRecord d : recordList) { long fa = d.getLongValue(BasicWalletDatabaseConstants.VALUE_CHUNKS_TABLE_FIAT_AMOUNT_COLUMN_NAME); long ca = d.getLongValue(BasicWalletDatabaseConstants.VALUE_CHUNKS_TABLE_CRYPTO_AMOUNT_COLUMN_NAME); if (cryptoAmount == 0) break; if (cryptoAmount >= ca) { cryptoAmount = cryptoAmount - ca; spent = spent + fa; } else { long fa1 = Converter.getProportionalFiatAmountRoundedDown(ca, fa, cryptoAmount); cryptoAmount = 0; spent = spent + fa1; } } return fiatAmount - spent; }
/** * Method that list the all entities on the data base. The valid value of the key are the att of * the <code>CommunicationNetworkServiceDatabaseConstants</code> * * @param filters * @return List<FermatMessage> * @throws CantReadRecordDataBaseException */ public List<FermatMessage> findAll(Map<String, Object> filters) throws CantReadRecordDataBaseException { if (filters == null || filters.isEmpty()) { throw new IllegalArgumentException("The filters are required, can not be null or empty"); } List<FermatMessage> list = null; List<DatabaseTableFilter> filtersTable = new ArrayList<>(); try { /* * 1- Prepare the filters */ DatabaseTable templateTable = getDatabaseTable(); for (String key : filters.keySet()) { DatabaseTableFilter newFilter = templateTable.getEmptyTableFilter(); newFilter.setType(DatabaseFilterType.EQUAL); newFilter.setColumn(key); newFilter.setValue((String) filters.get(key)); filtersTable.add(newFilter); } /* * 2 - load the data base to memory with filters */ templateTable.setFilterGroup(filtersTable, null, DatabaseFilterOperator.AND); templateTable.loadToMemory(); /* * 3 - read all records */ List<DatabaseTableRecord> records = templateTable.getRecords(); /* * 4 - Create a list of FermatMessage objects */ list = new ArrayList<>(); list.clear(); /* * 5 - Convert into FermatMessage objects */ for (DatabaseTableRecord record : records) { /* * 5.1 - Create and configure a FermatMessage */ FermatMessage outgoingTemplateNetworkServiceMessage = constructFrom(record); /* * 5.2 - Add to the list */ list.add(outgoingTemplateNetworkServiceMessage); } } catch (CantLoadTableToMemoryException cantLoadTableToMemory) { cantLoadTableToMemory.printStackTrace(); StringBuffer contextBuffer = new StringBuffer(); contextBuffer.append( "Database Name: " + CommunicationNetworkServiceDatabaseConstants.DATA_BASE_NAME); String context = contextBuffer.toString(); String possibleCause = "The data no exist"; CantReadRecordDataBaseException cantReadRecordDataBaseException = new CantReadRecordDataBaseException( CantReadRecordDataBaseException.DEFAULT_MESSAGE, cantLoadTableToMemory, context, possibleCause); throw cantReadRecordDataBaseException; } /* * return the list */ return list; };