Search in sources :

Example 1 with AlreadyExistsException

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

the class BusinessObjectDefinitionDaoHelper method createBusinessObjectDefinitionEntity.

/**
 * Create Business Object Definition Entity
 * @param request business object definition create request
 * @return Business Object Definition Entity
 */
public BusinessObjectDefinitionEntity createBusinessObjectDefinitionEntity(BusinessObjectDefinitionCreateRequest request) {
    // Perform the validation.
    validateBusinessObjectDefinitionCreateRequest(request);
    // Get the namespace and ensure it exists.
    NamespaceEntity namespaceEntity = namespaceDaoHelper.getNamespaceEntity(request.getNamespace());
    // Get the data provider and ensure it exists.
    DataProviderEntity dataProviderEntity = dataProviderDaoHelper.getDataProviderEntity(request.getDataProviderName());
    // Get business object definition key.
    BusinessObjectDefinitionKey businessObjectDefinitionKey = new BusinessObjectDefinitionKey(request.getNamespace(), request.getBusinessObjectDefinitionName());
    // Ensure a business object definition with the specified key doesn't already exist.
    BusinessObjectDefinitionEntity businessObjectDefinitionEntity = businessObjectDefinitionDao.getBusinessObjectDefinitionByKey(businessObjectDefinitionKey);
    if (businessObjectDefinitionEntity != null) {
        throw new AlreadyExistsException(String.format("Unable to create business object definition with name \"%s\" because it already exists for namespace \"%s\".", businessObjectDefinitionKey.getBusinessObjectDefinitionName(), businessObjectDefinitionKey.getNamespace()));
    }
    // Create a new entity.
    businessObjectDefinitionEntity = new BusinessObjectDefinitionEntity();
    businessObjectDefinitionEntity.setNamespace(namespaceEntity);
    businessObjectDefinitionEntity.setName(request.getBusinessObjectDefinitionName());
    businessObjectDefinitionEntity.setDescription(request.getDescription());
    businessObjectDefinitionEntity.setDataProvider(dataProviderEntity);
    businessObjectDefinitionEntity.setDisplayName(request.getDisplayName());
    // Create the attributes if they are specified.
    if (!CollectionUtils.isEmpty(request.getAttributes())) {
        List<BusinessObjectDefinitionAttributeEntity> attributeEntities = new ArrayList<>();
        businessObjectDefinitionEntity.setAttributes(attributeEntities);
        for (Attribute attribute : request.getAttributes()) {
            BusinessObjectDefinitionAttributeEntity attributeEntity = new BusinessObjectDefinitionAttributeEntity();
            attributeEntities.add(attributeEntity);
            attributeEntity.setBusinessObjectDefinition(businessObjectDefinitionEntity);
            attributeEntity.setName(attribute.getName());
            attributeEntity.setValue(attribute.getValue());
        }
    }
    // Persist the change event entity
    saveBusinessObjectDefinitionChangeEvents(businessObjectDefinitionEntity);
    // Persist and return the new entity.
    return businessObjectDefinitionDao.saveAndRefresh(businessObjectDefinitionEntity);
}
Also used : BusinessObjectDefinitionAttributeEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionAttributeEntity) NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) DataProviderEntity(org.finra.herd.model.jpa.DataProviderEntity) AlreadyExistsException(org.finra.herd.model.AlreadyExistsException) BusinessObjectDefinitionKey(org.finra.herd.model.api.xml.BusinessObjectDefinitionKey) Attribute(org.finra.herd.model.api.xml.Attribute) BusinessObjectDefinitionEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionEntity) ArrayList(java.util.ArrayList)

Example 2 with AlreadyExistsException

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

the class BusinessObjectDataStorageFileServiceImpl method validateStorageFiles.

/**
 * Validates a list of storage files to be added to the specified storage unit.
 *
 * @param storageFiles the list of storage files
 * @param storageUnitEntity the storage unit entity
 * @param validatePathPrefix the validate path prefix flag
 * @param validateFileExistence the validate file existence flag
 * @param validateFileSize the validate file size flag
 */
