Search in sources :

Example 31 with FileTypeEntity

use of org.finra.herd.model.jpa.FileTypeEntity in project herd by FINRAOS.

the class BusinessObjectDataDaoImpl method getBusinessObjectDataPartitionValue.

/**
 * Retrieves partition value per specified parameters that includes the aggregate function.
 *
 * <p>
 * Returns null if the business object format key does not exist.
 *
 * @param partitionColumnPosition the partition column position (1-based numbering)
 * @param businessObjectFormatKey the business object format key (case-insensitive). If a business object format version isn't specified, the latest
 * available format version for each partition value will be used.
 * @param businessObjectDataVersion the business object data version. If a business object data version isn't specified, the latest data version based on
 * the specified business object data status will be used for each partition value.
 * @param businessObjectDataStatus the business object data status. This parameter is ignored when the business object data version is specified.
 * @param storageNames the optional list of storage names (case-insensitive)
 * @param storagePlatformType the optional storage platform type, e.g. S3 for Hive DDL. It is ignored when the list of storages is not empty
 * @param excludedStoragePlatformType the optional storage platform type to be excluded from search. It is ignored when the list of storages is not empty or
 * the storage platform type is specified
 * @param aggregateFunction the aggregate function to use against partition values
 * @param upperBoundPartitionValue the optional inclusive upper bound for the maximum available partition value
 * @param lowerBoundPartitionValue the optional inclusive lower bound for the maximum available partition value
 *
 * @return the partition value
 */
