org.jbudget.io
Class LocalStore

java.lang.Object
  extended by org.jbudget.io.LocalStore
All Implemented Interfaces:
StorageInterface

public class LocalStore
extends java.lang.Object
implements StorageInterface

Author:
petrov

Field Summary
private  java.util.Map<java.lang.Integer,Element> accountElementsMap
          A map of JDOM Elements for Account objects that were loaded from the storage.
private  java.util.Map<java.lang.Integer,Element> budgetElementsMap
          A map of JDOM Elements for Budget objects that were loaded from the storage.
private  DataIndex dataIndex
          Index of available data
private  java.util.Map<java.lang.Integer,Element> monthElementsMap
          A map of JDOM Elements for Month objects that were loaded from the storage.
private  boolean newDataIndexFlag
          A flag that is set to true when a new DataIndex is created.
private  java.io.File rootDirectory
          Root directory for storing all the data
private  org.jbudget.Core.SharedPreferences sharedPreferences
          Global SharedPreference object used by the program.
private  java.util.Map<java.lang.Integer,Element> userElementsMap
          A map of JDOM Elements for User objects that were loaded from the storage.
private  java.util.Map<java.lang.Integer,Element> userPreferencesElementsMap
          A map of JDOM Elements for UserPreferences objects that were loaded from the storage.
 
Constructor Summary
LocalStore(java.io.File rootDirectory)
          Creates a new instance of LocalStore
LocalStore(java.lang.String rootDirectoryStr)
          Creates a new instance of LocalStore
 
Method Summary
 boolean addAllocation(org.jbudget.Core.Transaction allocation, int cMonth, int cYear)
          saves an Allocation.
 boolean addTransaction(org.jbudget.Core.Transaction transaction, int cMonth, int cYear)
          saves a Transaction.
private  void backupFile(java.io.File file)
          Creates a backup of a given file.
 void commit()
          Writes all JDOME elements stored in the cache and the data index to the permanent storage.
 int generateAccountID()
          Creates a new numeric account id and increases the counter.
 int generateBudgetID()
          Creates a new numeric budget id and increases the counter.
 int generateExpenseCategoryID()
          Creates a new numeric id for an expense category and increases the counter.
 int generateIncomeSourceID()
          Creates a new numeric id for an income source and increases the counter.
 long generateTransactionID()
          Creates a new numeric transaction id for and increases the counter.
 int generateUID()
          Creates a new uid for a new user and increases the counter.
 org.jbudget.Core.Account getAccount(int id)
          Implementing the interface.
private  Element getAccountElement(int id)
          Returns a JDOM Element containing the account with given id.
private  java.io.File getAccountFile(int id)
          Returns a File object that is initialized to point to the xml file corresponing to the Account with a given id.
 org.jbudget.Core.Budget getBudget(int id)
          Implementing the interface.
private  Element getBudgetElement(int id)
          Returns a JDOM Element containing the budget with given id.
private  java.io.File getBudgetFile(int id)
          Returns a File object that is initialized to point to the xml file corresponing to the Budget with a given id.
 DataIndex getDataIndex()
          Interface implementation
 java.lang.String getIdString()
          Returns string ID of this instance of the storage interface.
 org.jbudget.Core.Month getMonth(int cMonth, int cYear)
          Implementing the interface.
private  Element getMonthElement(int month, int year)
          Returns a JDOM element that corresponds to the given month.
private  java.io.File getMonthFile(int cMonth, int cYear)
          Returns a File object that is initialized to point to the xml file corresponding to the given month
 org.jbudget.Core.SharedPreferences getSharedPreferences()
          Returns the SharedPrefences object used by the program.
private  java.io.File getSharedPreferencesFile()
           
 org.jbudget.Core.User getUser(int uid)
          Implementing the interface.
private  Element getUserElement(int uid)
          Returns a JDOM Element containing the user with a given uid.
private  java.io.File getUserFile(int uid)
          Returns a File object that is initialized to point to the xml file corresponing to the User with a given uid.
 UserPreferences getUserPreferences(int uid)
          Implementing the interface.
