Example usage for org.hibernate.criterion Projections distinct

List of usage examples for org.hibernate.criterion Projections distinct

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections distinct.

Prototype

public static Projection distinct(Projection projection) 

Source Link

Document

Create a distinct projection from a projection.

Usage

From source file:to.etc.domui.hibernate.model.CriteriaCreatingVisitor.java

License:Open Source License

@Override
public void visitPropertySelection(QPropertySelection n) throws Exception {
    String name = parseSubcriteria(n.getProperty());

    switch (n.getFunction()) {
    default://from  ww w . j  a  v  a  2 s .c  o  m
        throw new IllegalStateException("Unexpected selection item function: " + n.getFunction());
    case AVG:
        m_lastProj = Projections.avg(name);
        break;
    case MAX:
        m_lastProj = Projections.max(name);
        break;
    case MIN:
        m_lastProj = Projections.min(name);
        break;
    case SUM:
        m_lastProj = Projections.sum(name);
        break;
    case COUNT:
        m_lastProj = Projections.count(name);
        break;
    case COUNT_DISTINCT:
        m_lastProj = Projections.countDistinct(name);
        break;
    case ID:
        m_lastProj = Projections.id();
        break;
    case PROPERTY:
        m_lastProj = Projections.groupProperty(name);
        break;
    case ROWCOUNT:
        m_lastProj = Projections.rowCount();
        break;
    case DISTINCT:
        m_lastProj = Projections.distinct(Projections.property(name));
        break;
    }
}

From source file:ubc.pavlab.aspiredb.server.dao.CriteriaBuilder.java

License:Apache License

private static Criterion createCharacteristicCriterion(CharacteristicProperty property, Operator operator,
        TextValue value, EntityType target) {
    DetachedCriteria subquery = DetachedCriteria.forClass(target.clazz);

    addCharacteristicAlias(subquery, target);

    Junction conjunction = Restrictions.conjunction()
            .add(Restrictions.eq("characteristic.key", property.getName()));

    switch (operator) {
    case TEXT_EQUAL:
    case TEXT_NOT_EQUAL:
        conjunction.add(createTextCriterion(operator, "characteristic.value", value.toString()));
        break;//from  w w  w.ja v  a 2 s. c  o  m
    case NUMERIC_EQUAL:
    case NUMERIC_GREATER:
    case NUMERIC_LESS:
    case NUMERIC_NOT_EQUAL:
        NumericValue numValue = new NumericValue(Integer.valueOf(value.getValue()));
        conjunction.add(createNumericalCriterion(operator, "characteristic.value", numValue));
        break;
    default:
        throw new IllegalArgumentException("Operator type not supported.");
    }

    subquery.add(conjunction);

    subquery.setProjection(Projections.distinct(Projections.id()));
    return Subqueries.propertyIn("id", subquery);
}

From source file:ubc.pavlab.aspiredb.server.dao.CriteriaBuilder.java

License:Apache License

private static Criterion createGenomicRangeCriterion(Operator operator, GenomicRange range, EntityType target) {
    DetachedCriteria subquery = DetachedCriteria.forClass(target.clazz);

    addLocationAlias(subquery, target);//from w w w . ja v  a 2s. c  om

    subquery.add(overlapsGenomicRegionCriterion(range));

    subquery.setProjection(Projections.distinct(Projections.id()));
    switch (operator) {
    case IS_IN_SET:
        return Subqueries.propertyIn("id", subquery);
    case IS_NOT_IN_SET:
        return Subqueries.propertyNotIn("id", subquery);
    default:
        throw new IllegalArgumentException("Operator not supported.");
    }
}

From source file:ubc.pavlab.aspiredb.server.dao.CriteriaBuilder.java

License:Apache License