private String getBusinessObjectDataPartitionValue(int partitionColumnPosition, final BusinessObjectFormatKey businessObjectFormatKey, final Integer businessObjectDataVersion, String businessObjectDataStatus, List<String> storageNames, String storagePlatformType, String excludedStoragePlatformType, final AggregateFunction aggregateFunction, String upperBoundPartitionValue, String lowerBoundPartitionValue) {
    // We cannot use businessObjectFormatKey passed in since it is case-insensitive. Case-insensitive values requires upper() function in the SQL query, and
    // it has caused performance problems. So we need to extract case-sensitive business object format key from database so we can eliminate the upper()
    // function.
    BusinessObjectFormatEntity businessObjectFormatEntity = businessObjectFormatDao.getBusinessObjectFormatByAltKey(businessObjectFormatKey);
    BusinessObjectFormatKey businessObjectFormatKeyCaseSensitive = (BusinessObjectFormatKey) businessObjectFormatKey.clone();
    if (businessObjectFormatEntity == null) {
        // Returns null if business object format key does not exist.
        return null;
    } else {
        // Sets the exact values for business object format key from the database. Note that usage type is still case-insensitive.
        businessObjectFormatKeyCaseSensitive.setNamespace(businessObjectFormatEntity.getBusinessObjectDefinition().getNamespace().getCode());
        businessObjectFormatKeyCaseSensitive.setBusinessObjectDefinitionName(businessObjectFormatEntity.getBusinessObjectDefinition().getName());
        businessObjectFormatKeyCaseSensitive.setBusinessObjectFormatFileType(businessObjectFormatEntity.getFileType().getCode());
    }
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<String> criteria = builder.createQuery(String.class);
    // The criteria root is the business object data.
    Root<BusinessObjectDataEntity> businessObjectDataEntityRoot = criteria.from(BusinessObjectDataEntity.class);
    // Join to the other tables we can filter on.
    Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntityJoin = businessObjectDataEntityRoot.join(BusinessObjectDataEntity_.businessObjectFormat);
    Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntityJoin = businessObjectFormatEntityJoin.join(BusinessObjectFormatEntity_.fileType);
    Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntityJoin = businessObjectFormatEntityJoin.join(BusinessObjectFormatEntity_.businessObjectDefinition);
    Join<BusinessObjectDefinitionEntity, NamespaceEntity> namespaceEntityJoin = businessObjectDefinitionEntityJoin.join(BusinessObjectDefinitionEntity_.namespace);
    Join<BusinessObjectDataEntity, StorageUnitEntity> storageUnitEntityJoin = businessObjectDataEntityRoot.join(BusinessObjectDataEntity_.storageUnits);
    Join<StorageUnitEntity, StorageUnitStatusEntity> storageUnitStatusEntityJoin = storageUnitEntityJoin.join(StorageUnitEntity_.status);
    Join<StorageUnitEntity, StorageEntity> storageEntityJoin = storageUnitEntityJoin.join(StorageUnitEntity_.storage);
    Join<StorageEntity, StoragePlatformEntity> storagePlatformEntityJoin = storageEntityJoin.join(StorageEntity_.storagePlatform);
    // Create the path.
    Expression<String> partitionValue;
    SingularAttribute<BusinessObjectDataEntity, String> singleValuedAttribute = BUSINESS_OBJECT_DATA_PARTITIONS.get(partitionColumnPosition - 1);
    switch(aggregateFunction) {
        case GREATEST:
            partitionValue = builder.greatest(businessObjectDataEntityRoot.get(singleValuedAttribute));
            break;
        case LEAST:
            partitionValue = builder.least(businessObjectDataEntityRoot.get(singleValuedAttribute));
            break;
        default:
            throw new IllegalArgumentException("Invalid aggregate function found: \"" + aggregateFunction + "\".");
    }
    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate mainQueryRestriction = builder.equal(namespaceEntityJoin.get(NamespaceEntity_.code), businessObjectFormatKeyCaseSensitive.getNamespace());
    mainQueryRestriction = builder.and(mainQueryRestriction, builder.equal(businessObjectDefinitionEntityJoin.get(BusinessObjectDefinitionEntity_.name), businessObjectFormatKeyCaseSensitive.getBusinessObjectDefinitionName()));
    mainQueryRestriction = builder.and(mainQueryRestriction, builder.equal(builder.upper(businessObjectFormatEntityJoin.get(BusinessObjectFormatEntity_.usage)), businessObjectFormatKeyCaseSensitive.getBusinessObjectFormatUsage().toUpperCase()));
    mainQueryRestriction = builder.and(mainQueryRestriction, builder.equal(fileTypeEntityJoin.get(FileTypeEntity_.code), businessObjectFormatKeyCaseSensitive.getBusinessObjectFormatFileType()));
    // If a business object format version was specified, use it.
    if (businessObjectFormatKey.getBusinessObjectFormatVersion() != null) {
        mainQueryRestriction = builder.and(mainQueryRestriction, builder.equal(businessObjectFormatEntityJoin.get(BusinessObjectFormatEntity_.businessObjectFormatVersion), businessObjectFormatKey.getBusinessObjectFormatVersion()));
    }
    // If a data version was specified, use it.
    if (businessObjectDataVersion != null) {
        mainQueryRestriction = builder.and(mainQueryRestriction, builder.equal(businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.version), businessObjectDataVersion));
    } else // Business object data version is not specified, so get the latest one as per specified business object data status in the specified storage.
    {
        Subquery<Integer> subQuery = getMaximumBusinessObjectDataVersionSubQuery(builder, criteria, businessObjectDataEntityRoot, businessObjectFormatEntityJoin, businessObjectDataStatus, storageNames, storagePlatformType, excludedStoragePlatformType, false);
        mainQueryRestriction = builder.and(mainQueryRestriction, builder.in(businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.version)).value(subQuery));
    }
    // Add an inclusive upper bound partition value restriction if specified.
    if (upperBoundPartitionValue != null) {
        mainQueryRestriction = builder.and(mainQueryRestriction, builder.lessThanOrEqualTo(businessObjectDataEntityRoot.get(singleValuedAttribute), upperBoundPartitionValue));
    }
    // Add an inclusive lower bound partition value restriction if specified.
    if (lowerBoundPartitionValue != null) {
        mainQueryRestriction = builder.and(mainQueryRestriction, builder.greaterThanOrEqualTo(businessObjectDataEntityRoot.get(singleValuedAttribute), lowerBoundPartitionValue));
    }
    // If specified, add restriction on storage.
    mainQueryRestriction = builder.and(mainQueryRestriction, getQueryRestrictionOnStorage(builder, storageEntityJoin, storagePlatformEntityJoin, storageNames, storagePlatformType, excludedStoragePlatformType));
    // Search across only "available" storage units.
    mainQueryRestriction = builder.and(mainQueryRestriction, builder.isTrue(storageUnitStatusEntityJoin.get(StorageUnitStatusEntity_.available)));
    criteria.select(partitionValue).where(mainQueryRestriction);
    return entityManager.createQuery(criteria).getSingleResult();
}
Also used : NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) BusinessObjectFormatEntity(org.finra.herd.model.jpa.BusinessObjectFormatEntity) Predicate(javax.persistence.criteria.Predicate) StoragePlatformEntity(org.finra.herd.model.jpa.StoragePlatformEntity) BusinessObjectFormatKey(org.finra.herd.model.api.xml.BusinessObjectFormatKey) StorageUnitStatusEntity(org.finra.herd.model.jpa.StorageUnitStatusEntity) BusinessObjectDataEntity(org.finra.herd.model.jpa.BusinessObjectDataEntity) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) FileTypeEntity(org.finra.herd.model.jpa.FileTypeEntity) StorageUnitEntity(org.finra.herd.model.jpa.StorageUnitEntity) StorageEntity(org.finra.herd.model.jpa.StorageEntity) BusinessObjectDefinitionEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionEntity)

