Example usage for javax.persistence.criteria CriteriaBuilder and

List of usage examples for javax.persistence.criteria CriteriaBuilder and

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder and.

Prototype

Predicate and(Expression<Boolean> x, Expression<Boolean> y);

Source Link

Document

Create a conjunction of the given boolean expressions.

Usage

From source file:org.finra.herd.dao.impl.HerdDaoImpl.java

/**
 * TODO: Make this method the main body of getStorageUploadStatsByBusinessObjectDefinition once we migrate away from Oracle TODO:
 * getStorageUploadStatsByBusinessObjectDefinition that uses storage file view. This method is meant to be database agnostic.
 *
 * @param storageAlternateKey the storage alternate key
 * @param dateRange the date range/*from   w  w  w . ja v a 2 s.c om*/
 *
 * @return the upload statistics
 */
private StorageBusinessObjectDefinitionDailyUploadStats getStorageUploadStatsByBusinessObjectDefinitionDatabaseAgnostic(
        StorageAlternateKeyDto storageAlternateKey, DateRangeDto dateRange) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = builder.createTupleQuery();

    Root<StorageFileViewEntity> storageFileViewEntity = criteria.from(StorageFileViewEntity.class);

    Path<String> namespaceCode = storageFileViewEntity.get(StorageFileViewEntity_.namespaceCode);
    Path<String> dataProviderCode = storageFileViewEntity.get(StorageFileViewEntity_.dataProviderCode);
    Path<String> businessObjectDefinitionName = storageFileViewEntity
            .get(StorageFileViewEntity_.businessObjectDefinitionName);
    Path<Date> createdDate = storageFileViewEntity.get(StorageFileViewEntity_.createdDate);
    Expression<Long> totalFilesExpression = builder
            .count(storageFileViewEntity.get(StorageFileViewEntity_.storageFileId));
    Expression<Long> totalBytesExpression = builder
            .sum(storageFileViewEntity.get(StorageFileViewEntity_.fileSizeInBytes));

    Predicate storageNameRestriction = builder.equal(
            builder.upper(storageFileViewEntity.get(StorageFileViewEntity_.storageCode)),
            storageAlternateKey.getStorageName().toUpperCase());
    Predicate createDateRestriction = builder.and(
            builder.greaterThanOrEqualTo(createdDate, dateRange.getLowerDate()),
            builder.lessThanOrEqualTo(createdDate, dateRange.getUpperDate()));

    criteria.multiselect(createdDate, namespaceCode, dataProviderCode, businessObjectDefinitionName,
            totalFilesExpression, totalBytesExpression);
    criteria.where(builder.and(storageNameRestriction, createDateRestriction));

    // Create the group by clause.
    List<Expression<?>> grouping = new ArrayList<>();
    grouping.add(createdDate);
    grouping.add(namespaceCode);
    grouping.add(dataProviderCode);
    grouping.add(businessObjectDefinitionName);
    criteria.groupBy(grouping);

    // Create the order by clause.
    criteria.orderBy(builder.asc(createdDate), builder.asc(namespaceCode), builder.asc(dataProviderCode),
            builder.asc(businessObjectDefinitionName));

    // 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(createdDate)));
        uploadStat.setNamespace(tuple.get(namespaceCode));
        uploadStat.setDataProviderName(tuple.get(dataProviderCode));
        uploadStat.setBusinessObjectDefinitionName(tuple.get(businessObjectDefinitionName));
        uploadStat.setTotalFiles(tuple.get(totalFilesExpression));
        uploadStat.setTotalBytes(tuple.get(totalBytesExpression));
    }

    return uploadStats;
}

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/* w  w  w . j  ava 2 s.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;
}

From source file:org.finra.herd.dao.impl.JobDefinitionDaoImpl.java

@Override
public JobDefinitionEntity getJobDefinitionByAltKey(String namespace, String jobName) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<JobDefinitionEntity> criteria = builder.createQuery(JobDefinitionEntity.class);

    // The criteria root is the job definition.
    Root<JobDefinitionEntity> jobDefinition = criteria.from(JobDefinitionEntity.class);

    // Join to the other tables we can filter on.
    Join<JobDefinitionEntity, NamespaceEntity> namespaceJoin = jobDefinition
            .join(JobDefinitionEntity_.namespace);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate namespaceRestriction = builder.equal(builder.upper(namespaceJoin.get(NamespaceEntity_.code)),
            namespace.toUpperCase());//from w  ww . j  a va2  s. c  o  m
    Predicate jobNameRestriction = builder.equal(builder.upper(jobDefinition.get(JobDefinitionEntity_.name)),
            jobName.toUpperCase());

    criteria.select(jobDefinition).where(builder.and(namespaceRestriction, jobNameRestriction));

    return executeSingleResultQuery(criteria, String.format(
            "Found more than one Activiti job definition with parameters {namespace=\"%s\", jobName=\"%s\"}.",
            namespace, jobName));
}

From source file:org.finra.herd.dao.impl.JobDefinitionDaoImpl.java

@Override
public JobDefinitionEntity getJobDefinitionByProcessDefinitionId(String processDefinitionId) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<JobDefinitionEntity> criteria = builder.createQuery(JobDefinitionEntity.class);

    // The criteria root is the job definition.
    Root<JobDefinitionEntity> jobDefinition = criteria.from(JobDefinitionEntity.class);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate namespaceRestriction = builder.equal(jobDefinition.get(JobDefinitionEntity_.activitiId),
            processDefinitionId);/*from www  .  j  a v  a2s.c  o m*/

    criteria.select(jobDefinition).where(builder.and(namespaceRestriction, namespaceRestriction));

    return executeSingleResultQuery(criteria,
            String.format("Found more than one Activiti job definition with processDefinitionId = \"%s\".",
                    processDefinitionId));
}

