Search in sources :

Example 26 with Message

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

the class Engine method setDefaultCurrency.

public void setDefaultCurrency(final CurrencyNode defaultCurrency) {
    // make sure the new default is persisted if it has not been
    if (!isStored(defaultCurrency)) {
        addCurrency(defaultCurrency);
    }
    dataLock.writeLock().lock();
    try {
        final Config currencyConfig = getConfig();
        currencyConfig.setDefaultCurrency(defaultCurrency);
        getConfigDAO().update(currencyConfig);
        logInfo("Setting default currency: " + defaultCurrency);
        Message message = new Message(MessageChannel.CONFIG, ChannelEvent.CONFIG_MODIFY, this);
        message.setObject(MessageProperty.CONFIG, currencyConfig);
        messageBus.fireEvent(message);
        Account root = getRootAccount();
        // The root account holds a reference to the default currency
        root.setCurrencyNode(defaultCurrency);
        getAccountDAO().updateAccount(root);
        message = new Message(MessageChannel.ACCOUNT, ChannelEvent.ACCOUNT_MODIFY, this);
        message.setObject(MessageProperty.ACCOUNT, root);
        messageBus.fireEvent(message);
    } finally {
        dataLock.writeLock().unlock();
    }
}
Also used : Message(jgnash.engine.message.Message)

Example 27 with Message

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

the class Engine method removeSecurity.

public boolean removeSecurity(final SecurityNode node) {
    boolean status = true;
    dataLock.writeLock().lock();
    try {
        if (isCommodityNodeUsed(node)) {
            status = false;
        } else {
            // Remove all history nodes first so they are not left behind
            // A copy is made to prevent a concurrent modification error to the underlying list, Bug #208
            final List<SecurityHistoryNode> hNodes = new ArrayList<>(node.getHistoryNodes());
            hNodes.stream().filter(hNode -> !removeSecurityHistory(node, hNode.getLocalDate())).forEach(hNode -> logSevere(ResourceUtils.getString("Message.Error.HistRemoval", hNode.getLocalDate(), node.getSymbol())));
            moveObjectToTrash(node);
        }
        Message message;
        if (status) {
            message = new Message(MessageChannel.COMMODITY, ChannelEvent.SECURITY_REMOVE, this);
        } else {
            message = new Message(MessageChannel.COMMODITY, ChannelEvent.SECURITY_REMOVE_FAILED, this);
        }
        message.setObject(MessageProperty.COMMODITY, node);
        messageBus.fireEvent(message);
        return status;
    } finally {
        dataLock.writeLock().unlock();
    }
}
Also used : EngineDAO(jgnash.engine.dao.EngineDAO) CurrencyUpdateFactory(jgnash.net.currency.CurrencyUpdateFactory) ScheduledFuture(java.util.concurrent.ScheduledFuture) UpdateFactory(jgnash.net.security.UpdateFactory) TimeoutException(java.util.concurrent.TimeoutException) LockManager(jgnash.engine.concurrent.LockManager) MessageBus(jgnash.engine.message.MessageBus) BigDecimal(java.math.BigDecimal) Nullable(jgnash.util.Nullable) Future(java.util.concurrent.Future) Budget(jgnash.engine.budget.Budget) MessageProperty(jgnash.engine.message.MessageProperty) RecurringIterator(jgnash.engine.recurring.RecurringIterator) MessageChannel(jgnash.engine.message.MessageChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultDaemonThreadFactory(jgnash.util.DefaultDaemonThreadFactory) Path(java.nio.file.Path) AttachmentManager(jgnash.engine.attachment.AttachmentManager) NotNull(jgnash.util.NotNull) CancellationException(java.util.concurrent.CancellationException) Collection(java.util.Collection) Set(java.util.Set) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) PendingReminder(jgnash.engine.recurring.PendingReminder) Objects(java.util.Objects) BudgetGoal(jgnash.engine.budget.BudgetGoal) List(java.util.List) LocalDate(java.time.LocalDate) Optional(java.util.Optional) Handler(java.util.logging.Handler) Message(jgnash.engine.message.Message) ConfigDAO(jgnash.engine.dao.ConfigDAO) TrashDAO(jgnash.engine.dao.TrashDAO) LocalDateTime(java.time.LocalDateTime) ResourceUtils(jgnash.util.ResourceUtils) MonthlyReminder(jgnash.engine.recurring.MonthlyReminder) Callable(java.util.concurrent.Callable) CommodityDAO(jgnash.engine.dao.CommodityDAO) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ChannelEvent(jgnash.engine.message.ChannelEvent) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) ResourceBundle(java.util.ResourceBundle) ExecutorService(java.util.concurrent.ExecutorService) DateUtils(jgnash.time.DateUtils) IOException(java.io.IOException) AccountDAO(jgnash.engine.dao.AccountDAO) TransactionDAO(jgnash.engine.dao.TransactionDAO) Reminder(jgnash.engine.recurring.Reminder) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ChronoUnit(java.time.temporal.ChronoUnit) DayOfWeek(java.time.DayOfWeek) BudgetDAO(jgnash.engine.dao.BudgetDAO) Collections(java.util.Collections) CollectionUtils(jgnash.util.CollectionUtils) RecurringDAO(jgnash.engine.dao.RecurringDAO) Message(jgnash.engine.message.Message) ArrayList(java.util.ArrayList)

Example 28 with Message

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

the class EngineFactory method closeEngine.