private void validateStorageFiles(List<StorageFile> storageFiles, StorageUnitEntity storageUnitEntity, boolean validatePathPrefix, boolean validateFileExistence, boolean validateFileSize) {
    // Retrieve all storage files already registered for this storage unit loaded in a map for easy access.
    Map<String, StorageFileEntity> storageFileEntities = storageFileHelper.getStorageFileEntitiesMap(storageUnitEntity.getStorageFiles());
    // Perform validation of storage files listed in the request per storage directory path and/or validation flags.
    String directoryPath = null;
    String directoryPathWithTrailingSlash = null;
    if (StringUtils.isNotBlank(storageUnitEntity.getDirectoryPath())) {
        // Use the storage directory path from the storage unit.
        directoryPath = storageUnitEntity.getDirectoryPath();
        // Add a trailing slash to the storage directory path if it doesn't already have it.
        directoryPathWithTrailingSlash = StringUtils.appendIfMissing(directoryPath, "/");
        // If a storage directory path exists, then validate that all files being added are contained within that directory.
        for (StorageFile storageFile : storageFiles) {
            Assert.isTrue(storageFile.getFilePath().startsWith(directoryPathWithTrailingSlash), String.format("Storage file path \"%s\" does not match the storage directory path \"%s\".", storageFile.getFilePath(), directoryPathWithTrailingSlash));
        }
    } else if (validatePathPrefix || validateFileExistence) {
        // Use the expected S3 key prefix value as the storage directory path.
        directoryPath = s3KeyPrefixHelper.buildS3KeyPrefix(storageUnitEntity.getStorage(), storageUnitEntity.getBusinessObjectData().getBusinessObjectFormat(), businessObjectDataHelper.getBusinessObjectDataKey(storageUnitEntity.getBusinessObjectData()));
        // Add a trailing slash to the expected S3 key prefix if it doesn't already have it.
        directoryPathWithTrailingSlash = StringUtils.appendIfMissing(directoryPath, "/");
        // Validate that all files are contained within the expected S3 key prefix.
        for (StorageFile storageFile : storageFiles) {
            Assert.isTrue(storageFile.getFilePath().startsWith(directoryPathWithTrailingSlash), String.format("Specified storage file path \"%s\" does not match the expected S3 key prefix \"%s\".", storageFile.getFilePath(), directoryPathWithTrailingSlash));
        }
    }
    // Validate that files in the request does not already exist in the database.
    if (StringUtils.isNotBlank(directoryPath)) {
        // Get a list of request storage file paths.
        List<String> requestStorageFilePaths = storageFileHelper.getFilePathsFromStorageFiles(storageFiles);
        // Retrieve all already registered storage files from the storage that start with the directory path.
        List<String> registeredStorageFilePaths = storageFileDao.getStorageFilesByStorageAndFilePathPrefix(storageUnitEntity.getStorage().getName(), directoryPathWithTrailingSlash);
        // Check if request contains any of the already registered files.
        registeredStorageFilePaths.retainAll(requestStorageFilePaths);
        if (!CollectionUtils.isEmpty(registeredStorageFilePaths)) {
            // Retrieve the storage file entity for the first "already registered" storage file.
            // Since the discovered storage file path exists in the database, we should not get a null back.
            StorageFileEntity storageFileEntity = storageFileDao.getStorageFileByStorageNameAndFilePath(storageUnitEntity.getStorage().getName(), registeredStorageFilePaths.get(0));
            // Throw an exception reporting the information on the "already registered" storage file.
            throw new AlreadyExistsException(String.format("S3 file \"%s\" in \"%s\" storage is already registered by the business object data {%s}.", registeredStorageFilePaths.get(0), storageUnitEntity.getStorage().getName(), businessObjectDataHelper.businessObjectDataEntityAltKeyToString(storageFileEntity.getStorageUnit().getBusinessObjectData())));
        }
    } else {
        // Since directory path is not available, we need to validate each storage file specified in the request individually.
        for (StorageFile storageFile : storageFiles) {
            // Ensure that the file is not already registered in this storage by some other business object data.
            StorageFileEntity storageFileEntity = storageFileDao.getStorageFileByStorageNameAndFilePath(storageUnitEntity.getStorage().getName(), storageFile.getFilePath());
            if (storageFileEntity != null) {
                throw new AlreadyExistsException(String.format("S3 file \"%s\" in \"%s\" storage is already registered by the business object data {%s}.", storageFile.getFilePath(), storageUnitEntity.getStorage().getName(), businessObjectDataHelper.businessObjectDataEntityAltKeyToString(storageFileEntity.getStorageUnit().getBusinessObjectData())));
            }
        }
    }
    // Validate file existence.
    if (validateFileExistence) {
        // Get S3 bucket access parameters and set the key prefix to the directory path with a trailing slash.
        // Please note that since we got here, the directory path can not be empty.
        S3FileTransferRequestParamsDto params = storageHelper.getS3BucketAccessParams(storageUnitEntity.getStorage());
        params.setS3KeyPrefix(directoryPathWithTrailingSlash);
        // When listing S3 files, we ignore 0 byte objects that represent S3 directories.
        Map<String, StorageFile> actualS3Keys = storageFileHelper.getStorageFilesMapFromS3ObjectSummaries(s3Service.listDirectory(params, true));
        // For the already registered storage files, validate each storage file against S3 keys and metadata reported by S3.
        for (Map.Entry<String, StorageFileEntity> entry : storageFileEntities.entrySet()) {
            storageFileHelper.validateStorageFileEntity(entry.getValue(), params.getS3BucketName(), actualS3Keys, validateFileSize);
        }
        // Validate each storage file listed in the request.
        for (StorageFile storageFile : storageFiles) {
            storageFileHelper.validateStorageFile(storageFile, params.getS3BucketName(), actualS3Keys, validateFileSize);
        }
    }
}
Also used : StorageFileEntity(org.finra.herd.model.jpa.StorageFileEntity) AlreadyExistsException(org.finra.herd.model.AlreadyExistsException) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) StorageFile(org.finra.herd.model.api.xml.StorageFile) Map(java.util.Map)

