Coverage Report - org.homeunix.thecave.buddi.model.impl.AccountImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
AccountImpl
0%
0/105
0%
0/62
3.556
 
 1  
 /*
 2  
  * Created on Jul 29, 2007 by wyatt
 3  
  */
 4  
 package org.homeunix.thecave.buddi.model.impl;
 5  
 
 6  
 import java.util.Date;
 7  
 import java.util.List;
 8  
 import java.util.Map;
 9  
 import java.util.logging.Level;
 10  
 import java.util.logging.Logger;
 11  
 
 12  
 import org.homeunix.thecave.buddi.model.Account;
 13  
 import org.homeunix.thecave.buddi.model.AccountType;
 14  
 import org.homeunix.thecave.buddi.model.Document;
 15  
 import org.homeunix.thecave.buddi.model.ModelObject;
 16  
 import org.homeunix.thecave.buddi.model.Transaction;
 17  
 import org.homeunix.thecave.buddi.model.TransactionSplit;
 18  
 import org.homeunix.thecave.buddi.plugin.api.exception.InvalidValueException;
 19  
 
 20  
 /**
 21  
  * Default implementation of an Account.  You should not create this object directly; 
 22  
  * instead, please use the ModelFactory to create it, as this will ensure that all
 23  
  * required fields are correctly set.
 24  
  * @author wyatt
 25  
  *
 26  
  */
 27  0
 public class AccountImpl extends SourceImpl implements Account {
 28  
         private long startingBalance;
 29  
         private long balance;
 30  
         private long overdraftCreditLimit;
 31  
         private long interestRate;
 32  
         private Day startDate;
 33  
         private AccountType type;
 34  
 
 35  
         public long getStartingBalance() {
 36  0
                 return startingBalance;
 37  
         }
 38  
         public void setStartingBalance(long startingBalance) {
 39  0
                 if (this.startingBalance != startingBalance)
 40  0
                         setChanged();
 41  0
                 this.startingBalance = startingBalance;
 42  0
         }
 43  
         public Date getStartDate() {
 44  0
                 List<Transaction> ts = getDocument().getTransactions(this);
 45  0
                 if (ts.size() > 0){
 46  0
                         if (startDate == null || startDate.after(ts.get(0).getDate()))
 47  0
                                 return ts.get(0).getDate();
 48  
                         else 
 49  0
                                 return startDate;
 50  
                 }
 51  0
                 if (startDate == null)
 52  0
                         return new Date();
 53  0
                 return startDate;
 54  
         }
 55  
         public void setStartDate(Date startDate) {
 56  0
                 if (startDate != null)
 57  0
                         this.startDate = new Day(startDate);
 58  
                 else
 59  0
                         this.startDate = null;
 60  0
         }
 61  
         public long getBalance() {
 62  0
                 return balance;
 63  
         }
 64  
         public void setBalance(long balance) {
 65  0
                 if (this.balance != balance)
 66  0
                         setChanged();
 67  0
                 this.balance = balance;
 68  0
         }
 69  
         public AccountType getAccountType() {
 70  0
                 return type;
 71  
         }
 72  
         public void setAccountType(AccountType type) {
 73  0
                 this.type = type;
 74  0
                 setChanged();
 75  0
         }
 76  
         @Override
 77  
         public void setName(String name) throws InvalidValueException {
 78  
                 //                if (getDocument() != null){
 79  
                 //                for (Account a : ((Document) getDocument()).getAccounts()) {
 80  
                 //                if (a.getName().equals(name) && !a.equals(this))
 81  
                 //                throw new InvalidValueException("The account name must be unique");
 82  
                 //                }
 83  
                 //                }
 84  0
                 super.setName(name);
 85  0
         }
 86  
         public void updateBalance(){                
 87  0
                 if (getDocument() == null)                        
 88  0
                         return;
 89  0
                 long balance = this.getStartingBalance();
 90  
 
 91  0
                 List<Transaction> transactions = getDocument().getTransactions(this);
 92  
 
 93  0
                 for (Transaction transaction : transactions) {                        
 94  
                         try {
 95  0
                                 if (!transaction.isDeleted()){                                        
 96  
                                         //We are moving money *to* this account                                        
 97  0
                                         if (transaction.getTo().equals(this)){
 98  0
                                                 balance += transaction.getAmount();                                                
 99  0
                                                 transaction.setBalance(this.getUid(), balance);
 100  
                                         }                                         
 101  
                                         //We are moving money *from* this account                                        
 102  0
                                         else if (transaction.getFrom().equals(this)){
 103  0
                                                 balance -= transaction.getAmount();                                                
 104  0
                                                 transaction.setBalance(this.getUid(), balance);                                        
 105  
                                         }                                        
 106  
                                         //We are moving money *to* this account                                        
 107  0
                                         for (TransactionSplit split : transaction.getToSplits()) {
 108  0
                                                 if(split.getSource().equals(this))                                                        
 109  0
                                                         balance += split.getAmount();                                        
 110  
                                         }                                        
 111  
                                         //We are moving money *from* this account                                        
 112  0
                                         for (TransactionSplit split : transaction.getFromSplits()) {
 113  0
                                                 if(split.getSource().equals(this))                                                        
 114  0
                                                         balance -= split.getAmount();                                        
 115  
                                         }                                
 116  
                                 }                        
 117  
                         }                        
 118  0
                         catch (InvalidValueException ive){                                
 119  0
                                 Logger.getLogger(AccountImpl.class.getName()).log(Level.WARNING, "Incorrect value", ive);                        
 120  0
                         }                
 121  
                 }
 122  0
                 setBalance(balance);        
 123  0
         }
 124  
 
 125  
         public String getFullName() {
 126  0
                 return this.getName() + " (" + getAccountType().getName() + ")";
 127  
         }
 128  
         public long getBalance(Date d) {
 129  0
                 if (getDocument() == null)
 130  0
                         return 0; //Document not set; not valid.  Possibly throw exception?
 131  0
                 if (d.before(getStartDate()))
 132  
                         //This used to be defined to return starting balance... I think that it makes more
 133  
                         // sense to return 0, though - if the account has not been defined, there is by
 134  
                         // definition, no balance.
 135  
                         //Update (Dec 5 2010): I reverted back to using getStartingBalance, as after
 136  
                         // re-thinking I think this makes more sense.
 137  0
                         return getStartingBalance();
 138  
                 //                        return 0;
 139  0
                 List<Transaction> ts = getDocument().getTransactions(this, getStartDate(), d);
 140  0
                 if (ts.size() > 0){
 141  0
                         Transaction t = ts.get(ts.size() - 1);
 142  0
                         if (t.getFrom().equals(this) || t.getTo().equals(this))
 143  0
                                 return t.getBalance(this.getUid());
 144  0
                 }
 145  
                 else {
 146  
                         //If there are no transactions, just return the starting balance.
 147  0
                         return getStartingBalance();
 148  
                 }
 149  
 
 150  
                 //                Logger.getLogger().error("AccountImpl.getBalance(Date) - Something is wrong... we should not be here.");
 151  0
                 return 0;
 152  
         }
 153  
         @Override
 154  
         public int compareTo(ModelObject arg0) {
 155  0
                 if (arg0 instanceof AccountImpl){
 156  0
                         AccountImpl a = (AccountImpl) arg0;
 157  0
                         if (this.getAccountType().isCredit() != a.getAccountType().isCredit()){
 158  0
                                 if (this.getAccountType().isCredit())
 159  0
                                         return 1;
 160  0
                                 return -1;
 161  
                         }
 162  0
                         return this.getName().compareTo(a.getName());
 163  
                 }
 164  0
                 return super.compareTo(arg0);
 165  
         }
 166  
 
 167  
         public long getOverdraftCreditLimit() {
 168  0
                 return overdraftCreditLimit;
 169  
         }
 170  
 
 171  
         public void setOverdraftCreditLimit(long overdraftCreditLimit) throws InvalidValueException {
 172  0
                 if (overdraftCreditLimit < 0)
 173  0
                         throw new InvalidValueException("Overdraft limit must be positive");
 174  0
                 if (this.overdraftCreditLimit != overdraftCreditLimit)
 175  0
                         setChanged();
 176  0
                 this.overdraftCreditLimit = overdraftCreditLimit;
 177  0
         }
 178  
 
 179  
         Account clone(Map<ModelObject, ModelObject> originalToCloneMap) throws CloneNotSupportedException {
 180  
 
 181  0
                 if (originalToCloneMap.get(this) != null)
 182  0
                         return (Account) originalToCloneMap.get(this);
 183  
 
 184  0
                 AccountImpl a = new AccountImpl();
 185  
 
 186  0
                 a.document = (Document) originalToCloneMap.get(document);
 187  0
                 a.type = (AccountType) originalToCloneMap.get(getAccountType());
 188  0
                 a.balance = balance;
 189  0
                 a.deleted = isDeleted();
 190  0
                 if (modifiedTime != null)
 191  0
                         a.modifiedTime = new Time(modifiedTime);
 192  0
                 a.name = name;
 193  0
                 a.notes = notes;
 194  0
                 a.overdraftCreditLimit = overdraftCreditLimit;
 195  0
                 a.interestRate = interestRate;
 196  0
                 if (startDate != null)
 197  0
                         a.startDate = new Day(startDate);
 198  0
                 a.startingBalance = startingBalance;
 199  
 
 200  0
                 originalToCloneMap.put(this, a);
 201  
 
 202  0
                 return a;
 203  
         }
 204  
 
 205  
         public long getInterestRate() {
 206  0
                 return interestRate;
 207  
         }
 208  
         public void setInterestRate(long interestRate) throws InvalidValueException {
 209  0
                 if (interestRate < 0) //Should only affect API setters, as the decimal input field does not allow negatives.
 210  0
                         throw new InvalidValueException("Interest rate must be positive");
 211  
                 //TODO Should we check for this?  Perhaps at a later date; for now, since this value is not used
 212  
                 // for anything other than 'FYI', it's probably not needed.
 213  
                 //                if (interestRate > 100000)
 214  
                 //                        throw new InvalidValueException("Interest rate cannot be greater than 100%");
 215  0
                 if (this.interestRate != interestRate)
 216  0
                         setChanged();
 217  0
                 this.interestRate = interestRate;
 218  0
         }
 219  
 }