Example usage for org.hibernate.criterion Projections countDistinct

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

Introduction

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

Prototype

public static CountProjection countDistinct(String propertyName) 

Source Link

Document

A distinct property value count projection

Usage

From source file:org.jasig.ssp.dao.EarlyAlertResponseDao.java

License:Apache License

public Long getRespondedToEarlyAlertCountForResponseCreatedDateRange(Date createDateFrom, Date createDateTo,
        Campus campus, String rosterStatus) {
    final Criteria query = createCriteria();

    if (createDateFrom != null) {
        query.add(Restrictions.ge("createdDate", createDateFrom));
    }/*from   w w w. ja v  a2s. c o  m*/

    if (createDateTo != null) {
        query.add(Restrictions.le("createdDate", createDateTo));
    }

    if (campus != null) {
        query.createAlias("earlyAlert", "earlyAlert").add(Restrictions.eq("earlyAlert.campus", campus));
    }

    // item count
    Long totalRows = (Long) query.setProjection(Projections.countDistinct("earlyAlert.id")).uniqueResult();

    return totalRows;
}

From source file:org.jasig.ssp.dao.EarlyAlertResponseDao.java

License:Apache License

public Long getRespondedToEarlyAlertCountForEarlyAlertCreatedDateRange(String termCode, Date createDateFrom,
        Date createDateTo, Campus campus, String rosterStatus) {
    final Criteria query = createCriteria();

    if (campus != null || termCode != null) {
        final Criteria alias = query.createAlias("earlyAlert", "earlyAlert");

        if (termCode != null) {
            alias.add(Restrictions.eq("earlyAlert.courseTermCode", termCode));
        }//  ww  w.j  a  v  a 2s .com

        if (createDateFrom != null) {
            alias.add(Restrictions.ge("earlyAlert.createdDate", createDateFrom));
        }

        if (createDateTo != null) {
            alias.add(Restrictions.le("earlyAlert.createdDate", createDateTo));
        }

        if (campus != null) {
            alias.add(Restrictions.eq("earlyAlert.campus", campus));
        }
    }

    // item count
    Long totalRows = (Long) query.setProjection(Projections.countDistinct("earlyAlert.id")).uniqueResult();

    return totalRows;
}

From source file:org.jasig.ssp.dao.EarlyAlertResponseDao.java

License:Apache License

@SuppressWarnings(UNCHECKED)
public Long getEarlyAlertOutcomeTypeCountByCriteria(String outcomeType, UUID outcomeId,
        EarlyAlertStudentSearchTO searchForm) throws ObjectNotFoundException {

    final Criteria criteria = getCriteriaForOutcomeType(searchForm, null);
    criteria.createAlias(outcomeType, outcomeType);
    criteria.add(Restrictions.eq(outcomeType + ".id", outcomeId));
    criteria.setProjection(Projections.countDistinct("id"));
    return (Long) criteria.uniqueResult();
}

From source file:org.jasig.ssp.dao.EarlyAlertResponseDao.java

License:Apache License

@SuppressWarnings(UNCHECKED)
public Long getEarlyAlertCountByOutcomeCriteria(EarlyAlertStudentSearchTO searchForm)
        throws ObjectNotFoundException {

    final Criteria criteria = getCriteriaForOutcomeType(searchForm, null);

    if (searchForm.getOutcomeIds() != null && searchForm.getOutcomeIds().size() > 0) {
        criteria.createAlias("earlyAlertOutcome", "earlyAlertOutcome");
        criteria.add(Restrictions.in("earlyAlertOutcome.id", searchForm.getOutcomeIds()));
    }//from w  w  w.  j  av  a  2  s. c o  m
    criteria.setProjection(Projections.countDistinct("earlyAlert.id"));
    return (Long) criteria.uniqueResult();
}

From source file:org.jasig.ssp.dao.EarlyAlertResponseDao.java

License:Apache License

