use of org.finra.herd.model.jpa.StoragePolicyStatusEntity in project herd by FINRAOS.
the class StoragePolicyStatusDaoImpl method getStoragePolicyStatusByCode.
@Override
public StoragePolicyStatusEntity getStoragePolicyStatusByCode(String code) {
// Create the criteria builder and the criteria.
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<StoragePolicyStatusEntity> criteria = builder.createQuery(StoragePolicyStatusEntity.class);
// The criteria root is the storage policy status.
Root<StoragePolicyStatusEntity> storagePolicyStatusEntity = criteria.from(StoragePolicyStatusEntity.class);
// Create the standard restrictions (i.e. the standard where clauses).
Predicate storagePolicyStatusCodeRestriction = builder.equal(builder.upper(storagePolicyStatusEntity.get(StoragePolicyStatusEntity_.code)), code.toUpperCase());
criteria.select(storagePolicyStatusEntity).where(storagePolicyStatusCodeRestriction);
return executeSingleResultQuery(criteria, String.format("Found more than one storage policy status with code \"%s\".", code));
}
use of org.finra.herd.model.jpa.StoragePolicyStatusEntity in project herd by FINRAOS.
the class StoragePolicyDaoTestHelper method createStoragePolicyEntity.
/**
* Creates and persists a storage policy entity.
*
* @param storagePolicyKey the storage policy key
* @param storagePolicyRuleType the storage policy rule type
* @param storagePolicyRuleValue the storage policy rule value
* @param businessObjectDefinitionNamespace the business object definition namespace
* @param businessObjectDefinitionName the business object definition name
* @param businessObjectFormatUsage the business object usage
* @param businessObjectFormatFileType the business object format file type
* @param storageName the storage name
* @param storagePolicyTransitionType the transition type of the storage policy
* @param storagePolicyStatus the storage policy status
* @param storagePolicyVersion the storage policy version
* @param storagePolicyLatestVersion specifies if this storage policy is flagged as latest version or not
*
* @return the newly created storage policy entity
*/
public StoragePolicyEntity createStoragePolicyEntity(StoragePolicyKey storagePolicyKey, String storagePolicyRuleType, Integer storagePolicyRuleValue, String businessObjectDefinitionNamespace, String businessObjectDefinitionName, String businessObjectFormatUsage, String businessObjectFormatFileType, String storageName, String storagePolicyTransitionType, String storagePolicyStatus, Integer storagePolicyVersion, Boolean storagePolicyLatestVersion) {
// Create a storage policy namespace entity if needed.
NamespaceEntity storagePolicyNamespaceEntity = namespaceDao.getNamespaceByCd(storagePolicyKey.getNamespace());
if (storagePolicyNamespaceEntity == null) {
storagePolicyNamespaceEntity = namespaceDaoTestHelper.createNamespaceEntity(storagePolicyKey.getNamespace());
}
// Create a storage policy rule type type entity if needed.
StoragePolicyRuleTypeEntity storagePolicyRuleTypeEntity = storagePolicyRuleTypeDao.getStoragePolicyRuleTypeByCode(storagePolicyRuleType);
if (storagePolicyRuleTypeEntity == null) {
storagePolicyRuleTypeEntity = storagePolicyRuleTypeDaoTestHelper.createStoragePolicyRuleTypeEntity(storagePolicyRuleType, AbstractDaoTest.DESCRIPTION);
}
// Create a business object definition entity if needed.
BusinessObjectDefinitionEntity businessObjectDefinitionEntity = null;
if (StringUtils.isNotBlank(businessObjectDefinitionName)) {
businessObjectDefinitionEntity = businessObjectDefinitionDao.getBusinessObjectDefinitionByKey(new BusinessObjectDefinitionKey(businessObjectDefinitionNamespace, businessObjectDefinitionName));
if (businessObjectDefinitionEntity == null) {
// Create a business object definition.
businessObjectDefinitionEntity = businessObjectDefinitionDaoTestHelper.createBusinessObjectDefinitionEntity(businessObjectDefinitionNamespace, businessObjectDefinitionName, AbstractDaoTest.DATA_PROVIDER_NAME, AbstractDaoTest.BDEF_DESCRIPTION);
}
}
// Create a business object format file type entity if needed.
FileTypeEntity fileTypeEntity = null;
if (StringUtils.isNotBlank(businessObjectFormatFileType)) {
fileTypeEntity = fileTypeDao.getFileTypeByCode(businessObjectFormatFileType);
if (fileTypeEntity == null) {
fileTypeEntity = fileTypeDaoTestHelper.createFileTypeEntity(businessObjectFormatFileType);
}
}
// Create a storage entity of S3 storage platform type if needed.
StorageEntity storageEntity = storageDao.getStorageByName(storageName);
if (storageEntity == null) {
storageEntity = storageDaoTestHelper.createStorageEntity(storageName, StoragePlatformEntity.S3);
}
// Create a storage policy transition type entity, if not exists.
StoragePolicyTransitionTypeEntity storagePolicyTransitionTypeEntity = storagePolicyTransitionTypeDao.getStoragePolicyTransitionTypeByCode(storagePolicyTransitionType);
if (storagePolicyTransitionTypeEntity == null) {
storagePolicyTransitionTypeEntity = createStoragePolicyTransitionTypeEntity(storagePolicyTransitionType);
}
// Create a storage policy status entity, if not exists.
StoragePolicyStatusEntity storagePolicyStatusEntity = storagePolicyStatusDao.getStoragePolicyStatusByCode(storagePolicyStatus);
if (storagePolicyStatusEntity == null) {
storagePolicyStatusEntity = createStoragePolicyStatusEntity(storagePolicyStatus);
}
// Create a storage policy entity.
StoragePolicyEntity storagePolicyEntity = new StoragePolicyEntity();
storagePolicyEntity.setNamespace(storagePolicyNamespaceEntity);
storagePolicyEntity.setName(storagePolicyKey.getStoragePolicyName());
storagePolicyEntity.setStoragePolicyRuleType(storagePolicyRuleTypeEntity);
storagePolicyEntity.setStoragePolicyRuleValue(storagePolicyRuleValue);
storagePolicyEntity.setBusinessObjectDefinition(businessObjectDefinitionEntity);
storagePolicyEntity.setUsage(businessObjectFormatUsage);
storagePolicyEntity.setFileType(fileTypeEntity);
storagePolicyEntity.setStorage(storageEntity);
storagePolicyEntity.setStoragePolicyTransitionType(storagePolicyTransitionTypeEntity);
storagePolicyEntity.setStatus(storagePolicyStatusEntity);
storagePolicyEntity.setVersion(storagePolicyVersion);
storagePolicyEntity.setLatestVersion(storagePolicyLatestVersion);
return storagePolicyDao.saveAndRefresh(storagePolicyEntity);
}
use of org.finra.herd.model.jpa.StoragePolicyStatusEntity in project herd by FINRAOS.
the class StoragePolicyDaoTestHelper method createStoragePolicyStatusEntity.
/**
* Creates and persists a new storage policy status entity.
*
* @param statusCode the code of the storage policy status
* @param description the description of the status code
*
* @return the newly created storage policy status entity
*/
public StoragePolicyStatusEntity createStoragePolicyStatusEntity(String statusCode, String description) {
StoragePolicyStatusEntity storagePolicyStatusEntity = new StoragePolicyStatusEntity();
storagePolicyStatusEntity.setCode(statusCode);
storagePolicyStatusEntity.setDescription(description);
return storagePolicyStatusDao.saveAndRefresh(storagePolicyStatusEntity);
}
use of org.finra.herd.model.jpa.StoragePolicyStatusEntity 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;
}
use of org.finra.herd.model.jpa.StoragePolicyStatusEntity in project herd by FINRAOS.
the class StoragePolicyServiceImpl method updateStoragePolicy.
@NamespacePermissions({ @NamespacePermission(fields = "#storagePolicyKey?.namespace", permissions = NamespacePermissionEnum.WRITE), @NamespacePermission(fields = "#request?.storagePolicyFilter?.namespace", permissions = NamespacePermissionEnum.WRITE) })
@Override
public StoragePolicy updateStoragePolicy(StoragePolicyKey storagePolicyKey, StoragePolicyUpdateRequest request) {
// Validate and trim the key.
storagePolicyHelper.validateStoragePolicyKey(storagePolicyKey);
// Retrieve and ensure that a storage policy exists with the specified key.
StoragePolicyEntity storagePolicyEntity = storagePolicyDaoHelper.getStoragePolicyEntityByKey(storagePolicyKey);
// Validate and trim the request parameters.
validateStoragePolicyUpdateRequest(request);
// Retrieve and ensure that storage policy type exists.
StoragePolicyRuleTypeEntity storagePolicyRuleTypeEntity = storagePolicyRuleTypeDaoHelper.getStoragePolicyRuleTypeEntity(request.getStoragePolicyRule().getRuleType());
// Get the storage policy filter.
StoragePolicyFilter storagePolicyFilter = request.getStoragePolicyFilter();
// If specified, retrieve and ensure that the business object definition exists.
BusinessObjectDefinitionEntity businessObjectDefinitionEntity = null;
if (StringUtils.isNotBlank(storagePolicyFilter.getBusinessObjectDefinitionName())) {
businessObjectDefinitionEntity = businessObjectDefinitionDaoHelper.getBusinessObjectDefinitionEntity(new BusinessObjectDefinitionKey(storagePolicyFilter.getNamespace(), storagePolicyFilter.getBusinessObjectDefinitionName()));
}
// If specified, retrieve and ensure that file type exists.
FileTypeEntity fileTypeEntity = null;
if (StringUtils.isNotBlank(storagePolicyFilter.getBusinessObjectFormatFileType())) {
fileTypeEntity = fileTypeDaoHelper.getFileTypeEntity(storagePolicyFilter.getBusinessObjectFormatFileType());
}
// Retrieve and ensure that storage policy filter storage exists.
StorageEntity storageEntity = storageDaoHelper.getStorageEntity(storagePolicyFilter.getStorageName());
// Validate the source storage.
storagePolicyDaoHelper.validateStoragePolicyFilterStorage(storageEntity);
// Retrieve and ensure that storage policy transition type exists.
StoragePolicyTransitionTypeEntity storagePolicyTransitionTypeEntity = storagePolicyTransitionTypeDaoHelper.getStoragePolicyTransitionTypeEntity(request.getStoragePolicyTransition().getTransitionType());
// Retrieve and ensure that specified storage policy status exists.
StoragePolicyStatusEntity storagePolicyStatusEntity = storagePolicyStatusDaoHelper.getStoragePolicyStatusEntity(request.getStatus());
// Create and persist a new storage policy entity from the request information.
// Please note that simply adding 1 to the latest version without "DB locking" is sufficient here,
// even for multi-threading, since we are relying on the DB having version as part of the alternate key.
StoragePolicyEntity newVersionStoragePolicyEntity = createStoragePolicyEntity(storagePolicyEntity.getNamespace(), storagePolicyEntity.getName(), storageEntity, storagePolicyRuleTypeEntity, request.getStoragePolicyRule().getRuleValue(), businessObjectDefinitionEntity, request.getStoragePolicyFilter().getBusinessObjectFormatUsage(), fileTypeEntity, storagePolicyTransitionTypeEntity, storagePolicyStatusEntity, storagePolicyEntity.getVersion() + 1, true);
// Update the existing latest version storage policy entity, so it would not be flagged as the latest version anymore.
storagePolicyEntity.setLatestVersion(false);
storagePolicyDao.saveAndRefresh(storagePolicyEntity);
// Create and return the storage policy object from the new version entity.
return createStoragePolicyFromEntity(newVersionStoragePolicyEntity);
}
Aggregations