Search in sources :

Example 36 with Message

use of jgnash.engine.message.Message in project jgnash by ccavanaugh.

the class Engine method setAccountAttribute.

/**
     * Sets an attribute for an {@code Account}.  The key and values are string based
     *
     * @param account {@code Account} to add or update an attribute
     * @param key     the key for the attribute
     * @param value   the value of the attribute
     */
public void setAccountAttribute(final Account account, @NotNull final String key, @Nullable final String value) {
    // Throw an error if the value exceeds the maximum length
    if (value != null && value.length() > Account.MAX_ATTRIBUTE_LENGTH) {
        Message message = new Message(MessageChannel.ACCOUNT, ChannelEvent.ACCOUNT_MODIFY_FAILED, this);
        message.setObject(MessageProperty.ACCOUNT, account);
        messageBus.fireEvent(message);
        logInfo("The maximum length of the attribute was exceeded");
        return;
    }
    dataLock.writeLock().lock();
    try {
        account.setAttribute(key, value);
        getAccountDAO().updateAccount(account);
        Message message = new Message(MessageChannel.ACCOUNT, ChannelEvent.ACCOUNT_ATTRIBUTE_MODIFY, this);
        message.setObject(MessageProperty.ACCOUNT, account);
        messageBus.fireEvent(message);
        logInfo(rb.getString("Message.AccountModify"));
    } finally {
        dataLock.writeLock().unlock();
    }
}
Also used : Message(jgnash.engine.message.Message)

Example 37 with Message

use of jgnash.engine.message.Message in project jgnash by ccavanaugh.

the class Engine method addCurrency.

/**
     * Adds a new CurrencyNode to the data set.
     * <p>
     * Checks and prevents the addition of a duplicate Currencies.
     *
     * @param node new CurrencyNode to add
     * @return {@code true} if the add it successful
     */
public boolean addCurrency(final CurrencyNode node) {
    dataLock.writeLock().lock();
    try {
        boolean status = isCommodityNodeValid(node);
        if (status) {
            node.setExchangeRateDAO(exchangeRateDAO);
            if (getCurrency(node.getSymbol()) != null) {
                logger.log(Level.INFO, "Prevented addition of a duplicate CurrencyNode: {0}", node.getSymbol());
                status = false;
            }
        }
        if (status) {
            status = getCommodityDAO().addCommodity(node);
            logger.log(Level.FINE, "Adding: {0}", node.toString());
        }
        Message message;
        if (status) {
            message = new Message(MessageChannel.COMMODITY, ChannelEvent.CURRENCY_ADD, this);
        } else {
            message = new Message(MessageChannel.COMMODITY, ChannelEvent.CURRENCY_ADD_FAILED, this);
        }
        message.setObject(MessageProperty.COMMODITY, node);
        messageBus.fireEvent(message);
        return status;
    } finally {
        dataLock.writeLock().unlock();
    }
}
Also used : Message(jgnash.engine.message.Message)

Example 38 with Message

use of jgnash.engine.message.Message in project jgnash by ccavanaugh.

the class Engine method postTransactionAdd.

private void postTransactionAdd(final Transaction transaction, final boolean result) {
    for (Account a : transaction.getAccounts()) {
        Message message;
        if (result) {
            message = new Message(MessageChannel.TRANSACTION, ChannelEvent.TRANSACTION_ADD, this);
        } else {
            message = new Message(MessageChannel.TRANSACTION, ChannelEvent.TRANSACTION_ADD_FAILED, this);
        }
        message.setObject(MessageProperty.ACCOUNT, a);
        message.setObject(MessageProperty.TRANSACTION, transaction);
        messageBus.fireEvent(message);
    }
}
Also used : Message(jgnash.engine.message.Message)

Example 39 with Message

use of jgnash.engine.message.Message in project jgnash by ccavanaugh.

the class Engine method removeSecurityHistoryEvent.

