Example usage for org.hibernate.criterion DetachedCriteria setProjection

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

Introduction

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

Prototype

public DetachedCriteria setProjection(Projection projection) 

Source Link

Document

Set the projection to use.

Usage

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()));
    }/*from   ww w . ja  v  a2s.c  om*/
    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.utils.graph.expression.ConstrainGroupExpression.java

License:Open Source License

@Override
public DetachedCriteria getCriteria() {
    DetachedCriteria crit = getParent().getCriteria();
    Junction junction = null;/*  w w w  .j  a va  2s  .c  o m*/
    if (disjunction) {
        junction = Restrictions.disjunction();
    } else {
        junction = Restrictions.conjunction();
    }
    for (Iterator iter = attributeExpressions.iterator(); iter.hasNext();) {
        AttributeExpression aExpr = (AttributeExpression) iter.next();
        DetachedCriteria attrCriteria = aExpr.getCriteria();
        junction.add(
                Property.forName("longId").in(attrCriteria.setProjection(Projections.property("ownerId"))));
    }
    crit.add(junction);

    return (crit);
}

From source file:org.onecmdb.core.utils.graph.expression.OffspringExpression.java

License:Open Source License

public DetachedCriteria getCriteria() {
    DetachedCriteria offsprings = DetachedCriteria.forClass(ConfigurationItem.class);
    //offsprings.addOrder(Order.asc("alias"));
    if (this.templateID != null) {

        if (limitToChild) {
            offsprings.add(Property.forName("derivedFromId").eq(this.templateID));
        } else {//from   ww w . j av a  2s.  c  o m
            if (this.templatePath != null) {
                offsprings.add(Expression.like("templatePath", this.templatePath + "/%"));
            } else {
                offsprings.add(Expression.like("templatePath", "%/" + this.templateID + "/%"));
            }
        }

        //offsprings.add(Expression.eq("derivedFromId", templateID));

    } else if (getParent() != null) {
        DetachedCriteria crit = getParent().getCriteria();
        DetachedCriteria ids = crit.setProjection(Projections.property("longId"));
        offsprings.add(Property.forName("derivedFromId").eq(ids));
    } else {
    }
    if (matchTemplate != null) {
        offsprings.add(Property.forName("isBlueprint").eq(matchTemplate));
    }
    return (offsprings);
}

From source file:org.onecmdb.core.utils.graph.expression.RelationExpression.java

License:Open Source License

public Criterion getTargetCriterion() {
    DetachedCriteria crit = getTargetCriteria();
    Criterion relation = Property.forName(getSourceProjection())
            .in(crit.setProjection(Projections.property(getRelationSourceProperty())));
    return (relation);
}

From source file:org.onecmdb.core.utils.graph.expression.RelationExpression.java

License:Open Source License

public Criterion getSourceCriterion() {
    DetachedCriteria crit = getSourceCriteria();
    Criterion relation = Property.forName(getTargetProjection())
            .in(crit.setProjection(Projections.property(getRelationTargetProperty())));
    return (relation);
}

From source file:org.onecmdb.core.utils.graph.handler.QueryHandler.java

License:Open Source License

private DetachedCriteria getAttributeOrderCriteria(String template, DetachedCriteria ciCrit,
        DetachedCriteria ci2Crit, OrderInfo info) {

    if (info.getAttrType().equals("complex")) {
        return (getComplexAttributeOrderCriteria2(template, ciCrit, ci2Crit, info));
    }//  w w  w .  j a v a2 s .co m

    DetachedCriteria crit = DetachedCriteria.forClass(BasicAttribute.class);
    DetachedCriteria ciIdProjection = ciCrit.setProjection(Projections.property("longId"));
    crit.add(Property.forName("ownerId").in(ciIdProjection));
    crit.add(Expression.eq("alias", info.getAttrAlias()));
    if (info.isDescenden()) {
        crit.addOrder(Order.desc(info.getAttrType()));
    } else {
        crit.addOrder(Order.asc(info.getAttrType()));
    }
    return (crit);
}

From source file:org.onecmdb.core.utils.graph.handler.QueryHandler.java

License:Open Source License

private DetachedCriteria getBackReferences(DetachedCriteria sourceCrit, List<Long> targetIds, OrderInfo info) {
    // Find references that point to targetCrit
    DetachedCriteria refCiCrit = DetachedCriteria.forClass(ConfigurationItem.class);
    refCiCrit.add(Property.forName("sourceId").in(sourceCrit.setProjection(Projections.property("longId"))));
    refCiCrit.add(Property.forName("targetId").in(targetIds));
    return (refCiCrit);
}

