Search in sources :

Example 41 with AlreadyExistsException

use of org.finra.herd.model.AlreadyExistsException in project herd by FINRAOS.

the class BusinessObjectDataDaoHelper method createStorageUnitEntity.

/**
 * Creates a storage unit entity per specified parameters.
 *
 * @param businessObjectDataEntity the business object data entity
 * @param storageEntity the storage entity
 * @param storageDirectory the storage directory
 * @param storageFiles the list of storage files
 * @param isDiscoverStorageFiles specifies if
 *
 * @return the newly created storage unit entity
 */
public StorageUnitEntity createStorageUnitEntity(BusinessObjectDataEntity businessObjectDataEntity, StorageEntity storageEntity, StorageDirectory storageDirectory, List<StorageFile> storageFiles, Boolean isDiscoverStorageFiles) {
    // Get the storage unit status entity for the ENABLED status.
    StorageUnitStatusEntity storageUnitStatusEntity = storageUnitStatusDaoHelper.getStorageUnitStatusEntity(StorageUnitStatusEntity.ENABLED);
    // Set up flags which are used to make flow logic easier.
    boolean isS3StoragePlatform = storageEntity.getStoragePlatform().getName().equals(StoragePlatformEntity.S3);
    boolean isStorageDirectorySpecified = (storageDirectory != null);
    boolean validatePathPrefix = storageHelper.getBooleanStorageAttributeValueByName(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_VALIDATE_PATH_PREFIX), storageEntity, false, true);
    boolean validateFileExistence = storageHelper.getBooleanStorageAttributeValueByName(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_VALIDATE_FILE_EXISTENCE), storageEntity, false, true);
    boolean validateFileSize = storageHelper.getBooleanStorageAttributeValueByName(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_VALIDATE_FILE_SIZE), storageEntity, false, true);
    // Ensure that file size validation is not enabled without file existence validation.
    if (validateFileSize) {
        Assert.isTrue(validateFileExistence, String.format("Storage \"%s\" has file size validation enabled without file existence validation.", storageEntity.getName()));
    }
    String expectedS3KeyPrefix = null;
    // Retrieve S3 key prefix velocity template storage attribute value and store it in memory.
    // Please note that it is not required, so we pass in a "false" flag.
    String s3KeyPrefixVelocityTemplate = storageHelper.getStorageAttributeValueByName(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_KEY_PREFIX_VELOCITY_TEMPLATE), storageEntity, false);
    if (StringUtils.isNotBlank(s3KeyPrefixVelocityTemplate)) {
        // If the storage has any validation configured, get the expected S3 key prefix.
        expectedS3KeyPrefix = s3KeyPrefixHelper.buildS3KeyPrefix(s3KeyPrefixVelocityTemplate, businessObjectDataEntity.getBusinessObjectFormat(), businessObjectDataHelper.getBusinessObjectDataKey(businessObjectDataEntity), storageEntity.getName());
    }
    if ((validatePathPrefix || validateFileExistence) && isS3StoragePlatform) {
        // If path prefix validation is enabled, validate that S3 key prefix velocity template is configured.
        Assert.isTrue(!validatePathPrefix || StringUtils.isNotBlank(s3KeyPrefixVelocityTemplate), String.format("Storage \"%s\" has enabled path validation without S3 key prefix velocity template configured.", storageEntity.getName()));
    }
    // Process storage directory path if it is specified.
    String directoryPath = null;
    if (isStorageDirectorySpecified) {
        // Get the specified directory path.
        directoryPath = storageDirectory.getDirectoryPath();
        // If the validate path prefix flag is configured for this storage, validate the directory path value.
        if (validatePathPrefix && isS3StoragePlatform) {
            // Ensure the directory path adheres to the S3 naming convention.
            Assert.isTrue(directoryPath.equals(expectedS3KeyPrefix), String.format("Specified directory path \"%s\" does not match the expected S3 key prefix \"%s\".", directoryPath, expectedS3KeyPrefix));
            // Ensure that the directory path is not already registered with another business object data instance.
            StorageUnitEntity alreadyRegisteredStorageUnitEntity = storageUnitDao.getStorageUnitByStorageNameAndDirectoryPath(storageEntity.getName(), directoryPath);
            if (alreadyRegisteredStorageUnitEntity != null) {
                throw new AlreadyExistsException(String.format("Storage directory \"%s\" in \"%s\" storage is already registered by the business object data {%s}.", directoryPath, storageEntity.getName(), businessObjectDataHelper.businessObjectDataEntityAltKeyToString(alreadyRegisteredStorageUnitEntity.getBusinessObjectData())));
            }
        }
    } else if (Boolean.TRUE.equals(businessObjectDataEntity.getStatus().getPreRegistrationStatus())) {
        directoryPath = expectedS3KeyPrefix;
    }
    // Create a storage unit entity.
    StorageUnitEntity storageUnitEntity = new StorageUnitEntity();
    storageUnitEntity.setStorage(storageEntity);
    storageUnitEntity.setBusinessObjectData(businessObjectDataEntity);
    storageUnitDaoHelper.setStorageUnitStatus(storageUnitEntity, storageUnitStatusEntity);
    storageUnitEntity.setDirectoryPath(directoryPath);
    // Discover storage files if storage file discovery is enabled. Otherwise, get the storage files specified in the request, if any.
    List<StorageFile> resultStorageFiles = BooleanUtils.isTrue(isDiscoverStorageFiles) ? discoverStorageFiles(storageEntity, directoryPath) : storageFiles;
    // Create the storage file entities.
    createStorageFileEntitiesFromStorageFiles(resultStorageFiles, storageEntity, BooleanUtils.isTrue(isDiscoverStorageFiles), expectedS3KeyPrefix, storageUnitEntity, directoryPath, validatePathPrefix, validateFileExistence, validateFileSize, isS3StoragePlatform);
    return storageUnitEntity;
}
Also used : AlreadyExistsException(org.finra.herd.model.AlreadyExistsException) StorageUnitEntity(org.finra.herd.model.jpa.StorageUnitEntity) StorageUnitStatusEntity(org.finra.herd.model.jpa.StorageUnitStatusEntity) StorageFile(org.finra.herd.model.api.xml.StorageFile)