Example 32 with FileTypeEntity

use of org.finra.herd.model.jpa.FileTypeEntity in project herd by FINRAOS.

the class BusinessObjectDataDaoImpl method getBusinessObjectDataCount.

@Override
public Long getBusinessObjectDataCount(BusinessObjectFormatKey businessObjectFormatKey) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = builder.createQuery(Long.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 path.
    Expression<Long> businessObjectDataCount = builder.count(businessObjectDataEntity.get(BusinessObjectDataEntity_.id));
    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate queryRestriction = builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)), businessObjectFormatKey.getNamespace().toUpperCase());
    queryRestriction = builder.and(queryRestriction, builder.equal(builder.upper(businessObjectDefinitionEntity.get(BusinessObjectDefinitionEntity_.name)), businessObjectFormatKey.getBusinessObjectDefinitionName().toUpperCase()));
    queryRestriction = builder.and(queryRestriction, builder.equal(builder.upper(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.usage)), businessObjectFormatKey.getBusinessObjectFormatUsage().toUpperCase()));
    queryRestriction = builder.and(queryRestriction, builder.equal(builder.upper(fileTypeEntity.get(FileTypeEntity_.code)), businessObjectFormatKey.getBusinessObjectFormatFileType().toUpperCase()));
    queryRestriction = builder.and(queryRestriction, builder.equal(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion), businessObjectFormatKey.getBusinessObjectFormatVersion()));
    criteria.select(businessObjectDataCount).where(queryRestriction);
    return entityManager.createQuery(criteria).getSingleResult();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) FileTypeEntity(org.finra.herd.model.jpa.FileTypeEntity) BusinessObjectFormatEntity(org.finra.herd.model.jpa.BusinessObjectFormatEntity) Predicate(javax.persistence.criteria.Predicate) BusinessObjectDefinitionEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionEntity) BusinessObjectDataEntity(org.finra.herd.model.jpa.BusinessObjectDataEntity)

Example 33 with FileTypeEntity

use of org.finra.herd.model.jpa.FileTypeEntity in project herd by FINRAOS.

the class BusinessObjectDataDaoImpl method getBusinessObjectDataMaxVersion.

@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(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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) FileTypeEntity(org.finra.herd.model.jpa.FileTypeEntity) BusinessObjectFormatEntity(org.finra.herd.model.jpa.BusinessObjectFormatEntity) Predicate(javax.persistence.criteria.Predicate) BusinessObjectDefinitionEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionEntity) BusinessObjectDataEntity(org.finra.herd.model.jpa.BusinessObjectDataEntity)