From source file:org.onecmdb.core.utils.graph.handler.QueryHandler.java

License:Open Source License

private DetachedCriteria getComplexAttributeOrderCriteria(String template, DetachedCriteria ciCrit,
        OrderInfo info) {/* w  w  w  . jav a 2s.c  om*/
    DetachedCriteria crit = DetachedCriteria.forClass(BasicAttribute.class);
    DetachedCriteria ciIdProjection = ciCrit.setProjection(Projections.property("longId"));
    crit.add(Property.forName("ownerId").in(ciIdProjection));
    crit.add(Expression.eq("alias", info.getAttrAlias()));
    //crit.add(Expression.isNotEmpty("valueAsLong"));

    ICi ci = this.msvc.findCi(new Path(template));
    IAttribute attr = ci.getAttributeDefinitionWithAlias(info.getAttrAlias());
    IType type = attr.getValueType();
    String targetTemplate = type.getAlias();

    DetachedCriteria refCrit = DetachedCriteria.forClass(ConfigurationItem.class);
    refCrit.add(Property.forName("longId").in(crit.setProjection(Projections.property("valueAsLong"))));

    DetachedCriteria refCiCrit = DetachedCriteria.forClass(ConfigurationItem.class);
    refCiCrit.add(Property.forName("longId").in(refCrit.setProjection(Projections.property("targetId"))));

    return (getDisplayNameOrder(targetTemplate, refCiCrit, info));
}

From source file:org.onecmdb.core.utils.graph.handler.QueryHandler.java

License:Open Source License

private DetachedCriteria getBackComplexAttributeOrder(List<Long> attrOwnerId, DetachedCriteria ci2Crit,
        OrderInfo info) {/*from w w  w. j a va  2  s .  c  om*/

    DetachedCriteria targetCrit = DetachedCriteria.forClass(ConfigurationItem.class);
    targetCrit.add(Property.forName("longId").in(attrOwnerId));

    DetachedCriteria refCrit = DetachedCriteria.forClass(ConfigurationItem.class);
    refCrit.add(Property.forName("targetId").in(targetCrit.setProjection(Projections.property("longId"))));

    DetachedCriteria attrCrit = DetachedCriteria.forClass(BasicAttribute.class);
    attrCrit.add(Property.forName("valueAsLong").in(refCrit.setProjection(Projections.property("longId"))));
    attrCrit.add(Property.forName("ownerId").in(ci2Crit.setProjection(Projections.property("longId"))));
    attrCrit.add(Expression.eq("alias", info.getAttrAlias()));

    attrCrit.addOrder(Order.desc("longId"));
    return (attrCrit);
}

From source file:org.onecmdb.core.utils.graph.handler.QueryHandler.java

License:Open Source License