Example 42 with AlreadyExistsException

use of org.finra.herd.model.AlreadyExistsException in project herd by FINRAOS.

the class EmrClusterDefinitionServiceImpl method createEmrClusterDefinition.

@NamespacePermission(fields = "#request?.emrClusterDefinitionKey?.namespace", permissions = NamespacePermissionEnum.WRITE)
@Override
public EmrClusterDefinitionInformation createEmrClusterDefinition(EmrClusterDefinitionCreateRequest request) throws Exception {
    // Perform validate and trim of the EMR cluster definition key.
    emrClusterDefinitionHelper.validateEmrClusterDefinitionKey(request.getEmrClusterDefinitionKey());
    // Validate the EMR cluster definition configuration.
    emrClusterDefinitionHelper.validateEmrClusterDefinitionConfiguration(request.getEmrClusterDefinition());
    // Get the namespace and ensure it exists.
    NamespaceEntity namespaceEntity = namespaceDaoHelper.getNamespaceEntity(request.getEmrClusterDefinitionKey().getNamespace());
    namespaceIamRoleAuthorizationHelper.checkPermissions(namespaceEntity, request.getEmrClusterDefinition().getServiceIamRole(), request.getEmrClusterDefinition().getEc2NodeIamProfileName());
    // Ensure a EMR cluster definition with the specified name doesn't already exist.
    EmrClusterDefinitionEntity emrClusterDefinitionEntity = emrClusterDefinitionDao.getEmrClusterDefinitionByAltKey(request.getEmrClusterDefinitionKey());
    if (emrClusterDefinitionEntity != null) {
        throw new AlreadyExistsException(String.format("Unable to create EMR cluster definition with name \"%s\" for namespace \"%s\" because it already exists.", request.getEmrClusterDefinitionKey().getEmrClusterDefinitionName(), request.getEmrClusterDefinitionKey().getNamespace()));
    }
    // Create a EMR cluster definition entity from the request information.
    emrClusterDefinitionEntity = createEmrClusterDefinitionEntity(namespaceEntity, request);
    // Persist the new entity.
    emrClusterDefinitionEntity = emrClusterDefinitionDao.saveAndRefresh(emrClusterDefinitionEntity);
    // Create and return the EMR cluster definition object from the persisted entity.
    return createEmrClusterDefinitionFromEntity(emrClusterDefinitionEntity);
}
Also used : NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) AlreadyExistsException(org.finra.herd.model.AlreadyExistsException) EmrClusterDefinitionEntity(org.finra.herd.model.jpa.EmrClusterDefinitionEntity) NamespacePermission(org.finra.herd.model.annotation.NamespacePermission)

