Example usage for javax.persistence.criteria CriteriaBuilder isNull

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

Introduction

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

Prototype

Predicate isNull(Expression<?> x);

Source Link

Document

Create a predicate to test whether the expression is null.

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.//from w  w w  .ja  va2  s  .c  o m
 *
 * @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  .  co 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

/**
 * {@inheritDoc}/*  w ww .  j a  v a 2 s . c om*/
 */
@Override
public List<BusinessObjectDataNotificationRegistrationEntity> getBusinessObjectDataNotificationRegistrations(
        String notificationEventTypeCode, BusinessObjectDataKey businessObjectDataKey) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BusinessObjectDataNotificationRegistrationEntity> criteria = builder
            .createQuery(BusinessObjectDataNotificationRegistrationEntity.class);

    // The criteria root is the business object data notification registration entity.
    Root<BusinessObjectDataNotificationRegistrationEntity> businessObjectDataNotificationEntity = criteria
            .from(BusinessObjectDataNotificationRegistrationEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectDataNotificationRegistrationEntity, NamespaceEntity> namespaceEntity = businessObjectDataNotificationEntity
            .join(BusinessObjectDataNotificationRegistrationEntity_.namespace);
    Join<BusinessObjectDataNotificationRegistrationEntity, NotificationEventTypeEntity> notificationEventTypeEntity = businessObjectDataNotificationEntity
            .join(BusinessObjectDataNotificationRegistrationEntity_.notificationEventType);
    Join<BusinessObjectDataNotificationRegistrationEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectDataNotificationEntity
            .join(BusinessObjectDataNotificationRegistrationEntity_.businessObjectDefinition);
    Join<BusinessObjectDefinitionEntity, NamespaceEntity> businessObjectDefinitionNamespaceEntity = businessObjectDefinitionEntity
            .join(BusinessObjectDefinitionEntity_.namespace);
    Join<BusinessObjectDataNotificationRegistrationEntity, FileTypeEntity> fileTypeEntity = businessObjectDataNotificationEntity
            .join(BusinessObjectDataNotificationRegistrationEntity_.fileType, JoinType.LEFT);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate queryRestriction = builder.equal(
            builder.upper(notificationEventTypeEntity.get(NotificationEventTypeEntity_.code)),
            notificationEventTypeCode.toUpperCase());
    queryRestriction = builder.and(queryRestriction,
            builder.equal(builder.upper(businessObjectDefinitionNamespaceEntity.get(NamespaceEntity_.code)),
                    businessObjectDataKey.getNamespace().toUpperCase()));
    queryRestriction = builder.and(queryRestriction,
            builder.equal(
                    builder.upper(businessObjectDefinitionEntity.get(BusinessObjectDefinitionEntity_.name)),
                    businessObjectDataKey.getBusinessObjectDefinitionName().toUpperCase()));
    queryRestriction = builder.and(queryRestriction,
            builder.or(
                    builder.isNull(businessObjectDataNotificationEntity
                            .get(BusinessObjectDataNotificationRegistrationEntity_.usage)),
                    builder.equal(
                            builder.upper(businessObjectDataNotificationEntity
                                    .get(BusinessObjectDataNotificationRegistrationEntity_.usage)),
                            businessObjectDataKey.getBusinessObjectFormatUsage().toUpperCase())));
    queryRestriction = builder.and(queryRestriction,
            builder.or(
                    builder.isNull(businessObjectDataNotificationEntity
                            .get(BusinessObjectDataNotificationRegistrationEntity_.fileType)),
                    builder.equal(builder.upper(fileTypeEntity.get(FileTypeEntity_.code)),
                            businessObjectDataKey.getBusinessObjectFormatFileType().toUpperCase())));
    queryRestriction = builder.and(queryRestriction, builder.or(
            builder.isNull(businessObjectDataNotificationEntity
                    .get(BusinessObjectDataNotificationRegistrationEntity_.businessObjectFormatVersion)),
            builder.equal(
                    businessObjectDataNotificationEntity
                            .get(BusinessObjectDataNotificationRegistrationEntity_.businessObjectFormatVersion),
                    businessObjectDataKey.getBusinessObjectFormatVersion())));

    // Order the results by namespace and notification name.
    List<Order> orderBy = new ArrayList<>();
    orderBy.add(builder.asc(namespaceEntity.get(NamespaceEntity_.code)));
    orderBy.add(builder.asc(
            businessObjectDataNotificationEntity.get(BusinessObjectDataNotificationRegistrationEntity_.name)));

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

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

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

/**
 * TODO This method may be bdata specific. Consider creating new abstract class to group all bdata related DAO. 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.
 *
 * @param builder the criteria builder//from   w  ww  .  j a v a2s  .com
 * @param businessObjectDataEntity the business object data entity that appears in the from clause
 * @param primaryPartitionValue the primary partition value of the business object data
 * @param subPartitionValues the list of sub-partition values for the business object data
 *
 * @return the query restriction predicate
 */
protected Predicate getQueryRestrictionOnPartitionValues(CriteriaBuilder builder,
        From<?, BusinessObjectDataEntity> businessObjectDataEntity, String primaryPartitionValue,
        List<String> subPartitionValues) {
    // Create a standard restriction on primary partition value.
    Predicate predicate = builder.equal(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue),
            primaryPartitionValue);

    // Create and add standard restrictions on sub-partition values. Please note that the subpartition value columns are nullable.
    int subPartitionValuesCount = CollectionUtils.size(subPartitionValues);
    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)),
                        subPartitionValues.get(i))
                : builder.isNull(businessObjectDataEntity.get(BUSINESS_OBJECT_DATA_SUBPARTITIONS.get(i))));
    }

    return predicate;
}

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

