package com.github.mhewedy.expressions;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.github.mhewedy.expressions.Expression.*;
import static com.github.mhewedy.expressions.Operator.$and;
import static com.github.mhewedy.expressions.Operator.$or;
/**
* Represents Group of expression using mongodb query api.
*
* Example:
*
* {
* "status": "A",
* "$or": [{ "qty": { "$lt": 30 } }, { "item": { "$in": ["A", "D"] } }]
* }
*
*
* Support a list of Operators defined in {@link Operator}.
*
* @see ExpressionsRepository#findAll(Expressions)
* @see ExpressionsRepository#findAll(Expressions, Sort)
* @see ExpressionsRepository#findAll(Expressions, Pageable)
* @see Mongo Query Documents
*/
public class Expressions extends HashMap {
/**
* Create a new $or expression on the root,
* then adds to it the current expressions attached at the root
* and the expression passes as parameter.
*
* Example:
* Suppose we have the following expression as json:
{"firstName": "ali"}
*
* Then we added the following:
*
* expressions.or(Expression.and(
* Expression.of("lastName", Operator.$eq, "ibrahim"),
* Expression.of("age", Operator.$gte, 10)
* ));
*
* Then the output could be represented as:
*
*
* {
* "$or": [
* {"firstName": "ali"},
* {
* "$and": [
* {"lastName": "ibrahim"},
* {"age": {"$gte": 10}},
* ]
* }
* ]
* }
*
* Or in sql as:
*
* where firstName = "ali" or lastName = "ibrahim" and age >= 10
*
*/
public Expressions or(Expression expression) {
Map tmp = new HashMap<>(this);
this.clear();
List