Example 34 with FileTypeEntity

use of org.finra.herd.model.jpa.FileTypeEntity in project herd by FINRAOS.

the class BusinessObjectDataDaoImpl method getBusinessObjectDataByBusinessObjectDefinition.

@Override
public List<BusinessObjectDataKey> getBusinessObjectDataByBusinessObjectDefinition(BusinessObjectDefinitionEntity businessObjectDefinitionEntity, Integer maxResults) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BusinessObjectDataEntity> criteria = builder.createQuery(BusinessObjectDataEntity.class);
    // The criteria root is the business object data.
    Root<BusinessObjectDataEntity> businessObjectDataEntityRoot = criteria.from(BusinessObjectDataEntity.class);
    // Join to other tables that we can order by on.
    Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntityJoin = businessObjectDataEntityRoot.join(BusinessObjectDataEntity_.businessObjectFormat);
    Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntityJoin = businessObjectFormatEntityJoin.join(BusinessObjectFormatEntity_.fileType);
    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate predicate = builder.equal(businessObjectFormatEntityJoin.get(BusinessObjectFormatEntity_.businessObjectDefinition), businessObjectDefinitionEntity);
    // Build the order by clause. The sort order is consistent with the search business object data implementation.
    List<Order> orderBy = new ArrayList<>();
    orderBy.add(builder.asc(businessObjectFormatEntityJoin.get(BusinessObjectFormatEntity_.usage)));
    orderBy.add(builder.asc(fileTypeEntityJoin.get(FileTypeEntity_.code)));
    orderBy.add(builder.desc(businessObjectFormatEntityJoin.get(BusinessObjectFormatEntity_.businessObjectFormatVersion)));
    for (SingularAttribute<BusinessObjectDataEntity, String> businessObjectDataPartition : BUSINESS_OBJECT_DATA_PARTITIONS) {
        orderBy.add(builder.desc(businessObjectDataEntityRoot.get(businessObjectDataPartition)));
    }
    orderBy.add(builder.desc(businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.version)));
    // Add the clauses for the query.
    criteria.select(businessObjectDataEntityRoot).where(predicate).orderBy(orderBy);
    // Create a query.
    TypedQuery<BusinessObjectDataEntity> query = entityManager.createQuery(criteria);
    // If specified, set the maximum number of results for query to return.
    if (maxResults != null) {
        query.setMaxResults(maxResults);
    }
    // Run the query to get a list of entities back.
    List<BusinessObjectDataEntity> businessObjectDataEntities = query.getResultList();
    // Populate the "keys" objects from the returned entities.
    List<BusinessObjectDataKey> businessObjectDataKeys = new ArrayList<>();
    for (BusinessObjectDataEntity businessObjectDataEntity : businessObjectDataEntities) {
        BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey();
        businessObjectDataKeys.add(businessObjectDataKey);
        businessObjectDataKey.setNamespace(businessObjectDataEntity.getBusinessObjectFormat().getBusinessObjectDefinition().getNamespace().getCode());
        businessObjectDataKey.setBusinessObjectDefinitionName(businessObjectDataEntity.getBusinessObjectFormat().getBusinessObjectDefinition().getName());
        businessObjectDataKey.setBusinessObjectFormatUsage(businessObjectDataEntity.getBusinessObjectFormat().getUsage());
        businessObjectDataKey.setBusinessObjectFormatFileType(businessObjectDataEntity.getBusinessObjectFormat().getFileType().getCode());
        businessObjectDataKey.setBusinessObjectFormatVersion(businessObjectDataEntity.getBusinessObjectFormat().getBusinessObjectFormatVersion());
        businessObjectDataKey.setPartitionValue(businessObjectDataEntity.getPartitionValue());
        businessObjectDataKey.setSubPartitionValues(getSubPartitionValues(businessObjectDataEntity));
        businessObjectDataKey.setBusinessObjectDataVersion(businessObjectDataEntity.getVersion());
    }
    return businessObjectDataKeys;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) FileTypeEntity(org.finra.herd.model.jpa.FileTypeEntity) ArrayList(java.util.ArrayList) BusinessObjectFormatEntity(org.finra.herd.model.jpa.BusinessObjectFormatEntity) BusinessObjectDataKey(org.finra.herd.model.api.xml.BusinessObjectDataKey) Predicate(javax.persistence.criteria.Predicate) BusinessObjectDataEntity(org.finra.herd.model.jpa.BusinessObjectDataEntity)

