Search code examples
javajava-streamjax-rsresteasyquerydsl

How can i handle multiple combinations of query parameters in a resource class efficiently?


I have the case where i need to write resource classes for our API that take more than four query parameters, and the number of if else statements i need to write is equivalent to the cardinality of the power set of all those query parameters, so in case of 4 query parameters, i need to write 16 if else statements, 5 -> 36 etc.

Is there a better was to do what i am trying to do? Stream API is not a good option since this is an enterprise application and the number of entities is 30000 and more and there are a lot of users using the API and we had OutOfMemoryError in the past.

I am using Querydsl, and i can't pass null values to the eq() method.


Solution

  • I passed the parameters from the controller to the facade and did the following using Querydsl

    public List<Entity> findAll(Optional<Integer> opId, Optional<String> opName) {
        final QEntity q = QEntity.entity;
    
        final BooleanBuilder where = new BooleanBuilder();
    
        opId.ifPresent(id -> where.and(q.id.eq(id)));
        opName.ifPresent(name -> where.and(q.name.eq(name)));
            
        return new JPAQuery.where(where).list(q);
    }