List of usage examples for javax.persistence.criteria CriteriaBuilder sum
<N extends Number> Expression<N> sum(Expression<N> x);
From source file:org.finra.herd.dao.impl.HerdDaoImpl.java
/** * TODO: Remove this method once we migrate away from Oracle getStorageUploadStatsByBusinessObjectDefinition that uses Oracle specific 'trunc' function. * * @param storageAlternateKey the storage alternate key * @param dateRange the date range/*from ww w. j ava 2s . c o m*/ * * @return the upload statistics */ private StorageBusinessObjectDefinitionDailyUploadStats getStorageUploadStatsByBusinessObjectDefinitionOracle( StorageAlternateKeyDto storageAlternateKey, DateRangeDto dateRange) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> criteria = builder.createTupleQuery(); // The criteria root is the storage file. Root<StorageFileEntity> storageFileEntity = criteria.from(StorageFileEntity.class); // Join to the other tables we can filter on. Join<StorageFileEntity, StorageUnitEntity> storageUnitEntity = storageFileEntity .join(StorageFileEntity_.storageUnit); Join<StorageUnitEntity, StorageEntity> storageEntity = storageUnitEntity.join(StorageUnitEntity_.storage); Join<StorageUnitEntity, BusinessObjectDataEntity> businessObjectDataEntity = storageUnitEntity .join(StorageUnitEntity_.businessObjectData); Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntity = businessObjectDataEntity .join(BusinessObjectDataEntity_.businessObjectFormat); Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectFormatEntity .join(BusinessObjectFormatEntity_.businessObjectDefinition); Join<BusinessObjectDefinitionEntity, DataProviderEntity> dataProviderEntity = businessObjectDefinitionEntity .join(BusinessObjectDefinitionEntity_.dataProvider); Join<BusinessObjectDefinitionEntity, NamespaceEntity> namespaceEntity = businessObjectDefinitionEntity .join(BusinessObjectDefinitionEntity_.namespace); // Create paths and expressions. Path<String> namespacePath = namespaceEntity.get(NamespaceEntity_.code); Path<String> dataProviderNamePath = dataProviderEntity.get(DataProviderEntity_.name); Path<String> businessObjectDefinitionNamePath = businessObjectDefinitionEntity .get(BusinessObjectDefinitionEntity_.name); Expression<Date> truncCreatedOnDateExpression = builder.function("trunc", Date.class, storageFileEntity.get(StorageFileEntity_.createdOn)); Expression<Long> totalFilesExpression = builder.count(storageFileEntity.get(StorageFileEntity_.id)); Expression<Long> totalBytesExpression = builder .sum(storageFileEntity.get(StorageFileEntity_.fileSizeBytes)); // Create the standard restrictions (i.e. the standard where clauses). Predicate storageNameRestriction = builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)), storageAlternateKey.getStorageName().toUpperCase()); Predicate createDateRestriction = builder.and( builder.greaterThanOrEqualTo(truncCreatedOnDateExpression, dateRange.getLowerDate()), builder.lessThanOrEqualTo(truncCreatedOnDateExpression, dateRange.getUpperDate())); criteria.multiselect(truncCreatedOnDateExpression, namespacePath, dataProviderNamePath, businessObjectDefinitionNamePath, totalFilesExpression, totalBytesExpression); criteria.where(builder.and(storageNameRestriction, createDateRestriction)); // Create the group by clause. List<Expression<?>> grouping = new ArrayList<>(); grouping.add(truncCreatedOnDateExpression); grouping.add(namespacePath); grouping.add(dataProviderNamePath); grouping.add(businessObjectDefinitionNamePath); criteria.groupBy(grouping); // Create the order by clause. criteria.orderBy(builder.asc(truncCreatedOnDateExpression), builder.asc(namespacePath), builder.asc(dataProviderNamePath), builder.asc(businessObjectDefinitionNamePath)); // Retrieve and return the storage upload statistics. List<Tuple> tuples = entityManager.createQuery(criteria).getResultList(); StorageBusinessObjectDefinitionDailyUploadStats uploadStats = new StorageBusinessObjectDefinitionDailyUploadStats(); for (Tuple tuple : tuples) { StorageBusinessObjectDefinitionDailyUploadStat uploadStat = new StorageBusinessObjectDefinitionDailyUploadStat(); uploadStats.getStorageBusinessObjectDefinitionDailyUploadStats().add(uploadStat); uploadStat.setUploadDate( HerdDateUtils.getXMLGregorianCalendarValue(tuple.get(truncCreatedOnDateExpression))); uploadStat.setNamespace(tuple.get(namespacePath)); uploadStat.setDataProviderName(tuple.get(dataProviderNamePath)); uploadStat.setBusinessObjectDefinitionName(tuple.get(businessObjectDefinitionNamePath)); uploadStat.setTotalFiles(tuple.get(totalFilesExpression)); uploadStat.setTotalBytes(tuple.get(totalBytesExpression)); } return uploadStats; }