specification-arg-resolver
specification-arg-resolver copied to clipboard
Is there a way to add the attribute constVal="CURRENT_DATE" for OffsetDateTime values?
Hi,
first of all: greate library!
I was wondering if there is the possibility to add the value "CURRENT_DATE" to the constVal aatribute?
I'm actually getting this error:
Parameter value [CURRENT_DATE] did not match expected type [java.time.OffsetDateTime (n/a)]
I'm defining the Query as follow:
@Conjunction({
@Or({
@Spec(path = "validTo", spec = GreaterThan.class),
@Spec(path = "validTo", spec = Null.class, constVal = "true")
}),
@Or({
@Spec(path = "name", spec = Equal.class),
@Spec(path = "name", spec = Null.class, constVal = "true")
}),
@Or({@Spec(path = "sourceSystem", spec = Equal.class)}),
@Or({
//@Spec(path = "a.expired", spec = GreaterThan.class, constVal = "CURRENT_DATE", config="yyyy-MM-dd'T'HH:mm:ssxxx"),
@Spec(path = "a.expired", spec = Null.class, constVal = "true")
}),
@Or({@Spec(path = "a.abonnementId", spec = In.class)}),
@Or({@Spec(path = "a.abonnementTyp", spec = Equal.class)}),
@Or({@Spec(path = "a.version", spec = Equal.class)})
})
Hi, there is no such functionality now. It's a great idea though, I'll try to add it to the next release.
Right now the easiest way is to implement a new spec class and use it instead of GreaterThan. It could look similar to this (I didn't test it, it's just a draft):
public class InTheFuture<T> extends ComparableSpecification<T> {
private static final long serialVersionUID = 1L;
public InTheFuture(QueryContext queryContext, String path, String[] httpParamValues, Converter converter) {
super(queryContext, path, httpParamValues, converter);
}
@Override
protected <Y extends Comparable<? super Y>> Predicate makePredicate(CriteriaBuilder cb, Expression<? extends Y> x, Y y) {
OffsetDateTime now = OffsetDateTime.now();
return cb.greaterThan(x, (Y) now);
}
}
Hello Tomasz, that was a great hint! By the way, I have been experiencing this fenomena: when one declares a query parameter to be Equal, and the client sends more then one values, it throws an IllegalStateException. Thats correct but It would be maybe better if throwing an own library exception with a message like "Wrong size of parameters for 'paramName'. Expected 1 but was x", for example. Anyway, thanks a lot for the hint above.
InTheFuture and InThePast were added in https://github.com/tkaczmarzyk/specification-arg-resolver/pull/160 and will be released in v2.15.0. The comment about more descriptive exception messages is being addressed in https://github.com/tkaczmarzyk/specification-arg-resolver/pull/165