try {
factory.beginTransaction();
if (trans == null) {
trans = new Transaction();
Payee payee = PayeeUtils.findPayee(this.name, true);
trans.setPayee(payee);
trans.setDatePosted(datePosted);
Transaction payeeTrans = PayeeUtils.findLastTransactionForPayee(payee, this.accountID);
if (payeeTrans != null) {
List transSplitList = payeeTrans.getTransSplitList();
TransSplit transSplit = null;
TransSplit newSplit = null;
double oldTotal = 0;
for (int i=0; i<transSplitList.size(); i++) {
transSplit = (TransSplit)transSplitList.get(i);
if (transSplit.getAmount().doubleValue() >= 0)
oldTotal += transSplit.getAmount().doubleValue();
}
for (int i=0; i<transSplitList.size(); i++) {
transSplit = (TransSplit)transSplitList.get(i);
newSplit = new TransSplit();
newSplit.setParentTransaction(trans);
newSplit.setAccount(transSplit.getAccount());
double splitAmount = Math.abs((transSplit.getAmount().doubleValue()/oldTotal)*amount);
if (newSplit.getAccountAccountID().equals(accountID)) {
if (amount >= 0) {
newSplit.setAmount(new Double(splitAmount));
}
else {
newSplit.setAmount(new Double(-splitAmount));
}
}
else {
if (amount >= 0) {
newSplit.setAmount(new Double(-splitAmount));
}
else {
newSplit.setAmount(new Double(splitAmount));
}
}
trans.appendTransSplit(newSplit);
}
}
else {
RegisterDAO registerDAO = (RegisterDAO)factory.getDAO("register");
List payeeTransList = registerDAO.loadTransactionsForAccountPayeeDate(this.accountID, payee.getPayeeID(), trans.getDatePosted());
if (payeeTransList.size()>0) {
boolean createTrans = false;
Transaction tempTrans = null;
for (int i=0; i<payeeTransList.size(); i++) {
tempTrans = (Transaction)payeeTransList.get(i);