/**
 * TODO This method may be bdata specific. Consider creating new abstract class to group all bdata related DAO. 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.
 *
 * @param builder the criteria builder// w w  w  .j a va  2s  .  c  om
 * @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
 */
protected 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.herd.dao.impl.BusinessObjectDataDaoImpl.java

@Override
public Integer getBusinessObjectDataMaxVersion(BusinessObjectDataKey businessObjectDataKey) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class);

    // The criteria root is the business object data.
    Root<BusinessObjectDataEntity> businessObjectDataEntity = criteria.from(BusinessObjectDataEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntity = businessObjectDataEntity
            .join(BusinessObjectDataEntity_.businessObjectFormat);
    Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntity = businessObjectFormatEntity
            .join(BusinessObjectFormatEntity_.fileType);
    Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectFormatEntity
            .join(BusinessObjectFormatEntity_.businessObjectDefinition);
    Join<BusinessObjectDefinitionEntity, NamespaceEntity> namespaceEntity = businessObjectDefinitionEntity
            .join(BusinessObjectDefinitionEntity_.namespace);

    // Create the path.
    Expression<Integer> maxBusinessObjectDataVersion = builder
            .max(businessObjectDataEntity.get(BusinessObjectDataEntity_.version));

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate queryRestriction = builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)),
            businessObjectDataKey.getNamespace().toUpperCase());
    queryRestriction = builder.and(queryRestriction,
            builder.equal(//from  w ww. java2s.  c o m
                    builder.upper(businessObjectDefinitionEntity.get(BusinessObjectDefinitionEntity_.name)),
                    businessObjectDataKey.getBusinessObjectDefinitionName().toUpperCase()));
    queryRestriction = builder.and(queryRestriction,
            builder.equal(builder.upper(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.usage)),
                    businessObjectDataKey.getBusinessObjectFormatUsage().toUpperCase()));
    queryRestriction = builder.and(queryRestriction,
            builder.equal(builder.upper(fileTypeEntity.get(FileTypeEntity_.code)),
                    businessObjectDataKey.getBusinessObjectFormatFileType().toUpperCase()));
    queryRestriction = builder.and(queryRestriction,
            builder.equal(
                    businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion),
                    businessObjectDataKey.getBusinessObjectFormatVersion()));
    queryRestriction = builder.and(queryRestriction,
            builder.equal(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue),
                    businessObjectDataKey.getPartitionValue()));

    for (int i = 0; i < BusinessObjectDataEntity.MAX_SUBPARTITIONS; i++) {
        queryRestriction = builder.and(queryRestriction,
                i < businessObjectDataKey.getSubPartitionValues().size()
                        ? builder.equal(businessObjectDataEntity.get(BUSINESS_OBJECT_DATA_SUBPARTITIONS.get(i)),
                                businessObjectDataKey.getSubPartitionValues().get(i))
                        : builder.isNull(
                                businessObjectDataEntity.get(BUSINESS_OBJECT_DATA_SUBPARTITIONS.get(i))));
    }

    criteria.select(maxBusinessObjectDataVersion).where(queryRestriction);

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

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