private Criterion getConstraint2(GraphQuery query, ItemConstraint cons) {
    if (cons instanceof ItemGroupConstraint) {

        ItemGroupConstraint group = (ItemGroupConstraint) cons;
        Junction j = null;/* w  ww  . j a v a  2  s  .c  o m*/
        if (group.conjunction()) {
            j = Restrictions.conjunction();
        } else {
            j = Restrictions.disjunction();
        }

        for (Iterator iter = group.fetchConstraints().iterator(); iter.hasNext();) {
            ItemConstraint con = (ItemConstraint) iter.next();
            Criterion criterion = getConstraint2(query, con);
            if (group.conjunction()) {
                if (criterion == null) {
                    return (null);
                }
            }
            if (criterion != null) {
                j.add(criterion);
            }
        }
        return (j);
    }
    if (cons instanceof ItemNotConstraint) {
        ItemConstraint notCons = ((ItemNotConstraint) cons).fetchConstraint();
        if (notCons == null) {
            throw new IllegalArgumentException("ItemNotGroupConstraint must containt a constraint");
        }
        Criterion notCrit = getConstraint2(query, notCons);
        return (Expression.not(notCrit));
    }
    if (cons instanceof RelationConstraint) {
        RelationConstraint rel = (RelationConstraint) cons;
        DetachedCriteria crit = null;
        String direction = null;
        ItemSelector selector = query.findSelector(rel.getSelector());
        if (!(selector instanceof ItemRelationSelector)) {
            throw new IllegalArgumentException(
                    "RelationExpression selector " + rel.getSelector() + " is not a ItemRelationSelection!");
        }

        RelationExpression relExpr = new RelationExpression();

        if (selector instanceof RFCItemRelationSelector) {
            relExpr = new RFCRelationExpression();
        } else if (selector instanceof TransactionRelationSelector) {
            relExpr = new TransactionRelationExpression();
        }

        ItemRelationSelector relSelector = (ItemRelationSelector) selector;
        Criterion relation = null;
        if (rel.isTarget()) {
            if (relSelector.getSourceRange() != null) {
                if (relSelector.getSourceRange().size() == 0) {
                    log.info("RelationConstraint RelationSelector[" + relSelector.getId()
                            + "] SourceRange Empty!");

                    return (null);
                }
                relExpr.setSourceIds(relSelector.getSourceRange());
            } else {
                String srcId = ((ItemRelationSelector) selector).getSource();
                ItemSelector sel = (ItemSelector) query.findSelector(srcId);
                QueryExpression sExpr = getExpression(query, sel);
                if (sExpr.empty) {
                    return (null);
                }
                DetachedCriteria source = sExpr.criteria;
                relExpr.setSource(source);
            }
            relation = relExpr.getSourceCriterion();
            /*
            crit = relExpr.getSourceCriteria();
            relation = Property.forName("longId").in(crit.setProjection(Projections.property("targetId")));
            */
        } else {

            if (relSelector.getTargetRange() != null) {
                if (relSelector.getTargetRange().size() == 0) {
                    log.info("RelationConstraint RelationSelector[" + relSelector.getId()
                            + "] TargetRange Empty!");
                    return (null);
                }
                relExpr.setTargetIds(relSelector.getTargetRange());
                //relation = Property.forName("longId").in(relSelector.getTargetRange());
            } else {
                String trgId = ((ItemRelationSelector) selector).getTarget();
                ItemSelector sel = query.findSelector(trgId);
                QueryExpression tExpr = getExpression(query, sel);
                if (tExpr.empty) {
                    return (null);
                }
                DetachedCriteria target = tExpr.criteria;
                relExpr.setTarget(target);
            }
            relation = relExpr.getTargetCriterion();
            /*
            crit = relExpr.getTargetCriteria();
            relation = Property.forName("longId").in(crit.setProjection(Projections.property("sourceId")));
            */
        }
        log.debug(relation.toString());
        return (relation);
    }

    if (cons instanceof AttributeValueConstraint) {
        AttributeValueConstraint aValue = (AttributeValueConstraint) cons;
        AttributeValueExpression aExpr = new AttributeValueExpression();
        aExpr.setAlias(aValue.getAlias());
        aExpr.setOperation(aValue.getOperation());
        aExpr.setType(aValue.getValueType());
        aExpr.setStringValue(aValue.getValue());

        if (aExpr.isInternal()) {
            return (aExpr.getInternalCriterion());
        }

        DetachedCriteria attr = aExpr.getCriteria();
        return (Property.forName("longId").in(attr.setProjection(Projections.property("ownerId"))));
    }

    if (cons instanceof ItemSecurityConstraint) {
        ItemSecurityConstraint sCon = (ItemSecurityConstraint) cons;

        if (sCon.getGid() != null) {
            return (Property.forName("gid").eq(sCon.getGid()));
        }
        ItemExpression expr = new ItemExpression();
        expr.setAlias(sCon.getGroupName());

        DetachedCriteria gid = expr.getCriteria();
        return (Property.forName("gid").in(gid.setProjection(Projections.property("longId"))));
    }

    if (cons instanceof ItemIdConstraint) {
        ItemIdConstraint idContrain = (ItemIdConstraint) cons;
        if (idContrain.getId() != null) {
            return (Restrictions.idEq(idContrain.getId()));
        }
        if (idContrain.getAlias() != null) {
            return (Property.forName("alias").eq(idContrain.getAlias()));
        }
    }

    if (cons instanceof RFCTargetConstraint) {
        return (Property.forName("targetId").eq(((RFCTargetConstraint) cons).getLongId()));
    }

    if (cons instanceof AttributeSourceRelationConstraint) {
        AttributeSourceRelationConstraint relACons = (AttributeSourceRelationConstraint) cons;
        AttributeValueExpression expr = new AttributeValueExpression();
        expr.setAlias(relACons.getAlias());
        DetachedCriteria crit = expr.getCriteria();
        return (Property.forName("sourceId").in(crit.setProjection(Projections.property("valueAsLong"))));
    }
    log.error("Constraint{" + cons.getClass().getSimpleName() + "] not implemented!");
    return (null);
}