package org.opennms.netmgt.model;

import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.transform.ResultTransformer;

public class OnmsCriteria {
    
    private DetachedCriteria m_criteria;
    private String m_entityName;
    
    public OnmsCriteria(String entityName) {
        this(entityName, DetachedCriteria.forEntityName(entityName));
    }
    
    public OnmsCriteria(String entityName, String alias) {
        this(entityName, DetachedCriteria.forEntityName(entityName, alias));
    }
    
    public OnmsCriteria(Class clazz) {
        this(clazz.getName(), DetachedCriteria.forClass(clazz));
    }
    
    public OnmsCriteria(Class clazz, String alias) {
        this(clazz.getName(), DetachedCriteria.forClass(clazz, alias));
    }
    
    protected OnmsCriteria(String entityName, DetachedCriteria criteria) {
        m_entityName = entityName;
        m_criteria = criteria;
    }
    
    public OnmsCriteria add(Criterion criterion) {
        m_criteria.add(criterion);
        return this;
    }

    public OnmsCriteria addOrder(Order order) {
        m_criteria.addOrder(order);
        return this;
    }

    public OnmsCriteria createAlias(String associationPath, String alias)
    throws HibernateException {
        m_criteria.createAlias(associationPath, alias);
        return this;
    }

    public OnmsCriteria createCriteria(String associationPath, String alias) {
        return new OnmsCriteria(null, m_criteria.createCriteria(associationPath, alias));
    }

    public OnmsCriteria createCriteria(String associationPath) {
        return new OnmsCriteria(null,  m_criteria.createCriteria(associationPath) );
    }

    public String getAlias() {
        return m_criteria.getAlias();
    }

    public OnmsCriteria setFetchMode(String associationPath, FetchMode mode)
    throws HibernateException {
        m_criteria.setFetchMode(associationPath, mode);
        return this;
    }

    public OnmsCriteria setProjection(Projection projection) {
        m_criteria.setProjection(projection);
        return this;
    }

    public OnmsCriteria setResultTransformer(ResultTransformer resultTransformer) {
        m_criteria.setResultTransformer(resultTransformer);
        return this;
    }
    
    public DetachedCriteria getDetachedCriteria() {
        return m_criteria;
    }

    public boolean resultsOfType(Class clazz) {
        if (m_entityName == null) {
            return true;
        } {
            return clazz.getName().endsWith(m_entityName);
        }
    }
    

}

@@ -427,24 +427,107 @@
 entityName;%0A
+    private Integer m_firstResult = null;%0A    private Integer m_maxResults = null;%0A
     %0A    pub
@@ -2812,12 +2812,331 @@
   %7D%0A    
+%0A    public Integer getFirstResult() %7B%0A    %09return m_firstResult;%0A    %7D%0A    %0A    public void setFirstResult(Integer offset) %7B%0A    %09m_firstResult = offset;%0A    %7D%0A    %0A    public Integer getMaxResults() %7B%0A    %09return m_maxResults;%0A    %7D%0A    %0A    public void setMaxResults(Integer limit) %7B%0A    %09m_maxResults = limit;%0A    %7D
 %0A%0A%7D%0A

Add support for limit and offset for fastjay