@SuppressWarnings("unchecked")
public PagingWrapper<EntityStudentCountByCoachTO> getStudentEarlyAlertResponseCountByCoaches(
        EntityCountByCoachSearchForm form) {

    List<Person> coaches = form.getCoaches();
    List<AuditPerson> auditCoaches = new ArrayList<AuditPerson>();
    for (Person person : coaches) {
        auditCoaches.add(new AuditPerson(person.getId()));
    }/*ww w .j  av  a2s  .com*/
    BatchProcessor<AuditPerson, EntityStudentCountByCoachTO> processor = new BatchProcessor<AuditPerson, EntityStudentCountByCoachTO>(
            auditCoaches, form.getSAndP());

    do {
        final Criteria query = createCriteria();
        if (form.getCreateDateFrom() != null) {
            query.add(Restrictions.ge("createdDate", form.getCreateDateFrom()));
        }

        if (form.getCreateDateTo() != null) {
            query.add(Restrictions.le("createdDate", form.getCreateDateTo()));
        }

        query.createAlias("earlyAlert", "earlyAlert");
        Criteria personCriteria = query.createAlias("earlyAlert.person", "person");
        if (form.getStudentTypeIds() != null && !form.getStudentTypeIds().isEmpty()) {
            personCriteria.add(Restrictions.in("person.studentType.id", form.getStudentTypeIds()));
        }

        if (form.getServiceReasonIds() != null && !form.getServiceReasonIds().isEmpty()) {
            query.createAlias("person.serviceReasons", "serviceReasons");
            query.createAlias("serviceReasons.serviceReason", "serviceReason");
            query.add(Restrictions.in("serviceReason.id", form.getServiceReasonIds()));
            query.add(Restrictions.eq("serviceReasons.objectStatus", ObjectStatus.ACTIVE));
        }

        if (form.getSpecialServiceGroupIds() != null && !form.getSpecialServiceGroupIds().isEmpty()) {
            query.createAlias("person.specialServiceGroups", "specialServiceGroups");
            query.createAlias("specialServiceGroups.specialServiceGroup", "specialServiceGroup");
            query.add(Restrictions.in("specialServiceGroup.id", form.getSpecialServiceGroupIds()));
            query.add(Restrictions.eq("specialServiceGroups.objectStatus", ObjectStatus.ACTIVE));
        }

        query.setProjection(Projections.projectionList()
                .add(Projections.countDistinct("earlyAlert.person").as("earlyalertresponse_studentCount"))
                .add(Projections.countDistinct("id").as("earlyalertresponse_entityCount"))
                .add(Projections.groupProperty("earlyAlert.createdBy").as("earlyalertresponse_coach")));

        query.setResultTransformer(new NamespacedAliasToBeanResultTransformer(EntityStudentCountByCoachTO.class,
                "earlyalertresponse_"));
        processor.process(query, "earlyAlert.createdBy");
    } while (processor.moreToProcess());

    return processor.getSortedAndPagedResults();
}

From source file:org.jasig.ssp.dao.JournalEntryDao.java

License:Apache License

public Long getStudentJournalCountForCoach(Person coach, Date createDateFrom, Date createDateTo,
        List<UUID> studentTypeIds) {

    final Criteria query = createCriteria();
    EntityCountByCoachSearchForm form = new EntityCountByCoachSearchForm(null, createDateFrom, createDateTo,
            studentTypeIds, null, null, null);

    setCriteria(query, form);// w ww .ja  va2s.co m

    Long totalRows = (Long) query.add(Restrictions.eq("createdBy", coach.getId()))
            .setProjection(Projections.countDistinct("person")).list().get(0);

    return totalRows;
}

From source file:org.jasig.ssp.dao.JournalEntryDao.java

License:Apache License

