Search in sources :

Example 11 with StoragePlatformEntity

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

the class StorageUnitDaoImpl method getS3StorageUnitsToRestore.

@Override
public List<StorageUnitEntity> getS3StorageUnitsToRestore(int maxResult) {
    // 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> storageUnitEntityRoot = criteria.from(StorageUnitEntity.class);
    // Join to the other tables we can filter on.
    Join<StorageUnitEntity, StorageEntity> storageEntityJoin = storageUnitEntityRoot.join(StorageUnitEntity_.storage);
    Join<StorageEntity, StoragePlatformEntity> storagePlatformEntityJoin = storageEntityJoin.join(StorageEntity_.storagePlatform);
    Join<StorageUnitEntity, StorageUnitStatusEntity> storageUnitStatusEntityJoin = storageUnitEntityRoot.join(StorageUnitEntity_.status);
    // Create the standard restrictions (i.e. the standard where clauses).
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(builder.equal(storagePlatformEntityJoin.get(StoragePlatformEntity_.name), StoragePlatformEntity.S3));
    predicates.add(builder.equal(storageUnitStatusEntityJoin.get(StorageUnitStatusEntity_.code), StorageUnitStatusEntity.RESTORING));
    // Order the results by storage unit updated on timestamp.
    Order orderBy = builder.asc(storageUnitEntityRoot.get(StorageUnitEntity_.updatedOn));
    // Add the clauses for the query.
    criteria.select(storageUnitEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).orderBy(orderBy);
    // Execute the query and return the results.
    return entityManager.createQuery(criteria).setMaxResults(maxResult).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) StorageUnitEntity(org.finra.herd.model.jpa.StorageUnitEntity) ArrayList(java.util.ArrayList) StorageEntity(org.finra.herd.model.jpa.StorageEntity) Predicate(javax.persistence.criteria.Predicate) StoragePlatformEntity(org.finra.herd.model.jpa.StoragePlatformEntity) StorageUnitStatusEntity(org.finra.herd.model.jpa.StorageUnitStatusEntity)

Example 12 with StoragePlatformEntity

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

the class BaseJpaDaoTest method testFindUniqueByNamedProperties.

@Test
public void testFindUniqueByNamedProperties() {
    // Retrieve the existing entity to get the createdBy value needed to use more than one property to query against.
    StoragePlatformEntity storagePlatformEntity = baseJpaDao.findById(StoragePlatformEntity.class, StoragePlatformEntity.S3);
    assertNotNull(storagePlatformEntity);
    Map<String, String> namedProperties = new HashMap<>();
    namedProperties.put(NAME_PROPERTY, StoragePlatformEntity.S3);
    namedProperties.put(CREATED_BY_PROPERTY, storagePlatformEntity.getCreatedBy());
    storagePlatformEntity = baseJpaDao.findUniqueByNamedProperties(StoragePlatformEntity.class, namedProperties);
    assertNotNull(storagePlatformEntity);
    assertEquals(StoragePlatformEntity.S3, storagePlatformEntity.getName());
}
Also used : HashMap(java.util.HashMap) StoragePlatformEntity(org.finra.herd.model.jpa.StoragePlatformEntity) Test(org.junit.Test)

Example 13 with StoragePlatformEntity

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

the class StorageUnitDaoImpl method getStorageUnitsByStoragePlatformAndBusinessObjectData.

@Override
public List<StorageUnitEntity> getStorageUnitsByStoragePlatformAndBusinessObjectData(String storagePlatform, BusinessObjectDataEntity businessObjectDataEntity) {
    // 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);
    // Join to the other tables we can filter on.
    Join<StorageUnitEntity, StorageEntity> storageEntity = storageUnitEntity.join(StorageUnitEntity_.storage);
    Join<StorageEntity, StoragePlatformEntity> storagePlatformEntity = storageEntity.join(StorageEntity_.storagePlatform);
    // Create the standard restrictions (i.e. the standard where clauses).
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(builder.equal(builder.upper(storagePlatformEntity.get(StoragePlatformEntity_.name)), storagePlatform.toUpperCase()));
    predicates.add(builder.equal(storageUnitEntity.get(StorageUnitEntity_.businessObjectData), businessObjectDataEntity));
    // Order by storage name.
    Order orderBy = builder.asc(storageEntity.get(StorageEntity_.name));
    // Add the clauses for the query.
    criteria.select(storageUnitEntity).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).orderBy(orderBy);
    // Execute the query and return the results.
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) StorageUnitEntity(org.finra.herd.model.jpa.StorageUnitEntity) StoragePlatformEntity(org.finra.herd.model.jpa.StoragePlatformEntity) ArrayList(java.util.ArrayList) StorageEntity(org.finra.herd.model.jpa.StorageEntity) Predicate(javax.persistence.criteria.Predicate)

Example 14 with StoragePlatformEntity

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

