Hello all,
do you have any experience with DefaultQueryModifierDelegate.queryWillBeBuilt() implementation please?
I am familiar with filtersWillBeAdded() method (or, better words are: “it simply works in my application” :-)) but I don’t know how to modifify other parts of the query. What I need is to add Group By clause according to user configuration (user selects in configurator which attribute should be grupped).
Now, in first step I try to modify select clause, but it doesn’t works and it doesn’t even throw any exception. The results are same as if I didn’t any modifications.
Could you give me some hint how to achieve my goal please?
Thank you in advance!
Here is my code:
public static DefaultQueryModifierDelegate getDefaultQueryModifierDelegate(
final Table filterTable, final Table groupTable, final Item entityItem,
final Class<?> entityClass, final Boolean isAnd) {
EntityManager em = JPAContainerFactory.createEntityManagerForPersistenceUnit("ranna");
final Metamodel metamodel = em.getMetamodel();
return new DefaultQueryModifierDelegate() {
@Override
public void queryWillBeBuilt(CriteriaBuilder cb, CriteriaQuery<?> query) {
Root<?> entity = query.getRoots().iterator().next();
List<Selection<?>> selection = new ArrayList<Selection<?>>();
CriteriaQuery<?> q = cb.createQuery(entityClass);
// in groupTable a user selects according which attributes he wants to group the result
for(Object o:groupTable.getItemIds()) {
String propValue = String.valueOf(((ComboBox) groupTable.getItem(o).getItemProperty("Pole").getValue()).getValue());
selection.add(entity.get(propValue));
}
// Attributes of type Double and Integer will be aggregated by SUM function
for(Object o:entityItem.getItemPropertyIds()) {
Attribute<?,?> attribute = metamodel.managedType(entityClass).getAttribute(String.valueOf(o));
Class<?> type = attribute.getJavaType();
if (type.getName().contains("Double")) {
selection.add(cb.sum(entity.<Double>get(String.valueOf(o))));
}
else if (type.getName().contains("Integer")) {
selection.add(cb.sum(entity.<Integer>get(String.valueOf(o))));
}
} //for
// new selection is assigned to query
query = q.multiselect(selection);
}
@Override
public void filtersWereAdded(CriteriaBuilder cb, CriteriaQuery<?> query) {
// here I want to put GROUP BY statement in next step
}
};
} // getDefaultQueryModifierDelegate