Example 3 with AlreadyExistsException

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

the class BusinessObjectDefinitionTagServiceImpl method createBusinessObjectDefinitionTag.

@NamespacePermission(fields = "#request.businessObjectDefinitionTagKey.businessObjectDefinitionKey.namespace", permissions = { NamespacePermissionEnum.WRITE_DESCRIPTIVE_CONTENT, NamespacePermissionEnum.WRITE })
@Override
public BusinessObjectDefinitionTag createBusinessObjectDefinitionTag(BusinessObjectDefinitionTagCreateRequest request) {
    // Validate and trim the business object definition tag create request.
    validateBusinessObjectDefinitionTagCreateRequest(request);
    // Get the business object definition entity and ensure it exists.
    BusinessObjectDefinitionEntity businessObjectDefinitionEntity = businessObjectDefinitionDaoHelper.getBusinessObjectDefinitionEntity(request.getBusinessObjectDefinitionTagKey().getBusinessObjectDefinitionKey());
    // Get the tag entity and ensure it exists.
    TagEntity tagEntity = tagDaoHelper.getTagEntity(request.getBusinessObjectDefinitionTagKey().getTagKey());
    // Ensure a business object definition tag for the specified business object definition and tag doesn't already exist.
    if (businessObjectDefinitionTagDao.getBusinessObjectDefinitionTagByParentEntities(businessObjectDefinitionEntity, tagEntity) != null) {
        throw new AlreadyExistsException(String.format("Tag with tag type \"%s\" and code \"%s\" already exists for business object definition {%s}.", request.getBusinessObjectDefinitionTagKey().getTagKey().getTagTypeCode(), request.getBusinessObjectDefinitionTagKey().getTagKey().getTagCode(), businessObjectDefinitionHelper.businessObjectDefinitionKeyToString(request.getBusinessObjectDefinitionTagKey().getBusinessObjectDefinitionKey())));
    }
    // Create and persist a business object definition tag entity.
    BusinessObjectDefinitionTagEntity businessObjectDefinitionTagEntity = createBusinessObjectDefinitionTagEntity(businessObjectDefinitionEntity, tagEntity);
    // 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 tag object from the persisted entity.
    return createBusinessObjectDefinitionTagFromEntity(businessObjectDefinitionTagEntity);
}
Also used : AlreadyExistsException(org.finra.herd.model.AlreadyExistsException) BusinessObjectDefinitionEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionEntity) TagEntity(org.finra.herd.model.jpa.TagEntity) BusinessObjectDefinitionTagEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionTagEntity) BusinessObjectDefinitionTagEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionTagEntity) NamespacePermission(org.finra.herd.model.annotation.NamespacePermission)

Example 4 with AlreadyExistsException

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

the class StorageUnitNotificationRegistrationServiceImpl method createStorageUnitNotificationRegistration.

