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();
}
}
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;
}
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();
}
}
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));
}
}
}
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;
}
Aggregations