BorrowService borrowService = (BorrowService) ApplicationContextProvider.getApplicationContext().getBean("borrowService");
// RETURN BORROW
StringValue borrowId = parameters.get("borrowId");
if (!borrowId.isEmpty()) {
BorrowTO borrow = borrowService.findBorrowByID(borrowId.toLong());
borrowService.returnBooks(borrow, borrow.getTitlesTO());
setResponsePage(ShowAllBorrow.class);
}
// EXTEND BORROW
StringValue extend = parameters.get("extend");
if (!extend.isEmpty()) {
BorrowTO borrow = borrowService.findBorrowByID(extend.toLong());
Calendar cal = Calendar.getInstance();
cal.setTime(borrow.getExpirationDate());
cal.add(Calendar.MONTH, 1); // add 1 month
borrow.setExpirationDate(cal.getTime());
borrowService.updateExpirationDate(borrow, borrow.getExpirationDate());
setResponsePage(ShowAllBorrow.class);
}
RepeatingView repeating = new RepeatingView("repeating");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
for (BorrowTO borrow : borrowService.findAllBorrows()) {
AbstractItem item = new AbstractItem(repeating.newChildId());
PageParameters pageParameters = new PageParameters();
pageParameters.add("borrowId", borrow.getBorrowID());
PageParameters pageParameters2 = new PageParameters();
pageParameters2.add("extend", borrow.getBorrowID());
item.add(new BookmarkablePageLink<Void>("extend", ShowAllBorrow.class, pageParameters2));
item.add(new BookmarkablePageLink<Void>("return", ShowAllBorrow.class, pageParameters));
item.add(new Label("reader", borrow.getReaderTO().toString()));
if (borrow.getTitlesTO().size() > 0) {
item.add(new Label("title", borrow.getTitlesTO().toString()));
} else {
item.add(new Label("title", new Model("[RETURNED]")));
}
item.add(new Label("borrowdate", dateFormat.format(borrow.getBorrowDate())));
item.add(new Label("expirationdate", dateFormat.format(borrow.getExpirationDate())));
repeating.add(item);
}
add(repeating);