private static Criterion createLabelCriterion(LabelProperty property, Operator operator, LabelValueObject value,
        EntityType target) {//from   ww w . ja v a 2s.c om
    DetachedCriteria subquery = DetachedCriteria.forClass(target.clazz);

    addLabelAlias(subquery, target);

    if (property instanceof VariantLabelProperty) {
        subquery.add(Restrictions.eq("variant_label.id", value.getId()));
    } else if (property instanceof SubjectLabelProperty) {
        subquery.add(Restrictions.eq("subject_label.id", value.getId()));
    }

    subquery.setProjection(Projections.distinct(Projections.id()));

    if (operator == Operator.TEXT_EQUAL) {
        return Subqueries.propertyIn("id", subquery);
    } else if (operator == Operator.TEXT_NOT_EQUAL) {
        return Subqueries.propertyNotIn("id", subquery);
    }

    throw new IllegalArgumentException();
}

From source file:ubc.pavlab.aspiredb.server.dao.CriteriaBuilder.java

License:Apache License

private static Criterion processRestrictionExpression(PhenotypeRestriction restriction, EntityType target) {
    DetachedCriteria subquery = DetachedCriteria.forClass(target.clazz);

    addPhenotypeAlias(subquery, target);

    subquery.add(Restrictions.conjunction().add(Restrictions.eq("phenotype.name", restriction.getName()))
            .add(Restrictions.eq("phenotype.value", restriction.getValue())));

    subquery.setProjection(Projections.distinct(Projections.id()));

    return Subqueries.propertyIn("id", subquery);
}

From source file:ubc.pavlab.aspiredb.server.dao.CriteriaBuilder.java

License:Apache License

private static Criterion processRestrictionExpression(SetRestriction setRestriction, EntityType target) {
    Property property = setRestriction.getProperty();
    Operator operator = setRestriction.getOperator();
    Set<Object> values = setRestriction.getValues();

    log.debug("Property=" + property + "; operator=" + operator + "; values="
            + StringUtils.collectionToCommaDelimitedString(values));

    DetachedCriteria subquery = DetachedCriteria.forClass(target.clazz);

    Junction criteriaDisjunction = Restrictions.disjunction();

    if (property instanceof CharacteristicProperty) {
        for (Object value : values) {
            criteriaDisjunction.add(createCharacteristicCriterion((CharacteristicProperty) property,
                    Operator.TEXT_EQUAL, (TextValue) value, target));
        }//from ww w. j av  a  2 s  .c  o  m
    } else if (property instanceof LabelProperty) {
        for (Object value : values) {
            criteriaDisjunction.add(createLabelCriterion((LabelProperty) property, Operator.TEXT_EQUAL,
                    (LabelValueObject) value, target));
        }
    } else if (property instanceof CNVTypeProperty) {
        EntityType propertyOf = EntityType.VARIANT;
        for (Object value : values) {
            criteriaDisjunction.add(createCNVTypeCriterion(Operator.TEXT_EQUAL,
                    fullEntityPropertyName(target, propertyOf, property), (TextValue) value));
        }
    } else if (property instanceof VariantTypeProperty) {
        for (Object value : values) {
            criteriaDisjunction.add(createVariantTypeCriterion(target, (TextValue) value));
        }
    } else if (property instanceof ExternalSubjectIdProperty) {
        EntityType propertyOf = EntityType.SUBJECT;
        for (Object value : values) {
            criteriaDisjunction.add(createTextCriterion(Operator.TEXT_EQUAL,
                    fullEntityPropertyName(target, propertyOf, property), ((TextValue) value).getValue()));
        }
    } else if (property instanceof TextProperty) {
        EntityType propertyOf = EntityType.VARIANT;
        for (Object value : values) {
            criteriaDisjunction.add(createTextCriterion(Operator.TEXT_EQUAL,
                    fullEntityPropertyName(target, propertyOf, property), ((TextValue) value).getValue()));
        }
    } else if (property instanceof GenomicLocationProperty) {
        for (Object value : values) {
            criteriaDisjunction.add(overlapsGenomicRegionCriterion((GenomicRange) value));
        }
    } else if (property instanceof GeneProperty) {
        for (Object value : values) {
            GeneValueObject gene = (GeneValueObject) value;
            criteriaDisjunction.add(overlapsGenomicRegionCriterion(gene.getGenomicRange()));
        }
    } else if (property instanceof NeurocartaPhenotypeProperty) {
        for (Object value : values) {
            NeurocartaPhenotypeValueObject neurocartaPhenotype = (NeurocartaPhenotypeValueObject) value;
            for (GeneValueObject gene : neurocartaPhenotype.getGenes()) {
                criteriaDisjunction.add(overlapsGenomicRegionCriterion(gene.getGenomicRange()));
            }
        }
    } else {
        throw new IllegalArgumentException("Not supported!");
    }

    subquery.add(criteriaDisjunction);
    subquery.setProjection(Projections.distinct(Projections.id()));
    log.debug("subquery = " + subquery);

    switch (operator) {
    case IS_IN_SET:
        return Subqueries.propertyIn("id", subquery);
    case IS_NOT_IN_SET:
        return Subqueries.propertyNotIn("id", subquery);
    default:
        throw new IllegalArgumentException("Operator not supported.");
    }
}

