jgnash.engine
Class Engine

java.lang.Object
  extended by jgnash.engine.Engine

public class Engine
extends java.lang.Object

Engine class

When objects are removed, they are wrapped in a TrashObject so they may still be referenced for messaging and cleanup operations. After a predefined period of time, they are permanently removed.

Version:
$Id$
Author:
Craig Cavanaugh

Field Summary
private  java.util.concurrent.locks.ReentrantReadWriteLock accountLock
           
private  java.lang.String accountSeparator
          Cached for performance
private  java.util.concurrent.locks.ReentrantReadWriteLock commodityLock
           
private  Config config
          Cached for performance
private  java.util.concurrent.locks.ReentrantReadWriteLock configLock
           
static float CURRENT_VERSION
          Current version for the file format
private  EngineDAO eDAO
           
private  java.util.concurrent.locks.ReentrantReadWriteLock engineLock
           
private  ExchangeRateDAO exchangeRateDAO
           
private  java.util.logging.Logger logger
           
private static long MAXIMUM_TRASH_AGE
           
private static java.math.MathContext mc
           
private  MessageBus messageBus
          All engine instances will share the same message bus
private  java.lang.String name
          Named identifier for this engine instance
private  Resource rb
           
private  RootAccount rootAccount
          Cached for performance
private static java.math.RoundingMode roundingMode
           
private  java.util.concurrent.ScheduledExecutorService trashExecutor
           
private  java.lang.String uuid
          Unique identifier for this engine instance
 
Constructor Summary
Engine(EngineDAO eDAO, java.lang.String name)
           
 
Method Summary
 boolean addAccount(Account parent, Account child)
          Adds a new account
private  boolean addAccountSecurity(Account account, SecurityNode node)
          Adds a SecurityNode from a InvestmentAccount
 boolean addCommodity(CommodityNode node)
           
 boolean addReminder(Reminder reminder)
           
 boolean addSecurityHistory(SecurityNode node, SecurityHistoryNode hNode)
          Add a SecurityHistoryNode node to a SecurityNode
 boolean addTransaction(Transaction transaction)
           
(package private) static java.lang.String buildExchangeRateId(CurrencyNode baseCurrency, CurrencyNode exchangeCurrency)
           
private  void checkAndCorrect()
          Corrects minor issues with a database that may occur because of prior bugs
private  void clearCachedAccountBalance(Account account)
          Clears an Accounts cached balance and recursively works up the tree to the root.
private  void clearCachedAccountBalance(SecurityNode node)
          Forces all investment accounts containing the security to clear the cached account balance and reconciled account balance and recalculate when queried.
private  void clearObsoleteExchangeRates()
           
private  void emptyTrash()
          Empty the trash if any objects are older than the defined time
 Account getAccountByName(java.lang.String accountName)
          Search for an account with a matching account number
 Account getAccountByUuid(java.lang.String id)
           
private  AccountDAO getAccountDAO()
           
 java.util.List<Account> getAccountList()
          Returns a list of all Accounts excluding the rootAccount
 java.lang.String getAccountSeparator()
           
 java.util.Set<CurrencyNode> getActiveCurrencies()
          Returns an array of currencies being used in accounts
 java.util.List<Account> getBankAccountList()
          Returns a list of all accounts excluding the rootAccount and IncomeAccounts and ExpenseAccounts
(package private)  CurrencyNode[] getBaseCurrencies(java.lang.String exchangeRateId)
           
private  CommodityDAO getCommodityDAO()
           
private  Config getConfig()
           
private  ConfigDAO getConfigDAO()
           
static java.math.MathContext getContext()
           
 java.util.List<CurrencyNode> getCurrencies()
           
 CurrencyNode getCurrency(java.lang.String symbol)
          Returns a CurrencyNode given the symbol.
 CurrencyNode getCurrency(java.lang.String symbol, java.util.Locale locale)
          Returns a CurrencyNode given the symbol and Locale.
 CurrencyNode getDefaultCurrency()
           
 ExchangeRate getExchangeRate(CurrencyNode baseCurrency, CurrencyNode exchangeCurrency)
           
 java.util.List<Account> getExpenseAccountList()
          Returns a list of ExpenseAccounts excluding the rootExpenseAccount
 java.util.List<Account> getIncomeAccountList()
          Returns a list of IncomeAccounts excluding the rootIncomeAccount
 java.util.List<Account> getInvestmentAccountList()
          Returns a list of all accounts excluding the rootAccount and IncomeAccounts and ExpenseAccounts
 java.util.Set<Account> getInvestmentAccountList(SecurityNode node)
          Returns a list of investment accounts that use the given security node