@Override
public Map<BusinessObjectDataEntity, StoragePolicyEntity> getBusinessObjectDataEntitiesMatchingStoragePolicies(
        StoragePolicyPriorityLevel storagePolicyPriorityLevel, List<String> supportedBusinessObjectDataStatuses,
        int storagePolicyTransitionMaxAllowedAttempts, int startPosition, int maxResult) {
    // Create the criteria builder and a tuple style criteria query.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = builder.createTupleQuery();

    // The criteria root is the business object data along with the storage policy.
    Root<BusinessObjectDataEntity> businessObjectDataEntityRoot = criteria.from(BusinessObjectDataEntity.class);
    Root<StoragePolicyEntity> storagePolicyEntityRoot = criteria.from(StoragePolicyEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectDataEntity, StorageUnitEntity> storageUnitEntityJoin = businessObjectDataEntityRoot
            .join(BusinessObjectDataEntity_.storageUnits);
    Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntityJoin = businessObjectDataEntityRoot
            .join(BusinessObjectDataEntity_.businessObjectFormat);

    // Create main query restrictions based on the specified parameters.
    List<Predicate> predicates = new ArrayList<>();

    // Add restriction on business object definition.
    predicates.add(storagePolicyPriorityLevel.isBusinessObjectDefinitionIsNull()
            ? builder.isNull(storagePolicyEntityRoot.get(StoragePolicyEntity_.businessObjectDefinitionId))
            : builder.equal(/*ww  w. j  av  a2 s .co  m*/
                    businessObjectFormatEntityJoin.get(BusinessObjectFormatEntity_.businessObjectDefinitionId),
                    storagePolicyEntityRoot.get(StoragePolicyEntity_.businessObjectDefinitionId)));

    // Add restriction on business object format usage.
    predicates.add(storagePolicyPriorityLevel.isUsageIsNull()
            ? builder.isNull(storagePolicyEntityRoot.get(StoragePolicyEntity_.usage))
            : builder.equal(
                    builder.upper(businessObjectFormatEntityJoin.get(BusinessObjectFormatEntity_.usage)),
                    builder.upper(storagePolicyEntityRoot.get(StoragePolicyEntity_.usage))));

    // Add restriction on business object format file type.
    predicates.add(storagePolicyPriorityLevel.isFileTypeIsNull()
            ? builder.isNull(storagePolicyEntityRoot.get(StoragePolicyEntity_.fileType))
            : builder.equal(businessObjectFormatEntityJoin.get(BusinessObjectFormatEntity_.fileTypeCode),
                    storagePolicyEntityRoot.get(StoragePolicyEntity_.fileTypeCode)));

    // Add restriction on storage policy filter storage.
    predicates.add(builder.equal(storageUnitEntityJoin.get(StorageUnitEntity_.storageName),
            storagePolicyEntityRoot.get(StoragePolicyEntity_.storageName)));

    // Add restriction on storage policy latest version flag.
    predicates.add(builder.isTrue(storagePolicyEntityRoot.get(StoragePolicyEntity_.latestVersion)));

    // Add restriction on storage policy status.
    predicates.add(builder.equal(storagePolicyEntityRoot.get(StoragePolicyEntity_.statusCode),
            StoragePolicyStatusEntity.ENABLED));

    // Add restriction on supported business object data statuses.
    predicates.add(businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.statusCode)
            .in(supportedBusinessObjectDataStatuses));

    // Add restrictions as per storage policy transition type.
    predicates.add(
            builder.equal(storagePolicyEntityRoot.get(StoragePolicyEntity_.storagePolicyTransitionTypeCode),
                    StoragePolicyTransitionTypeEntity.GLACIER));
    predicates.add(storageUnitEntityJoin.get(StorageUnitEntity_.statusCode)
            .in(Lists.newArrayList(StorageUnitStatusEntity.ENABLED, StorageUnitStatusEntity.ARCHIVING)));

    // If specified, add restriction on maximum allowed attempts for a storage policy transition.
    if (storagePolicyTransitionMaxAllowedAttempts > 0) {
        predicates.add(builder.or(
                builder.isNull(
                        storageUnitEntityJoin.get(StorageUnitEntity_.storagePolicyTransitionFailedAttempts)),
                builder.lessThan(
                        storageUnitEntityJoin.get(StorageUnitEntity_.storagePolicyTransitionFailedAttempts),
                        storagePolicyTransitionMaxAllowedAttempts)));
    }

    // Order the results by business object data "created on" value.
    Order orderByCreatedOn = builder.asc(businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.createdOn));

    // Add the select clause to the main query.
    criteria.multiselect(businessObjectDataEntityRoot, storagePolicyEntityRoot);

    // Add the where clause to the main query.
    criteria.where(predicates.toArray(new Predicate[] {}));

    // Add the order by clause to the main query.
    criteria.orderBy(orderByCreatedOn);

    // Run the query to get a list of tuples back.
    List<Tuple> tuples = entityManager.createQuery(criteria).setFirstResult(startPosition)
            .setMaxResults(maxResult).getResultList();

    // Populate the result map from the returned tuples (i.e. 1 tuple for each row).
    Map<BusinessObjectDataEntity, StoragePolicyEntity> result = new LinkedHashMap<>();
    for (Tuple tuple : tuples) {
        // Since multiple storage policies can contain identical filters, we add the below check to select each business object data instance only once.
        if (!result.containsKey(tuple.get(businessObjectDataEntityRoot))) {
            result.put(tuple.get(businessObjectDataEntityRoot), tuple.get(storagePolicyEntityRoot));
        }
    }

    return result;
}

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