Example 35 with FileTypeEntity

use of org.finra.herd.model.jpa.FileTypeEntity in project herd by FINRAOS.

the class BusinessObjectDataDaoImpl method getBusinessObjectDataEntitiesMatchingStoragePolicies.

@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.
    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<StorageUnitEntity, StorageUnitStatusEntity> storageUnitStatusEntityJoin = storageUnitEntityJoin.join(StorageUnitEntity_.status);
    Join<BusinessObjectDataEntity, BusinessObjectDataStatusEntity> businessObjectDataStatusEntityJoin = businessObjectDataEntityRoot.join(BusinessObjectDataEntity_.status);
    Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntityJoin = businessObjectDataEntityRoot.join(BusinessObjectDataEntity_.businessObjectFormat);
    Join<BusinessObjectFormatEntity, FileTypeEntity> fileTypeEntityJoin = businessObjectFormatEntityJoin.join(BusinessObjectFormatEntity_.fileType);
    Join<BusinessObjectFormatEntity, BusinessObjectDefinitionEntity> businessObjectDefinitionEntityJoin = businessObjectFormatEntityJoin.join(BusinessObjectFormatEntity_.businessObjectDefinition);
    Join<StoragePolicyEntity, StoragePolicyTransitionTypeEntity> storagePolicyTransitionTypeEntityJoin = storagePolicyEntityRoot.join(StoragePolicyEntity_.storagePolicyTransitionType);
    Join<StoragePolicyEntity, StoragePolicyStatusEntity> storagePolicyStatusEntityJoin = storagePolicyEntityRoot.join(StoragePolicyEntity_.status);
    // 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(storagePolicyEntityRoot.get(StoragePolicyEntity_.businessObjectDefinition)) : builder.equal(businessObjectDefinitionEntityJoin, storagePolicyEntityRoot.get(StoragePolicyEntity_.businessObjectDefinition)));
    // Add a restriction on business object format usage.
    mainQueryPredicates.add(storagePolicyPriorityLevel.isUsageIsNull() ? builder.isNull(storagePolicyEntityRoot.get(StoragePolicyEntity_.usage)) : builder.equal(builder.upper(businessObjectFormatEntityJoin.get(BusinessObjectFormatEntity_.usage)), builder.upper(storagePolicyEntityRoot.get(StoragePolicyEntity_.usage))));
    // Add a restriction on business object format file type.
    mainQueryPredicates.add(storagePolicyPriorityLevel.isFileTypeIsNull() ? builder.isNull(storagePolicyEntityRoot.get(StoragePolicyEntity_.fileType)) : builder.equal(fileTypeEntityJoin, storagePolicyEntityRoot.get(StoragePolicyEntity_.fileType)));
    // Add a restriction on storage policy filter storage.
    mainQueryPredicates.add(builder.equal(storageUnitEntityJoin.get(StorageUnitEntity_.storage), storagePolicyEntityRoot.get(StoragePolicyEntity_.storage)));
    // Add a restriction on storage policy latest version flag.
    mainQueryPredicates.add(builder.isTrue(storagePolicyEntityRoot.get(StoragePolicyEntity_.latestVersion)));
    // Add a restriction on storage policy status.
    mainQueryPredicates.add(builder.equal(storagePolicyStatusEntityJoin.get(StoragePolicyStatusEntity_.code), StoragePolicyStatusEntity.ENABLED));
    // Add a restriction on supported business object data statuses.
    mainQueryPredicates.add(businessObjectDataStatusEntityJoin.get(BusinessObjectDataStatusEntity_.code).in(supportedBusinessObjectDataStatuses));
    // Add a restriction as per storage policy transition type.
    mainQueryPredicates.add(builder.and(builder.equal(storagePolicyTransitionTypeEntityJoin.get(StoragePolicyTransitionTypeEntity_.code), StoragePolicyTransitionTypeEntity.GLACIER), builder.or(builder.equal(storageUnitStatusEntityJoin.get(StorageUnitStatusEntity_.code), StorageUnitStatusEntity.ENABLED), builder.equal(storageUnitStatusEntityJoin.get(StorageUnitStatusEntity_.code), StorageUnitStatusEntity.ARCHIVING))));
    // If specified, add restriction on maximum allowed attempts for a storage policy transition.
    if (storagePolicyTransitionMaxAllowedAttempts > 0) {
        mainQueryPredicates.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(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(businessObjectDataEntityRoot))) {
            result.put(tuple.get(businessObjectDataEntityRoot), tuple.get(storagePolicyEntityRoot));
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) StoragePolicyTransitionTypeEntity(org.finra.herd.model.jpa.StoragePolicyTransitionTypeEntity) BusinessObjectFormatEntity(org.finra.herd.model.jpa.BusinessObjectFormatEntity) Predicate(javax.persistence.criteria.Predicate) LinkedHashMap(java.util.LinkedHashMap) StoragePolicyEntity(org.finra.herd.model.jpa.StoragePolicyEntity) StorageUnitStatusEntity(org.finra.herd.model.jpa.StorageUnitStatusEntity) StoragePolicyStatusEntity(org.finra.herd.model.jpa.StoragePolicyStatusEntity) BusinessObjectDataEntity(org.finra.herd.model.jpa.BusinessObjectDataEntity) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) StorageUnitEntity(org.finra.herd.model.jpa.StorageUnitEntity) FileTypeEntity(org.finra.herd.model.jpa.FileTypeEntity) BusinessObjectDataStatusEntity(org.finra.herd.model.jpa.BusinessObjectDataStatusEntity) BusinessObjectDefinitionEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionEntity) Tuple(javax.persistence.Tuple)

