One use case is to show the logged in user only his/her own payments and not all payments.
In the “old web mvc scaffold” list view I pushed in the list method and changed it in order to retrieve all payments records of the current logged in user, see code below.
Now I want to migrate to gvNIX datatables. Default it shows all the records in the database.
Question: how can I filter only the records of this user?
A simple example would be great or more documentation.
@RequestMapping(produces = "text/html")
public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, @RequestParam(value = "sortFieldName", required = false) String sortFieldName, @RequestParam(value = "sortOrder", required = false) String sortOrder, Model uiModel) {
System.out.println("In payment list controller");
if (page != null || size != null) {
int sizeNo = size == null ? 10 : size.intValue();
final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
// uiModel.addAttribute("payments", Payment.findPaymentEntries(firstResult, sizeNo, sortFieldName, sortOrder));
User currentUser = getCurrentUser();
List<Payment> paymentList = Payment.findPaymentsByPayUser(getCurrentUser(), firstResult, sizeNo, "transactionDate", "DESC");
uiModel.addAttribute("payments", paymentList);
float nrOfPages = (float) Payment.countFindPaymentsByPayUser(currentUser) / sizeNo;
uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
} else {
// uiModel.addAttribute("payments", Payment.findAllPayments(sortFieldName, sortOrder));
uiModel.addAttribute("payments", Payment.findPaymentsByPayUser(getCurrentUser(), "transactionDate", "DESC").getResultList());
}
addDateTimeFormatPatterns(uiModel);
return "payments/list";
}
private User getCurrentUser() {
// Retrieve the current logged in user and his authentication
String username = SecurityContextHolder.getContext().getAuthentication().getName();
// Retrieve the user object of the current user
User user = User.findUsersByEmailAddress(username).getSingleResult();
return user;
}
As Datatables uses Ajax request to retrieve data, the
list
method is used just as page drawer (this is done bydeclare precedence
AspectJ directive on_Roo_GvNIXDatatables.aj
).To customize the data get by Datatables you should push-in the
setDatatablesBaseFilter
. You can found it in the_Roo_GvNIXDatatables.aj
file. You can try something like this in your controller:If you need a more complex filter you should customize the
findAllXXX
andretrieveData
methods (the first for data visualization and the second for the export feature).Good luck!