Example usage for org.hibernate.criterion DetachedCriteria getExecutableCriteria

List of usage examples for org.hibernate.criterion DetachedCriteria getExecutableCriteria

Introduction

In this page you can find the example usage for org.hibernate.criterion DetachedCriteria getExecutableCriteria.

Prototype

public Criteria getExecutableCriteria(Session session) 

Source Link

Document

Get an executable instance of Criteria to actually run the query.

Usage

From source file:org.molasdin.wbase.hibernate.BasicHibernateEngine.java

License:Apache License

@Override
public <U> U invokeCriteriaForSingle(DetachedCriteria criteria, Class<U> clazz) {
    return invokeCriteriaForSingle(criteria.getExecutableCriteria(session()), clazz);
}

From source file:org.molasdin.wbase.hibernate.cursor.BasicHibernateCursor.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public List<T> dataCallback(Session session) {
    DetachedCriteria criteria = searchSpecification().query();
    List<Pair<String, org.molasdin.wbase.storage.Order>> orders = orders();
    if (!orders.isEmpty()) {
        for (Pair<String, org.molasdin.wbase.storage.Order> order : orders) {
            String prop = translateProperty(order.getLeft());
            criteria.addOrder(org.molasdin.wbase.storage.Order.ASC.equals(order.getRight())
                    ? org.hibernate.criterion.Order.asc(prop)
                    : org.hibernate.criterion.Order.desc(prop));
        }/*from  www.  j  ava  2s  .  c  o  m*/
    }

    criteria.add(populateFilters(searchSpecification().filterModes()));
    return postProcessData((List<T>) criteria.getExecutableCriteria(session)
            .setFirstResult(calculatedRowOffset()).setMaxResults(pageSize()).list());
}

From source file:org.molasdin.wbase.hibernate.cursor.BasicHibernateCursor.java

License:Apache License

@Override
public Long totalCallback(Session session) {
    DetachedCriteria criteria = searchSpecification().query();
    if (searchSpecification().distinctProperty() != null) {
        criteria.setProjection(Projections.countDistinct(searchSpecification().distinctProperty()));
    } else {/*from   w  ww. j a v a 2  s.  c  o m*/
        criteria.setProjection(Projections.rowCount());
    }
    criteria.add(populateFilters(searchSpecification().filterModes()));
    return (Long) criteria.getExecutableCriteria(session).uniqueResult();
}

From source file:org.n2.app.beans.hibernate.UserDao.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from ww w .  ja v a 2 s .c o m*/
public List<User> find(DetachedCriteria criteria) {
    return criteria.getExecutableCriteria(getSession()).list();
}

From source file:org.onecmdb.core.internal.storage.hibernate.HibernateDao.java

License:Open Source License

