public void testFindFirstPivotAfter_WhenNoPivot() { Date date = new GregorianCalendar(2016, Calendar.APRIL, 22).getTime(); logTest( "testFindFirstPivotAfter_WhenNoPivot: %s, which: %s", Util.SIMPLE_DATE_FORMAT.format(date), ss.pkez().getName()); TTransaction fromPivot = ts.findLastPivotBefore(date, ss.pkez()); log("Pivot found before: %s", fromPivot); TTransaction toPivot = ts.findFirstPivotAfter(date, ss.pkez()); log("Pivot found after : %s [should be null]", toPivot); List<TTransaction> dirtyElements = ts.findElementsBetween( fromPivot == null ? date : fromPivot.getDate(), toPivot == null ? null : toPivot.getDate(), ss.pkez()); log("The dirty list is as follows:"); for (TTransaction t : dirtyElements) log("dirty: %s", t); }
private List<TTransaction> findDirtyElements(Date from, Date to, TChargeAccount ca) { TTransaction fromPivot = service.findLastPivotBefore(from, ca); TTransaction toPivot = service.findFirstPivotAfter(to, ca); info("from, to PIVOT elements [can be null as well]: "); info("%s", fromPivot); info("%s", toPivot); Assert.assertTrue( fromPivot == null || toPivot == null || fromPivot.getDate().getTime() <= toPivot.getDate().getTime()); List<TTransaction> dirtyElements = service.findElementsBetween( fromPivot == null ? from : fromPivot.getDate(), toPivot == null ? null : toPivot.getDate(), ca); info("the dirty list is as follows:"); for (TTransaction t : dirtyElements) info("%s", t); return dirtyElements; }
private TransactionUpdater recalculate(List<TTransaction> list) { // the first one should not be updated at all (that is a pivot element) if (list.isEmpty()) { System.out.println( "[WARNING] pivot is null, list is empty. " + TransactionUpdater.class.getSimpleName()); return this; } // list.isEmpty() == FALSE int offset; for (offset = 0; offset < list.size() && !list.get(offset).isPivot(); ++offset) ; /* in case when no pivot found: nothing to be done */ if (offset == list.size()) return this; int balance = list.get(offset).getBalance(); TChargeAccount ca = list.get(offset).getCa(); info("offset = %d, list.size = %d", offset, list.size()); for (int i = offset + 1; i < list.size(); ++i) { TTransaction t = list.get(i); if (t.isPivot()) { t.setAmount(t.getBalance() - balance); info("IS PIVOT: %s", t); /* henceforth use the new balance */ balance = t.getBalance(); } else { if (t.getCatransfer().equals(ca)) { /** * Transfer transaction (beneficiary side, who gets), balance: don't modify, because this * balance is that of the other one (who gives) */ Assert.assertTrue(!t.getCa().equals(ca) && !t.getCa().equals(none)); Assert.assertTrue(t.getCluster().equals(athelyezes) && athelyezes.getSgn() == -1); // t.setBalance(balance + t.getAmount()); balance += t.getAmount(); } else if (t.getCa().equals(ca)) { /** * Simple transaction, knowing the sign (direction: in/out) of the amount, which we are * talking about */ Assert.assertTrue(!t.getCatransfer().equals(ca)); t.setBalance(balance + t.getCluster().getSgn() * t.getAmount()); balance = t.getBalance(); } } } return this; }
public void execute() { TTransaction old = args.getOldtr(); TTransaction tr = args.getActtr(); Assert.assertNotNull(tr.getCatransfer()); Assert.assertTrue(!tr.getCa().equals(none)); Assert.assertTrue(!tr.getCa().equals(tr.getCatransfer())); Date fromDate, toDate; if (old == null) { fromDate = tr.getDate(); toDate = tr.getDate(); } else { info("Old = " + old); info("New = " + tr); if (tr.getDate().getTime() < old.getDate().getTime()) { fromDate = tr.getDate(); toDate = old.getDate(); } else { fromDate = old.getDate(); toDate = tr.getDate(); } info("date from: " + Util.str(fromDate)); info("date to: " + Util.str(toDate)); } Assert.assertTrue(fromDate.getTime() <= toDate.getTime()); /* update new from:ca */ updateDirtyElements(fromDate, toDate, tr.getCa()); /* update new to:ca */ if (!tr.getCatransfer().equals(none)) { updateDirtyElements(fromDate, toDate, tr.getCatransfer()); } // to be sure, tr.getCa() [new from:ca] & tr.getCatransfer() [new to:ca] // are already validated if (old == null) return; Assert.assertNotNull(old.getCatransfer()); Assert.assertTrue(!old.getCa().equals(none)); Assert.assertTrue(!old.getCa().equals(old.getCatransfer())); /* update old from:ca */ if (!old.getCa().equals(tr.getCa()) && !old.getCa().equals(tr.getCatransfer())) { updateDirtyElements(fromDate, toDate, old.getCa()); } /* update old to:ca */ if (!old.getCatransfer().equals(none) && !old.getCatransfer().equals(tr.getCa()) && !old.getCatransfer().equals(tr.getCatransfer())) { updateDirtyElements(fromDate, toDate, old.getCatransfer()); } }