static java.math.BigDecimal getMarketPrice(java.util.Collection<Transaction> transactions, SecurityNode node, CurrencyNode baseCurrency, java.util.Date date)
           
 java.lang.String getName()
           
 java.util.List<PendingReminder> getPendingReminders()
           
private  RecurringDAO getReminderDAO()
           
 java.util.List<Reminder> getReminders()
          Returns a list of reminders
 RootAccount getRootAccount()
          Return the root account
static java.math.RoundingMode getRoundingMode()
           
 java.util.List<SecurityNode> getSecurities()
           
 SecurityNode getSecurity(java.lang.String symbol)
          Find a SecurityNode given it's symbol
 java.util.List<SecurityHistoryNode> getSecurityHistory(SecurityNode node)
           
 StoredObject getStoredObjectByUuid(java.lang.String id)
           
 java.util.Collection<StoredObject> getStoredObjects()
          Returns a Collection of all StoredObjects in a consistent order.
private  TransactionDAO getTransactionDAO()
           
 java.util.List<java.lang.String> getTransactionNumberList()
           
 java.util.List<Transaction> getTransactions()
          Get all transactions
private  TrashDAO getTrashDAO()
           
 java.lang.String getUuid()
          Returns the unique identifier for this engine instance
private  void initialize()
          Creates a RootAccount and default currency only if necessary
private  boolean isCommodityNodeUsed(CommodityNode node)
           
private  boolean isCommodityNodeValid(CommodityNode node)
          Validate a CommodityNode for correctness
 boolean isStored(StoredObject object)
          Determine if a StoredObject is persisted in the database
 boolean isTransactionValid(Transaction transaction)
           
private  void logInfo(java.lang.String message)
          Log a informational message
private  void logSevere(java.lang.String message)
          Log a severe message
private  void logWarning(java.lang.String message)
          Log a warning message
 boolean modifyAccount(Account template, Account account)
          Modifies an existing account given an account as a template.
 boolean moveAccount(Account account, Account newParent)
          Move an account to a new parent account
private  void moveObjectToTrash(StoredObject object)
           
private  void postTransactionAdd(Transaction transaction, boolean result)
           
private  void postTransactionRemove(Transaction transaction, boolean result)
           
 void refreshAccount(Account account)
           
 void refreshCommodity(CommodityNode node)
           
 void refreshExchangeRate(ExchangeRate rate)
           
 void refreshReminder(Reminder reminder)
           
 void refreshTransaction(Transaction transaction)
           
 boolean removeAccount(Account account)
          Removes an existing account given it's ID.
private  boolean removeAccountSecurity(Account account, SecurityNode node)
          Removes a SecurityNode from an InvestmentAccount
 boolean removeCommodity(CommodityNode node)
           
private  void removeExchangeRate(ExchangeRate rate)
           
 void removeExchangeRateHistory(ExchangeRate exchangeRate, ExchangeRateHistoryNode history)
           
 boolean removeReminder(Reminder reminder)
           
 boolean removeSecurityHistory(SecurityNode node, SecurityHistoryNode hNode)
           
 boolean removeTransaction(Transaction transaction)
           
 void setAccountNumber(Account account, java.lang.String number)
          Sets the account number of an account
 void setAccountSeparator(java.lang.String separator)
           
 boolean setAmortizeObject(Account account, AmortizeObject amortizeObject)
          Sets the amortize object of an account
 void setDefaultCurrency(CurrencyNode defaultCurrency)
           
 void setExchangeRate(CurrencyNode baseCurrency, CurrencyNode exchangeCurrency, java.math.BigDecimal rate)
           
 void setExchangeRate(CurrencyNode baseCurrency, CurrencyNode exchangeCurrency, java.math.BigDecimal rate, java.util.Date date)
           
 void setTransactionNumberList(java.util.List<java.lang.String> list)
           
 void setTransactionReconciled(Transaction transaction, Account account, ReconciledState state)
          Changes the reconciled state of a transaction
(package private)  void shutdown()
           
 boolean toggleAccountVisibility(Account account)
          Toggles the visibility of an account given its ID.
 boolean updateAccountSecurities(Account acc, java.util.Collection<SecurityNode> list)
          Update an account's securities list.
 boolean updateCommodity(CommodityNode oldNode, CommodityNode templateNode)
          Modifies an existing currency node in place.
 void updateReminder(Reminder reminder)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CURRENT_VERSION