private  Element getUserPreferencesElement(int uid)
          Returns a JDOM Element containing the user preferences for the user with a given uid.
private  java.io.File getUserPreferencesFile(int uid)
          Returns a File object that is initialized to point to the xml file corresponing to the UserPreferences for the user with a given uid.
private  void initialize()
          Makes sure that the root directory for storing all the files exists.
private  void initializeIndex()
          Initializes an index of available data.
 boolean isNewDataInidex()
          Returns true if a new data index file was created when the Storage object was constructed.
private  Element readAccountElement(int id)
          Reads a JDOM Element containing the account with given account id.
private  Element readBudgetElement(int id)
          Reads a JDOM Element containing the budget with given budget id.
private  Element readMonthElement(int cMonth, int cYear)
          Reads a JDOM Element for a given Month from a corresponding file.
private  Element readSharedPreferencesElement()
          Reads a JDOM Element containing the user preferences for a user with a given uid.
private  Element readUserElement(int uid)
          Reads a JDOM Element containing the user with a given uid.
private  Element readUserPreferencesElement(int uid)
          Reads a JDOM Element containing the user preferences for a user with a given uid.
 boolean removeAccount(org.jbudget.Core.Account account)
          removes a account.
 boolean removeAccount(int id)
          removes a account.
 boolean removeAllocation(long allocationID, int cMonth, int cYear)
          removes an Allocation
 boolean removeBudget(org.jbudget.Core.Budget budget)
          removes a budget.
 boolean removeBudget(int id)
          removes a budget.
 boolean removeMonth(int cMonth, int cYear)
          Implementing the interfaces.
 boolean removeMonth(org.jbudget.Core.Month month)
          Implementing the interfaces.
 boolean removeTransaction(long transactionID, int cMonth, int cYear)
          removes a Transaction
 boolean removeUser(int uid)
          removes a user.
 boolean removeUser(org.jbudget.Core.User user)
          removes a user.
private  void saveAccountElement(Element element, int id)
          Saves a JDOM Element containing the account with given account id.
private  void saveBudgetElement(Element element, int id)
          Saves a JDOM Element containing the budget with given budget id.
private  void saveMonthElement(Element element, int cMonth, int cYear)
          Stores a JDOM Element corresponding to a Month object to a file.
private  void saveSharedPreferences()
           
private  void saveSharedPreferencesElement(Element element)
          Saves a JDOM Element containing the user preferences for a user with a given uid.
private  void saveUserElement(Element element, int uid)
          Saves a JDOM Element containing the user with given uid.
private  void saveUserPreferencesElement(Element element, int uid)
          Saves a JDOM Element containing the user preferences for a user with a given uid.
 boolean storeAccount(org.jbudget.Core.Account account)
          Stores a given account.
 boolean storeBudget(org.jbudget.Core.Budget budget)
          Stores a given budget.
private  void storeIndex()
          Saves the index of available data to a file
 boolean storeMonth(org.jbudget.Core.Month month)
          Implementing the interface.
 boolean storeUser(org.jbudget.Core.User user)
          Stores a given user.
 boolean storeUserPreferences(int uid, UserPreferences preferences)
          Stores the preferences for the user with a given uid.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rootDirectory

private final java.io.File rootDirectory
Root directory for storing all the data


dataIndex

private DataIndex dataIndex
Index of available data


newDataIndexFlag

private boolean newDataIndexFlag
A flag that is set to true when a new DataIndex is created. This means that an empty data directory is initialized.


monthElementsMap

private final java.util.Map<java.lang.Integer,Element> monthElementsMap
A map of JDOM Elements for Month objects that were loaded from the storage. Used for caching to prevent excessive reading and saving of XML data.


budgetElementsMap

private final java.util.Map<java.lang.Integer,Element> budgetElementsMap
A map of JDOM Elements for Budget objects that were loaded from the storage. Used for caching to prevent excessive reading and saving/reading of XML data.


accountElementsMap

private final java.util.Map<java.lang.Integer,Element> accountElementsMap
A map of JDOM Elements for Account objects that were loaded from the storage. Used for caching to prevent excessive reading and saving/reading of XML data.