Example 43 with AlreadyExistsException

use of org.finra.herd.model.AlreadyExistsException in project herd by FINRAOS.

the class ExpectedPartitionValueServiceImpl method createExpectedPartitionValues.

/**
 * Creates a list of expected partition values for an existing partition key group.
 *
 * @param expectedPartitionValuesCreateRequest the information needed to create the expected partition values
 *
 * @return the newly created expected partition values
 */
@Override
public ExpectedPartitionValuesInformation createExpectedPartitionValues(ExpectedPartitionValuesCreateRequest expectedPartitionValuesCreateRequest) {
    // Perform request validation and trim request parameters.
    validateExpectedPartitionValuesCreateRequest(expectedPartitionValuesCreateRequest);
    // Retrieve and ensure that a partition key group exists with the specified name.
    PartitionKeyGroupEntity partitionKeyGroupEntity = partitionKeyGroupDaoHelper.getPartitionKeyGroupEntity(expectedPartitionValuesCreateRequest.getPartitionKeyGroupKey());
    // Load all existing expected partition value entities into a map for quick access.
    Map<String, ExpectedPartitionValueEntity> expectedPartitionValueEntityMap = getExpectedPartitionValueEntityMap(partitionKeyGroupEntity.getExpectedPartitionValues());
    // Fail if any of the expected partition values to be created already exist.
    for (String expectedPartitionValue : expectedPartitionValuesCreateRequest.getExpectedPartitionValues()) {
        if (expectedPartitionValueEntityMap.containsKey(expectedPartitionValue)) {
            throw new AlreadyExistsException(String.format("Expected partition value \"%s\" already exists in \"%s\" partition key group.", expectedPartitionValue, partitionKeyGroupEntity.getPartitionKeyGroupName()));
        }
    }
    // Create and persist the expected partition value entities.
    Collection<ExpectedPartitionValueEntity> createdExpectedPartitionValueEntities = new ArrayList<>();
    for (String expectedPartitionValue : expectedPartitionValuesCreateRequest.getExpectedPartitionValues()) {
        ExpectedPartitionValueEntity expectedPartitionValueEntity = new ExpectedPartitionValueEntity();
        createdExpectedPartitionValueEntities.add(expectedPartitionValueEntity);
        expectedPartitionValueEntity.setPartitionKeyGroup(partitionKeyGroupEntity);
        partitionKeyGroupEntity.getExpectedPartitionValues().add(expectedPartitionValueEntity);
        expectedPartitionValueEntity.setPartitionValue(expectedPartitionValue);
        expectedPartitionValueDao.saveAndRefresh(expectedPartitionValueEntity);
    }
    return createExpectedPartitionValuesInformationFromEntities(partitionKeyGroupEntity, createdExpectedPartitionValueEntities);
}
Also used : ExpectedPartitionValueEntity(org.finra.herd.model.jpa.ExpectedPartitionValueEntity) AlreadyExistsException(org.finra.herd.model.AlreadyExistsException) ArrayList(java.util.ArrayList) PartitionKeyGroupEntity(org.finra.herd.model.jpa.PartitionKeyGroupEntity)

Example 44 with AlreadyExistsException

use of org.finra.herd.model.AlreadyExistsException in project herd by FINRAOS.

the class AllowedAttributeValueServiceImpl method createAllowedAttributeValues.

/**
 * Creates a list of allowed attribute values for an existing attribute value list key.
 *
 * @param request the information needed to create the allowed attribute values
 *
 * @return the newly created allowed attribute values
 */