public static final float CURRENT_VERSION
Current version for the file format

See Also:
Constant Field Values

roundingMode

private static final java.math.RoundingMode roundingMode

mc

private static final java.math.MathContext mc

rb

private final Resource rb

logger

private final java.util.logging.Logger logger

messageBus

private MessageBus messageBus
All engine instances will share the same message bus


config

private Config config
Cached for performance


rootAccount

private RootAccount rootAccount
Cached for performance


exchangeRateDAO

private ExchangeRateDAO exchangeRateDAO

accountLock

private final java.util.concurrent.locks.ReentrantReadWriteLock accountLock

commodityLock

private final java.util.concurrent.locks.ReentrantReadWriteLock commodityLock

configLock

private final java.util.concurrent.locks.ReentrantReadWriteLock configLock

engineLock

private final java.util.concurrent.locks.ReentrantReadWriteLock engineLock

eDAO

private EngineDAO eDAO

accountSeparator

private java.lang.String accountSeparator
Cached for performance


trashExecutor

private java.util.concurrent.ScheduledExecutorService trashExecutor

MAXIMUM_TRASH_AGE

private static final long MAXIMUM_TRASH_AGE
See Also:
Constant Field Values

name

private final java.lang.String name
Named identifier for this engine instance


uuid

private final java.lang.String uuid
Unique identifier for this engine instance

Constructor Detail

Engine

public Engine(EngineDAO eDAO,
              java.lang.String name)
Method Detail

initialize

private void initialize()
Creates a RootAccount and default currency only if necessary


checkAndCorrect

private void checkAndCorrect()
Corrects minor issues with a database that may occur because of prior bugs


clearObsoleteExchangeRates

private void clearObsoleteExchangeRates()

removeExchangeRate

private void removeExchangeRate(ExchangeRate rate)

shutdown

void shutdown()

getName

public java.lang.String getName()

getContext

public static java.math.MathContext getContext()

getRoundingMode

public static java.math.RoundingMode getRoundingMode()

getAccountDAO

private AccountDAO getAccountDAO()

getCommodityDAO

private CommodityDAO getCommodityDAO()

getConfigDAO

private ConfigDAO getConfigDAO()

getReminderDAO

private RecurringDAO getReminderDAO()

getTransactionDAO

private TransactionDAO getTransactionDAO()

getTrashDAO

private TrashDAO getTrashDAO()

moveObjectToTrash

private void moveObjectToTrash(StoredObject object)

emptyTrash

private void emptyTrash()
Empty the trash if any objects are older than the defined time


addReminder

public boolean addReminder(Reminder reminder)

removeReminder

public boolean removeReminder(Reminder reminder)

getReminders

public java.util.List<Reminder> getReminders()
Returns a list of reminders

Returns:
List of reminders

getPendingReminders

public java.util.List<PendingReminder> getPendingReminders()

getStoredObjectByUuid

public StoredObject getStoredObjectByUuid(java.lang.String id)

getStoredObjects

public java.util.Collection<StoredObject> getStoredObjects()
Returns a Collection of all StoredObjects in a consistent order. StoredObjects marked for removal are filtered from the collection.

Returns:
Collection of StoredObjects
See Also:
Collection, StoredObjectComparator

isCommodityNodeValid

private boolean isCommodityNodeValid(CommodityNode node)
Validate a CommodityNode for correctness

Parameters:
node - CommodityNode to validate
Returns:
true if valid

addCommodity

public boolean addCommodity(CommodityNode node)

addSecurityHistory

public boolean addSecurityHistory(SecurityNode node,
                                  SecurityHistoryNode hNode)
Add a SecurityHistoryNode node to a SecurityNode

Parameters:
node - SecurityNode to add to
hNode - SecurityHistoryNode to add
Returns:
true if successful

getInvestmentAccountList

public java.util.Set<Account> getInvestmentAccountList(SecurityNode node)
Returns a list of investment accounts that use the given security node

Parameters:
node - security node
Returns:
list of investment accounts

getMarketPrice

public static java.math.BigDecimal getMarketPrice(java.util.Collection<Transaction> transactions,
                                                  SecurityNode node,
                                                  CurrencyNode baseCurrency,
                                                  java.util.Date date)

clearCachedAccountBalance

private void clearCachedAccountBalance(SecurityNode node)
Forces all investment accounts containing the security to clear the cached account balance and reconciled account balance and recalculate when queried.

