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