@SuppressWarnings("unchecked")
public PagingWrapper<EntityStudentCountByCoachTO> getStudentJournalCountForCoaches(
        EntityCountByCoachSearchForm form) {

    List<Person> coaches = form.getCoaches();
    List<AuditPerson> auditCoaches = new ArrayList<AuditPerson>();
    for (Person person : coaches) {
        auditCoaches.add(new AuditPerson(person.getId()));
    }//from   www  .  j  av a 2 s .  c  om
    BatchProcessor<AuditPerson, EntityStudentCountByCoachTO> processor = new BatchProcessor<AuditPerson, EntityStudentCountByCoachTO>(
            auditCoaches, form.getSAndP());
    do {
        final Criteria query = createCriteria();
        setCriteria(query, form);

        query.setProjection(
                Projections.projectionList().add(Projections.countDistinct("person").as("journal_studentCount"))
                        .add(Projections.countDistinct("id").as("journal_entityCount"))
                        .add(Projections.groupProperty("createdBy").as("journal_coach")))
                .setResultTransformer(new NamespacedAliasToBeanResultTransformer(
                        EntityStudentCountByCoachTO.class, "journal_"));

        processor.process(query, "createdBy");
    } while (processor.moreToProcess());

    return processor.getSortedAndPagedResults();
}

From source file:org.jasig.ssp.dao.PlanDao.java

License:Apache License

@SuppressWarnings("unchecked")
public List<PlanAdvisorCountTO> getPlanCountByOwner(SearchPlanTO form) {
    Criteria criteria = createCriteria();

    if (form.getDateFrom() != null)
        criteria.add(Restrictions.ge("modifiedDate", form.getDateFrom()));
    if (form.getDateTo() != null)
        criteria.add(Restrictions.lt("modifiedDate", form.getDateTo()));

    criteria.add(Restrictions.eq("objectStatus", ObjectStatus.ACTIVE));

    criteria.createAlias("owner", "owner");
    criteria.setProjection(/*from w  w w  . ja va 2s  .  c  om*/
            Projections.projectionList().add(Projections.countDistinct("id").as("plan_entityCount")).
            // cannot just group by owner, else you get a N+1
            // http://stackoverflow.com/questions/4330480/prevent-hibernate-n1-selects-when-grouping-by-an-entity
            // (plus EntityStudentCountByCoachTO doesn't map Persons, just AuditPersons)
                    add(Projections.groupProperty("owner.id").as("plan_coachId"))
                    .add(Projections.groupProperty("owner.firstName").as("plan_coachFirstName"))
                    .add(Projections.groupProperty("owner.lastName").as("plan_coachLastName")));

    List<EntityStudentCountByCoachTO> activePlansByCoaches = criteria
            .setResultTransformer(
                    new NamespacedAliasToBeanResultTransformer(EntityStudentCountByCoachTO.class, "plan_"))
            .list();

    criteria = createCriteria();

    if (form.getDateFrom() != null)
        criteria.add(Restrictions.ge("modifiedDate", form.getDateFrom()));
    if (form.getDateTo() != null)
        criteria.add(Restrictions.lt("modifiedDate", form.getDateTo()));

    criteria.add(Restrictions.eq("objectStatus", ObjectStatus.INACTIVE));

    criteria.createAlias("owner", "owner");
    criteria.setProjection(
            Projections.projectionList().add(Projections.countDistinct("id").as("plan_entityCount")).
            // cannot just group by owner, else you get a N+1
            // http://stackoverflow.com/questions/4330480/prevent-hibernate-n1-selects-when-grouping-by-an-entity
            // (plus EntityStudentCountByCoachTO doesn't map Persons, just AuditPersons)
                    add(Projections.groupProperty("owner.id").as("plan_coachId"))
                    .add(Projections.groupProperty("owner.firstName").as("plan_coachFirstName"))
                    .add(Projections.groupProperty("owner.lastName").as("plan_coachLastName")));

    List<EntityStudentCountByCoachTO> inactivePlansByCoaches = criteria
            .setResultTransformer(
                    new NamespacedAliasToBeanResultTransformer(EntityStudentCountByCoachTO.class, "plan_"))
            .list();

    Map<UUID, PlanAdvisorCountTO> results = new HashMap<UUID, PlanAdvisorCountTO>();
    for (EntityStudentCountByCoachTO inactivePlan : inactivePlansByCoaches) {
        if (results.containsKey(inactivePlan.getCoach().getId())) {
            PlanAdvisorCountTO result = results.get(inactivePlan.getCoach().getId());
            result.setInactivePlanCount(inactivePlan.getEntityCount());
        } else {
            PlanAdvisorCountTO result = new PlanAdvisorCountTO();
            result.setCoachName(
                    inactivePlan.getCoach().getFirstName() + " " + inactivePlan.getCoach().getLastName());
            result.setInactivePlanCount(inactivePlan.getEntityCount());
            results.put(inactivePlan.getCoach().getId(), result);
        }
    }

    for (EntityStudentCountByCoachTO activePlan : activePlansByCoaches) {
        if (results.containsKey(activePlan.getCoach().getId())) {
            PlanAdvisorCountTO result = results.get(activePlan.getCoach().getId());
            result.setActivePlanCount(activePlan.getEntityCount());
        } else {
            PlanAdvisorCountTO result = new PlanAdvisorCountTO();
            result.setCoachName(
                    activePlan.getCoach().getFirstName() + " " + activePlan.getCoach().getLastName());
            result.setActivePlanCount(activePlan.getEntityCount());
            results.put(activePlan.getCoach().getId(), result);
        }
    }
    List<PlanAdvisorCountTO> sortedResults = Lists.newArrayList(results.values());
    Collections.sort(sortedResults, PlanAdvisorCountTO.COACH_NAME_COMPARATOR);
    return sortedResults;
}