userElementsMap

private final java.util.Map<java.lang.Integer,Element> userElementsMap
A map of JDOM Elements for User objects that were loaded from the storage. Used for caching to prevent excessive reading and saving/reading of XML data.


userPreferencesElementsMap

private final java.util.Map<java.lang.Integer,Element> userPreferencesElementsMap
A map of JDOM Elements for UserPreferences objects that were loaded from the storage. Used for caching to prevent excessive reading and saving/reading of XML data.


sharedPreferences

private org.jbudget.Core.SharedPreferences sharedPreferences
Global SharedPreference object used by the program.

Constructor Detail

LocalStore

public LocalStore(java.io.File rootDirectory)
           throws java.io.IOException,
                  JDOMException
Creates a new instance of LocalStore

Throws:
java.io.IOException
JDOMException

LocalStore

public LocalStore(java.lang.String rootDirectoryStr)
           throws java.io.IOException,
                  JDOMException
Creates a new instance of LocalStore

Throws:
java.io.IOException
JDOMException
Method Detail

initialize

private void initialize()
                 throws java.io.IOException,
                        JDOMException
Makes sure that the root directory for storing all the files exists.

Throws:
java.io.IOException
JDOMException

initializeIndex

private void initializeIndex()
                      throws JDOMException,
                             java.io.IOException
Initializes an index of available data. Reads it from a file or creates a new one.

Throws:
JDOMException
java.io.IOException

storeIndex

private void storeIndex()
                 throws java.io.IOException
Saves the index of available data to a file

Throws:
java.io.IOException

getDataIndex

public DataIndex getDataIndex()
Interface implementation

Specified by:
getDataIndex in interface StorageInterface

isNewDataInidex

public boolean isNewDataInidex()
Description copied from interface: StorageInterface
Returns true if a new data index file was created when the Storage object was constructed. This usually means that a new data directory was initialized

Specified by:
isNewDataInidex in interface StorageInterface

readMonthElement

private Element readMonthElement(int cMonth,
                                 int cYear)
                          throws java.io.IOException,
                                 JDOMException
Reads a JDOM Element for a given Month from a corresponding file. Adds the element to the monthsElementsMap.

Throws:
java.io.IOException - if there is a problem reading the file.
JDOMExceptoin - if there is a problem parsing the contents of the file.
JDOMException

saveMonthElement

private void saveMonthElement(Element element,
                              int cMonth,
                              int cYear)
                       throws java.io.IOException,
                              JDOMException
Stores a JDOM Element corresponding to a Month object to a file.

Throws:
java.io.IOException - if there are IO errors.
JDOMException

getMonthElement

private Element getMonthElement(int month,
                                int year)
                         throws java.io.IOException,
                                JDOMException
Returns a JDOM element that corresponds to the given month. Returns a cached version if it is available. Otherwise reads the object from permanent storage.

Throws:
java.lang.IllegalArgumentException - if month is not available.
java.io.IOException
JDOMException

getMonth

public org.jbudget.Core.Month getMonth(int cMonth,
                                       int cYear)
                                throws java.io.IOException
Implementing the interface. Gets a JDOM Element for a month converts it to a Month object and returns it.

Specified by:
getMonth in interface StorageInterface
Throws:
java.io.IOException - if there are IO errors.
JDOMException - if there are parsing errors.

storeMonth

public boolean storeMonth(org.jbudget.Core.Month month)
Implementing the interface. Converts a give month object to a JDOM Element and stores it in the cache. Also adds a record in the data index. Does not actually save the Element to disk, this is done when the cache is synchronized to disk.

Specified by:
storeMonth in interface StorageInterface
Returns:
true when the Month was successfully saved. For networked storage that means after a confirmation from server was received.

removeMonth

public boolean removeMonth(int cMonth,
                           int cYear)
Implementing the interfaces. Removes a month.

Specified by:
removeMonth in interface StorageInterface
Returns:
true when the operation has completed successfully.
Throws:
java.lang.IllegalArgumentException - if there is no such month

