@Override public boolean deleteTransactionGroup(Long tgId) { TransactionGroup tg = SearchHelper.getFacade().readModelWithId(TransactionGroup.class, tgId, true); List<Object> toBeDeleted = new ArrayList<Object>(); List<Object> toBeSaved = new ArrayList<Object>(); for (TransactionTable tt : tg.getTransactions()) { toBeDeleted.add(tt); // TODO not working code.. since it is not required in this release.. so need to worry.. :) Map<String, List<Object>> modifiedMap = modifedMapAfterDeletingTransaction(getTransactionEntriesWithoutTT(tt.getTxnId())); toBeDeleted.addAll(modifiedMap.get("toBeDeleted")); toBeSaved.addAll(modifiedMap.get("toBeSaved")); } for (Object o : toBeDeleted) { if (o instanceof TransactionTable) { tg.getTransactions().remove(o); } } toBeDeleted.add(tg); return SearchHelper.getFacade().saveDeleteModels(toBeSaved, toBeDeleted); }
private TransactionDTO getTransactionDTO(TransactionTable tt) { if (null == tt) { return null; } TransactionDTO d = new TransactionDTO(); d.setTransactionId(tt.getTxnId()); d.setNotes(tt.getNotes()); d.setDate(tt.getCreationDate()); d.setName(tt.getTxnName()); d.setEntryType(tt.getEntryType()); for (Tag t : tt.getTags()) { d.getSelectedTags().add(t.getTagName()); } for (TransactionEntry te : tt.getTransactionEntries()) { d.getTransactionEntries().add(getTransactionEntryDTO(te)); } d.setGroupId(tt.getTransactionGroup().getTxnGroupId()); d.setGroupName(tt.getTransactionGroup().getTgName()); return d; }
@Override public TransactionDTO getTransactionDetail(Long transactionId) { TransactionTable tt = SearchHelper.getFacade().readModelWithId(TransactionTable.class, transactionId, true); // check for userId is same or not if (getCurrentUser(this.getThreadLocalRequest().getSession()) != tt.getTransactionGroup().getUser().getUid()) { return null; // request for a transaction of a different user } return getTransactionDTO(tt); }
private Map<String, List<Object>> modifedMapAfterDeletingTransaction(TransactionTable tt) { Map<String, List<Object>> map = new LinkedHashMap<String, List<Object>>(); List<Object> toBeSaved = new ArrayList<Object>(); List<Object> toBeDeleted = new ArrayList<Object>(); for (TransactionEntry te : tt.getTransactionEntries()) { toBeDeleted.add(te); toBeSaved.add(updateCurrentBalance(te.getInwardAccount(), te.getAmount(), "sub")); toBeSaved.add(updateCurrentBalance(te.getOutwardAccount(), te.getAmount(), "add")); } map.put("toBeSaved", toBeSaved); map.put("toBeDeleted", toBeDeleted); return map; }
@Override public boolean deleteTransaction(long transactionId, boolean deleteFullTransaction) { List<Object> toBeSaved = new ArrayList<Object>(); List<Object> toBeDeleted = new ArrayList<Object>(); long txnId = -1; if (deleteFullTransaction) { // delete the whole transaction TransactionTable tt = SearchHelper.getFacade().readModelWithId(TransactionTable.class, transactionId, true); if (getCurrentUser(this.getThreadLocalRequest().getSession()) != tt.getTransactionGroup().getUser().getUid()) { logger.error( "Unauthorized removal of Transaction with id: " + transactionId + ", User: "******", Actual User: "******"toBeDeleted")); toBeSaved.addAll(modifiedMap.get("toBeSaved")); // now remove all the te's from the tt collection for (Object te : toBeDeleted) { tt.getTransactionEntries().remove(te); } toBeDeleted.add(tt); } else { // delete the specified transaction entry TransactionEntry te = SearchHelper.getFacade().readModelWithId(TransactionEntry.class, transactionId, false); if (getCurrentUser(this.getThreadLocalRequest().getSession()) != te.getTransaction().getTransactionGroup().getUser().getUid()) { logger.error( "Unauthorized removal of Transaction with id: " + transactionId + ", User: "******", Actual User: "******"sub")); toBeSaved.add(updateCurrentBalance(te.getOutwardAccount(), te.getAmount(), "add")); toBeDeleted.add(te); txnId = te.getTransaction().getTxnId(); } boolean result = SearchHelper.getFacade().saveDeleteModels(toBeSaved, toBeDeleted); if (!deleteFullTransaction) { // check if the transaction has no transaction entry then delete the transaction also Map<String, Object> criteria = new LinkedHashMap<String, Object>(); criteria.put("transaction.txnId", txnId); int count = SearchHelper.getFacade().getCount(TransactionEntry.class, criteria, false); if (0 == count) { SearchHelper.getFacade() .deleteModel( SearchHelper.getFacade().readModelWithId(TransactionTable.class, txnId, false)); } } return result; }
@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; }