From source file:org.finra.herd.dao.impl.StorageFileDaoImpl.java

@Override
public StorageFileEntity getStorageFileByStorageNameAndFilePath(String storageName, String filePath) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<StorageFileEntity> criteria = builder.createQuery(StorageFileEntity.class);

    // The criteria root is the storage files.
    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);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate filePathRestriction = builder.equal(storageFileEntity.get(StorageFileEntity_.path), filePath);
    Predicate storageNameRestriction = builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)),
            storageName.toUpperCase());//w w  w. j a  v a  2  s  . c o m

    criteria.select(storageFileEntity).where(builder.and(filePathRestriction, storageNameRestriction));

    return executeSingleResultQuery(criteria, String.format(
            "Found more than one storage file with parameters {storageName=\"%s\"," + " filePath=\"%s\"}.",
            storageName, filePath));
}

From source file:org.finra.herd.dao.impl.StorageFileDaoImpl.java

@Override
public StorageFileEntity getStorageFileByStorageUnitEntityAndFilePath(StorageUnitEntity storageUnitEntity,
        String filePath) {/*  w w w.  j a v  a 2  s. co  m*/
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<StorageFileEntity> criteria = builder.createQuery(StorageFileEntity.class);

    // The criteria root is the storage files.
    Root<StorageFileEntity> storageFileEntity = criteria.from(StorageFileEntity.class);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate filePathRestriction = builder.equal(storageFileEntity.get(StorageFileEntity_.path), filePath);
    Predicate storageUnitRestriction = builder.equal(storageFileEntity.get(StorageFileEntity_.storageUnitId),
            storageUnitEntity.getId());

    criteria.select(storageFileEntity).where(builder.and(filePathRestriction, storageUnitRestriction));

    return executeSingleResultQuery(criteria, String.format(
            "Found more than one storage file with parameters {storageUnitId=\"%s\"," + " filePath=\"%s\"}.",
            storageUnitEntity.getId(), filePath));
}

From source file:org.finra.herd.dao.impl.StorageFileDaoImpl.java

