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.dm.dao.impl.DmDaoImpl.java

/**
 * Builds a query restriction predicate for the specified business object data entity as per primary and sub-partition values in the business object data
 * key.//w ww .  j a  v  a 2 s  . com
 *
 * @param builder the criteria builder
 * @param businessObjectDataEntity the business object data entity that appears in the from clause
 * @param businessObjectDataKey the business object data key
 *
 * @return the query restriction predicate
 */
private Predicate getQueryRestrictionOnPartitionValues(CriteriaBuilder builder,
        From<?, BusinessObjectDataEntity> businessObjectDataEntity,
        BusinessObjectDataKey businessObjectDataKey) {
    // Create a standard restriction on primary partition value.
    Predicate predicate = builder.equal(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue),
            businessObjectDataKey.getPartitionValue());

    // Create and add standard restrictions on sub-partition values. Please note that the subpartition value columns are nullable.
    int subPartitionValuesCount = getSubPartitionValuesCount(businessObjectDataKey);
    for (int i = 0; i < BusinessObjectDataEntity.MAX_SUBPARTITIONS; i++) {
        predicate = builder.and(predicate, i < subPartitionValuesCount
                ? builder.equal(businessObjectDataEntity.get(BUSINESS_OBJECT_DATA_SUBPARTITIONS.get(i)),
                        businessObjectDataKey.getSubPartitionValues().get(i))
                : builder.isNull(businessObjectDataEntity.get(BUSINESS_OBJECT_DATA_SUBPARTITIONS.get(i))));
    }

    return predicate;
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * Builds a query restriction predicate for the sub-query business object data entity as per partition values from the specified main query business object
 * data entity.//from www. j a va  2 s.c  o  m
 *
 * @param builder the criteria builder
 * @param subBusinessObjectDataEntity the sub-query business object data entity that appears in the from clause
 * @param mainBusinessObjectDataEntity the main query business object data entity that appears in the from clause
 *
 * @return the query restriction predicate
 */
private Predicate getQueryRestrictionOnPartitionValues(CriteriaBuilder builder,
        From<?, BusinessObjectDataEntity> subBusinessObjectDataEntity,
        From<?, BusinessObjectDataEntity> mainBusinessObjectDataEntity) {
    // Create a standard restriction on primary partition value.
    Predicate predicate = builder.equal(
            subBusinessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue),
            mainBusinessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue));

    // Create and add standard restrictions on sub-partition values. Please note that the subpartition value columns are nullable.
    for (SingularAttribute<BusinessObjectDataEntity, String> businessObjectDataPartitionValueSingularAttribute : BUSINESS_OBJECT_DATA_SUBPARTITIONS) {
        predicate = builder.and(predicate, builder.or(
                builder.and(
                        builder.isNull(subBusinessObjectDataEntity
                                .get(businessObjectDataPartitionValueSingularAttribute)),
                        builder.isNull(mainBusinessObjectDataEntity
                                .get(businessObjectDataPartitionValueSingularAttribute))),
                builder.equal(
                        subBusinessObjectDataEntity.get(businessObjectDataPartitionValueSingularAttribute),
                        mainBusinessObjectDataEntity.get(businessObjectDataPartitionValueSingularAttribute))));
    }

    return predicate;
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * Builds a query restriction predicate for the sub-query business object data entity as per partition values from the specified main query business object
 * data entity./*from  w w  w  .j  av a2  s.  c  o  m*/
 *
 * @param builder the criteria builder
 * @param businessObjectDataEntity the business object data entity that appears in the from clause
 * @param partitionFilters the list of partition filter to be used to select business object data instances. Each partition filter contains a list of
 * primary and sub-partition values in the right order up to the maximum partition levels allowed by business object data registration - with partition
 * values for the relative partitions not to be used for selection passed as nulls.
 *
 * @return the query restriction predicate
 */
private Predicate getQueryRestrictionOnPartitionValues(CriteriaBuilder builder,
        From<?, BusinessObjectDataEntity> businessObjectDataEntity, List<List<String>> partitionFilters) {
    // Create a query restriction as per specified primary and/or sub-partition values.
    Predicate predicate = null;
    for (List<String> partitionFilter : partitionFilters) {
        // Add restriction for each partition level if the relative partition value is specified in the partition filter.
        Predicate partitionRestriction = null;
        for (int partitionLevel = 0; partitionLevel < BusinessObjectDataEntity.MAX_SUBPARTITIONS
                + 1; partitionLevel++) {
            String partitionValue = partitionFilter.get(partitionLevel);
            if (StringUtils.isNotBlank(partitionValue)) {
                Predicate partitionValueRestriction = builder.equal(
                        businessObjectDataEntity.get(BUSINESS_OBJECT_DATA_PARTITIONS.get(partitionLevel)),
                        partitionValue);
                partitionRestriction = (partitionRestriction == null ? partitionValueRestriction
                        : builder.and(partitionRestriction, partitionValueRestriction));
            }
        }
        predicate = (predicate == null ? partitionRestriction : builder.or(predicate, partitionRestriction));
    }

    return predicate;
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * {@inheritDoc}/*from  ww  w  .j a v  a 2 s .c  o m*/
 */
@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());

    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.dm.dao.impl.DmDaoImpl.java

/**
 * {@inheritDoc}//from w  w w .j av a 2s.  co  m
 */
@Override
public StorageUnitEntity getStorageUnitByStorageNameAndDirectoryPath(String storageName, String directoryPath) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<StorageUnitEntity> criteria = builder.createQuery(StorageUnitEntity.class);

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

    // Join to the other tables we can filter on.
    Join<StorageUnitEntity, StorageEntity> storageEntity = storageUnitEntity.join(StorageUnitEntity_.storage);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate storageNameRestriction = builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)),
            storageName.toUpperCase());
    Predicate directoryPathRestriction = builder.equal(storageUnitEntity.get(StorageUnitEntity_.directoryPath),
            directoryPath);

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

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

    // Return the first found storage unit or null if none were found.
    return resultList.size() >= 1 ? resultList.get(0) : null;
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * {@inheritDoc}//from  w ww .ja v a2 s  . c  o  m
 */
@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);
    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();
}

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * {@inheritDoc}/*from ww w .  j a v a 2  s.  c  om*/
 */
@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());

    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.dm.dao.impl.DmDaoImpl.java

/**
 * {@inheritDoc}/*from w  ww .  j  a  va2  s.  co  m*/
 */
@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());
    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.dm.dao.impl.DmDaoImpl.java

/**
 * {@inheritDoc}/*from ww  w  .  j ava 2 s . c o  m*/
 */
@Override
public List<StorageFileEntity> getStorageFileEntities(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());

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

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

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

From source file:org.finra.dm.dao.impl.DmDaoImpl.java

/**
 * {@inheritDoc}//from   w w w.  ja va2 s  . c o m
 */
@Override
public List<StorageFileEntity> getStorageFilesByStorageAndBusinessObjectData(StorageEntity storageEntity,
        List<BusinessObjectDataEntity> businessObjectDataEntities) {
    // 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);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate queryRestriction = builder.equal(storageUnitEntity.get(StorageUnitEntity_.storage),
            storageEntity);
    queryRestriction = builder.and(queryRestriction, getPredicateForInClause(builder,
            storageUnitEntity.get(StorageUnitEntity_.businessObjectData), businessObjectDataEntities));

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

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

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