package org.ofbiz.entity.condition;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.ofbiz.entity.DelegatorInterface;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityConditionList;
import org.ofbiz.entity.condition.EntityJoinOperator;
import org.ofbiz.entity.util.EntityListIterator;

public class EntityQuery {
    private String entityName;

    private EntityJoinOperator operator;

    private List conditions;

    private List orderBy;

    private List fields;

    public EntityQuery(String entityName) {
        this.operator = EntityJoinOperator.AND;
        this.entityName = entityName;
    }

    public EntityQuery() {
        this.operator = EntityJoinOperator.AND;
    };

    public EntityQuery(EntityJoinOperator operator) {
        this.operator = operator;
    }

    public EntityQuery(String entityName, EntityJoinOperator operator) {
        this.entityName = entityName;
        this.operator = operator;
    }

    public Collection findByCondition(DelegatorInterface delegator) throws GenericEntityException {

        return delegator.findByCondition(entityName, getCondition(), getFields(), getOrderBy());
    }

    public EntityListIterator findListIteratorByCondition(DelegatorInterface delegator) throws GenericEntityException {
        return delegator.findListIteratorByCondition(entityName, getCondition(), getFields(), getOrderBy());
    }

    public EntityCondition getCondition() {
        EntityCondition condition = null;
        if (conditions != null) {
            if (conditions.size() == 1) {
                condition = (EntityCondition) conditions.get(0);
            } else {
                condition = new EntityConditionList(getConditions(), operator);
            }
        }
        return condition;
    }

    public void addCondition(EntityCondition condition) {
        getConditions().add(condition);
    }

    public void addOrderBy(String field) {
        addOrderBy(field, true);
    }

    public void addOrderBy(String field, boolean asc) {
        getOrderBy().add(field + (asc ? " ASC" : " DSC"));
    }

    public List getConditions() {
        if (conditions == null) {
            conditions = new ArrayList();
        }
        return conditions;
    }

    public void setConditions(List conditions) {
        this.conditions = conditions;
    }

    public String getEntityName() {
        return entityName;
    }

    public void setEntityName(String entityName) {
        this.entityName = entityName;
    }

    public List getFields() {
        if (fields == null) {
            fields = new ArrayList();
        }
        return fields;
    }

    public void setFields(List fields) {
        this.fields = fields;
    }

    public EntityJoinOperator getOperator() {
        return operator;
    }

    public void setOperator(EntityJoinOperator operator) {
        this.operator = operator;
    }

    public List getOrderBy() {
        if (orderBy == null) {
            orderBy = new ArrayList();
        }
        return orderBy;
    }

    public void setOrderBy(List orderBy) {
        this.orderBy = orderBy;
    }
}