@Override
public Long getStorageFileCount(String storageName, String filePathPrefix) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = builder.createQuery(Long.class);

    // The criteria root is the storage files.
    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);

    // Create path.
    Expression<Long> storageFileCount = builder.count(storageFileEntity.get(StorageFileEntity_.id));

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate storageNameRestriction = builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)),
            storageName.toUpperCase());//from  w w  w  .  ja va  2 s.c  o  m
    Predicate filePathRestriction = builder.like(storageFileEntity.get(StorageFileEntity_.path),
            String.format("%s%%", filePathPrefix));

    // Add the clauses for the query.
    criteria.select(storageFileCount).where(builder.and(storageNameRestriction, filePathRestriction));

    return entityManager.createQuery(criteria).getSingleResult();
}

From source file:org.finra.herd.dao.impl.StorageFileDaoImpl.java

@Override
public List<String> getStorageFilesByStorageAndFilePathPrefix(String storageName, String filePathPrefix) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<StorageFileEntity> criteria = builder.createQuery(StorageFileEntity.class);

    // The criteria root is the storage files.
    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);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate filePathRestriction = builder.like(storageFileEntity.get(StorageFileEntity_.path),
            String.format("%s%%", filePathPrefix));
    Predicate storageNameRestriction = builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)),
            storageName.toUpperCase());//from w  ww . j a v  a  2 s  .  com

    // Order the results by file path.
    Order orderByFilePath = builder.asc(storageFileEntity.get(StorageFileEntity_.path));

    criteria.select(storageFileEntity).where(builder.and(filePathRestriction, storageNameRestriction))
            .orderBy(orderByFilePath);

    // Retrieve the storage files.
    List<StorageFileEntity> storageFileEntities = entityManager.createQuery(criteria).getResultList();

    // Build the result list.
    List<String> storageFilePaths = new ArrayList<>();
    for (StorageFileEntity storageFile : storageFileEntities) {
        storageFilePaths.add(storageFile.getPath());
    }

    return storageFilePaths;
}

From source file:org.finra.herd.dao.impl.StorageUnitDaoImpl.java

@Override
public StorageUnitEntity getStorageUnitByBusinessObjectDataAndStorageName(
        BusinessObjectDataEntity businessObjectDataEntity, String storageName) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<StorageUnitEntity> criteria = builder.createQuery(StorageUnitEntity.class);
    Root<StorageUnitEntity> storageUnitEntity = criteria.from(StorageUnitEntity.class);

    // join storage unit to storage to retrieve name
    Join<StorageUnitEntity, StorageEntity> storageEntity = storageUnitEntity.join(StorageUnitEntity_.storage);

    // where business object data equals
    Predicate businessObjectDataRestriction = builder
            .equal(storageUnitEntity.get(StorageUnitEntity_.businessObjectData), businessObjectDataEntity);
    // where storage name equals, ignoring case
    Predicate storageNameRestriction = builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)),
            storageName.toUpperCase());//  w  w w.  j  a v  a 2  s .c  om

    criteria.select(storageUnitEntity)
            .where(builder.and(businessObjectDataRestriction, storageNameRestriction));

    List<StorageUnitEntity> resultList = entityManager.createQuery(criteria).getResultList();

    // return single result or null
    return resultList.size() >= 1 ? resultList.get(0) : null;
}

From source file:org.finra.herd.dao.impl.StorageUnitDaoImpl.java

@Override
public List<StorageUnitEntity> getStorageUnitsByStorageAndBusinessObjectData(StorageEntity storageEntity,
        List<BusinessObjectDataEntity> businessObjectDataEntities) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<StorageUnitEntity> criteria = builder.createQuery(StorageUnitEntity.class);

    // The criteria root is the storage unit.
    Root<StorageUnitEntity> storageUnitEntity = criteria.from(StorageUnitEntity.class);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate queryRestriction = builder.equal(storageUnitEntity.get(StorageUnitEntity_.storage),
            storageEntity);/*from w w w .j  av a  2  s.  co m*/
    queryRestriction = builder.and(queryRestriction, getPredicateForInClause(builder,
            storageUnitEntity.get(StorageUnitEntity_.businessObjectData), businessObjectDataEntities));

    // Add the clauses for the query.
    criteria.select(storageUnitEntity).where(queryRestriction);

    // Execute the query and return the results.
    return entityManager.createQuery(criteria).getResultList();
}