the class StorageUnitDaoImpl method getStorageUnitsByPartitionFiltersAndStorages.

/**
 * Retrieves a list of storage unit entities per specified parameters. This method processes a sublist of partition filters specified by
 * partitionFilterSubListFromIndex and partitionFilterSubListSize parameters.
 *
 * @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 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
 * @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 is returned
 * @param businessObjectDataStatus the business object data status. This parameter is ignored when the business object data version is specified. When
 * business object data version and business object data status both are not specified, the latest data version for each set of partition values will be
 * used regardless of the status
 * @param storageNames the list of storage names where the business object data storage units should be looked for (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 partitionFilterSubListFromIndex the index of the first element in the partition filter sublist
 * @param partitionFilterSubListSize the size of the partition filter sublist
 * @param selectOnlyAvailableStorageUnits specifies if only available storage units will be selected or any storage units regardless of their status
 *
 * @return the list of storage unit entities sorted by partition values
 */
private List<StorageUnitEntity> getStorageUnitsByPartitionFiltersAndStorages(BusinessObjectFormatKey businessObjectFormatKey, List<List<String>> partitionFilters, Integer businessObjectDataVersion, String businessObjectDataStatus, List<String> storageNames, String storagePlatformType, String excludedStoragePlatformType, boolean selectOnlyAvailableStorageUnits, int partitionFilterSubListFromIndex, int partitionFilterSubListSize) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
    // The criteria root is the storage unit.
    Root<StorageUnitEntity> storageUnitEntity = criteria.from(StorageUnitEntity.class);
    // Join to the other tables we can filter on.
    Join<StorageUnitEntity, BusinessObjectDataEntity> businessObjectDataEntity = storageUnitEntity.join(StorageUnitEntity_.businessObjectData);
    Join<StorageUnitEntity, StorageEntity> storageEntity = storageUnitEntity.join(StorageUnitEntity_.storage);
    Join<StorageEntity, StoragePlatformEntity> storagePlatformEntity = storageEntity.join(StorageEntity_.storagePlatform);
    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<StorageUnitEntity, StorageUnitStatusEntity> storageUnitStatusEntity = storageUnitEntity.join(StorageUnitEntity_.status);
    // Create the standard restrictions (i.e. the standard where clauses).
    // Create a standard restriction based on the business object format key values.
    // Please note that we specify not to ignore the business object format version.
    Predicate mainQueryRestriction = getQueryRestriction(builder, businessObjectFormatEntity, fileTypeEntity, businessObjectDefinitionEntity, businessObjectFormatKey, false);
    // If a format version was not specified, use the latest available for this set of partition values.
    if (businessObjectFormatKey.getBusinessObjectFormatVersion() == null) {
        // Get the latest available format version for this set of partition values and per other restrictions.
        Subquery<Integer> subQuery = getMaximumBusinessObjectFormatVersionSubQuery(builder, criteria, businessObjectDefinitionEntity, businessObjectFormatEntity, fileTypeEntity, businessObjectDataEntity, businessObjectDataVersion, businessObjectDataStatus, storageNames, storagePlatformType, excludedStoragePlatformType, selectOnlyAvailableStorageUnits);
        mainQueryRestriction = builder.and(mainQueryRestriction, builder.in(businessObjectFormatEntity.get(BusinessObjectFormatEntity_.businessObjectFormatVersion)).value(subQuery));
    }
    // Add restriction as per specified primary and/or sub-partition values.
    mainQueryRestriction = builder.and(mainQueryRestriction, getQueryRestrictionOnPartitionValues(builder, businessObjectDataEntity, partitionFilters.subList(partitionFilterSubListFromIndex, partitionFilterSubListFromIndex + partitionFilterSubListSize)));
    // If a data version was specified, use it. Otherwise, use the latest one as per specified business object data status.
    if (businessObjectDataVersion != null) {
        mainQueryRestriction = builder.and(mainQueryRestriction, builder.equal(businessObjectDataEntity.get(BusinessObjectDataEntity_.version), businessObjectDataVersion));
    } else {
        // Business object data version is not specified, so get the latest one as per specified business object data status, if any.
        // Meaning, when both business object data version and business object data status are not specified, we just return
        // the latest business object data version in the specified storage.
        Subquery<Integer> subQuery = getMaximumBusinessObjectDataVersionSubQuery(builder, criteria, businessObjectDataEntity, businessObjectFormatEntity, businessObjectDataStatus, storageNames, storagePlatformType, excludedStoragePlatformType, selectOnlyAvailableStorageUnits);
        mainQueryRestriction = builder.and(mainQueryRestriction, builder.in(businessObjectDataEntity.get(BusinessObjectDataEntity_.version)).value(subQuery));
    }
    // If specified, add restriction on storage.
    mainQueryRestriction = builder.and(mainQueryRestriction, getQueryRestrictionOnStorage(builder, storageEntity, storagePlatformEntity, storageNames, storagePlatformType, excludedStoragePlatformType));
    // If specified, add a restriction on storage unit status availability flag.
    if (selectOnlyAvailableStorageUnits) {
        mainQueryRestriction = builder.and(mainQueryRestriction, builder.isTrue(storageUnitStatusEntity.get(StorageUnitStatusEntity_.available)));
    }
    // Order by partitions and storage names.
    List<Order> orderBy = new ArrayList<>();
    for (SingularAttribute<BusinessObjectDataEntity, String> businessObjectDataPartition : BUSINESS_OBJECT_DATA_PARTITIONS) {
        orderBy.add(builder.asc(businessObjectDataEntity.get(businessObjectDataPartition)));
    }
    orderBy.add(builder.asc(storageEntity.get(StorageEntity_.name)));
    // Add the clauses for the query.
    // Please note that we use multiselect here in order to eliminate the Hibernate N+1 SELECT's problem,
    // happening when we select storage unit entities and access their relative business object data entities.
    // This is an alternative approach, since adding @Fetch(FetchMode.JOIN) failed to address the issue.
    criteria.multiselect(storageUnitEntity, storageUnitStatusEntity, storageEntity, storagePlatformEntity, businessObjectDataEntity, businessObjectFormatEntity).where(mainQueryRestriction).orderBy(orderBy);
    // Run the query to get a list of tuples back.
    List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();
    // Build a list of storage unit entities to return.
    List<StorageUnitEntity> storageUnitEntities = new ArrayList<>();
    for (Tuple tuple : tuples) {
        storageUnitEntities.add(tuple.get(storageUnitEntity));
    }
    return storageUnitEntities;
}
Also used : ArrayList(java.util.ArrayList) BusinessObjectFormatEntity(org.finra.herd.model.jpa.BusinessObjectFormatEntity) Predicate(javax.persistence.criteria.Predicate) StoragePlatformEntity(org.finra.herd.model.jpa.StoragePlatformEntity) StorageUnitStatusEntity(org.finra.herd.model.jpa.StorageUnitStatusEntity) 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) StorageEntity(org.finra.herd.model.jpa.StorageEntity) BusinessObjectDefinitionEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionEntity) Tuple(javax.persistence.Tuple)

