List of usage examples for org.hibernate.criterion Projections countDistinct
public static CountProjection countDistinct(String propertyName)
From source file:org.iternine.jeppetto.dao.hibernate.HibernateQueryModelDAO.java
License:Apache License
@Override public Projection buildProjection(String projectionField, ProjectionType projectionType, Iterator argsIterator) {/* www.jav a 2 s .c o m*/ Projection projection = new Projection(); projection.setField(projectionField); switch (projectionType) { case RowCount: projection.setDetails(Projections.rowCount()); break; case Count: projection.setDetails(Projections.count(projectionField)); break; case CountDistinct: projection.setDetails(Projections.countDistinct(projectionField)); break; case Maximum: projection.setDetails(Projections.max(projectionField)); break; case Minimum: projection.setDetails(Projections.min(projectionField)); break; case Average: projection.setDetails(Projections.avg(projectionField)); break; case Sum: projection.setDetails(Projections.sum(projectionField)); break; default: throw new RuntimeException("Unexpected projection type: " + projectionType); } return projection; }
From source file:org.jasig.ssp.dao.EarlyAlertDao.java
License:Apache License
public Long getStudentEarlyAlertCountForCoach(Person coach, Date createDateFrom, Date createDateTo, List<UUID> studentTypeIds) { final Criteria query = createCriteria(); // add possible studentTypeId Check if (studentTypeIds != null && !studentTypeIds.isEmpty()) { query.createAlias("person", "person").add(Restrictions.in("person.studentType.id", studentTypeIds)); }// w w w.j a v a 2s. co m if (createDateFrom != null) { query.add(Restrictions.ge("createdDate", createDateFrom)); } if (createDateTo != null) { query.add(Restrictions.le("createdDate", createDateTo)); } Long totalRows = (Long) query.add(Restrictions.eq("createdBy", new AuditPerson(coach.getId()))) .setProjection(Projections.countDistinct("person")).list().get(0); return totalRows; }
From source file:org.jasig.ssp.dao.EarlyAlertDao.java
License:Apache License
@SuppressWarnings("unchecked") public PagingWrapper<EarlyAlertStudentReportTO> getStudentsEarlyAlertCountSetForCriteria( EarlyAlertStudentSearchTO criteriaTO, SortingAndPaging sAndP) { final Criteria query = createCriteria(); setPersonCriteria(query.createAlias("person", "person"), criteriaTO.getAddressLabelSearchTO()); if (criteriaTO.getTermCode() != null) { query.add(Restrictions.eq("courseTermCode", criteriaTO.getTermCode())); }/*from ww w . j av a 2s. c o m*/ if (criteriaTO.getStartDate() != null) { query.add(Restrictions.ge("createdDate", criteriaTO.getStartDate())); } if (criteriaTO.getEndDate() != null) { query.add(Restrictions.le("createdDate", criteriaTO.getEndDate())); } query.setProjection(null); List<UUID> ids = query.setProjection(Projections.distinct(Projections.property("id"))).list(); if (ids.size() <= 0) { return null; } BatchProcessor<UUID, EarlyAlertStudentReportTO> processor = new BatchProcessor<UUID, EarlyAlertStudentReportTO>( ids, sAndP); do { final Criteria criteria = createCriteria(); ProjectionList projections = Projections.projectionList() .add(Projections.countDistinct("id").as("earlyalert_total")) .add(Projections.countDistinct("closedBy").as("earlyalert_closed")); addBasicStudentProperties(projections, criteria); projections.add(Projections.groupProperty("id").as("earlyalert_earlyAlertId")); criteria.setProjection(projections); criteria.setResultTransformer( new NamespacedAliasToBeanResultTransformer(EarlyAlertStudentReportTO.class, "earlyalert_")); processor.process(criteria, "id"); } while (processor.moreToProcess()); return processor.getSortedAndPagedResults(); }
From source file:org.jasig.ssp.dao.EarlyAlertDao.java
License:Apache License
public Long getCountOfAlertsForSchoolIds(Collection<String> schoolIds, Campus campus) { BatchProcessor<String, Long> processor = new BatchProcessor<String, Long>(schoolIds); do {/*from w w w . j ava 2 s . c om*/ final Criteria query = createCriteria(); query.createAlias("person", "person"); if (campus != null) { query.add(Restrictions.eq("campus", campus)); } query.setProjection(Projections.countDistinct("person")); processor.countDistinct(query, "person.schoolId"); } while (processor.moreToProcess()); return processor.getCount(); }
From source file:org.jasig.ssp.dao.EarlyAlertDao.java
License:Apache License
public Long getStudentCountForEarlyAlertCreatedDateRange(String termCode, Date createdDateFrom, Date createdDateTo, Campus campus, String rosterStatus) { final Criteria query = createCriteria(); if (termCode != null) { query.add(Restrictions.eq("courseTermCode", termCode)); }/*from w ww. j a v a2 s. c o m*/ if (createdDateFrom != null) { query.add(Restrictions.ge("createdDate", createdDateFrom)); } if (createdDateTo != null) { query.add(Restrictions.le("createdDate", createdDateTo)); } if (campus != null) { query.add(Restrictions.eq("campus", campus)); } query.createAlias("person", "person"); // item count Long totalRows = (Long) query.setProjection(Projections.countDistinct("person")).uniqueResult(); return totalRows; }
From source file:org.jasig.ssp.dao.EarlyAlertDao.java
License:Apache License
@SuppressWarnings("unchecked") public PagingWrapper<EntityStudentCountByCoachTO> getStudentEarlyAlertCountByCoaches( EntityCountByCoachSearchForm form) { List<Person> coaches = form.getCoaches(); List<AuditPerson> auditCoaches = new ArrayList<AuditPerson>(); for (Person person : coaches) { auditCoaches.add(new AuditPerson(person.getId())); }/*from w ww . j a v a 2 s. c om*/ BatchProcessor<AuditPerson, EntityStudentCountByCoachTO> processor = new BatchProcessor<AuditPerson, EntityStudentCountByCoachTO>( auditCoaches, form.getSAndP()); do { final Criteria query = createCriteria(); setBasicCriteria(query, form); query.setProjection(Projections.projectionList() .add(Projections.countDistinct("person").as("earlyalert_studentCount")) .add(Projections.countDistinct("id").as("earlyalert_entityCount")) .add(Projections.groupProperty("createdBy").as("earlyalert_coach"))); query.setResultTransformer( new NamespacedAliasToBeanResultTransformer(EntityStudentCountByCoachTO.class, "earlyalert_")); processor.process(query, "createdBy"); } while (processor.moreToProcess()); return processor.getSortedAndPagedResults(); }
From source file:org.jasig.ssp.dao.EarlyAlertDao.java
License:Apache License
public Long getEarlyAlertCountSetForCriteria(EarlyAlertStudentSearchTO searchForm) { final Criteria criteria = setPersonCriteria(createCriteria().createAlias("person", "person"), searchForm.getAddressLabelSearchTO()); if (searchForm.getStartDate() != null) { criteria.add(Restrictions.ge("createdDate", searchForm.getStartDate())); }/*from w ww . ja v a 2 s . com*/ if (searchForm.getEndDate() != null) { criteria.add(Restrictions.le("createdDate", searchForm.getEndDate())); } Long total = (Long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult(); return total; }
From source file:org.jasig.ssp.dao.EarlyAlertDao.java
License:Apache License
public List<Triple<String, Long, Long>> getEarlyAlertReasonTypeCountByCriteria(Campus campus, String termCode, Date createdDateFrom, Date createdDateTo, ObjectStatus objectStatus) { final Criteria criteria = createCriteria(); if (termCode != null) { criteria.add(Restrictions.eq("courseTermCode", termCode)); }/*from w w w.j av a 2 s . com*/ if (createdDateFrom != null) { criteria.add(Restrictions.ge("createdDate", createdDateFrom)); } if (createdDateTo != null) { criteria.add(Restrictions.le("createdDate", createdDateTo)); } if (campus != null) { criteria.add(Restrictions.eq("campus", campus)); } if (objectStatus != null) { criteria.add(Restrictions.eq("objectStatus", objectStatus)); } criteria.createAlias("earlyAlertReasonIds", "eareasons"); ProjectionList projections = Projections.projectionList().add(Projections.property("eareasons.name")) .add(Projections.countDistinct("person")).add(Projections.count("id")); projections.add(Projections.groupProperty("eareasons.name")); criteria.setProjection(projections); criteria.addOrder(Order.asc("eareasons.name")); final List<Triple<String, Long, Long>> reasonCounts = new ArrayList<>(); for (final Object result : criteria.list()) { Object[] resultReasonCounts = (Object[]) result; reasonCounts.add(new Triple((String) resultReasonCounts[0], (Long) resultReasonCounts[1], (Long) resultReasonCounts[2])); } return reasonCounts; }
From source file:org.jasig.ssp.dao.EarlyAlertResponseDao.java
License:Apache License
public EarlyAlertResponseCounts getCountEarlyAlertRespondedToForEarlyAlerts(List<UUID> earlyAlertIds) { final Criteria query = createCriteria(); final EarlyAlertResponseCounts counts = new EarlyAlertResponseCounts(); query.createAlias("earlyAlert", "earlyAlert"); query.add(Restrictions.in("earlyAlert.id", earlyAlertIds)); query.setProjection(Projections.rowCount()); if (!query.list().isEmpty()) counts.setTotalResponses((Long) (query.list().get(0))); query.setProjection(Projections.countDistinct("earlyAlert.id")); if (!query.list().isEmpty()) counts.setTotalEARespondedTo((Long) (query.list().get(0))); query.add(Restrictions.isNull("earlyAlert.closedBy")); if (!query.list().isEmpty()) counts.setTotalEARespondedToNotClosed((Long) (query.list().get(0))); return counts; }
From source file:org.jasig.ssp.dao.EarlyAlertResponseDao.java
License:Apache License
public EarlyAlertResponseCounts getCountEarlyAlertRespondedToForEarlyAlertsByOutcome(List<UUID> earlyAlertIds, UUID outcomeId) {//from ww w.j a v a 2s .c o m final Criteria query = createCriteria(); final EarlyAlertResponseCounts counts = new EarlyAlertResponseCounts(); query.createAlias("earlyAlert", "earlyAlert"); query.add(Restrictions.in("earlyAlert.id", earlyAlertIds)); query.add(Restrictions.eq("earlyAlertOutcome.id", outcomeId)); query.setProjection(Projections.rowCount()); if (!query.list().isEmpty()) counts.setTotalResponses((Long) (query.list().get(0))); query.setProjection(Projections.countDistinct("earlyAlert.id")); if (!query.list().isEmpty()) counts.setTotalEARespondedTo((Long) (query.list().get(0))); query.add(Restrictions.isNull("earlyAlert.closedBy")); if (!query.list().isEmpty()) counts.setTotalEARespondedToNotClosed((Long) (query.list().get(0))); return counts; }