removeMonth

public boolean removeMonth(org.jbudget.Core.Month month)
Implementing the interfaces. Removes a month.

Specified by:
removeMonth in interface StorageInterface
Returns:
true when the operation has completed successfully.
Throws:
java.lang.IllegalArgumentException - if there is no such month

readBudgetElement

private Element readBudgetElement(int id)
                           throws java.io.IOException,
                                  JDOMException
Reads a JDOM Element containing the budget with given budget id.

Throws:
java.io.IOException
java.lang.IllegalArgumentException - if there is no such budget.
JDOMException

saveBudgetElement

private void saveBudgetElement(Element element,
                               int id)
                        throws java.io.IOException,
                               JDOMException
Saves a JDOM Element containing the budget with given budget id.

Throws:
java.io.IOException
JDOMException

getBudgetElement

private Element getBudgetElement(int id)
                          throws java.io.IOException,
                                 JDOMException
Returns a JDOM Element containing the budget with given id. Uses a cached version if it is avalilable. Otherwise the Element is read from storage.

Throws:
java.lang.IllegalArgumentException - if there is no budget with the given id
java.io.IOException
JDOMException

getBudget

public org.jbudget.Core.Budget getBudget(int id)
                                  throws java.io.IOException
Implementing the interface. Returns a Budget object with a given id.

Specified by:
getBudget in interface StorageInterface
Throws:
java.lang.IllegalArgumentException - if there is no budget with the given id
java.io.IOException - if there are IO errors.

storeBudget

public boolean storeBudget(org.jbudget.Core.Budget budget)
Stores a given budget.

Specified by:
storeBudget in interface StorageInterface
Returns:
true when the operation has completed successfully.

removeBudget

public boolean removeBudget(int id)
removes a budget.

Specified by:
removeBudget in interface StorageInterface
Returns:
true when the operation has completed successfully.
Throws:
java.lang.IllegalArgumentException - if no such budget exists.

removeBudget

public boolean removeBudget(org.jbudget.Core.Budget budget)
removes a budget.

Specified by:
removeBudget in interface StorageInterface
Returns:
true when the operation has completed successfully.
Throws:
java.lang.IllegalArgumentException - if no such budget exists.

readAccountElement

private Element readAccountElement(int id)
                            throws java.io.IOException,
                                   JDOMException
Reads a JDOM Element containing the account with given account id.

Throws:
java.io.IOException
java.lang.IllegalArgumentException - if there is no such account.
JDOMException

saveAccountElement

private void saveAccountElement(Element element,
                                int id)
                         throws java.io.IOException,
                                JDOMException
Saves a JDOM Element containing the account with given account id.

Throws:
java.io.IOException
JDOMException

getAccountElement

private Element getAccountElement(int id)
                           throws java.io.IOException,
                                  JDOMException
Returns a JDOM Element containing the account with given id. Uses a cached version if it is avalilable. Otherwise the Element is read from storage.

Throws:
java.lang.IllegalArgumentException - if there is no account with the given id
java.io.IOException
JDOMException

getAccount

public org.jbudget.Core.Account getAccount(int id)
                                    throws java.io.IOException
Implementing the interface. Returns a Account object with a given id.

Specified by:
getAccount in interface StorageInterface
Throws:
java.lang.IllegalArgumentException - if there is no account with the given id
java.io.IOException - if there are IO errors.

storeAccount

public boolean storeAccount(org.jbudget.Core.Account account)
Stores a given account.

Specified by:
storeAccount in interface StorageInterface
Returns:
true when the operation has completed successfully.

removeAccount

public boolean removeAccount(int id)
removes a account.

Specified by:
removeAccount in interface StorageInterface
Returns:
true when the operation has completed successfully.
Throws:
java.lang.IllegalArgumentException - if no such account exists.

removeAccount

public boolean removeAccount(org.jbudget.Core.Account account)
removes a account.

Specified by:
removeAccount in interface StorageInterface
Returns:
true when the operation has completed successfully.
Throws:
java.lang.IllegalArgumentException - if no such account exists.

readUserElement