/**
     * Remove a {@code SecurityHistoryEvent} from a {@code SecurityNode}.
     *
     * @param node         {@code SecurityNode} to remove history from
     * @param historyEvent the {@code SecurityHistoryEvent} to remove
     * @return {@code true} if the {@code SecurityHistoryEvent} was found and removed
     */
public boolean removeSecurityHistoryEvent(@NotNull final SecurityNode node, @NotNull final SecurityHistoryEvent historyEvent) {
    dataLock.writeLock().lock();
    boolean status;
    try {
        status = node.removeSecurityHistoryEvent(historyEvent);
        if (status) {
            // removal was a success, make sure we cleanup properly
            moveObjectToTrash(historyEvent);
            status = getCommodityDAO().removeSecurityHistoryEvent(node, historyEvent);
        }
        Message message;
        if (status) {
            clearCachedAccountBalance(node);
            message = new Message(MessageChannel.COMMODITY, ChannelEvent.SECURITY_HISTORY_EVENT_REMOVE, this);
        } else {
            message = new Message(MessageChannel.COMMODITY, ChannelEvent.SECURITY_HISTORY_EVENT_REMOVE_FAILED, this);
        }
        message.setObject(MessageProperty.COMMODITY, node);
        messageBus.fireEvent(message);
        return status;
    } finally {
        dataLock.writeLock().unlock();
    }
}
Also used : Message(jgnash.engine.message.Message)

Example 40 with Message

use of jgnash.engine.message.Message in project jgnash by ccavanaugh.

the class Engine method addSecurityHistory.

/**
     * Add a SecurityHistoryNode node to a SecurityNode.  If the SecurityNode already contains
     * an equivalent SecurityHistoryNode, the old SecurityHistoryNode is removed first.
     *
     * @param node  SecurityNode to add to
     * @param hNode SecurityHistoryNode to add
     * @return <tt>true</tt> if successful
     */
public boolean addSecurityHistory(@NotNull final SecurityNode node, @NotNull final SecurityHistoryNode hNode) {
    dataLock.writeLock().lock();
    try {
        // Remove old history of the same date if it exists
        if (node.contains(hNode.getLocalDate())) {
            if (!removeSecurityHistory(node, hNode.getLocalDate())) {
                logSevere(ResourceUtils.getString("Message.Error.HistRemoval", hNode.getLocalDate(), node.getSymbol()));
                return false;
            }
        }
        boolean status = node.addHistoryNode(hNode);
        if (status) {
            status = getCommodityDAO().addSecurityHistory(node, hNode);
        }
        Message message;
        if (status) {
            clearCachedAccountBalance(node);
            message = new Message(MessageChannel.COMMODITY, ChannelEvent.SECURITY_HISTORY_ADD, this);
        } else {
            message = new Message(MessageChannel.COMMODITY, ChannelEvent.SECURITY_HISTORY_ADD_FAILED, this);
        }
        message.setObject(MessageProperty.COMMODITY, node);
        messageBus.fireEvent(message);
        return status;
    } finally {
        dataLock.writeLock().unlock();
    }
}
Also used : Message(jgnash.engine.message.Message)

Aggregations

Message (jgnash.engine.message.Message)46 MessageBus (jgnash.engine.message.MessageBus)9 List (java.util.List)7 Objects (java.util.Objects)7 MessageChannel (jgnash.engine.message.MessageChannel)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 ResourceBundle (java.util.ResourceBundle)6 Level (java.util.logging.Level)6 Logger (java.util.logging.Logger)6 ResourceUtils (jgnash.util.ResourceUtils)6 BigDecimal (java.math.BigDecimal)5 Optional (java.util.Optional)5 ExecutionException (java.util.concurrent.ExecutionException)5 Budget (jgnash.engine.budget.Budget)5 ChannelEvent (jgnash.engine.message.ChannelEvent)5 MessageProperty (jgnash.engine.message.MessageProperty)5 LocalDate (java.time.LocalDate)4 Collections (java.util.Collections)4 HashSet (java.util.HashSet)4