Parameters:
node - SecurityNode that was changed

clearCachedAccountBalance

private void clearCachedAccountBalance(Account account)
Clears an Accounts cached balance and recursively works up the tree to the root.

Parameters:
account - Account to clear

buildExchangeRateId

static java.lang.String buildExchangeRateId(CurrencyNode baseCurrency,
                                            CurrencyNode exchangeCurrency)

getBaseCurrencies

CurrencyNode[] getBaseCurrencies(java.lang.String exchangeRateId)

getActiveCurrencies

public java.util.Set<CurrencyNode> getActiveCurrencies()
Returns an array of currencies being used in accounts

Returns:
Set of CurrencyNodes

getCurrency

public CurrencyNode getCurrency(java.lang.String symbol,
                                java.util.Locale locale)
Returns a CurrencyNode given the symbol and Locale. This will not generate a new CurrencyNode. It must be explicitly created and added.

Parameters:
symbol - Currency symbol
locale - Currency Locale
Returns:
null if the CurrencyNode as not been defined

getCurrency

public CurrencyNode getCurrency(java.lang.String symbol)
Returns a CurrencyNode given the symbol. This will not generate a new CurrencyNode. It must be explicitly created and added.

getCurrencyNode(String symbol, Locale locale) is the preferred method of obtaining an established currency

Parameters:
symbol - Currency symbol
Returns:
null if the CurrencyNode as not been defined

getCurrencies

public java.util.List<CurrencyNode> getCurrencies()

getSecurityHistory

public java.util.List<SecurityHistoryNode> getSecurityHistory(SecurityNode node)

getExchangeRate

public ExchangeRate getExchangeRate(CurrencyNode baseCurrency,
                                    CurrencyNode exchangeCurrency)

getSecurities

public java.util.List<SecurityNode> getSecurities()

getSecurity

public SecurityNode getSecurity(java.lang.String symbol)
Find a SecurityNode given it's symbol

Parameters:
symbol - symbol of security to find
Returns:
null if not found

isCommodityNodeUsed

private boolean isCommodityNodeUsed(CommodityNode node)

removeCommodity

public boolean removeCommodity(CommodityNode node)

removeSecurityHistory

public boolean removeSecurityHistory(SecurityNode node,
                                     SecurityHistoryNode hNode)

setDefaultCurrency

public void setDefaultCurrency(CurrencyNode defaultCurrency)

getConfig

private Config getConfig()

getDefaultCurrency

public CurrencyNode getDefaultCurrency()

setExchangeRate

public void setExchangeRate(CurrencyNode baseCurrency,
                            CurrencyNode exchangeCurrency,
                            java.math.BigDecimal rate)

setExchangeRate

public void setExchangeRate(CurrencyNode baseCurrency,
                            CurrencyNode exchangeCurrency,
                            java.math.BigDecimal rate,
                            java.util.Date date)

removeExchangeRateHistory

public void removeExchangeRateHistory(ExchangeRate exchangeRate,
                                      ExchangeRateHistoryNode history)

updateCommodity

public boolean updateCommodity(CommodityNode oldNode,
                               CommodityNode templateNode)
Modifies an existing currency node in place. The supplied node should not be a reference to the original

Parameters:
oldNode - old CommodityNode
templateNode - template CommodityNode
Returns:
true if successful

updateReminder

public void updateReminder(Reminder reminder)

logInfo

private void logInfo(java.lang.String message)
Log a informational message

Parameters:
message - message to display

logWarning

private void logWarning(java.lang.String message)
Log a warning message

Parameters:
message - message to display

logSevere

private void logSevere(java.lang.String message)
Log a severe message

Parameters:
message - message to display

setAccountSeparator

public void setAccountSeparator(java.lang.String separator)

getAccountSeparator

public java.lang.String getAccountSeparator()

getAccountList

public java.util.List<Account> getAccountList()
Returns a list of all Accounts excluding the rootAccount

Returns:
List of accounts

getAccountByUuid

public Account getAccountByUuid(java.lang.String id)

getAccountByName

public Account getAccountByName(java.lang.String accountName)
Search for an account with a matching account number

Parameters:
accountName - Account name to search for. Must not be null
Returns:
The matching account. null if not found.

getIncomeAccountList

public java.util.List<Account> getIncomeAccountList()
Returns a list of IncomeAccounts excluding the rootIncomeAccount

Returns:
List of income accounts