From source file:ubc.pavlab.aspiredb.server.dao.PhenotypeDaoImpl.java

License:Apache License

@Override
@Transactional(readOnly = true)/*www  .  java 2s  .co  m*/
public Integer findPhenotypeCountBySubjectId(Long id) {

    Criteria criteria = currentSession().createCriteria(Phenotype.class).createAlias("subject", "subject")
            .add(Restrictions.eq("subject.id", id));
    criteria.setProjection(Projections.distinct(Projections.id()));
    List<Long> ids = criteria.list();

    return ids.size();
}

From source file:ubc.pavlab.aspiredb.server.dao.PhenotypeDaoImpl.java

License:Apache License

@Override
@Transactional(readOnly = true)/*from  ww w.  j  a  va  2s  .c om*/
public List<String> getDistinctOntologyUris(Collection<Long> activeProjects) {

    Session session = currentSession();

    Criteria criteria = session.createCriteria(Phenotype.class).createAlias("subject", "subject")
            .createAlias("subject.project", "project").add(Restrictions.in("project.id", activeProjects))
            .add(Restrictions.eq("valueType", "HPONTOLOGY"));

    criteria.setProjection(Projections.distinct(Projections.property("uri")));

    return criteria.list();

}

From source file:ubc.pavlab.aspiredb.server.dao.PhenotypeDaoImpl.java

License:Apache License

@Override
public List<String> getExistingNames(Collection<Long> activeProjectIds) {
    Session session = currentSession();//from ww  w .ja v a2 s .  c  o m

    Criteria criteria;

    if (activeProjectIds != null) {
        criteria = session.createCriteria(Phenotype.class).createAlias("subject", "subject")
                .createAlias("subject.project", "project").add(Restrictions.in("project.id", activeProjectIds));
    } else {
        criteria = session.createCriteria(Phenotype.class);
    }

    criteria.setProjection(Projections.distinct(Projections.property("name")));

    return criteria.list();
}

From source file:ubc.pavlab.aspiredb.server.dao.PhenotypeDaoImpl.java

License:Apache License

@Override
public List<String> getExistingPhenotypes(String name, boolean isExactMatch, Collection<Long> activeProjects) {
    String queryString = isExactMatch ? name : "%" + name + "%";

    Session session = currentSession();/*  w  ww. j a  v a  2  s . com*/

    Criteria criteria = session.createCriteria(Phenotype.class).createAlias("subject", "subject")
            .createAlias("subject.project", "project").add(Restrictions.in("project.id", activeProjects))
            .add(Restrictions.like("name", queryString));

    criteria.setProjection(Projections.distinct(Projections.property("name")));

    return criteria.list();
}