private TransactionEntryDTO getTransactionEntryDTO(TransactionEntry te) { TransactionEntryDTO d = new TransactionEntryDTO(); d.setTxnEntryId(te.getTxnEntryId()); d.setInwardAccount(getAccDTOFromAcc(te.getInwardAccount())); d.setOutwardAccount(getAccDTOFromAcc(te.getOutwardAccount())); d.setAmount(te.getAmount()); return d; }
@Override public Long saveTransaction(TransactionDTO dto) { List<Object> toBeSaved = new ArrayList<Object>(); List<Object> toBeDeleted = new ArrayList<Object>(); TransactionTable tt = null; if (dto.getTransactionId() > 0) { // old transaction tt = SearchHelper.getFacade() .readModelWithId(TransactionTable.class, dto.getTransactionId(), true); Map<String, List<Object>> modifiedMap = modifedMapAfterDeletingTransaction(tt); toBeDeleted.addAll(modifiedMap.get("toBeDeleted")); toBeSaved.addAll(modifiedMap.get("toBeSaved")); // now remove all the te's from the tt collection for (Object te : toBeDeleted) { tt.getTransactionEntries().remove(te); } } else { // new transaction tt = new TransactionTable(); tt.setTransactionGroup( SearchHelper.getFacade() .readModelWithId(TransactionGroup.class, dto.getGroupId(), false)); } // save general details tt.setTxnName(dto.getName()); tt.setEntryType(dto.getEntryType()); tt.setCreationDate(dto.getDate()); tt.setNotes(dto.getNotes()); // save tags ArrayList<Tag> tags = new ArrayList<Tag>(); for (String tag : dto.getSelectedTags()) { Tag t = new Tag(); t.setTagName(tag); tags.add(t); } tt.setTags(tags); // save the TransactionEntries for (TransactionEntryDTO ted : dto.getTransactionEntries()) { Account outwardAccount = getAccountFromList(toBeSaved, ted.getOutwardAccount().getAccountId()); Account inwardAccount = getAccountFromList(toBeSaved, ted.getInwardAccount().getAccountId()); TransactionEntry te = new TransactionEntry(); te.setAmount(ted.getAmount()); te.setTransaction(tt); te.setOutwardAccount(outwardAccount); te.setInwardAccount(inwardAccount); toBeSaved.add(updateCurrentBalance(inwardAccount, ted.getAmount(), "add")); toBeSaved.add(updateCurrentBalance(outwardAccount, ted.getAmount(), "sub")); toBeSaved.add(te); } toBeSaved.add(tt); boolean b = SearchHelper.getFacade().saveDeleteModels(toBeSaved, toBeDeleted); return b ? tt.getTxnId() : -1L; }