Monday, February 13, 2017

XPath: check if property is null

Suppose you are receiving an object with a testProperty property in input...
For example:
case 1)
{
 "testProperty":null
}
or
case 2)
{
 "testProperty":"test"
}

Please declare the property in this way:
<property xmlns:cct="http://www.tempuri.org/" name="custom_property_name" expression="//testProperty/text()" scope="default" type="STRING"/>

and test it in this way:
<log level="custom">
    <property expression="boolean(get-property('custom_property_name'))" name="message"/>

</log>

The output will be:
case 1)
INFO - LogMediator message= false
case 2)
INFO - LogMediator message= true


Bye...


Friday, January 27, 2017

Spring: conversion of List to Page with pagination (PageImpl/Pageable)

Suppose you're retrieving via typedQuery some data.
You probably are getting a List<T> via getResultList() method(TypedQuery class).
Now you need to convert into into a Page<T> result, preserving pagination, obtained with page, size(, ecc.) parameters.....
In my case I solve it with this:
[....]
int pageNumber = 1 ;
int size = 1 ;
PageRequest page = new PageRequest(pageNumber, size);
List<Article> articlesList = articleTypedQuery.getResultList();
int start = page.getOffset();
int end = (start + page.getPageSize()) > articlesList .size() ? articlesList .size() : (start + page.getPageSize());       
int totalRows = articlesList .size();
Page<Article> pageToReturn = new PageImpl<Article>(articlesList .subList(start, end), page, totalRows); 


return pageToReturn;

That's all.....
Bye..