public QueryResult<ICi> query(QueryCriteria criteria, boolean count) {
    DetachedCriteria hibCiCriteria = DetachedCriteria.forClass(ConfigurationItem.class);
    DetachedCriteria hibAttributeCriteria = DetachedCriteria.forClass(BasicAttribute.class);

    // Search in the db...
    if (criteria.getOffspringOfId() != null) {
        try {/*  w ww.j  av  a2 s  .c om*/
            // Query for an unique id.
            Long longId = Long.parseLong(criteria.getOffspringOfId());
            hibCiCriteria.add(Expression.eq("derivedFromId", longId));
        } catch (NumberFormatException e) {
            log.warn("QueryCriteria contained not a long offspringId <" + criteria.getCiId());
            throw new IllegalArgumentException("Not a correct long ci id <" + criteria.getCiId());
        }
    } else if (criteria.getOffspringOfAlias() != null) {
        ICi ci = findCiByAlias(new Path<String>(criteria.getOffspringOfAlias()));
        if (criteria.getOffspringDepth() != null) {
            if (ci == null) {
                // Is an error, but we don't throw an exception, instead it will return empty/0 
                DetachedCriteria hibAliasCiCriteria = DetachedCriteria.forClass(ConfigurationItem.class);
                hibAliasCiCriteria.add(Expression.eq("alias", criteria.getOffspringOfAlias()));
                DetachedCriteria idCriteria = hibAliasCiCriteria.setProjection(Projections.property("longId"));
                hibCiCriteria.add(Property.forName("derivedFromId").in(idCriteria));
            } else {
                // TODO: append %/%/% according to offspring depth. 
                hibCiCriteria.add(Expression.ilike("templatePath", ci.getTemplatePath() + "/%"));
            }
        } else {
            if (ci != null) {
                hibCiCriteria.add(Expression.eq("derivedFromId", ci.getId().asLong()));
            } else {
                hibCiCriteria.add(Expression.eq("derivedFromId", new Long(0)));
            }
        }

        //hibAttributeCriteria.add(Expression.eq("alias", criteria.getOffspringOfAlias()));
    }

    if (criteria.getCiAlias() != null) {
        hibCiCriteria.add(Expression.eq("alias", criteria.getCiAlias()));
    } else if (criteria.getCiId() != null) {
        try {
            // Query for an unique id.
            Long longId = Long.parseLong(criteria.getCiId());
            hibCiCriteria.add(Expression.eq("longId", longId));
        } catch (NumberFormatException e) {
            log.warn("QueryCriteria contained not a long ci id <" + criteria.getCiId());
            throw new IllegalArgumentException("Not a correct long ci id <" + criteria.getCiId());
        }
        /*
        if (ci == null || ci instanceof IAttribute) {
           if (count) {
              result.setTotalHits(0);
           } 
        } else {
           if (count) {
              result.setTotalHits(1);
           }
           result.add(ci);
        }
        return(result);
        */
    }
    if (criteria.getMatchType() != null) {
        ICi type = findCiByAlias(new Path<String>(criteria.getMatchType()));
        if (type != null) {
            Disjunction orAttribute = Restrictions.disjunction();
            String path = type.getTemplatePath();
            String paths[] = path.split("/");
            if (paths.length > 1) {
                for (int i = 1; i < paths.length; i++) {
                    orAttribute.add(Expression.ilike("typeName", "%#" + paths[i], MatchMode.START));

                }
                DetachedCriteria typeCrit = DetachedCriteria.forClass(BasicAttribute.class);
                typeCrit.add(Expression.isNull("derivedFromId"));
                typeCrit.add(orAttribute);
                DetachedCriteria idCrit = typeCrit.setProjection(Projections.property("ownerId"));
                hibCiCriteria.add(Property.forName("longId").in(idCrit));
                if (criteria.getMatchCiPath() != null) {
                    String idPath = "";
                    String ciPath[] = criteria.getMatchCiPath().split("/");
                    if (ciPath.length > 0) {
                        for (int i = 0; i < ciPath.length; i++) {
                            ICi ci = findCiByAlias(new Path<String>(ciPath[i]));
                            if (ci != null) {
                                idPath += "/" + ci.getId().asLong();
                            }
                        }
                        // TODO: append %/%/% according to offspring depth. 
                        hibCiCriteria.add(Expression.ilike("templatePath", idPath + "/%"));

                    }
                }
            }

        }
    }

    if (criteria.isMatchCiTemplates() && criteria.isMatchCiInstances()) {
        // Search Both.
    } else if (criteria.isMatchCiTemplates()) {
        hibCiCriteria.add(Expression.eq("isBlueprint", Boolean.TRUE));
    } else if (criteria.isMatchCiInstances()) {
        hibCiCriteria.add(Expression.eq("isBlueprint", Boolean.FALSE));
    }
    if (criteria.isMatchAttributeTemplates() && criteria.isMatchAttributeInstances()) {
        // Search both
    } else if (criteria.isMatchAttributeTemplates()) {
        hibAttributeCriteria.add(Expression.eq("isBlueprint", Boolean.TRUE));
    } else if (criteria.isMatchAttributeInstances()) {
        hibAttributeCriteria.add(Expression.eq("isBlueprint", Boolean.FALSE));
    }

    if (criteria.getText() != null) {
        Disjunction orAttribute = Restrictions.disjunction();
        Disjunction orCi = Restrictions.disjunction();
        boolean orAttributeAdded = false;
        boolean orCiAdded = false;

        if (criteria.isTextMatchAlias()) {
            orCi.add(Expression.ilike("alias", criteria.getText(), MatchMode.ANYWHERE));
            orAttribute.add(Expression.ilike("alias", criteria.getText(), MatchMode.ANYWHERE));
            orAttributeAdded = true;
            orCiAdded = true;

        }

        if (criteria.isTextMatchDescription()) {
            orCi.add(Expression.ilike("description", criteria.getText(), MatchMode.ANYWHERE));
            orAttribute.add(Expression.ilike("description", criteria.getText(), MatchMode.ANYWHERE));
            orAttributeAdded = true;
            orCiAdded = true;

        }

        if (criteria.isTextMatchValue()) {
            orAttribute.add(Expression.ilike("valueAsString", criteria.getText(), MatchMode.ANYWHERE));
            orAttributeAdded = true;
            // Enable Attribute serach....
            criteria.setMatchAttribute(true);
        }
        /*
        DetachedCriteria idCriteria = hibAttributeCriteria.setProjection(Projections.property("ownerId"));
        orCi.add(Property.forName("longId").in(idCriteria));
        */

        if (orAttributeAdded) {
            if (criteria.getMatchAttributeAlias() != null) {
                hibAttributeCriteria.add(Expression.eq("alias", criteria.getMatchAttributeAlias()));
            }
            hibAttributeCriteria.add(orAttribute);
            DetachedCriteria idCriteria = hibAttributeCriteria.setProjection(Projections.property("ownerId"));
            orCi.add(Property.forName("longId").in(idCriteria));
            orCiAdded = true;
        }
        if (orCiAdded) {
            hibCiCriteria.add(orCi);
        }
    }

    QueryResult<ICi> result = new QueryResult<ICi>();
    /*
    if (criteria.isMatchAttribute()) {
       DetachedCriteria idCriteria = hibAttributeCriteria.setProjection(Projections.property("ownerId"));
       hibCiCriteria.add(Property.forName("longId").in(idCriteria));
    }
    */

    // Search ICi.
    Session session = getSession();
    try {
        Profiler.start("QueryCi():");
        if (count) {
            Criteria hibCriteria = hibCiCriteria.getExecutableCriteria(session);
            hibCriteria.setProjection(Projections.rowCount());
            List list = hibCriteria.list();
            if (list != null && !list.isEmpty()) {
                Integer itemCount = ((Integer) list.get(0)).intValue();
                result.setTotalHits(itemCount);
            }
        } else {

            if (criteria.getOrderAttAlias() != null) {
                DetachedCriteria idCriteria = hibCiCriteria.setProjection(Projections.property("longId"));

                DetachedCriteria attr = DetachedCriteria.forClass(BasicAttribute.class);
                attr.add(Expression.eq("alias", criteria.getOrderAttAlias()));
                attr.add(Property.forName("ownerId").in(idCriteria));
                if (criteria.isOrderAscending()) {
                    attr.addOrder(Order.asc(criteria.getOrderType()));
                } else {
                    attr.addOrder(Order.desc(criteria.getOrderType()));
                }

                Criteria attrCriteria = attr.getExecutableCriteria(session);
                if (criteria.getMaxResult() != null) {
                    attrCriteria.setMaxResults(criteria.getMaxResult());
                }

                if (criteria.getFirstResult() != null) {
                    attrCriteria.setFirstResult(criteria.getFirstResult());
                }

                List<IAttribute> attrs = attrCriteria.list();
                for (IAttribute a : attrs) {
                    result.add(a.getOwner());
                }
            } else {
                hibCiCriteria.addOrder(Order.asc("alias"));

                Criteria hibCriteria = hibCiCriteria.getExecutableCriteria(session);
                if (criteria.getMaxResult() != null) {
                    hibCriteria.setMaxResults(criteria.getMaxResult());
                }

                if (criteria.getFirstResult() != null) {
                    hibCriteria.setFirstResult(criteria.getFirstResult());
                }

                List objects = hibCriteria.list();
                result.addAll(objects);
            }
        }
    } finally {
        Profiler.stop();

        closeSession(session);
    }
    return (result);
}

