Search in sources :

Example 31 with Message

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

the class Engine method setRemoveOldBackups.

public void setRemoveOldBackups(final boolean removeOldBackups) {
    dataLock.writeLock().lock();
    try {
        final Config backupConfig = getConfig();
        backupConfig.setRemoveOldBackups(removeOldBackups);
        getConfigDAO().update(backupConfig);
        // clear stale cached reference
        config = null;
        Message message = new Message(MessageChannel.CONFIG, ChannelEvent.CONFIG_MODIFY, this);
        message.setObject(MessageProperty.CONFIG, backupConfig);
        messageBus.fireEvent(message);
    } finally {
        dataLock.writeLock().unlock();
    }
}
Also used : Message(jgnash.engine.message.Message)

Example 32 with Message

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

the class Engine method addReminder.

public boolean addReminder(final Reminder reminder) {
    Objects.requireNonNull(reminder.getUuid());
    boolean result = getReminderDAO().addReminder(reminder);
    Message message;
    if (result) {
        message = new Message(MessageChannel.REMINDER, ChannelEvent.REMINDER_ADD, this);
    } else {
        message = new Message(MessageChannel.REMINDER, ChannelEvent.REMINDER_ADD_FAILED, this);
    }
    message.setObject(MessageProperty.REMINDER, reminder);
    messageBus.fireEvent(message);
    return result;
}
Also used : Message(jgnash.engine.message.Message)

Example 33 with Message

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

the class Engine method addAccountSecurity.

/**
     * Adds a SecurityNode from a InvestmentAccount.
     *
     * @param account destination account
     * @param node    SecurityNode to add
     * @return true if add was successful
     */
public boolean addAccountSecurity(final Account account, final SecurityNode node) {
    dataLock.writeLock().lock();
    try {
        Message message;
        boolean result = account.addSecurity(node);
        if (result) {
            result = getAccountDAO().addAccountSecurity(account, node);
        }
        if (result) {
            message = new Message(MessageChannel.ACCOUNT, ChannelEvent.ACCOUNT_SECURITY_ADD, this);
        } else {
            message = new Message(MessageChannel.ACCOUNT, ChannelEvent.ACCOUNT_SECURITY_ADD_FAILED, this);
        }
        message.setObject(MessageProperty.ACCOUNT, account);
        message.setObject(MessageProperty.COMMODITY, node);
        messageBus.fireEvent(message);
        return result;
    } finally {
        dataLock.writeLock().unlock();
    }
}
Also used : Message(jgnash.engine.message.Message)

Example 34 with Message

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

the class Engine method emptyTrash.

/**
     * Empty the trash if any objects are older than the defined time.
     */
private void emptyTrash() {
    if (backGroundCounter.incrementAndGet() == 1) {
        messageBus.fireEvent(new Message(MessageChannel.SYSTEM, ChannelEvent.BACKGROUND_PROCESS_STARTED, Engine.this));
    }
    dataLock.writeLock().lock();
    try {
        logger.info("Checking for trash");
        final List<TrashObject> trash = getTrashDAO().getTrashObjects();
        /* always sort by the timestamp of the trash object to prevent
             * foreign key removal exceptions when multiple related accounts
             * or objects are removed */
        Collections.sort(trash);
        if (trash.isEmpty()) {
            logger.info("No trash was found");
        }
        trash.stream().filter(o -> ChronoUnit.MILLIS.between(o.getDate(), LocalDateTime.now()) >= MAXIMUM_TRASH_AGE).forEach(o -> getTrashDAO().remove(o));
    } finally {
        dataLock.writeLock().unlock();
        if (backGroundCounter.decrementAndGet() == 0) {
            messageBus.fireEvent(new Message(MessageChannel.SYSTEM, ChannelEvent.BACKGROUND_PROCESS_STOPPED, Engine.this));
        }
    }
}
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)

Example 35 with Message

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

the class EngineFactory method bootLocalEngine.

/**
     * Boots a local Engine for a file. If the file does not exist, it will be created. Otherwise it will be loaded.
     * If successful, a new {@code Engine} instance will be returned.
     *
     * @param fileName   filename to load or create
     * @param engineName engine identifier
     * @param password   password for the file
     * @param type       {@code DataStoreType} type to use for storage
     * @return new {@code Engine} instance if successful
     * @see Engine
     * @see DataStoreType
     */
public static synchronized Engine bootLocalEngine(final String fileName, final String engineName, final char[] password, final DataStoreType type) throws IOException {
    Instant start = Instant.now();
    MessageBus.getInstance(engineName).setLocal();
    final DataStore dataStore = type.getDataStore();
    // If the file exist, check for need to manually upgrade first
    if (Files.exists(Paths.get(fileName)) && type == DataStoreType.HSQL_DATABASE) {
        if (SqlUtils.useOldPersistenceUnit(fileName, password)) {
            throw new IOException("HyperSQL files must be converted before opening in this release.");
        }
    }
    final Engine engine = dataStore.getLocalEngine(fileName, engineName, password);
    if (engine != null) {
        logger.info(ResourceUtils.getString("Message.EngineStart"));
        engineMap.put(engineName, engine);
        dataStoreMap.put(engineName, dataStore);
        Message message = new Message(MessageChannel.SYSTEM, ChannelEvent.FILE_LOAD_SUCCESS, engine);
        MessageBus.getInstance(engineName).fireEvent(message);
        if (engineName.equals(EngineFactory.DEFAULT)) {
            Preferences pref = Preferences.userNodeForPackage(EngineFactory.class);
            pref.putBoolean(USED_PASSWORD, password.length > 0);
            pref.put(LAST_DATABASE, fileName);
            pref.putBoolean(LAST_REMOTE, false);
        }
        System.out.println("Boot time was " + ChronoUnit.MILLIS.between(start, Instant.now()) + " milliseconds");
    }
    return engine;
}
Also used : Message(jgnash.engine.message.Message) Instant(java.time.Instant) BinaryXStreamDataStore(jgnash.engine.xstream.BinaryXStreamDataStore) XMLDataStore(jgnash.engine.xstream.XMLDataStore) IOException(java.io.IOException) Preferences(java.util.prefs.Preferences)

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