use of jgnash.engine.Transaction in project jgnash by ccavanaugh.
the class CsvExport method exportAccount.
public static void exportAccount(final Account account, final LocalDate startDate, final LocalDate endDate, final File file) {
Objects.requireNonNull(account);
Objects.requireNonNull(startDate);
Objects.requireNonNull(endDate);
Objects.requireNonNull(file);
// force a correct file extension
final String fileName = FileUtils.stripFileExtension(file.getAbsolutePath()) + ".csv";
try (final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(Files.newOutputStream(Paths.get(fileName)), StandardCharsets.UTF_8);
final AutoCloseableCSVWriter writer = new AutoCloseableCSVWriter(new BufferedWriter(outputStreamWriter))) {
// write UTF-8 byte order mark to the file for easier imports
outputStreamWriter.write('');
writer.writeNextRow("Account", "Number", "Debit", "Credit", "Balance", "Date", "Timestamp", "Memo", "Payee", "Reconciled");
// write the transactions
final List<Transaction> transactions = account.getTransactions(startDate, endDate);
final DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder().appendValue(YEAR, 4).appendValue(MONTH_OF_YEAR, 2).appendValue(DAY_OF_MONTH, 2).toFormatter();
final DateTimeFormatter timestampFormatter = new DateTimeFormatterBuilder().appendValue(YEAR, 4).appendLiteral('-').appendValue(MONTH_OF_YEAR, 2).appendLiteral('-').appendValue(DAY_OF_MONTH, 2).appendLiteral(' ').appendValue(HOUR_OF_DAY, 2).appendLiteral(':').appendValue(MINUTE_OF_HOUR, 2).appendLiteral(':').appendValue(SECOND_OF_MINUTE, 2).toFormatter();
for (final Transaction transaction : transactions) {
final String date = dateTimeFormatter.format(transaction.getLocalDate());
final String timeStamp = timestampFormatter.format(transaction.getTimestamp());
final String credit = transaction.getAmount(account).compareTo(BigDecimal.ZERO) == -1 ? "" : transaction.getAmount(account).abs().toPlainString();
final String debit = transaction.getAmount(account).compareTo(BigDecimal.ZERO) == 1 ? "" : transaction.getAmount(account).abs().toPlainString();
final String balance = account.getBalanceAt(transaction).toPlainString();
final String reconciled = transaction.getReconciled(account) == ReconciledState.NOT_RECONCILED ? Boolean.FALSE.toString() : Boolean.TRUE.toString();
writer.writeNextRow(account.getName(), transaction.getNumber(), debit, credit, balance, date, timeStamp, transaction.getMemo(), transaction.getPayee(), reconciled);
}
} catch (final IOException e) {
Logger.getLogger(CsvExport.class.getName()).log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}
use of jgnash.engine.Transaction in project jgnash by ccavanaugh.
the class OfxExport method writeInvestmentTransactions.
/**
* Writes all investment account transactions within the date range
*/
private void writeInvestmentTransactions() {
for (final Transaction transaction : account.getTransactions(startDate, endDate)) {
if (transaction instanceof InvestmentTransaction) {
final InvestmentTransaction invTransaction = (InvestmentTransaction) transaction;
switch(invTransaction.getTransactionType()) {
case ADDSHARE:
case BUYSHARE:
writeBuyStockTransaction(invTransaction);
break;
case REMOVESHARE:
case SELLSHARE:
writeSellStockTransaction(invTransaction);
break;
case DIVIDEND:
writeDividendTransaction(invTransaction);
break;
case REINVESTDIV:
writeReinvestStockTransaction(invTransaction);
break;
default:
break;
}
} else {
// bank transaction, write it
indentedWriter.println(wrapOpen(INVBANKTRAN), indentLevel++);
writeBankTransaction(transaction);
indentedWriter.println(wrapClose(INVBANKTRAN), --indentLevel);
}
}
}
use of jgnash.engine.Transaction in project jgnash by ccavanaugh.
the class MessageBusClient method processRemoteMessage.
/**
* Takes a remote message and forces remote updates before sending the message to the MessageBus to notify UI
* components of changes.
*
* @param message Message to process and send
*/
private void processRemoteMessage(final Message message) {
logger.fine("processing a remote message");
final Engine engine = EngineFactory.getEngine(name);
Objects.requireNonNull(engine);
if (message.getChannel() == MessageChannel.ACCOUNT) {
final Account account = message.getObject(MessageProperty.ACCOUNT);
switch(message.getEvent()) {
case ACCOUNT_ADD:
case ACCOUNT_REMOVE:
engine.refresh(account);
message.setObject(MessageProperty.ACCOUNT, engine.getAccountByUuid(account.getUuid()));
engine.refresh(account.getParent());
break;
case ACCOUNT_MODIFY:
case ACCOUNT_SECURITY_ADD:
case ACCOUNT_SECURITY_REMOVE:
case ACCOUNT_VISIBILITY_CHANGE:
engine.refresh(account);
message.setObject(MessageProperty.ACCOUNT, engine.getAccountByUuid(account.getUuid()));
break;
default:
break;
}
}
if (message.getChannel() == MessageChannel.BUDGET) {
final Budget budget = message.getObject(MessageProperty.BUDGET);
switch(message.getEvent()) {
case BUDGET_ADD:
case BUDGET_UPDATE:
case BUDGET_REMOVE:
case BUDGET_GOAL_UPDATE:
engine.refresh(budget);
message.setObject(MessageProperty.BUDGET, engine.getBudgetByUuid(budget.getUuid()));
break;
default:
break;
}
}
if (message.getChannel() == MessageChannel.COMMODITY) {
switch(message.getEvent()) {
case CURRENCY_ADD:
case CURRENCY_MODIFY:
final CommodityNode currency = message.getObject(MessageProperty.COMMODITY);
engine.refresh(currency);
message.setObject(MessageProperty.COMMODITY, engine.getCurrencyNodeByUuid(currency.getUuid()));
break;
case SECURITY_ADD:
case SECURITY_MODIFY:
case SECURITY_HISTORY_ADD:
case SECURITY_HISTORY_REMOVE:
final CommodityNode node = message.getObject(MessageProperty.COMMODITY);
engine.refresh(node);
message.setObject(MessageProperty.COMMODITY, engine.getSecurityNodeByUuid(node.getUuid()));
break;
case EXCHANGE_RATE_ADD:
case EXCHANGE_RATE_REMOVE:
final ExchangeRate rate = message.getObject(MessageProperty.EXCHANGE_RATE);
engine.refresh(rate);
message.setObject(MessageProperty.EXCHANGE_RATE, engine.getExchangeRateByUuid(rate.getUuid()));
break;
default:
break;
}
}
if (message.getChannel() == MessageChannel.CONFIG) {
switch(message.getEvent()) {
case CONFIG_MODIFY:
final Config config = message.getObject(MessageProperty.CONFIG);
engine.refresh(config);
message.setObject(MessageProperty.CONFIG, engine.getStoredObjectByUuid(Config.class, config.getUuid()));
break;
default:
break;
}
}
if (message.getChannel() == MessageChannel.REMINDER) {
switch(message.getEvent()) {
case REMINDER_ADD:
case REMINDER_REMOVE:
final Reminder reminder = message.getObject(MessageProperty.REMINDER);
engine.refresh(reminder);
message.setObject(MessageProperty.REMINDER, engine.getReminderByUuid(reminder.getUuid()));
break;
default:
break;
}
}
if (message.getChannel() == MessageChannel.TRANSACTION) {
switch(message.getEvent()) {
case TRANSACTION_ADD:
case TRANSACTION_REMOVE:
final Transaction transaction = message.getObject(MessageProperty.TRANSACTION);
engine.refresh(transaction);
message.setObject(MessageProperty.TRANSACTION, engine.getTransactionByUuid(transaction.getUuid()));
final Account account = message.getObject(MessageProperty.ACCOUNT);
engine.refresh(account);
message.setObject(MessageProperty.ACCOUNT, engine.getAccountByUuid(account.getUuid()));
break;
default:
break;
}
}
/* Flag the message as remote */
message.setRemote();
logger.fine("fire remote message");
MessageBus.getInstance(name).fireEvent(message);
}
use of jgnash.engine.Transaction in project jgnash by ccavanaugh.
the class RegisterActions method deleteTransactionAction.
static void deleteTransactionAction(final Transaction... transactions) {
if (Options.confirmOnTransactionDeleteProperty().get()) {
if (confirmTransactionRemoval(transactions.length).getButtonData().isCancelButton()) {
return;
}
}
// Move to a thread so the UI does not block
Thread thread = new Thread(() -> {
final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
if (engine != null) {
for (final Transaction transaction : transactions) {
if (engine.removeTransaction(transaction)) {
if (transaction.getAttachment() != null) {
if (confirmAttachmentDeletion().getButtonData().isCancelButton()) {
if (!engine.removeAttachment(transaction.getAttachment())) {
StaticUIMethods.displayError(ResourceUtils.getString("Message.Error.DeleteAttachment", transaction.getAttachment()));
}
}
}
}
}
}
});
thread.start();
}
use of jgnash.engine.Transaction in project jgnash by ccavanaugh.
the class RegisterActions method duplicateTransaction.
static void duplicateTransaction(final Account account, final List<Transaction> transactions) {
final ResourceBundle resourceBundle = ResourceUtils.getBundle();
final String eftNumber = resourceBundle.getString("Item.EFT");
for (final Transaction transaction : transactions) {
try {
final Transaction clone = (Transaction) transaction.clone();
clone.setDate(LocalDate.now());
if (!clone.getNumber().isEmpty() && !clone.getNumber().equals(eftNumber)) {
// may return an empty string
final String nextTransactionNumber = account.getNextTransactionNumber();
if (!nextTransactionNumber.isEmpty()) {
clone.setNumber(nextTransactionNumber);
}
}
if (transaction instanceof InvestmentTransaction) {
InvestmentTransactionDialog.show(account, clone, RegisterActions::addTransaction);
} else {
TransactionDialog.showAndWait(account, clone, RegisterActions::addTransaction);
}
} catch (final CloneNotSupportedException e) {
Logger.getLogger(RegisterActions.class.getName()).log(Level.SEVERE, e.getMessage(), e);
}
}
}
Aggregations