@Override
public List<NotificationRegistrationKey> getBusinessObjectDataNotificationRegistrationKeysByNotificationFilter(
        BusinessObjectDataNotificationFilter businessObjectDataNotificationFilter) {
    // Create the criteria builder and a tuple style criteria query.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = builder.createTupleQuery();

    // The criteria root is the business object data notification registration.
    Root<BusinessObjectDataNotificationRegistrationEntity> notificationRegistrationEntityRoot = criteria
            .from(BusinessObjectDataNotificationRegistrationEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectDataNotificationRegistrationEntity, NamespaceEntity> notificationRegistrationNamespaceEntityJoin = notificationRegistrationEntityRoot
            .join(BusinessObjectDataNotificationRegistrationEntity_.namespace);
    Join<BusinessObjectDataNotificationRegistrationEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = notificationRegistrationEntityRoot
            .join(BusinessObjectDataNotificationRegistrationEntity_.businessObjectDefinition);
    Join<BusinessObjectDefinitionEntity, NamespaceEntity> businessObjectDefinitionNamespaceEntity = businessObjectDefinitionEntity
            .join(BusinessObjectDefinitionEntity_.namespace);
    Join<BusinessObjectDataNotificationRegistrationEntity, FileTypeEntity> fileTypeEntity = notificationRegistrationEntityRoot
            .join(BusinessObjectDataNotificationRegistrationEntity_.fileType, JoinType.LEFT);

    // Get the columns.
    Path<String> notificationRegistrationNamespaceColumn = notificationRegistrationNamespaceEntityJoin
            .get(NamespaceEntity_.code);
    Path<String> notificationRegistrationNameColumn = notificationRegistrationEntityRoot
            .get(BusinessObjectDataNotificationRegistrationEntity_.name);

    // Create the standard restrictions (i.e. the standard where clauses).
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(/*  ww  w  .  j  ava  2  s  .c o m*/
            builder.equal(builder.upper(businessObjectDefinitionNamespaceEntity.get(NamespaceEntity_.code)),
                    businessObjectDataNotificationFilter.getNamespace().toUpperCase()));
    predicates.add(builder.equal(
            builder.upper(businessObjectDefinitionEntity.get(BusinessObjectDefinitionEntity_.name)),
            businessObjectDataNotificationFilter.getBusinessObjectDefinitionName().toUpperCase()));
    if (StringUtils.isNotBlank(businessObjectDataNotificationFilter.getBusinessObjectFormatUsage())) {
        predicates.add(builder.or(
                builder.isNull(notificationRegistrationEntityRoot
                        .get(BusinessObjectDataNotificationRegistrationEntity_.usage)),
                builder.equal(
                        builder.upper(notificationRegistrationEntityRoot
                                .get(BusinessObjectDataNotificationRegistrationEntity_.usage)),
                        businessObjectDataNotificationFilter.getBusinessObjectFormatUsage().toUpperCase())));
    }
    if (StringUtils.isNotBlank(businessObjectDataNotificationFilter.getBusinessObjectFormatFileType())) {
        predicates.add(builder.or(
                builder.isNull(notificationRegistrationEntityRoot
                        .get(BusinessObjectDataNotificationRegistrationEntity_.fileType)),
                builder.equal(builder.upper(fileTypeEntity.get(FileTypeEntity_.code)),
                        businessObjectDataNotificationFilter.getBusinessObjectFormatFileType().toUpperCase())));
    }

    // Add the select and where clauses to the query.
    criteria.multiselect(notificationRegistrationNamespaceColumn, notificationRegistrationNameColumn)
            .where(builder.and(predicates.toArray(new Predicate[predicates.size()])));

    // Add the order by clause to the query.
    criteria.orderBy(builder.asc(notificationRegistrationNamespaceColumn),
            builder.asc(notificationRegistrationNameColumn));

    // Run the query to get a list of tuples back.
    List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();

    // Populate the list of keys from the returned tuples.
    return getNotificationRegistrationKeys(tuples, notificationRegistrationNamespaceColumn,
            notificationRegistrationNameColumn);
}

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

@Override
public List<BusinessObjectDataNotificationRegistrationEntity> getBusinessObjectDataNotificationRegistrations(
        String notificationEventTypeCode, BusinessObjectDataKey businessObjectDataKey,
        String newBusinessObjectDataStatus, String oldBusinessObjectDataStatus,
        String notificationRegistrationStatus) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BusinessObjectDataNotificationRegistrationEntity> criteria = builder
            .createQuery(BusinessObjectDataNotificationRegistrationEntity.class);

    // The criteria root is the business object data notification registration entity.
    Root<BusinessObjectDataNotificationRegistrationEntity> businessObjectDataNotificationEntityRoot = criteria
            .from(BusinessObjectDataNotificationRegistrationEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectDataNotificationRegistrationEntity, NamespaceEntity> namespaceEntityJoin = businessObjectDataNotificationEntityRoot
            .join(BusinessObjectDataNotificationRegistrationEntity_.namespace);
    Join<BusinessObjectDataNotificationRegistrationEntity, NotificationEventTypeEntity> notificationEventTypeEntityJoin = businessObjectDataNotificationEntityRoot
            .join(BusinessObjectDataNotificationRegistrationEntity_.notificationEventType);
    Join<BusinessObjectDataNotificationRegistrationEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntityJoin = businessObjectDataNotificationEntityRoot
            .join(BusinessObjectDataNotificationRegistrationEntity_.businessObjectDefinition);
    Join<BusinessObjectDefinitionEntity, NamespaceEntity> businessObjectDefinitionNamespaceEntityJoin = businessObjectDefinitionEntityJoin
            .join(BusinessObjectDefinitionEntity_.namespace);
    Join<BusinessObjectDataNotificationRegistrationEntity, FileTypeEntity> fileTypeEntityJoin = businessObjectDataNotificationEntityRoot
            .join(BusinessObjectDataNotificationRegistrationEntity_.fileType, JoinType.LEFT);
    Join<BusinessObjectDataNotificationRegistrationEntity, BusinessObjectDataStatusEntity> newBusinessObjectDataStatusEntityJoin = businessObjectDataNotificationEntityRoot
            .join(BusinessObjectDataNotificationRegistrationEntity_.newBusinessObjectDataStatus, JoinType.LEFT);
    Join<BusinessObjectDataNotificationRegistrationEntity, BusinessObjectDataStatusEntity> oldBusinessObjectDataStatusEntityJoin = businessObjectDataNotificationEntityRoot
            .join(BusinessObjectDataNotificationRegistrationEntity_.oldBusinessObjectDataStatus, JoinType.LEFT);
    Join<BusinessObjectDataNotificationRegistrationEntity, NotificationRegistrationStatusEntity> notificationRegistrationStatusEntityJoin = businessObjectDataNotificationEntityRoot
            .join(BusinessObjectDataNotificationRegistrationEntity_.notificationRegistrationStatus);

    // Create the standard restrictions (i.e. the standard where clauses).
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(/*from   w  w w . ja  v  a  2  s  .  c  o m*/
            builder.equal(builder.upper(notificationEventTypeEntityJoin.get(NotificationEventTypeEntity_.code)),
                    notificationEventTypeCode.toUpperCase()));
    predicates.add(
            builder.equal(builder.upper(businessObjectDefinitionNamespaceEntityJoin.get(NamespaceEntity_.code)),
                    businessObjectDataKey.getNamespace().toUpperCase()));
    predicates.add(builder.equal(
            builder.upper(businessObjectDefinitionEntityJoin.get(BusinessObjectDefinitionEntity_.name)),
            businessObjectDataKey.getBusinessObjectDefinitionName().toUpperCase()));
    predicates.add(builder.or(
            builder.isNull(businessObjectDataNotificationEntityRoot
                    .get(BusinessObjectDataNotificationRegistrationEntity_.usage)),
            builder.equal(
                    builder.upper(businessObjectDataNotificationEntityRoot
                            .get(BusinessObjectDataNotificationRegistrationEntity_.usage)),
                    businessObjectDataKey.getBusinessObjectFormatUsage().toUpperCase())));
    predicates.add(builder.or(
            builder.isNull(businessObjectDataNotificationEntityRoot
                    .get(BusinessObjectDataNotificationRegistrationEntity_.fileType)),
            builder.equal(builder.upper(fileTypeEntityJoin.get(FileTypeEntity_.code)),
                    businessObjectDataKey.getBusinessObjectFormatFileType().toUpperCase())));
    predicates.add(builder.or(
            builder.isNull(businessObjectDataNotificationEntityRoot
                    .get(BusinessObjectDataNotificationRegistrationEntity_.businessObjectFormatVersion)),
            builder.equal(
                    businessObjectDataNotificationEntityRoot
                            .get(BusinessObjectDataNotificationRegistrationEntity_.businessObjectFormatVersion),
                    businessObjectDataKey.getBusinessObjectFormatVersion())));
    predicates.add(builder.or(
            builder.isNull(businessObjectDataNotificationEntityRoot
                    .get(BusinessObjectDataNotificationRegistrationEntity_.newBusinessObjectDataStatus)),
            builder.equal(
                    builder.upper(
                            newBusinessObjectDataStatusEntityJoin.get(BusinessObjectDataStatusEntity_.code)),
                    newBusinessObjectDataStatus.toUpperCase())));
    // Please note that old business object data status parameter value is null for a business object data registration event.
    predicates.add(builder.or(
            builder.isNull(businessObjectDataNotificationEntityRoot
                    .get(BusinessObjectDataNotificationRegistrationEntity_.oldBusinessObjectDataStatus)),
            builder.equal(
                    builder.upper(
                            oldBusinessObjectDataStatusEntityJoin.get(BusinessObjectDataStatusEntity_.code)),
                    oldBusinessObjectDataStatus == null ? null : oldBusinessObjectDataStatus.toUpperCase())));
    predicates.add(builder.equal(
            builder.upper(
                    notificationRegistrationStatusEntityJoin.get(NotificationRegistrationStatusEntity_.code)),
            notificationRegistrationStatus.toUpperCase()));

    // Order the results by namespace and notification name.
    List<Order> orderBy = new ArrayList<>();
    orderBy.add(builder.asc(namespaceEntityJoin.get(NamespaceEntity_.code)));
    orderBy.add(builder.asc(businessObjectDataNotificationEntityRoot
            .get(BusinessObjectDataNotificationRegistrationEntity_.name)));

    // Add the clauses for the query.
    criteria.select(businessObjectDataNotificationEntityRoot)
            .where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).orderBy(orderBy);

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

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

/**
 * {@inheritDoc}/*ww w.ja v a 2  s  . c  o m*/
 */
@Override
public Map<BusinessObjectDataEntity, StoragePolicyEntity> getBusinessObjectDataEntitiesMatchingStoragePolicies(
        StoragePolicyPriorityLevel storagePolicyPriorityLevel, List<String> supportedBusinessObjectDataStatuses,
        int startPosition, int maxResult) {
    // Create the criteria builder and a tuple style criteria query.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = builder.createTupleQuery();

    // The criteria root is the business object data.
    Root<BusinessObjectDataEntity> businessObjectDataEntity = criteria.from(BusinessObjectDataEntity.class);
    Root<StoragePolicyEntity> storagePolicyEntity = criteria.from(StoragePolicyEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectDataEntity, StorageUnitEntity> storageUnitEntity = businessObjectDataEntity
            .join(BusinessObjectDataEntity_.storageUnits);
    Join<BusinessObjectDataEntity, BusinessObjectDataStatusEntity> businessObjectDataStatusEntity = businessObjectDataEntity
            .join(BusinessObjectDataEntity_.status);
    Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntity = businessObjectDataEntity
            .join(BusinessObjectDataEntity_.businessObjectFormat);
    Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntity = businessObjectFormatEntity
            .join(BusinessObjectFormatEntity_.fileType);
    Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntity = businessObjectFormatEntity
            .join(BusinessObjectFormatEntity_.businessObjectDefinition);

    // Create main query restrictions based on the specified parameters.
    List<Predicate> mainQueryPredicates = new ArrayList<>();

    // Add a restriction on business object definition.
    mainQueryPredicates.add(storagePolicyPriorityLevel.isBusinessObjectDefinitionIsNull()
            ? builder.isNull(storagePolicyEntity.get(StoragePolicyEntity_.businessObjectDefinition))
            : builder.equal(businessObjectDefinitionEntity,
                    storagePolicyEntity.get(StoragePolicyEntity_.businessObjectDefinition)));

    // Add a restriction on business object format usage.
    mainQueryPredicates.add(storagePolicyPriorityLevel.isUsageIsNull()
            ? builder.isNull(storagePolicyEntity.get(StoragePolicyEntity_.usage))
            : builder.equal(builder.upper(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.usage)),
                    builder.upper(storagePolicyEntity.get(StoragePolicyEntity_.usage))));

    // Add a restriction on business object format file type.
    mainQueryPredicates.add(storagePolicyPriorityLevel.isFileTypeIsNull()
            ? builder.isNull(storagePolicyEntity.get(StoragePolicyEntity_.fileType))
            : builder.equal(fileTypeEntity, storagePolicyEntity.get(StoragePolicyEntity_.fileType)));

    // Add a restriction on storage policy filter storage.
    mainQueryPredicates.add(builder.equal(storageUnitEntity.get(StorageUnitEntity_.storage),
            storagePolicyEntity.get(StoragePolicyEntity_.storage)));

    // Add a restriction on supported business object data statuses.
    mainQueryPredicates.add(businessObjectDataStatusEntity.get(BusinessObjectDataStatusEntity_.code)
            .in(supportedBusinessObjectDataStatuses));

    // Build a subquery to eliminate business object data instances that already have storage unit in the storage policy destination storage.
    Subquery<BusinessObjectDataEntity> subquery = criteria.subquery(BusinessObjectDataEntity.class);
    Root<BusinessObjectDataEntity> subBusinessObjectDataEntity = subquery.from(BusinessObjectDataEntity.class);
    subquery.select(subBusinessObjectDataEntity);

    // Join to the other tables we can filter on for the subquery.
    Join<BusinessObjectDataEntity, StorageUnitEntity> subStorageUnitEntity = subBusinessObjectDataEntity
            .join(BusinessObjectDataEntity_.storageUnits);

    // Create the subquery restrictions based on the main query and storage policy destination storage.
    List<Predicate> subQueryPredicates = new ArrayList<>();
    subQueryPredicates.add(builder.equal(subBusinessObjectDataEntity, businessObjectDataEntity));
    subQueryPredicates.add(builder.equal(subStorageUnitEntity.get(StorageUnitEntity_.storage),
            storagePolicyEntity.get(StoragePolicyEntity_.destinationStorage)));

    // Add all clauses to the subquery.
    subquery.select(subBusinessObjectDataEntity).where(subQueryPredicates.toArray(new Predicate[] {}));

    // Add a main query restriction based on the subquery to eliminate business object data
    // instances that already have storage unit in the storage policy destination storage.
    mainQueryPredicates.add(builder.not(builder.exists(subquery)));

    // Order the results by business object data "created on" value.
    Order orderByCreatedOn = builder.asc(businessObjectDataEntity.get(BusinessObjectDataEntity_.createdOn));

    // Add the select clause to the main query.
    criteria.multiselect(businessObjectDataEntity, storagePolicyEntity);

    // Add the where clause to the main query.
    criteria.where(mainQueryPredicates.toArray(new Predicate[] {}));

    // Add the order by clause to the main query.
    criteria.orderBy(orderByCreatedOn);

    // Run the query to get a list of tuples back.
    List<Tuple> tuples = entityManager.createQuery(criteria).setFirstResult(startPosition)
            .setMaxResults(maxResult).getResultList();

    // Populate the result map from the returned tuples (i.e. 1 tuple for each row).
    Map<BusinessObjectDataEntity, StoragePolicyEntity> result = new LinkedHashMap<>();
    for (Tuple tuple : tuples) {
        // Since multiple storage policies can contain identical filters, we add the below check to select each business object data instance only once.
        if (!result.containsKey(tuple.get(businessObjectDataEntity))) {
            result.put(tuple.get(businessObjectDataEntity), tuple.get(storagePolicyEntity));
        }
    }

    return result;
}