All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.impetus.kundera.persistence.KunderaCritieriaQuery Maven / Gradle / Ivy

There is a newer version: 3.13
Show newest version
/**
 * 
 */
package com.impetus.kundera.persistence;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Tuple;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.criteria.Subquery;
import javax.persistence.metamodel.EntityType;


/**
 * @author vivek.mishra
 *
 */
public class KunderaCritieriaQuery implements CriteriaQuery
{

    private KunderaCriteriaBuilder criteriaBuilder;
    private Class returnType;
    private QueryPlan queryPlan = new QueryPlan();
    
    KunderaCritieriaQuery(KunderaCriteriaBuilder kunderaCriteriaBuilder)
    {
        this.criteriaBuilder = kunderaCriteriaBuilder;
        this.returnType = (Class) Object.class;
    }

    KunderaCritieriaQuery(KunderaCriteriaBuilder kunderaCriteriaBuilder, Class returnClazz)
    {
        this.criteriaBuilder = kunderaCriteriaBuilder;
        this.returnType = returnClazz;
    }

    @Override
    public  Root from(Class paramClass)
    {
        EntityType entityType = this.criteriaBuilder.getMetaModel().entity(paramClass);
        return fromEntityType(entityType);
    }

    
    @Override
    public  Root from(EntityType paramEntityType)
    {
        
        if(!this.criteriaBuilder.getMetaModel().getEntities().contains(paramEntityType))
        {
            throw new IllegalArgumentException("Invalid entity type, {class:" + paramEntityType.getName() + "}");   
        }
        
        return fromEntityType(paramEntityType);
    }

    @Override
    public  Subquery subquery(Class paramClass)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Set> getRoots()
    {
        return this.queryPlan.from;
    }

    @Override
    public Selection getSelection()
    {
        return (Selection) this.queryPlan.select;
    }

    @Override
    public Predicate getRestriction()
    {
        return this.queryPlan.where;
    }

    @Override
    public List> getGroupList()
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Predicate getGroupRestriction()
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean isDistinct()
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Class getResultType()
    {
        return this.returnType;
    }

    @Override
    public CriteriaQuery select(Selection paramSelection)
    {
        this.queryPlan.setSelection(paramSelection);
        return this;
    }

    @Override
    public CriteriaQuery multiselect(Selection... paramArrayOfSelection)
    {
        SelectionType type = SelectionType.getSelectionType(getResultType());
        switch (type)
        {
        case TUPLE:
                this.queryPlan.setSelection(this.criteriaBuilder.tuple(paramArrayOfSelection));
            break;

        case ARRAY:
            this.queryPlan.setSelection(this.criteriaBuilder.array(this.getResultType(),paramArrayOfSelection));
            break;

        case OBJECT:
            this.queryPlan.setSelection(this.criteriaBuilder.construct(getResultType(), paramArrayOfSelection));
            break;

        }

        return this;
    }

    @Override
    public CriteriaQuery multiselect(List> paramList)
    {
//        this.queryPlan
        return multiselect((Selection[]) paramList.toArray());
    }

    @Override
    public CriteriaQuery where(Expression paramExpression)
    {
        if (Predicate.class.isAssignableFrom(paramExpression.getClass()))
        {
            this.queryPlan.setWhere(this.criteriaBuilder.and((Predicate)paramExpression));
        }
        
        return this;
    }

    @Override
    public CriteriaQuery where(Predicate... paramArrayOfPredicate)
    {
        this.queryPlan.setWhere(this.criteriaBuilder.and(paramArrayOfPredicate));
        return this;
    }

    @Override
    public CriteriaQuery groupBy(Expression... paramArrayOfExpression)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public CriteriaQuery groupBy(List> paramList)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public CriteriaQuery having(Expression paramExpression)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public CriteriaQuery having(Predicate... paramArrayOfPredicate)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public CriteriaQuery orderBy(Order... paramArrayOfOrder)
    {
        return orderBy(Arrays.asList(paramArrayOfOrder));
    }

    @Override
    public CriteriaQuery orderBy(List paramList)
    {
        // TODO Auto-generated method stub
        this.queryPlan.setOrderBy(paramList);
        return this;
    }

    @Override
    public CriteriaQuery distinct(boolean paramBoolean)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List getOrderList()
    {
        return this.queryPlan.orderBy;
    }

    @Override
    public Set> getParameters()
    {
        // TODO Auto-generated method stub
        return null;
    }

    private  Root fromEntityType(EntityType paramEntityType)
    {
        Root root =  new DefaultRoot(paramEntityType);
        this.queryPlan.add(root);
        return root;
    }

    enum SelectionType
    {
        TUPLE, ARRAY, OBJECT;
        
        static SelectionType getSelectionType(Class clazz)
        {
            if(clazz.isAssignableFrom(Tuple.class))
            {
                return TUPLE;
            }else if(clazz.isArray())
            {
                return ARRAY;
            }else
            {
                return OBJECT;
            }
        }
    }
    
    class QueryPlan
    {
        private Selection select;
        private Set> from = new HashSet>();
        private Predicate where;
        private List orderBy;
     
        void add(Root root)
        {
            from.add(root);
        }
        
        void setWhere(Predicate predicate)
        {
            this.where = predicate;
        }
        
        void setSelection(Selection selection)
        {
            this.select = selection;
        }
        
        
        void setOrderBy(List orderBy)
        {
            this.orderBy = orderBy;
        }
    }
}