@NamespacePermission(fields = "#request.attributeValueListKey.namespace", permissions = NamespacePermissionEnum.WRITE)
@Override
public AllowedAttributeValuesInformation createAllowedAttributeValues(AllowedAttributeValuesCreateRequest request) {
    // Perform request validation and trim request parameters.
    validateAllowedAttributeValuesCreateRequest(request);
    // Retrieve and ensure that a attribute value list exists with the specified name.
    AttributeValueListEntity attributeValueListEntity = attributeValueListDaoHelper.getAttributeValueListEntity(request.getAttributeValueListKey());
    // Load all existing allowed attribute value entities into a map for quick access.
    Map<String, AllowedAttributeValueEntity> allowedAttributeValueEntityMap = getAllowedAttributeValueEntityMap(attributeValueListEntity.getAllowedAttributeValues());
    // Fail if any of the allowed attribute values to be created already exist.
    for (String allowedAttributeValue : request.getAllowedAttributeValues()) {
        if (allowedAttributeValueEntityMap.containsKey(allowedAttributeValue)) {
            throw new AlreadyExistsException(String.format("Allowed attribute value \"%s\" already exists in \"%s\" attribute value list.", allowedAttributeValue, attributeValueListEntity.getName()));
        }
    }
    // Create and persist the allowed attribute value entities.
    Collection<AllowedAttributeValueEntity> createdAllowedAttributeValueEntities = new ArrayList<>();
    for (String allowedAttributeValue : request.getAllowedAttributeValues()) {
        AllowedAttributeValueEntity allowedAttributeValueEntity = new AllowedAttributeValueEntity();
        createdAllowedAttributeValueEntities.add(allowedAttributeValueEntity);
        allowedAttributeValueEntity.setAttributeValueList(attributeValueListEntity);
        allowedAttributeValueEntity.setAllowedAttributeValue(allowedAttributeValue);
        allowedAttributeValueDao.saveAndRefresh(allowedAttributeValueEntity);
    }
    allowedAttributeValueDao.saveAndRefresh(attributeValueListEntity);
    return createAllowedAttributeValuesInformationFromEntities(attributeValueListEntity, createdAllowedAttributeValueEntities);
}
Also used : AlreadyExistsException(org.finra.herd.model.AlreadyExistsException) ArrayList(java.util.ArrayList) AttributeValueListEntity(org.finra.herd.model.jpa.AttributeValueListEntity) AllowedAttributeValueEntity(org.finra.herd.model.jpa.AllowedAttributeValueEntity) NamespacePermission(org.finra.herd.model.annotation.NamespacePermission)

Example 45 with AlreadyExistsException

use of org.finra.herd.model.AlreadyExistsException in project herd by FINRAOS.

the class BusinessObjectDefinitionColumnServiceImpl method createBusinessObjectDefinitionColumn.