Example 15 with StoragePlatformEntity

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

the class StoragePlatformDaoTest method testGetStoragePlatformByName.

@Test
public void testGetStoragePlatformByName() {
    // Create relative database entities.
    StoragePlatformEntity storagePlatformEntity = storagePlatformDaoTestHelper.createStoragePlatformEntity(STORAGE_PLATFORM_CODE);
    // Retrieve the storage platform entity by its name.
    assertEquals(storagePlatformEntity, storagePlatformDao.getStoragePlatformByName(STORAGE_PLATFORM_CODE));
    // Retrieve the storage platform entity using its name in upper and lower case.
    assertEquals(storagePlatformEntity, storagePlatformDao.getStoragePlatformByName(STORAGE_PLATFORM_CODE.toUpperCase()));
    assertEquals(storagePlatformEntity, storagePlatformDao.getStoragePlatformByName(STORAGE_PLATFORM_CODE.toLowerCase()));
    // Try to retrieve a storage platform entity using an invalid name.
    assertNull(storagePlatformDao.getStoragePlatformByName("I_DO_NOT_EXIST"));
}
Also used : StoragePlatformEntity(org.finra.herd.model.jpa.StoragePlatformEntity) Test(org.junit.Test)

Aggregations

StoragePlatformEntity (org.finra.herd.model.jpa.StoragePlatformEntity)20 StorageEntity (org.finra.herd.model.jpa.StorageEntity)13 StorageUnitEntity (org.finra.herd.model.jpa.StorageUnitEntity)12 Predicate (javax.persistence.criteria.Predicate)9 BusinessObjectDataEntity (org.finra.herd.model.jpa.BusinessObjectDataEntity)9 StorageUnitStatusEntity (org.finra.herd.model.jpa.StorageUnitStatusEntity)9 ArrayList (java.util.ArrayList)7 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)7 BusinessObjectFormatEntity (org.finra.herd.model.jpa.BusinessObjectFormatEntity)7 Test (org.junit.Test)7 Order (javax.persistence.criteria.Order)5 BusinessObjectDataStatusEntity (org.finra.herd.model.jpa.BusinessObjectDataStatusEntity)5 BusinessObjectDefinitionEntity (org.finra.herd.model.jpa.BusinessObjectDefinitionEntity)4 FileTypeEntity (org.finra.herd.model.jpa.FileTypeEntity)4 Timestamp (java.sql.Timestamp)3 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)3 StorageFileEntity (org.finra.herd.model.jpa.StorageFileEntity)3 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)3 HashMap (java.util.HashMap)2 BusinessObjectFormatKey (org.finra.herd.model.api.xml.BusinessObjectFormatKey)2