public static synchronized void closeEngine(final String engineName) {
    Engine oldEngine = engineMap.get(engineName);
    DataStore oldDataStore = dataStoreMap.get(engineName);
    if (oldEngine != null) {
        // stop and wait for all working background services to complete
        oldEngine.stopBackgroundServices();
        // Post a message so the GUI knows what is going on
        Message message = new Message(MessageChannel.SYSTEM, ChannelEvent.FILE_CLOSING, oldEngine);
        MessageBus.getInstance(engineName).fireEvent(message);
        // Dump an XML backup
        if (oldEngine.createBackups() && !oldDataStore.isRemote()) {
            exportCompressedXML(engineName);
        }
        // Purge old backups
        if (oldEngine.removeOldBackups() && !oldDataStore.isRemote()) {
            removeOldCompressedXML(oldDataStore.getFileName(), oldEngine.getRetainedBackupLimit());
        }
        // Initiate a complete shutdown
        oldEngine.shutdown();
        MessageBus.getInstance(engineName).setLocal();
        oldDataStore.closeEngine();
        engineMap.remove(engineName);
        dataStoreMap.remove(engineName);
    }
}
Also used : Message(jgnash.engine.message.Message) BinaryXStreamDataStore(jgnash.engine.xstream.BinaryXStreamDataStore) XMLDataStore(jgnash.engine.xstream.XMLDataStore)

Example 29 with Message

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

the class Engine method setExchangeRate.

public void setExchangeRate(final CurrencyNode baseCurrency, final CurrencyNode exchangeCurrency, final BigDecimal rate, final LocalDate localDate) {
    Objects.requireNonNull(rate);
    assert rate.compareTo(BigDecimal.ZERO) > 0;
    if (baseCurrency.equals(exchangeCurrency)) {
        return;
    }
    // find the correct ExchangeRate and create if needed
    ExchangeRate exchangeRate = getExchangeRate(baseCurrency, exchangeCurrency);
    if (exchangeRate == null) {
        exchangeRate = new ExchangeRate(buildExchangeRateId(baseCurrency, exchangeCurrency));
        getCommodityDAO().addExchangeRate(exchangeRate);
    }
    // Remove old history of the same date if it exists
    if (exchangeRate.contains(localDate)) {
        removeExchangeRateHistory(exchangeRate, exchangeRate.getHistory(localDate));
    }
    dataLock.writeLock().lock();
    try {
        // create the new history node
        ExchangeRateHistoryNode historyNode;
        if (baseCurrency.getSymbol().compareToIgnoreCase(exchangeCurrency.getSymbol()) > 0) {
            historyNode = new ExchangeRateHistoryNode(localDate, rate);
        } else {
            historyNode = new ExchangeRateHistoryNode(localDate, BigDecimal.ONE.divide(rate, MathConstants.mathContext));
        }
        final Message message;
        boolean result = false;
        if (exchangeRate.addHistoryNode(historyNode)) {
            result = getCommodityDAO().addExchangeRateHistory(exchangeRate);
        }
        if (result) {
            message = new Message(MessageChannel.COMMODITY, ChannelEvent.EXCHANGE_RATE_ADD, this);
        } else {
            message = new Message(MessageChannel.COMMODITY, ChannelEvent.EXCHANGE_RATE_ADD_FAILED, this);
        }
        message.setObject(MessageProperty.EXCHANGE_RATE, exchangeRate);
        messageBus.fireEvent(message);
    } finally {
        dataLock.writeLock().unlock();
    }
}
Also used : Message(jgnash.engine.message.Message)

Example 30 with Message

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

the class Engine method updateCommodity.

/**
     * Modifies an existing currency node in place. The supplied node should not be a reference to the original
     *
     * @param oldNode      old CommodityNode
     * @param templateNode template CommodityNode
     * @return true if successful
     */
public boolean updateCommodity(final CommodityNode oldNode, final CommodityNode templateNode) {
    Objects.requireNonNull(oldNode);
    Objects.requireNonNull(templateNode);
    assert oldNode != templateNode;
    dataLock.writeLock().lock();
    try {
        boolean status;
        if (oldNode.getClass().equals(templateNode.getClass())) {
            oldNode.setDescription(templateNode.getDescription());
            oldNode.setPrefix(templateNode.getPrefix());
            oldNode.setScale(templateNode.getScale());
            oldNode.setSuffix(templateNode.getSuffix());
            if (templateNode instanceof SecurityNode) {
                // allow symbol to change
                oldNode.setSymbol(templateNode.getSymbol());
                ((SecurityNode) oldNode).setReportedCurrencyNode(((SecurityNode) templateNode).getReportedCurrencyNode());
                ((SecurityNode) oldNode).setQuoteSource(((SecurityNode) templateNode).getQuoteSource());
                ((SecurityNode) oldNode).setISIN(((SecurityNode) templateNode).getISIN());
            }
            status = getCommodityDAO().updateCommodityNode(oldNode);
        } else {
            status = false;
            logger.warning("Template object class did not match old object class");
        }
        final Message message;
        if (templateNode instanceof SecurityNode) {
            if (status) {
                message = new Message(MessageChannel.COMMODITY, ChannelEvent.SECURITY_MODIFY, this);
                message.setObject(MessageProperty.COMMODITY, oldNode);
            } else {
                message = new Message(MessageChannel.COMMODITY, ChannelEvent.SECURITY_MODIFY_FAILED, this);
                message.setObject(MessageProperty.COMMODITY, templateNode);
            }
        } else {
            if (status) {
                message = new Message(MessageChannel.COMMODITY, ChannelEvent.CURRENCY_MODIFY, this);
                message.setObject(MessageProperty.COMMODITY, oldNode);
            } else {
                message = new Message(MessageChannel.COMMODITY, ChannelEvent.CURRENCY_MODIFY_FAILED, this);
                message.setObject(MessageProperty.COMMODITY, templateNode);
            }
        }
        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