@NamespacePermissions({ @NamespacePermission(fields = "#request?.storageUnitNotificationRegistrationKey?.namespace", permissions = NamespacePermissionEnum.WRITE), @NamespacePermission(fields = "#request?.storageUnitNotificationFilter?.namespace", permissions = NamespacePermissionEnum.READ), @NamespacePermission(fields = "#request?.jobActions?.![namespace]", permissions = NamespacePermissionEnum.EXECUTE) })
@Override
public StorageUnitNotificationRegistration createStorageUnitNotificationRegistration(StorageUnitNotificationRegistrationCreateRequest request) {
    // Validate and trim the request parameters.
    validateStorageUnitNotificationRegistrationCreateRequest(request);
    // Get the notification registration key.
    NotificationRegistrationKey notificationRegistrationKey = request.getStorageUnitNotificationRegistrationKey();
    // Retrieve and ensure that notification registration namespace exists.
    NamespaceEntity namespaceEntity = namespaceDaoHelper.getNamespaceEntity(notificationRegistrationKey.getNamespace());
    // Retrieve and validate the notification event type entity.
    NotificationEventTypeEntity notificationEventTypeEntity = getAndValidateNotificationEventTypeEntity(request.getStorageUnitEventType());
    // Validate the notification event type.
    Assert.isTrue(NotificationEventTypeEntity.EventTypesStorageUnit.STRGE_UNIT_STTS_CHG.name().equalsIgnoreCase(request.getStorageUnitEventType()), String.format("Notification event type \"%s\" is not supported for storage unit notification registration.", request.getStorageUnitEventType()));
    // Get the storage unit notification filter.
    StorageUnitNotificationFilter filter = request.getStorageUnitNotificationFilter();
    // Retrieve and ensure that business object definition exists.
    BusinessObjectDefinitionEntity businessObjectDefinitionEntity = businessObjectDefinitionDaoHelper.getBusinessObjectDefinitionEntity(new BusinessObjectDefinitionKey(filter.getNamespace(), filter.getBusinessObjectDefinitionName()));
    // If specified, retrieve and ensure that file type exists.
    FileTypeEntity fileTypeEntity = null;
    if (StringUtils.isNotBlank(filter.getBusinessObjectFormatFileType())) {
        fileTypeEntity = fileTypeDaoHelper.getFileTypeEntity(filter.getBusinessObjectFormatFileType());
    }
    // Retrieve and ensure that storage exists.
    StorageEntity storageEntity = storageDaoHelper.getStorageEntity(filter.getStorageName());
    // If specified, retrieve and ensure that new storage unit status exists.
    StorageUnitStatusEntity newStorageUnitStatus = null;
    if (StringUtils.isNotBlank(filter.getNewStorageUnitStatus())) {
        newStorageUnitStatus = storageUnitStatusDaoHelper.getStorageUnitStatusEntity(filter.getNewStorageUnitStatus());
    }
    // If specified, retrieve and ensure that old storage unit status exists.
    StorageUnitStatusEntity oldStorageUnitStatus = null;
    if (StringUtils.isNotBlank(filter.getOldStorageUnitStatus())) {
        oldStorageUnitStatus = storageUnitStatusDaoHelper.getStorageUnitStatusEntity(filter.getOldStorageUnitStatus());
    }
    // TODO: We need to add a null/empty list check here, if/when list of job actions will become optional (due to addition of other action types).
    for (JobAction jobAction : request.getJobActions()) {
        // Ensure that job definition exists.
        jobDefinitionDaoHelper.getJobDefinitionEntity(jobAction.getNamespace(), jobAction.getJobName());
    }
    // If specified, retrieve and validate the notification registration status entity. Otherwise, default it to ENABLED.
    NotificationRegistrationStatusEntity notificationRegistrationStatusEntity = notificationRegistrationStatusDaoHelper.getNotificationRegistrationStatusEntity(StringUtils.isNotBlank(request.getNotificationRegistrationStatus()) ? request.getNotificationRegistrationStatus() : NotificationRegistrationStatusEntity.ENABLED);
    // Ensure a storage unit notification with the specified name doesn't already exist for the specified namespace.
    StorageUnitNotificationRegistrationEntity storageUnitNotificationRegistrationEntity = storageUnitNotificationRegistrationDao.getStorageUnitNotificationRegistrationByAltKey(notificationRegistrationKey);
    if (storageUnitNotificationRegistrationEntity != null) {
        throw new AlreadyExistsException(String.format("Unable to create storage unit notification with name \"%s\" because it already exists for namespace \"%s\".", notificationRegistrationKey.getNotificationName(), notificationRegistrationKey.getNamespace()));
    }
    // Create a storage unit notification registration entity from the request information.
    storageUnitNotificationRegistrationEntity = createStorageUnitNotificationEntity(namespaceEntity, notificationEventTypeEntity, businessObjectDefinitionEntity, fileTypeEntity, storageEntity, newStorageUnitStatus, oldStorageUnitStatus, request.getStorageUnitNotificationRegistrationKey(), request.getStorageUnitNotificationFilter(), request.getJobActions(), notificationRegistrationStatusEntity);
    // Persist the new entity.
    storageUnitNotificationRegistrationEntity = storageUnitNotificationRegistrationDao.saveAndRefresh(storageUnitNotificationRegistrationEntity);
    // Create and return the storage unit notification object from the persisted entity.
    return createStorageUnitNotificationFromEntity(storageUnitNotificationRegistrationEntity);
}
Also used : NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) AlreadyExistsException(org.finra.herd.model.AlreadyExistsException) BusinessObjectDefinitionKey(org.finra.herd.model.api.xml.BusinessObjectDefinitionKey) FileTypeEntity(org.finra.herd.model.jpa.FileTypeEntity) StorageEntity(org.finra.herd.model.jpa.StorageEntity) NotificationEventTypeEntity(org.finra.herd.model.jpa.NotificationEventTypeEntity) JobAction(org.finra.herd.model.api.xml.JobAction) BusinessObjectDefinitionEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionEntity) StorageUnitStatusEntity(org.finra.herd.model.jpa.StorageUnitStatusEntity) NotificationRegistrationStatusEntity(org.finra.herd.model.jpa.NotificationRegistrationStatusEntity) StorageUnitNotificationFilter(org.finra.herd.model.api.xml.StorageUnitNotificationFilter) StorageUnitNotificationRegistrationEntity(org.finra.herd.model.jpa.StorageUnitNotificationRegistrationEntity) NotificationRegistrationKey(org.finra.herd.model.api.xml.NotificationRegistrationKey) NamespacePermissions(org.finra.herd.model.annotation.NamespacePermissions)