getExpenseAccountList

public java.util.List<Account> getExpenseAccountList()
Returns a list of ExpenseAccounts excluding the rootExpenseAccount

Returns:
List if expense accounts

getBankAccountList

public java.util.List<Account> getBankAccountList()
Returns a list of all accounts excluding the rootAccount and IncomeAccounts and ExpenseAccounts

Returns:
List of bank accounts

getInvestmentAccountList

public java.util.List<Account> getInvestmentAccountList()
Returns a list of all accounts excluding the rootAccount and IncomeAccounts and ExpenseAccounts

Returns:
List of investment accounts

refreshAccount

public void refreshAccount(Account account)

refreshCommodity

public void refreshCommodity(CommodityNode node)

refreshExchangeRate

public void refreshExchangeRate(ExchangeRate rate)

refreshReminder

public void refreshReminder(Reminder reminder)

refreshTransaction

public void refreshTransaction(Transaction transaction)

addAccount

public boolean addAccount(Account parent,
                          Account child)
Adds a new account

Parameters:
parent - The parent account
child - A new Account object
Returns:
true if successful

getRootAccount

public RootAccount getRootAccount()
Return the root account

Returns:
RootAccount

moveAccount

public boolean moveAccount(Account account,
                           Account newParent)
Move an account to a new parent account

Parameters:
account - account to move
newParent - the new parent account
Returns:
true if successful

modifyAccount

public boolean modifyAccount(Account template,
                             Account account)
Modifies an existing account given an account as a template. The type of the account cannot be changed.

Parameters:
template - The Account object to use as a template
account - The existing account
Returns:
true if successful

setAccountNumber

public void setAccountNumber(Account account,
                             java.lang.String number)
Sets the account number of an account

Parameters:
account - account to change
number - new account number

removeAccount

public boolean removeAccount(Account account)
Removes an existing account given it's ID.

Parameters:
account - The account to remove
Returns:
true if successful

setAmortizeObject

public boolean setAmortizeObject(Account account,
                                 AmortizeObject amortizeObject)
Sets the amortize object of an account

Parameters:
account - The Liability account to change
amortizeObject - the new AmortizeObject
Returns:
true if successful

toggleAccountVisibility

public boolean toggleAccountVisibility(Account account)
Toggles the visibility of an account given its ID.

Parameters:
account - The account to toggle visibility
Returns:
true if the supplied account ID was found false if the supplied account ID was not found

addAccountSecurity

private boolean addAccountSecurity(Account account,
                                   SecurityNode node)
Adds a SecurityNode from a InvestmentAccount

Parameters:
account - destination account
node - SecurityNode to add
Returns:
true if add was successful

removeAccountSecurity

private boolean removeAccountSecurity(Account account,
                                      SecurityNode node)
Removes a SecurityNode from an InvestmentAccount

Parameters:
account - Account to remove SecurityNode from
node - SecurityNode to remove
Returns:
true if successful

updateAccountSecurities

public boolean updateAccountSecurities(Account acc,
                                       java.util.Collection<SecurityNode> list)
Update an account's securities list. This compares the old list of securities and the supplied list and adds or removes securities to make sure the lists are the same.

Parameters:
acc - Destination account
list - Collection of SecurityNodes
Returns:
true if successful

isTransactionValid

public boolean isTransactionValid(Transaction transaction)

isStored

public boolean isStored(StoredObject object)
Determine if a StoredObject is persisted in the database

Parameters:
object - StoredObject to check
Returns:
true if persisted

addTransaction

public boolean addTransaction(Transaction transaction)

removeTransaction

public boolean removeTransaction(Transaction transaction)

setTransactionReconciled

public void setTransactionReconciled(Transaction transaction,
                                     Account account,
                                     ReconciledState state)
Changes the reconciled state of a transaction

Parameters:
transaction - transaction to change
account - account to change state for
state - new reconciled state

getTransactionNumberList

public java.util.List<java.lang.String> getTransactionNumberList()

setTransactionNumberList

public void setTransactionNumberList(java.util.List<java.lang.String> list)

getTransactions

public java.util.List<Transaction> getTransactions()
Get all transactions

Returns:
all transactions

postTransactionAdd

private void postTransactionAdd(Transaction transaction,
                                boolean result)

postTransactionRemove

private void postTransactionRemove(Transaction transaction,
                                   boolean result)

getUuid

public java.lang.String getUuid()
Returns the unique identifier for this engine instance

Returns:
uuid