From source file:org.onecmdb.core.internal.storage.hibernate.HibernateDao.java

License:Open Source License

public Integer queryCriteriaCount(DetachedCriteria detachedCrit) {
    Session session = getSession();/*w ww. j  a va  2 s .co  m*/
    List result = Collections.EMPTY_LIST;
    Integer itemCount = new Integer(0);
    try {
        Profiler.start("QueryCriteria():");
        Criteria criteria = detachedCrit.getExecutableCriteria(session);
        criteria.setProjection(Projections.rowCount());
        List list = criteria.list();
        if (list != null && !list.isEmpty()) {
            itemCount = ((Integer) list.get(0)).intValue();
        }

    } finally {
        Profiler.stop();
        closeSession(session);
    }
    return (itemCount);

}

From source file:org.onecmdb.core.internal.storage.hibernate.HibernateDao.java

License:Open Source License

public List queryCriteria(DetachedCriteria detachedCrit, PageInfo info) {
    Session session = getSession();//from  w w  w  .j  a  v  a 2  s  . c  o m
    List result = Collections.EMPTY_LIST;
    try {
        Profiler.start("QueryCriteria():");
        Criteria criteria = detachedCrit.getExecutableCriteria(session);

        if (info != null) {
            if (info.getFirstResult() != null) {
                criteria.setFirstResult(info.getFirstResult());
            }
            if (info.getMaxResult() != null) {
                criteria.setMaxResults(info.getMaxResult());
            }
        }
        result = criteria.list();
    } finally {
        Profiler.stop();
        closeSession(session);
    }
    return (result);

}