Aggregations

FileTypeEntity (org.finra.herd.model.jpa.FileTypeEntity)45 BusinessObjectDefinitionEntity (org.finra.herd.model.jpa.BusinessObjectDefinitionEntity)38 NamespaceEntity (org.finra.herd.model.jpa.NamespaceEntity)25 BusinessObjectFormatEntity (org.finra.herd.model.jpa.BusinessObjectFormatEntity)24 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)23 Predicate (javax.persistence.criteria.Predicate)23 ArrayList (java.util.ArrayList)20 StorageEntity (org.finra.herd.model.jpa.StorageEntity)17 BusinessObjectDataEntity (org.finra.herd.model.jpa.BusinessObjectDataEntity)14 BusinessObjectDefinitionKey (org.finra.herd.model.api.xml.BusinessObjectDefinitionKey)13 BusinessObjectDataStatusEntity (org.finra.herd.model.jpa.BusinessObjectDataStatusEntity)10 Order (javax.persistence.criteria.Order)8 NotificationEventTypeEntity (org.finra.herd.model.jpa.NotificationEventTypeEntity)8 NotificationRegistrationStatusEntity (org.finra.herd.model.jpa.NotificationRegistrationStatusEntity)8 StorageUnitEntity (org.finra.herd.model.jpa.StorageUnitEntity)8 StorageUnitStatusEntity (org.finra.herd.model.jpa.StorageUnitStatusEntity)8 Tuple (javax.persistence.Tuple)6 NamespacePermissions (org.finra.herd.model.annotation.NamespacePermissions)6 JobAction (org.finra.herd.model.api.xml.JobAction)6 BusinessObjectDataNotificationRegistrationEntity (org.finra.herd.model.jpa.BusinessObjectDataNotificationRegistrationEntity)5