Example 5 with AlreadyExistsException

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

the class StorageServiceImpl method createStorage.

@Override
public Storage createStorage(StorageCreateRequest storageCreateRequest) {
    // Perform validation and trim.
    validateAndTrimStorageCreateRequest(storageCreateRequest);
    // Retrieve storage platform.
    StoragePlatformEntity storagePlatformEntity = storagePlatformHelper.getStoragePlatformEntity(storageCreateRequest.getStoragePlatformName());
    // See if a storage with the specified name already exists.
    StorageEntity storageEntity = storageDao.getStorageByName(storageCreateRequest.getName());
    if (storageEntity != null) {
        throw new AlreadyExistsException(String.format("Storage with name \"%s\" already exists.", storageCreateRequest.getName()));
    }
    // Create a storage entity.
    storageEntity = new StorageEntity();
    storageEntity.setName(storageCreateRequest.getName());
    storageEntity.setStoragePlatform(storagePlatformEntity);
    // Create attributes if they are specified.
    if (!CollectionUtils.isEmpty(storageCreateRequest.getAttributes())) {
        List<StorageAttributeEntity> attributeEntities = new ArrayList<>();
        storageEntity.setAttributes(attributeEntities);
        for (Attribute attribute : storageCreateRequest.getAttributes()) {
            StorageAttributeEntity attributeEntity = new StorageAttributeEntity();
            attributeEntities.add(attributeEntity);
            attributeEntity.setStorage(storageEntity);
            attributeEntity.setName(attribute.getName());
            attributeEntity.setValue(attribute.getValue());
        }
    }
    // Persist the storage entity.
    storageEntity = storageDao.saveAndRefresh(storageEntity);
    // Return the storage information.
    return createStorageFromEntity(storageEntity);
}
Also used : AlreadyExistsException(org.finra.herd.model.AlreadyExistsException) Attribute(org.finra.herd.model.api.xml.Attribute) StoragePlatformEntity(org.finra.herd.model.jpa.StoragePlatformEntity) StorageAttributeEntity(org.finra.herd.model.jpa.StorageAttributeEntity) ArrayList(java.util.ArrayList) StorageEntity(org.finra.herd.model.jpa.StorageEntity)

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