From source file:org.onecmdb.core.internal.storage.hibernate.HibernateDao.java

License:Open Source License

public QueryResult queryExpression(OneCMDBExpression expr) {
    QueryResult<ICi> result = new QueryResult<ICi>();
    Session session = getSession();/*from   www. j a  v a  2 s  .  co  m*/
    try {
        Profiler.start("QueryCi():");
        if (expr.isCount()) {
            Criteria criteria = expr.composeCriteria().getExecutableCriteria(session);
            criteria.setProjection(Projections.rowCount());
            List list = criteria.list();
            if (list != null && !list.isEmpty()) {
                Integer itemCount = ((Integer) list.get(0)).intValue();
                result.setTotalHits(itemCount);
            }
        } else {

            // Debug test.
            DetachedCriteria aCrit = expr.getOrderCriteria();
            if (aCrit != null) {
                Criteria criteria = aCrit.getExecutableCriteria(session);
                if (expr.getMaxResult() != null) {
                    criteria.setMaxResults(expr.getMaxResult());
                }

                if (expr.getFirstResult() != null) {
                    criteria.setFirstResult(expr.getFirstResult());
                }

                List objects = criteria.list();
                for (Object o : objects) {
                    result.add(((IAttribute) o).getOwner());
                }
            } else {
                Criteria criteria = expr.composeCriteria().getExecutableCriteria(session);
                if (expr.getMaxResult() != null) {
                    criteria.setMaxResults(expr.getMaxResult());
                }

                if (expr.getFirstResult() != null) {
                    criteria.setFirstResult(expr.getFirstResult());
                }
                List objects = criteria.list();
                result.addAll(objects);
            }
        }
    } finally {
        Profiler.stop();

        closeSession(session);
    }
    return (result);

}

From source file:org.onecmdb.core.internal.storage.hibernate.HibernateDao.java

License:Open Source License