private Element readUserElement(int uid)
                         throws java.io.IOException,
                                JDOMException
Reads a JDOM Element containing the user with a given uid.

Throws:
java.io.IOException
java.lang.IllegalArgumentException - if there is no such user.
JDOMException

saveUserElement

private void saveUserElement(Element element,
                             int uid)
                      throws java.io.IOException,
                             JDOMException
Saves a JDOM Element containing the user with given uid.

Throws:
java.io.IOException
JDOMException

getUserElement

private Element getUserElement(int uid)
                        throws java.io.IOException,
                               JDOMException
Returns a JDOM Element containing the user with a given uid. Uses a cached version if it is avalilable. Otherwise the Element is read from storage.

Throws:
java.lang.IllegalArgumentException - if there is no user with the given id
java.io.IOException
JDOMException

getUser

public org.jbudget.Core.User getUser(int uid)
                              throws java.io.IOException
Implementing the interface. Returns a User object with a given uid.

Specified by:
getUser in interface StorageInterface
Throws:
java.lang.IllegalArgumentException - if there is no user with the given uid
java.io.IOException - if there are IO errors.

storeUser

public boolean storeUser(org.jbudget.Core.User user)
Stores a given user.

Specified by:
storeUser in interface StorageInterface
Returns:
true when the operation has completed successfully.

removeUser

public boolean removeUser(int uid)
removes a user.

Specified by:
removeUser in interface StorageInterface
Returns:
true when the operation has completed successfully.
Throws:
java.lang.IllegalArgumentException - if no such user exists.

removeUser

public boolean removeUser(org.jbudget.Core.User user)
removes a user.

Specified by:
removeUser in interface StorageInterface
Returns:
true when the operation has completed successfully.
Throws:
java.lang.IllegalArgumentException - if no such user exists.

readUserPreferencesElement

private Element readUserPreferencesElement(int uid)
                                    throws java.io.IOException,
                                           JDOMException
Reads a JDOM Element containing the user preferences for a user with a given uid.

Throws:
java.io.IOException
java.lang.IllegalArgumentException - if there is no such user.
JDOMException

saveUserPreferencesElement

private void saveUserPreferencesElement(Element element,
                                        int uid)
                                 throws java.io.IOException,
                                        JDOMException
Saves a JDOM Element containing the user preferences for a user with a given uid.

Throws:
java.io.IOException
JDOMException

getUserPreferencesElement

private Element getUserPreferencesElement(int uid)
                                   throws java.io.IOException,
                                          JDOMException
Returns a JDOM Element containing the user preferences for the user with a given uid. Uses a cached version if it is avalilable. Otherwise the Element is read from storage.

Throws:
java.lang.IllegalArgumentException - if there is no user with the given id
java.io.IOException
JDOMException

getUserPreferences

public UserPreferences getUserPreferences(int uid)
                                   throws java.io.IOException
Implementing the interface. Returns a UserPreferences object for the user with a given uid.

Specified by:
getUserPreferences in interface StorageInterface
Throws:
java.lang.IllegalArgumentException - if there is no user with the given uid
java.io.IOException - if there are IO errors.

storeUserPreferences

public boolean storeUserPreferences(int uid,
                                    UserPreferences preferences)
Stores the preferences for the user with a given uid.

Specified by:
storeUserPreferences in interface StorageInterface
Returns:
true when the operation has completed successfully.

readSharedPreferencesElement

private Element readSharedPreferencesElement()
                                      throws java.io.IOException,
                                             JDOMException
Reads a JDOM Element containing the user preferences for a user with a given uid.

Throws:
java.io.IOException
java.lang.IllegalArgumentException - if there is no such user.
JDOMException

saveSharedPreferencesElement

private void saveSharedPreferencesElement(Element element)
                                   throws java.io.IOException,
                                          JDOMException
Saves a JDOM Element containing the user preferences for a user with a given uid.

Throws:
java.io.IOException
JDOMException

getSharedPreferences

public org.jbudget.Core.SharedPreferences getSharedPreferences()
Returns the SharedPrefences object used by the program.