From source file:org.jasig.ssp.dao.TaskDao.java

License:Apache License

public Long getStudentTaskCountForCoach(Person coach, Date createDateFrom, Date createDateTo,
        List<UUID> studentTypeIds) {

    final Criteria query = createCriteria();
    EntityCountByCoachSearchForm form = new EntityCountByCoachSearchForm(null, createDateFrom, createDateTo,
            studentTypeIds, null, null, null);
    setCriteria(query, form);//from  w w  w.j  a  v  a 2 s .  c  o  m

    Long totalRows = (Long) query.add(Restrictions.eq("createdBy", coach.getId()))
            .setProjection(Projections.countDistinct("person")).list().get(0);

    return totalRows;
}

From source file:org.jasig.ssp.dao.TaskDao.java

License:Apache License

@SuppressWarnings("unchecked")
public PagingWrapper<EntityStudentCountByCoachTO> getStudentTaskCountForCoaches(
        EntityCountByCoachSearchForm form) {

    List<AuditPerson> auditCoaches = new ArrayList<AuditPerson>();
    for (Person person : form.getCoaches()) {
        auditCoaches.add(new AuditPerson(person.getId()));
    }/*from www . jav a 2  s . c o  m*/
    BatchProcessor<AuditPerson, EntityStudentCountByCoachTO> processor = new BatchProcessor<AuditPerson, EntityStudentCountByCoachTO>(
            auditCoaches, form.getSAndP());
    do {
        final Criteria query = createCriteria();
        setCriteria(query, form);

        query.setProjection(
                Projections.projectionList().add(Projections.countDistinct("person").as("task_studentCount"))
                        .add(Projections.countDistinct("id").as("task_entityCount"))
                        .add(Projections.groupProperty("createdBy").as("task_coach")))
                .setResultTransformer(
                        new NamespacedAliasToBeanResultTransformer(EntityStudentCountByCoachTO.class, "task_"));
        processor.process(query, "createdBy");
    } while (processor.moreToProcess());

    return processor.getSortedAndPagedResults();
}