public QueryResult<ICi> queryOld(QueryCriteria criteria, boolean count) {
    DetachedCriteria hibCiCriteria = DetachedCriteria.forClass(ConfigurationItem.class);
    DetachedCriteria hibAttributeCriteria = DetachedCriteria.forClass(BasicAttribute.class);

    // Search in the db...
    if (criteria.getOffspringOfId() != null) {
        hibCiCriteria.add(Expression.eq("derivedFromId", criteria.getOffspringOfId()));
        hibAttributeCriteria.add(Expression.eq("derivedFromId", criteria.getOffspringOfId()));
    }// www .ja v  a 2 s . c  o m
    if (criteria.isMatchCiTemplates() && criteria.isMatchCiInstances()) {
        // Search Both.
    } else if (criteria.isMatchCiTemplates()) {
        hibCiCriteria.add(Expression.eq("isBlueprint", Boolean.TRUE));
    } else if (criteria.isMatchCiInstances()) {
        hibCiCriteria.add(Expression.eq("isBlueprint", Boolean.FALSE));
    }
    if (criteria.isMatchAttributeTemplates() && criteria.isMatchAttributeInstances()) {
        // Search both
    } else if (criteria.isMatchAttributeTemplates()) {
        hibAttributeCriteria.add(Expression.eq("isBlueprint", Boolean.TRUE));
    } else if (criteria.isMatchAttributeInstances()) {
        hibAttributeCriteria.add(Expression.eq("isBlueprint", Boolean.FALSE));
    }

    if (criteria.getText() != null) {
        Disjunction orAttribute = Restrictions.disjunction();
        Disjunction orCi = Restrictions.disjunction();
        boolean orAttributeAdded = false;
        boolean orCiAdded = false;

        if (criteria.isTextMatchAlias()) {
            orCi.add(Expression.ilike("alias", criteria.getText(), MatchMode.ANYWHERE));
            orAttribute.add(Expression.ilike("alias", criteria.getText(), MatchMode.ANYWHERE));
            orAttributeAdded = true;
            orCiAdded = true;

        }

        if (criteria.isTextMatchDescription()) {
            orCi.add(Expression.ilike("description", criteria.getText(), MatchMode.ANYWHERE));
            orAttribute.add(Expression.ilike("description", criteria.getText(), MatchMode.ANYWHERE));
            orAttributeAdded = true;
            orCiAdded = true;

        }

        if (criteria.isTextMatchValue()) {
            orAttribute.add(Expression.ilike("valueAsString", criteria.getText(), MatchMode.ANYWHERE));
            orAttributeAdded = true;
        }
        if (orAttributeAdded) {
            hibAttributeCriteria.add(orAttribute);
        }
        if (orCiAdded) {
            hibCiCriteria.add(orCi);
        }
    }

    QueryResult<ICi> result = new QueryResult<ICi>();
    if (criteria.isMatchCi()) {
        // Search ICi.
        Session session = getSession();
        // Lock taken, can not do anything else.
        try {
            Profiler.start("QueryCi():");
            Criteria hibCriteria = hibCiCriteria.getExecutableCriteria(session);

            if (count) {

                hibCriteria.setProjection(Projections.rowCount());
                List list = hibCriteria.list();
                if (list != null && !list.isEmpty()) {
                    Integer itemCount = ((Integer) list.get(0)).intValue();
                    result.setTotalHits(itemCount);
                }
            } else {
                if (criteria.getMaxResult() != null) {
                    hibCriteria.setMaxResults(criteria.getMaxResult());
                }

                if (criteria.getFirstResult() != null) {
                    hibCriteria.setFirstResult(criteria.getFirstResult());
                }

                hibCriteria.addOrder(Order.asc("alias"));
                List objects = hibCriteria.list();
                result.addAll(objects);
            }
        } finally {
            Profiler.stop();

            closeSession(session);
        }
    }
    if (criteria.isMatchAttribute()) {
        // Serach Attributes.
        List<ICi> cis = null;
        Session session = getSession();
        // Lock taken, can not do anything else.
        try {
            Profiler.start("QueryAttribute():");

            DetachedCriteria idCriteria = hibAttributeCriteria.setProjection(Projections.property("ownerId"));

            DetachedCriteria crit = DetachedCriteria.forClass(ConfigurationItem.class);
            crit.add(Property.forName("longId").in(idCriteria));

            Criteria hibCriteria = crit.getExecutableCriteria(session);
            if (count) {

                hibCriteria.setProjection(Projections.rowCount());

                List list = hibCriteria.list();
                if (list != null && !list.isEmpty()) {
                    Integer itemCount = ((Integer) list.get(0)).intValue();
                    result.setTotalHits(result.getTotalHits() + itemCount);
                }
            } else {
                if (criteria.getMaxResult() != null) {
                    hibCriteria.setMaxResults(criteria.getMaxResult());
                }

                if (criteria.getFirstResult() != null) {
                    hibCriteria.setFirstResult(criteria.getFirstResult());
                }

                hibCriteria.addOrder(Order.asc("alias"));

                cis = hibCriteria.list();
            }
        } finally {
            Profiler.stop();

            closeSession(session);
        }
        if (!count) {
            if (cis != null) {
                for (ICi ci : cis) {
                    if (ci.isBlueprint()) {
                        if (!criteria.isMatchCiTemplates()) {
                            continue;
                        }
                    } else {
                        if (!criteria.isMatchCiInstances()) {
                            continue;
                        }
                    }

                    if (!result.contains(ci)) {
                        result.add(ci);
                    }
                }
            }
        }
    }
    return (result);
}

From source file:org.onecmdb.core.internal.storage.hibernate.HibernateDao2.java

License:Open Source License

public List queryCriteria(ISession s, DetachedCriteria detachedCrit, PageInfo info) {
    Session session = getSession(s);/* w  ww  . j  a va2 s .c  om*/
    List result = Collections.EMPTY_LIST;
    try {
        Profiler.start("QueryCriteria():");
        Criteria criteria = detachedCrit.getExecutableCriteria(session);
        criteria.addOrder(Order.asc("alias"));
        if (info != null) {
            if (info.getFirstResult() != null) {
                criteria.setFirstResult(info.getFirstResult());
            }
            if (info.getMaxResult() != null) {
                criteria.setMaxResults(info.getMaxResult());
            }
        }
        result = criteria.list();
    } finally {
        Profiler.stop();
        closeSession(session);
    }
    return (result);
}