Specified by:
getSharedPreferences in interface StorageInterface

saveSharedPreferences

private void saveSharedPreferences()
                            throws java.io.IOException,
                                   JDOMException
Throws:
java.io.IOException
JDOMException

addTransaction

public boolean addTransaction(org.jbudget.Core.Transaction transaction,
                              int cMonth,
                              int cYear)
                       throws java.io.IOException,
                              JDOMException
Description copied from interface: StorageInterface
saves a Transaction.

Specified by:
addTransaction in interface StorageInterface
Returns:
true when the operation has completed successfully.
Throws:
java.io.IOException
JDOMException

removeTransaction

public boolean removeTransaction(long transactionID,
                                 int cMonth,
                                 int cYear)
                          throws java.io.IOException,
                                 JDOMException
Description copied from interface: StorageInterface
removes a Transaction

Specified by:
removeTransaction in interface StorageInterface
Returns:
true when the operation has completed successfully.
Throws:
java.io.IOException
JDOMException

addAllocation

public boolean addAllocation(org.jbudget.Core.Transaction allocation,
                             int cMonth,
                             int cYear)
                      throws java.io.IOException,
                             JDOMException
Description copied from interface: StorageInterface
saves an Allocation.

Specified by:
addAllocation in interface StorageInterface
Returns:
true when the operation has completed successfully.
Throws:
java.io.IOException
JDOMException

removeAllocation

public boolean removeAllocation(long allocationID,
                                int cMonth,
                                int cYear)
                         throws java.io.IOException,
                                JDOMException
Description copied from interface: StorageInterface
removes an Allocation

Specified by:
removeAllocation in interface StorageInterface
Returns:
true when the operation has completed successfully.
Throws:
java.io.IOException
JDOMException

commit

public void commit()
            throws java.io.IOException,
                   JDOMException
Writes all JDOME elements stored in the cache and the data index to the permanent storage.

Specified by:
commit in interface StorageInterface
Throws:
java.io.IOException
JDOMException

getIdString

public java.lang.String getIdString()
Returns string ID of this instance of the storage interface.

Specified by:
getIdString in interface StorageInterface

getMonthFile

private java.io.File getMonthFile(int cMonth,
                                  int cYear)
Returns a File object that is initialized to point to the xml file corresponding to the given month


getBudgetFile

private java.io.File getBudgetFile(int id)
Returns a File object that is initialized to point to the xml file corresponing to the Budget with a given id.


getAccountFile

private java.io.File getAccountFile(int id)
Returns a File object that is initialized to point to the xml file corresponing to the Account with a given id.


getUserFile

private java.io.File getUserFile(int uid)
Returns a File object that is initialized to point to the xml file corresponing to the User with a given uid.


getUserPreferencesFile

private java.io.File getUserPreferencesFile(int uid)
Returns a File object that is initialized to point to the xml file corresponing to the UserPreferences for the user with a given uid.


getSharedPreferencesFile

private java.io.File getSharedPreferencesFile()

generateBudgetID

public int generateBudgetID()
Creates a new numeric budget id and increases the counter.

Specified by:
generateBudgetID in interface StorageInterface

generateAccountID

public int generateAccountID()
Creates a new numeric account id and increases the counter.

Specified by:
generateAccountID in interface StorageInterface

generateExpenseCategoryID

public int generateExpenseCategoryID()
Creates a new numeric id for an expense category and increases the counter.

Specified by:
generateExpenseCategoryID in interface StorageInterface

generateIncomeSourceID

public int generateIncomeSourceID()
Creates a new numeric id for an income source and increases the counter.

Specified by:
generateIncomeSourceID in interface StorageInterface

generateUID

public int generateUID()
Creates a new uid for a new user and increases the counter.

Specified by:
generateUID in interface StorageInterface

generateTransactionID

public long generateTransactionID()
Creates a new numeric transaction id for and increases the counter.

Specified by:
generateTransactionID in interface StorageInterface

backupFile

private void backupFile(java.io.File file)
                 throws java.io.IOException
Creates a backup of a given file.

Throws:
java.io.IOException