@NamespacePermission(fields = "#request.businessObjectDefinitionColumnKey.namespace", permissions = { NamespacePermissionEnum.WRITE_DESCRIPTIVE_CONTENT, NamespacePermissionEnum.WRITE })
@Override
public BusinessObjectDefinitionColumn createBusinessObjectDefinitionColumn(BusinessObjectDefinitionColumnCreateRequest request) {
    // Validate and trim the business object definition column create request.
    validateBusinessObjectDefinitionColumnCreateRequest(request);
    // Get the business object definition key.
    BusinessObjectDefinitionKey businessObjectDefinitionKey = businessObjectDefinitionHelper.getBusinessObjectDefinitionKey(request.getBusinessObjectDefinitionColumnKey());
    // Get the business object definition and ensure it exists.
    BusinessObjectDefinitionEntity businessObjectDefinitionEntity = businessObjectDefinitionDaoHelper.getBusinessObjectDefinitionEntity(businessObjectDefinitionKey);
    // Ensure a business object definition column with the specified name doesn't already exist for the business object definition.
    if (businessObjectDefinitionColumnDao.getBusinessObjectDefinitionColumnByBusinessObjectDefinitionColumnName(businessObjectDefinitionEntity, request.getBusinessObjectDefinitionColumnKey().getBusinessObjectDefinitionColumnName()) != null) {
        throw new AlreadyExistsException(String.format("Unable to create business object definition column with name \"%s\" because it already exists for the business object definition {%s}.", request.getBusinessObjectDefinitionColumnKey().getBusinessObjectDefinitionColumnName(), businessObjectDefinitionHelper.businessObjectDefinitionKeyToString(businessObjectDefinitionKey)));
    }
    // Retrieve schema column entities from all format instances for the business object definition that match the specified schema column name.
    Collection<SchemaColumnEntity> schemaColumnEntities = schemaColumnDao.getSchemaColumns(businessObjectDefinitionEntity, request.getSchemaColumnName());
    // Ensure that exists at least one schema column that matches the specified schema column name.
    if (CollectionUtils.isEmpty(schemaColumnEntities)) {
        if (businessObjectDefinitionEntity.getDescriptiveBusinessObjectFormat() == null) {
            throw new ObjectNotFoundException(String.format("Unable to create business object definition column because there are no format schema " + "columns with name \"%s\" for the business object definition {%s}.", request.getSchemaColumnName(), businessObjectDefinitionHelper.businessObjectDefinitionKeyToString(businessObjectDefinitionKey)));
        } else {
            throw new ObjectNotFoundException(String.format("Unable to create business object definition column because there are no format schema " + "columns with name \"%s\" in the descriptive business object format for the business object definition {%s}.", request.getSchemaColumnName(), businessObjectDefinitionHelper.businessObjectDefinitionKeyToString(businessObjectDefinitionKey)));
        }
    }
    // Ensure a business object definition column with the specified schema column name doesn't already exist for this business object definition.
    for (SchemaColumnEntity schemaColumnEntity : schemaColumnEntities) {
        if (schemaColumnEntity.getBusinessObjectDefinitionColumn() != null) {
            throw new AlreadyExistsException(String.format("Unable to create business object definition column because a business object definition column " + "with schema column name \"%s\" already exists for the business object definition {%s}.", request.getSchemaColumnName(), businessObjectDefinitionHelper.businessObjectDefinitionKeyToString(businessObjectDefinitionKey)));
        }
    }
    // Create a business object definition column entity from the request information.
    BusinessObjectDefinitionColumnEntity businessObjectDefinitionColumnEntity = createBusinessObjectDefinitionColumnEntity(businessObjectDefinitionEntity, request);
    // Link schema columns with the business object definition column.
    for (SchemaColumnEntity schemaColumnEntity : schemaColumnEntities) {
        schemaColumnEntity.setBusinessObjectDefinitionColumn(businessObjectDefinitionColumnEntity);
    }
    // Persist the change event entity
    businessObjectDefinitionColumnDaoHelper.saveBusinessObjectDefinitionColumnChangeEvents(businessObjectDefinitionColumnEntity);
    // Persist the new entity.
    businessObjectDefinitionColumnEntity = businessObjectDefinitionColumnDao.saveAndRefresh(businessObjectDefinitionColumnEntity);
    // Notify the search index that a business object definition must be updated.
    searchIndexUpdateHelper.modifyBusinessObjectDefinitionInSearchIndex(businessObjectDefinitionEntity, SEARCH_INDEX_UPDATE_TYPE_UPDATE);
    // Create and return the business object definition column object from the persisted entity.
    return createBusinessObjectDefinitionColumnFromEntity(businessObjectDefinitionColumnEntity, true, getValidSearchResponseFields(), false);
}
Also used : AlreadyExistsException(org.finra.herd.model.AlreadyExistsException) BusinessObjectDefinitionKey(org.finra.herd.model.api.xml.BusinessObjectDefinitionKey) SchemaColumnEntity(org.finra.herd.model.jpa.SchemaColumnEntity) BusinessObjectDefinitionEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionEntity) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) BusinessObjectDefinitionColumnEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionColumnEntity) NamespacePermission(org.finra.herd.model.annotation.NamespacePermission)

Aggregations

AlreadyExistsException (org.finra.herd.model.AlreadyExistsException)62 Test (org.junit.Test)35 NamespaceEntity (org.finra.herd.model.jpa.NamespaceEntity)12 BusinessObjectDefinitionKey (org.finra.herd.model.api.xml.BusinessObjectDefinitionKey)11 NamespacePermission (org.finra.herd.model.annotation.NamespacePermission)9 ArrayList (java.util.ArrayList)7 BusinessObjectDefinitionEntity (org.finra.herd.model.jpa.BusinessObjectDefinitionEntity)7 BusinessObjectFormatEntity (org.finra.herd.model.jpa.BusinessObjectFormatEntity)6 StorageEntity (org.finra.herd.model.jpa.StorageEntity)6 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)5 BusinessObjectFormatKey (org.finra.herd.model.api.xml.BusinessObjectFormatKey)5 BusinessObjectDataEntity (org.finra.herd.model.jpa.BusinessObjectDataEntity)5 NotificationRegistrationKey (org.finra.herd.model.api.xml.NotificationRegistrationKey)4 StorageFile (org.finra.herd.model.api.xml.StorageFile)4 TagKey (org.finra.herd.model.api.xml.TagKey)4 NamespacePermissions (org.finra.herd.model.annotation.NamespacePermissions)3 AttributeValueListKey (org.finra.herd.model.api.xml.AttributeValueListKey)3 BusinessObjectDataCreateRequest (org.finra.herd.model.api.xml.BusinessObjectDataCreateRequest)3 RelationalTableRegistrationCreateRequest (org.finra.herd.model.api.xml.RelationalTableRegistrationCreateRequest)3 TagTypeKey (org.finra.herd.model.api.xml.TagTypeKey)3