2015-11-22 3 views
1

У меня есть pojo, которое содержит имя свойства, логический оператор как String и значение свойства. То, что я хочу выполнить, это создать Predicate или Expression и т. Д. Динамически из данных pojo. Ниже мой код:Как создать динамический оператор QueryDSL на основе цитированного значения String

public class QueryParam { 
    private String property = "acctType"; //can be any property of classname 
    private String operator = "eqic"  //can be any logic operator !=, >, <, >=, <= etc 
    private Object value;     //will store the value of 

    // getters/setters here 
} 

public interface CustomerRepository extends JpaRepository<Customer, Long>, QueryDslPredicateExecutor<Customer>{ 

} 

@Service("CustomerService") 
class MyCustomerServiceImpl { 
    @Resource 
    private CustomerRepository custRpstry; 

    //if classname is Customer, property is "acctType", operator is "eqic", and value is "Corporate" 
    //I want my findAll below to retrieve all Customers having acctType = "Corporate" 
    List<Customer> findAll(List<QueryParam> qryParam) { 
     QCustomer qcust = QCustomer.customer; 

     BooleanBuilder where = new BooleanBuilder(); 
     for(QueryParam param : qryParam) { 

      //within this block, i want a BooleanBuilder to resolve to: 
      where.and(qcust.acctType.equalsIgnoreCase("Corporate")); 

      something like: 
      where.and(param.getClassname().param.getProperty().param.getOperator().param.getValue()) 
     } 

     return custRpstry.findAll(where.getValue()).getContent(); 

    } 
} 

Я не могу понять, сформулировать свою BooleanBuilder особенно ту часть, которая будет конвертировать getOperator() в .equalIgnoreCase().

Любая помощь была бы принята с благодарностью.

Спасибо заранее, Марио

ответ

1

После объединения нескольких ответов на некоторые вопросы, связанные здесь в это так, я был в состоянии сформулировать решение, которое работает для меня.

BooleanBuilder where = new BooleanBuilder(); 
for(QueryParam param: qryParam) { 
    //create: Expressions.predicate(Operator<Boolean> opr, StringPath sp, filter value) 

    //create an Operator<Boolean> 
    Operator<Boolean> opr = OperationUtils.getOperator(param.getOperator().getValue()); 

    //create a StringPath to a class' property 
    Path<User> entityPath = Expressions.path(Customer.class, "customer"); 
    Path<String> propPath = Expressions.path(String.class, entityPath, param.getProperty()); 

    //create Predicate expression 
    Predicate predicate = Expressions.predicate(opr, propPath, Expressions.constant(param.getValue())); 
    where.and(predicate); 
} 

list = repository.findAll(where.getValue(), pageReq).getContent(); 

Мои OperationUtils.java

public class OperationUtils { 

    public static com.mysema.query.types.Operator<Boolean> getOperator(String key) { 
     Map<String, com.mysema.query.types.Operator<Boolean>> operators = ImmutableMap.<String, com.mysema.query.types.Operator<Boolean>>builder() 
        .put(Operator.EQ.getValue() ,Ops.EQ) 
        .put(Operator.NE.getValue() ,Ops.NE) 
        .put(Operator.GT.getValue() ,Ops.GT) 
        .put(Operator.GTE.getValue() ,Ops.GOE) 
        .put(Operator.LT.getValue() ,Ops.LT) 
        .put(Operator.LTE.getValue() ,Ops.LOE) 
        .build(); 

     return operators.get(key); 
    } 
} 
Смежные вопросы