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; }
// 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(); } }
// GET NEGOTIATION TRANSACTION FROM TRANSACTION ID public CustomerBrokerClose getRegisterCustomerBrokerCloseNegotiationTranasction( UUID transactionId) throws CantRegisterCustomerBrokerCloseNegotiationTransactionException { try { CustomerBrokerClose getTransaction = null; List<DatabaseTableRecord> record; DatabaseTable table = this.database.getTable( CustomerBrokerCloseNegotiationTransactionDatabaseConstants .CUSTOMER_BROKER_CLOSE_TABLE_NAME); if (table == null) { throw new CantGetUserDeveloperIdentitiesException( "Cant check if customer broker close exists", "customer broker close Negotiation Transaction", ""); } table.addUUIDFilter( CustomerBrokerCloseNegotiationTransactionDatabaseConstants .CUSTOMER_BROKER_CLOSE_TRANSACTION_ID_COLUMN_NAME, transactionId, DatabaseFilterType.EQUAL); table.loadToMemory(); record = table.getRecords(); if (record.size() == 0) throw new CantRegisterCustomerBrokerCloseNegotiationTransactionException( "The number of records is 0 ", null, "", ""); for (DatabaseTableRecord records : record) { getTransaction = getCustomerBrokerCloseFromRecord(records); } return getTransaction; } catch (CantLoadTableToMemoryException em) { throw new CantRegisterCustomerBrokerCloseNegotiationTransactionException( em.getMessage(), em, "customer broker close Negotiation Transaction not return register", "Cant load " + CustomerBrokerCloseNegotiationTransactionDatabaseConstants .CUSTOMER_BROKER_CLOSE_TABLE_NAME + " table in memory."); } catch (Exception e) { throw new CantRegisterCustomerBrokerCloseNegotiationTransactionException( e.getMessage(), FermatException.wrapException(e), "customer broker close Negotiation Transaction not return register", "unknown failure."); } }
// GET LIST NEW NEGOTIATION TRANSACTION PENDING TO CONFIRM public List<CustomerBrokerClose> getPendingToConfirmtNegotiation() throws CantGetNegotiationTransactionListException { try { List<CustomerBrokerClose> getTransactions = new ArrayList<>(); List<DatabaseTableRecord> record; DatabaseTable table = this.database.getTable( CustomerBrokerCloseNegotiationTransactionDatabaseConstants .CUSTOMER_BROKER_CLOSE_TABLE_NAME); if (table == null) throw new CantGetUserDeveloperIdentitiesException( "Cant check if customer broker update exists", "Customer Broker New Negotiation Transaction", ""); table.addStringFilter( CustomerBrokerCloseNegotiationTransactionDatabaseConstants .CUSTOMER_BROKER_CLOSE_STATUS_COLUMN_NAME, NegotiationTransactionStatus.PENDING_SUBMIT_CONFIRM.getCode(), DatabaseFilterType.EQUAL); table.loadToMemory(); record = table.getRecords(); if (record.isEmpty()) return getTransactions; for (DatabaseTableRecord records : record) { getTransactions.add(getCustomerBrokerCloseFromRecord(records)); } return getTransactions; } catch (CantLoadTableToMemoryException em) { throw new CantGetNegotiationTransactionListException( em.getMessage(), em, "Customer Broker Update Negotiation Transaction not return register", "Cant load " + CustomerBrokerCloseNegotiationTransactionDatabaseConstants .CUSTOMER_BROKER_CLOSE_TABLE_NAME + " table in memory."); } catch (Exception e) { throw new CantGetNegotiationTransactionListException( e.getMessage(), FermatException.wrapException(e), "Customer Broker Update Negotiation Transaction not return register", "unknown failure."); } }
// GET LIST NEW NEGOTIATION TRANSACTION public List<CustomerBrokerNew> getAllRegisterCustomerBrokerNewNegotiationTranasction() throws CantRegisterCustomerBrokerNewNegotiationTransactionException { try { List<CustomerBrokerNew> getTransactions = new ArrayList<>(); List<DatabaseTableRecord> record; DatabaseTable table = this.database.getTable( CustomerBrokerNewNegotiationTransactionDatabaseConstants .CUSTOMER_BROKER_NEW_TABLE_NAME); if (table == null) throw new CantGetUserDeveloperIdentitiesException( "Cant check if customer broker new exists", "Customer Broker New Negotiation Transaction", ""); table.loadToMemory(); record = table.getRecords(); if (record.size() == 0) return getTransactions; for (DatabaseTableRecord records : record) { getTransactions.add(getCustomerBrokerNewFromRecord(records)); } return getTransactions; } catch (CantLoadTableToMemoryException em) { throw new CantRegisterCustomerBrokerNewNegotiationTransactionException( em.getMessage(), em, "Customer Broker New Negotiation Transaction not return register", "Cant load " + CustomerBrokerNewNegotiationTransactionDatabaseConstants .CUSTOMER_BROKER_NEW_TABLE_NAME + " table in memory."); } catch (Exception e) { throw new CantRegisterCustomerBrokerNewNegotiationTransactionException( e.getMessage(), FermatException.wrapException(e), "Customer Broker New Negotiation Transaction not return register", "unknown failure."); } }
private boolean transactionExists(UUID transactionId) throws CantRegisterCustomerBrokerNewNegotiationTransactionException { try { DatabaseTable table = this.database.getTable( CustomerBrokerNewNegotiationTransactionDatabaseConstants .CUSTOMER_BROKER_NEW_TABLE_NAME); if (table == null) throw new CantGetUserDeveloperIdentitiesException( "Cant check if customer broker new tablet exists", "Customer Broker New Negotiation Transaction", ""); table.addUUIDFilter( CustomerBrokerNewNegotiationTransactionDatabaseConstants .CUSTOMER_BROKER_NEW_TRANSACTION_ID_COLUMN_NAME, transactionId, DatabaseFilterType.EQUAL); table.loadToMemory(); return table.getRecords().size() > 0; } catch (CantLoadTableToMemoryException em) { throw new CantRegisterCustomerBrokerNewNegotiationTransactionException( em.getMessage(), em, "Customer Broker New Negotiation Transaction Id Not Exists", "Cant load " + CustomerBrokerNewNegotiationTransactionDatabaseConstants .CUSTOMER_BROKER_NEW_TABLE_NAME + " table in memory."); } catch (Exception e) { throw new CantRegisterCustomerBrokerNewNegotiationTransactionException( e.getMessage(), FermatException.wrapException(e), "Customer Broker New Negotiation Transaction Id Not Exists", "unknown failure."); } }
/** * Method that check if alias exists. * * @param alias * @return boolean exists * @throws CantCreateNewDeveloperException */ private boolean aliasExists(String alias) throws CantCreateNewDeveloperException { DatabaseTable table; /** Get developers identities list. I select records on table */ try { table = this.database.getTable( AssetRedeemPointIdentityDatabaseConstants.ASSET_REDEEM_POINT_IDENTITY_TABLE_NAME); if (table == null) { throw new CantGetUserDeveloperIdentitiesException( "Cant check if alias exists", "Asset Issuer Identity", ""); } table.setStringFilter( AssetRedeemPointIdentityDatabaseConstants.ASSET_REDEEM_POINT_IDENTITY_ALIAS_COLUMN_NAME, alias, DatabaseFilterType.EQUAL); table.loadToMemory(); return table.getRecords().size() > 0; } catch (CantLoadTableToMemoryException em) { throw new CantCreateNewDeveloperException( em.getMessage(), em, "Asset Issuer Identity", "Cant load " + AssetRedeemPointIdentityDatabaseConstants.ASSET_REDEEM_POINT_IDENTITY_TABLE_NAME + " table in memory."); } catch (Exception e) { throw new CantCreateNewDeveloperException( e.getMessage(), FermatException.wrapException(e), "Asset Issuer Identity", "unknown failure."); } }
/*PRIVATE METHOD*/ private boolean eventExists(UUID eventId) throws CantRegisterCustomerBrokerCloseEventException { try { DatabaseTable table = this.database.getTable( CustomerBrokerCloseNegotiationTransactionDatabaseConstants .CUSTOMER_BROKER_CLOSE_EVENT_TABLE_NAME); if (table == null) throw new CantRegisterCustomerBrokerCloseEventException( "Cant check if event tablet exists", "Customer Broker New Negotiation Transaction", ""); table.addUUIDFilter( CustomerBrokerCloseNegotiationTransactionDatabaseConstants .CUSTOMER_BROKER_CLOSE_EVENT_ID_COLUMN_NAME, eventId, DatabaseFilterType.EQUAL); table.loadToMemory(); return table.getRecords().size() > 0; } catch (CantLoadTableToMemoryException em) { throw new CantRegisterCustomerBrokerCloseEventException( em.getMessage(), em, "Customer Broker New Negotiation Transaction Event Id Not Exists", "Cant load " + CustomerBrokerCloseNegotiationTransactionDatabaseConstants .CUSTOMER_BROKER_CLOSE_TABLE_NAME + " table in memory."); } catch (Exception e) { throw new CantRegisterCustomerBrokerCloseEventException( e.getMessage(), FermatException.wrapException(e), "Customer Broker New Negotiation Transaction Event Id Not Exists", "unknown failure."); } }
public List<RedeemPointIdentity> getIdentityAssetRedeemPointsFromCurrentDeviceUser( DeviceUser deviceUser) throws CantListAssetRedeemPointIdentitiesException { // Setup method. List<RedeemPointIdentity> list = new ArrayList<>(); // Intra User list. DatabaseTable table; // Intra User table. // Get Redeem Point identities list. try { /** 1) Get the table. */ table = this.database.getTable( AssetRedeemPointIdentityDatabaseConstants.ASSET_REDEEM_POINT_IDENTITY_TABLE_NAME); if (table == null) { /** Table not found. */ throw new CantGetUserDeveloperIdentitiesException( "Cant get Asset Issuer identity list, table not found.", "Asset IssuerIdentity", "Cant get Intra User identity list, table not found."); } // 2) Find the Redeem Point. table.setStringFilter( AssetRedeemPointIdentityDatabaseConstants .ASSET_REDEEM_POINT_IDENTITY_DEVICE_USER_PUBLIC_KEY_COLUMN_NAME, deviceUser.getPublicKey(), DatabaseFilterType.EQUAL); table.loadToMemory(); // 3) Get Redeem Point. for (DatabaseTableRecord record : table.getRecords()) { // Add records to list. list.add( new IdentityAssetRedeemPointImpl( record.getStringValue( AssetRedeemPointIdentityDatabaseConstants .ASSET_REDEEM_POINT_IDENTITY_ALIAS_COLUMN_NAME), record.getStringValue( AssetRedeemPointIdentityDatabaseConstants .ASSET_REDEEM_POINT_IDENTITY_PUBLIC_KEY_COLUMN_NAME), getAssetIssuerIdentityPrivateKey( record.getStringValue( AssetRedeemPointIdentityDatabaseConstants .ASSET_REDEEM_POINT_IDENTITY_PUBLIC_KEY_COLUMN_NAME)), getAssetIssuerProfileImagePrivateKey( record.getStringValue( AssetRedeemPointIdentityDatabaseConstants .ASSET_REDEEM_POINT_IDENTITY_DEVICE_USER_PUBLIC_KEY_COLUMN_NAME)), pluginFileSystem, pluginId)); } } catch (CantLoadTableToMemoryException e) { throw new CantListAssetRedeemPointIdentitiesException( e.getMessage(), e, "Asset Redeem Point Identity", "Cant load " + AssetRedeemPointIdentityDatabaseConstants.ASSET_REDEEM_POINT_IDENTITY_TABLE_NAME + " table in memory."); } catch (CantGetAssetRedeemPointIdentityPrivateKeyException e) { // Failure unknown. throw new CantListAssetRedeemPointIdentitiesException( e.getMessage(), e, "Asset Redeem Point Identity", "Can't get private key."); } catch (Exception e) { throw new CantListAssetRedeemPointIdentitiesException( e.getMessage(), FermatException.wrapException(e), "Asset Redeem Point Identity", "Cant get Asset Issuer identity list, unknown failure."); } // Return the list values. return list; }
/* * 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; }
public Artist getIdentityArtist(String publicKey) throws CantGetArtistIdentityException { // Setup method. Artist artist = null; DatabaseTable table; // Intra User table. // Get Asset Issuers identities list. try { /** 1) Get the table. */ table = this.database.getTable(ArtistIdentityDatabaseConstants.ARTIST_IDENTITY_TABLE_NAME); if (table == null) { /** Table not found. */ throw new CantUpdateArtistIdentityException( "Cant get Asset Issuer identity list, table not found.", "Asset IssuerIdentity", "Cant get Intra User identity list, table not found."); } // 2) Find the Identity Issuers. table.addStringFilter( ArtistIdentityDatabaseConstants.ARTIST_IDENTITY_PUBLIC_KEY_COLUMN_NAME, publicKey, DatabaseFilterType.EQUAL); table.loadToMemory(); // 3) Get Identity Issuers. for (DatabaseTableRecord record : table.getRecords()) { // Add records to list. /*artist = new IdentityAssetRedeemPointImpl( record.getStringValue(ArtistIdentityDatabaseConstants.ARTIST_IDENTITY_ALIAS_COLUMN_NAME), record.getStringValue(ArtistIdentityDatabaseConstants.ARTIST_IDENTITY_PUBLIC_KEY_COLUMN_NAME), getArtistProfileImagePrivateKey(record.getStringValue(ArtistIdentityDatabaseConstants.ARTIST_IDENTITY_PUBLIC_KEY_COLUMN_NAME)));*/ artist = new ArtistIdentityImp( record.getStringValue( ArtistIdentityDatabaseConstants.ARTIST_IDENTITY_ALIAS_COLUMN_NAME), record.getStringValue( ArtistIdentityDatabaseConstants.ARTIST_IDENTITY_PUBLIC_KEY_COLUMN_NAME), getArtistProfileImagePrivateKey( record.getStringValue( ArtistIdentityDatabaseConstants.ARTIST_IDENTITY_PUBLIC_KEY_COLUMN_NAME)), record.getStringValue( ArtistIdentityDatabaseConstants.ARTIST_IDENTITY_EXTERNAL_USER_NAME_COLUMN_NAME), record.getStringValue( ArtistIdentityDatabaseConstants .ARTIST_IDENTITY_EXTERNAL_ACCESS_TOKEN_COLUMN_NAME), ExternalPlatform.getByCode( record.getStringValue( ArtistIdentityDatabaseConstants .ARTIST_IDENTITY_EXTERNAL_PLATFORM_COLUMN_NAME)), ExposureLevel.getByCode( record.getStringValue( ArtistIdentityDatabaseConstants .ARTIST_IDENTITY_EXPOSURE_LEVEL_COLUMN_NAME)), ArtistAcceptConnectionsType.getByCode( record.getStringValue( ArtistIdentityDatabaseConstants .ARTIST_IDENTITY_ACEEPTS_CONNECTIONS_TYPE_COLUMN_NAME)), pluginFileSystem, pluginId); } } catch (CantLoadTableToMemoryException e) { throw new CantGetArtistIdentityException( e.getMessage(), e, "Asset Redeem Point Identity", "Cant load " + ArtistIdentityDatabaseConstants.ARTIST_IDENTITY_TABLE_NAME + " table in memory."); } catch (Exception e) { throw new CantGetArtistIdentityException( e.getMessage(), FermatException.wrapException(e), "Asset Redeem Point Identity", "Cant get Asset Redeem Point identity list, unknown failure."); } // Return the list values. return artist; }
/** * 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; };
public void debit(String walletPublicKey, BalanceType balanceType, BigDecimal debitAmount) throws CantRegisterDebitException, CashMoneyWalletInsufficientFundsException { DatabaseTable table; DatabaseTableRecord record; try { table = this.database.getTable(CashMoneyWalletDatabaseConstants.WALLETS_TABLE_NAME); record = this.getWalletRecordByPublicKey(walletPublicKey); if (balanceType == BalanceType.AVAILABLE) { BigDecimal available = new BigDecimal( record.getStringValue( CashMoneyWalletDatabaseConstants.WALLETS_AVAILABLE_BALANCE_COLUMN_NAME)); available = available.subtract(debitAmount); if (available.compareTo(new BigDecimal(0)) < 0) throw new CashMoneyWalletInsufficientFundsException( CashMoneyWalletInsufficientFundsException.DEFAULT_MESSAGE, null, "Debit in Cash wallet", "Cant debit available balance by this amount, insufficient funds."); record.setStringValue( CashMoneyWalletDatabaseConstants.WALLETS_AVAILABLE_BALANCE_COLUMN_NAME, available.toPlainString()); } else if (balanceType == BalanceType.BOOK) { BigDecimal book = new BigDecimal( record.getStringValue( CashMoneyWalletDatabaseConstants.WALLETS_BOOK_BALANCE_COLUMN_NAME)); book = book.subtract(debitAmount); if (book.compareTo(new BigDecimal(0)) < 0) throw new CashMoneyWalletInsufficientFundsException( CantRegisterDebitException.DEFAULT_MESSAGE, null, "Debit in Cash wallet", "Cant debit book balance by this amount, insufficient funds."); record.setStringValue( CashMoneyWalletDatabaseConstants.WALLETS_BOOK_BALANCE_COLUMN_NAME, book.toPlainString()); } else if (balanceType == BalanceType.ALL) { BigDecimal available = new BigDecimal( record.getStringValue( CashMoneyWalletDatabaseConstants.WALLETS_AVAILABLE_BALANCE_COLUMN_NAME)); BigDecimal book = new BigDecimal( record.getStringValue( CashMoneyWalletDatabaseConstants.WALLETS_BOOK_BALANCE_COLUMN_NAME)); available = available.subtract(debitAmount); book = book.subtract(debitAmount); if (available.compareTo(new BigDecimal(0)) < 0) throw new CashMoneyWalletInsufficientFundsException( CantRegisterDebitException.DEFAULT_MESSAGE, null, "Debit in Cash wallet", "Cant debit available balance by this amount, insufficient funds."); if (book.compareTo(new BigDecimal(0)) < 0) throw new CashMoneyWalletInsufficientFundsException( CantRegisterDebitException.DEFAULT_MESSAGE, null, "Debit in Cash wallet", "Cant debit book balance by this amount, insufficient funds."); record.setStringValue( CashMoneyWalletDatabaseConstants.WALLETS_AVAILABLE_BALANCE_COLUMN_NAME, available.toPlainString()); record.setStringValue( CashMoneyWalletDatabaseConstants.WALLETS_BOOK_BALANCE_COLUMN_NAME, book.toPlainString()); } table.updateRecord(record); } catch (CashMoneyWalletInconsistentTableStateException e) { throw new CantRegisterDebitException( e.getMessage(), e, "Debit in Cash wallet", "Cant debit balance. Inconsistent Table State"); } catch (CantUpdateRecordException e) { throw new CantRegisterDebitException( e.getMessage(), e, "Debit in Cash wallet", "Cant debit balance. Cant update record"); } catch (CantLoadTableToMemoryException e) { throw new CantRegisterDebitException( e.getMessage(), e, "Debit in Cash wallet", "Cant debit balance. Cant load table to memory"); } catch (CashMoneyWalletDoesNotExistException e) { throw new CantRegisterDebitException( e.getMessage(), e, "Debit in Cash wallet", "Cant debit balance. Wallet does not exist"); } }
public void credit(String walletPublicKey, BalanceType balanceType, BigDecimal creditAmount) throws CantRegisterCreditException { DatabaseTable table; DatabaseTableRecord record; try { table = this.database.getTable(CashMoneyWalletDatabaseConstants.WALLETS_TABLE_NAME); record = this.getWalletRecordByPublicKey(walletPublicKey); if (balanceType == BalanceType.AVAILABLE) { BigDecimal available = new BigDecimal( record.getStringValue( CashMoneyWalletDatabaseConstants.WALLETS_AVAILABLE_BALANCE_COLUMN_NAME)); available = available.add(creditAmount); record.setStringValue( CashMoneyWalletDatabaseConstants.WALLETS_AVAILABLE_BALANCE_COLUMN_NAME, available.toPlainString()); } else if (balanceType == BalanceType.BOOK) { BigDecimal book = new BigDecimal( record.getStringValue( CashMoneyWalletDatabaseConstants.WALLETS_BOOK_BALANCE_COLUMN_NAME)); book = book.add(creditAmount); record.setStringValue( CashMoneyWalletDatabaseConstants.WALLETS_BOOK_BALANCE_COLUMN_NAME, book.toPlainString()); } else if (balanceType == BalanceType.ALL) { BigDecimal available = new BigDecimal( record.getStringValue( CashMoneyWalletDatabaseConstants.WALLETS_AVAILABLE_BALANCE_COLUMN_NAME)); BigDecimal book = new BigDecimal( record.getStringValue( CashMoneyWalletDatabaseConstants.WALLETS_BOOK_BALANCE_COLUMN_NAME)); available = available.add(creditAmount); book = book.add(creditAmount); record.setStringValue( CashMoneyWalletDatabaseConstants.WALLETS_AVAILABLE_BALANCE_COLUMN_NAME, available.toPlainString()); record.setStringValue( CashMoneyWalletDatabaseConstants.WALLETS_BOOK_BALANCE_COLUMN_NAME, book.toPlainString()); } table.updateRecord(record); } catch (CashMoneyWalletInconsistentTableStateException e) { throw new CantRegisterCreditException( e.getMessage(), e, "Credit in Cash wallet", "Cant credit balance. Inconsistent Table State"); } catch (CantUpdateRecordException e) { throw new CantRegisterCreditException( e.getMessage(), e, "Credit in Cash wallet", "Cant credit balance. Cant update record"); } catch (CantLoadTableToMemoryException e) { throw new CantRegisterCreditException( e.getMessage(), e, "Credit in Cash wallet", "Cant credit balance. Cant load table to memory"); } catch (CashMoneyWalletDoesNotExistException e) { throw new CantRegisterCreditException( e.getMessage(), e, "Credit in Cash wallet", "Cant credit balance. Wallet does not exist"); } }