specification-arg-resolver icon indicating copy to clipboard operation
specification-arg-resolver copied to clipboard

Is there a way to add the attribute constVal="CURRENT_DATE" for OffsetDateTime values?

Open rubenlara opened this issue 6 years ago • 3 comments

rubenlara avatar Mar 12 '19 21:03 rubenlara

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)})
})

rubenlara avatar Mar 12 '19 21:03 rubenlara

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);
	}
	
}

tkaczmarzyk avatar Mar 14 '19 16:03 tkaczmarzyk

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.

rubenlara avatar Mar 17 '19 17:03 rubenlara

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

tkaczmarzyk avatar Dec 29 '22 13:12 tkaczmarzyk