Search in sources :

Example 1 with PublishNotificationMessages

use of org.finra.herd.model.annotation.PublishNotificationMessages in project herd by FINRAOS.

the class BusinessObjectFormatServiceImpl method createBusinessObjectFormat.

@PublishNotificationMessages
@NamespacePermission(fields = "#request.namespace", permissions = NamespacePermissionEnum.WRITE)
@Override
public BusinessObjectFormat createBusinessObjectFormat(BusinessObjectFormatCreateRequest request) {
    // Perform the validation of the request parameters, except for the alternate key.
    validateBusinessObjectFormatCreateRequest(request);
    // Get business object format key from the request.
    BusinessObjectFormatKey businessObjectFormatKey = getBusinessObjectFormatKey(request);
    // Get the business object definition and ensure it exists.
    BusinessObjectDefinitionEntity businessObjectDefinitionEntity = businessObjectDefinitionDaoHelper.getBusinessObjectDefinitionEntity(new BusinessObjectDefinitionKey(businessObjectFormatKey.getNamespace(), businessObjectFormatKey.getBusinessObjectDefinitionName()));
    // Get business object format file type and ensure it exists.
    FileTypeEntity fileTypeEntity = fileTypeDaoHelper.getFileTypeEntity(request.getBusinessObjectFormatFileType());
    // Get the latest format version for this business format, if it exists.
    BusinessObjectFormatEntity latestVersionBusinessObjectFormatEntity = businessObjectFormatDao.getBusinessObjectFormatByAltKey(businessObjectFormatKey);
    // If the latest version exists, perform the additive schema validation and update the latest entity.
    if (latestVersionBusinessObjectFormatEntity != null) {
        // Get the latest version business object format model object.
        BusinessObjectFormat latestVersionBusinessObjectFormat = businessObjectFormatHelper.createBusinessObjectFormatFromEntity(latestVersionBusinessObjectFormatEntity);
        // If the latest version format has schema, check the new format version schema is "additive" to the previous format version.
        if (latestVersionBusinessObjectFormat.getSchema() != null) {
            validateNewSchemaIsAdditiveToOldSchema(request.getSchema(), latestVersionBusinessObjectFormat.getSchema());
        }
        // Update the latest entity.
        latestVersionBusinessObjectFormatEntity.setLatestVersion(false);
        businessObjectFormatDao.saveAndRefresh(latestVersionBusinessObjectFormatEntity);
    }
    // Create a business object format entity from the request information.
    Integer businessObjectFormatVersion = latestVersionBusinessObjectFormatEntity == null ? 0 : latestVersionBusinessObjectFormatEntity.getBusinessObjectFormatVersion() + 1;
    BusinessObjectFormatEntity newBusinessObjectFormatEntity = createBusinessObjectFormatEntity(request, businessObjectDefinitionEntity, fileTypeEntity, businessObjectFormatVersion, null);
    // latest version format is the descriptive format for the bdef, update the bdef descriptive format to the newly created one
    if (latestVersionBusinessObjectFormatEntity != null && latestVersionBusinessObjectFormatEntity.equals(businessObjectDefinitionEntity.getDescriptiveBusinessObjectFormat())) {
        businessObjectDefinitionEntity.setDescriptiveBusinessObjectFormat(newBusinessObjectFormatEntity);
        businessObjectDefinitionDao.saveAndRefresh(businessObjectDefinitionEntity);
    }
    // latest version business object exists, carry the retention information to the new entity
    if (latestVersionBusinessObjectFormatEntity != null) {
        // for each of the previous version's  child, remove the parent link to the previous version and set the parent to the new version
        for (BusinessObjectFormatEntity childFormatEntity : latestVersionBusinessObjectFormatEntity.getBusinessObjectFormatChildren()) {
            childFormatEntity.getBusinessObjectFormatParents().remove(latestVersionBusinessObjectFormatEntity);
            childFormatEntity.getBusinessObjectFormatParents().add(newBusinessObjectFormatEntity);
            newBusinessObjectFormatEntity.getBusinessObjectFormatChildren().add(childFormatEntity);
            businessObjectFormatDao.saveAndRefresh(childFormatEntity);
        }
        // for each of the previous version's parent, remove the child link to the previous version and set the child link to the new version
        for (BusinessObjectFormatEntity parentFormatEntity : latestVersionBusinessObjectFormatEntity.getBusinessObjectFormatParents()) {
            parentFormatEntity.getBusinessObjectFormatChildren().remove(latestVersionBusinessObjectFormatEntity);
            parentFormatEntity.getBusinessObjectFormatChildren().add(newBusinessObjectFormatEntity);
            newBusinessObjectFormatEntity.getBusinessObjectFormatParents().add(parentFormatEntity);
            businessObjectFormatDao.saveAndRefresh(parentFormatEntity);
        }
        // mark the latest version business object format
        latestVersionBusinessObjectFormatEntity.setBusinessObjectFormatParents(null);
        latestVersionBusinessObjectFormatEntity.setBusinessObjectFormatChildren(null);
        // carry the retention information from the latest entity to the new entity
        newBusinessObjectFormatEntity.setRetentionPeriodInDays(latestVersionBusinessObjectFormatEntity.getRetentionPeriodInDays());
        newBusinessObjectFormatEntity.setRecordFlag(latestVersionBusinessObjectFormatEntity.isRecordFlag());
        newBusinessObjectFormatEntity.setRetentionType(latestVersionBusinessObjectFormatEntity.getRetentionType());
        businessObjectFormatDao.saveAndRefresh(newBusinessObjectFormatEntity);
        // reset the retention information of the latest version business object format
        latestVersionBusinessObjectFormatEntity.setRetentionType(null);
        latestVersionBusinessObjectFormatEntity.setRecordFlag(null);
        latestVersionBusinessObjectFormatEntity.setRetentionPeriodInDays(null);
        businessObjectFormatDao.saveAndRefresh(latestVersionBusinessObjectFormatEntity);
    }
    // Notify the search index that a business object definition must be updated.
    searchIndexUpdateHelper.modifyBusinessObjectDefinitionInSearchIndex(businessObjectDefinitionEntity, SEARCH_INDEX_UPDATE_TYPE_UPDATE);
    // Create a version change notification to be sent on create business object format event.
    messageNotificationEventService.processBusinessObjectFormatVersionChangeNotificationEvent(businessObjectFormatHelper.getBusinessObjectFormatKey(newBusinessObjectFormatEntity), latestVersionBusinessObjectFormatEntity != null ? latestVersionBusinessObjectFormatEntity.getBusinessObjectFormatVersion().toString() : "");
    // Create and return the business object format object from the persisted entity.
    return businessObjectFormatHelper.createBusinessObjectFormatFromEntity(newBusinessObjectFormatEntity);
}
Also used : BusinessObjectDefinitionKey(org.finra.herd.model.api.xml.BusinessObjectDefinitionKey) FileTypeEntity(org.finra.herd.model.jpa.FileTypeEntity) BusinessObjectDefinitionEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionEntity) BusinessObjectFormatKey(org.finra.herd.model.api.xml.BusinessObjectFormatKey) BusinessObjectFormatEntity(org.finra.herd.model.jpa.BusinessObjectFormatEntity) BusinessObjectFormat(org.finra.herd.model.api.xml.BusinessObjectFormat) PublishNotificationMessages(org.finra.herd.model.annotation.PublishNotificationMessages) NamespacePermission(org.finra.herd.model.annotation.NamespacePermission)

Example 2 with PublishNotificationMessages

use of org.finra.herd.model.annotation.PublishNotificationMessages in project herd by FINRAOS.

the class BusinessObjectFormatServiceImpl method updateBusinessObjectFormat.

@PublishNotificationMessages
@NamespacePermission(fields = "#businessObjectFormatKey.namespace", permissions = NamespacePermissionEnum.WRITE)
@Override
public BusinessObjectFormat updateBusinessObjectFormat(BusinessObjectFormatKey businessObjectFormatKey, BusinessObjectFormatUpdateRequest request) {
    // Perform validation and trim the alternate key parameters.
    businessObjectFormatHelper.validateBusinessObjectFormatKey(businessObjectFormatKey);
    // Validate optional attributes. This is also going to trim the attribute names.
    attributeHelper.validateFormatAttributes(request.getAttributes());
    // Retrieve and ensure that a business object format exists.
    BusinessObjectFormatEntity businessObjectFormatEntity = businessObjectFormatDaoHelper.getBusinessObjectFormatEntity(businessObjectFormatKey);
    // Update business object format description.
    businessObjectFormatEntity.setDescription(request.getDescription());
    // Validate optional schema information.  This is also going to trim the relative schema column field values.
    validateBusinessObjectFormatSchema(request.getSchema(), businessObjectFormatEntity.getPartitionKey());
    // Update business object format attributes
    updateBusinessObjectFormatAttributesHelper(businessObjectFormatEntity, request.getAttributes());
    // Get business object format model object.
    BusinessObjectFormat businessObjectFormat = businessObjectFormatHelper.createBusinessObjectFormatFromEntity(businessObjectFormatEntity);
    // Check if we need to update business object format schema information.
    if ((request.getSchema() != null && !request.getSchema().equals(businessObjectFormat.getSchema())) || (request.getSchema() == null && businessObjectFormat.getSchema() != null)) {
        // TODO: Check if we are allowed to update schema information for this business object format.
        // if (businessObjectFormat.getSchema() != null && herdDao.getBusinessObjectDataCount(businessObjectFormatKey) > 0L)
        // {
        // throw new IllegalArgumentException(String
        // .format("Can not update schema information for a business object format that has an existing schema " +
        // "defined and business object data associated with it. Business object format: {%s}",
        // herdDaoHelper.businessObjectFormatEntityAltKeyToString(businessObjectFormatEntity)));
        // }
        // Update schema information by clearing and setting the relative business object
        // format entity fields and by re-creating schema column entities.  Please note that
        // this approach results in changing all schema column Id's which could have
        // ramifications down the road if other entities have relations to schema columns.
        // Also, performance will be slightly slower doing a bunch of deletes followed by a bunch
        // of inserts for what could otherwise be a single SQL statement if only one column was changed.
        // Nevertheless, the below approach results in a simpler code.
        // Removes business object format schema information from the business object format entity.
        clearBusinessObjectFormatSchema(businessObjectFormatEntity);
        // In order to avoid INSERT-then-DELETE, we need to flush the session before we add new schema column entities.
        businessObjectFormatDao.saveAndRefresh(businessObjectFormatEntity);
        // Populates schema information within the business object format entity.
        populateBusinessObjectFormatSchema(businessObjectFormatEntity, request.getSchema());
    }
    // Persist and refresh the entity.
    businessObjectFormatEntity = businessObjectFormatDao.saveAndRefresh(businessObjectFormatEntity);
    // Notify the search index that a business object definition must be updated.
    searchIndexUpdateHelper.modifyBusinessObjectDefinitionInSearchIndex(businessObjectFormatEntity.getBusinessObjectDefinition(), SEARCH_INDEX_UPDATE_TYPE_UPDATE);
    // Create a version change notification to be sent on create business object format event.
    messageNotificationEventService.processBusinessObjectFormatVersionChangeNotificationEvent(businessObjectFormatHelper.getBusinessObjectFormatKey(businessObjectFormatEntity), businessObjectFormatEntity.getBusinessObjectFormatVersion().toString());
    // Create and return the business object format object from the persisted entity.
    return businessObjectFormatHelper.createBusinessObjectFormatFromEntity(businessObjectFormatEntity);
}
Also used : BusinessObjectFormatEntity(org.finra.herd.model.jpa.BusinessObjectFormatEntity) BusinessObjectFormat(org.finra.herd.model.api.xml.BusinessObjectFormat) PublishNotificationMessages(org.finra.herd.model.annotation.PublishNotificationMessages) NamespacePermission(org.finra.herd.model.annotation.NamespacePermission)

Example 3 with PublishNotificationMessages

use of org.finra.herd.model.annotation.PublishNotificationMessages in project herd by FINRAOS.

the class UploadDownloadServiceImpl method initiateUploadSingle.

@PublishNotificationMessages
@NamespacePermission(fields = { "#uploadSingleInitiationRequest?.sourceBusinessObjectFormatKey?.namespace", "#uploadSingleInitiationRequest?.targetBusinessObjectFormatKey?.namespace" }, permissions = NamespacePermissionEnum.WRITE)
@Override
public UploadSingleInitiationResponse initiateUploadSingle(UploadSingleInitiationRequest uploadSingleInitiationRequest) {
    // Validate and trim the request parameters.
    validateUploadSingleInitiationRequest(uploadSingleInitiationRequest);
    // Get the business object format for the specified parameters and make sure it exists.
    BusinessObjectFormatEntity sourceBusinessObjectFormatEntity = businessObjectFormatDaoHelper.getBusinessObjectFormatEntity(uploadSingleInitiationRequest.getSourceBusinessObjectFormatKey());
    // Get the target business object format entity for the specified parameters and make sure it exists.
    BusinessObjectFormatEntity targetBusinessObjectFormatEntity = businessObjectFormatDaoHelper.getBusinessObjectFormatEntity(uploadSingleInitiationRequest.getTargetBusinessObjectFormatKey());
    // Get the S3 managed "loading dock" storage entity and make sure it exists.
    StorageEntity sourceStorageEntity = storageDaoHelper.getStorageEntity(StorageEntity.MANAGED_LOADING_DOCK_STORAGE);
    // Get S3 bucket name for the storage. Please note that since those values are required we pass a "true" flag.
    String s3BucketName = storageHelper.getStorageBucketName(sourceStorageEntity);
    // Get the S3 managed "external" storage entity and make sure it exists.
    String targetStorageName;
    if (uploadSingleInitiationRequest.getTargetStorageName() != null) {
        targetStorageName = uploadSingleInitiationRequest.getTargetStorageName();
    } else {
        targetStorageName = configurationHelper.getProperty(ConfigurationValue.S3_EXTERNAL_STORAGE_NAME_DEFAULT);
    }
    StorageEntity targetStorageEntity = storageDaoHelper.getStorageEntity(targetStorageName);
    assertTargetStorageEntityValid(targetStorageEntity);
    // Generate a random UUID value.
    String uuid = UUID.randomUUID().toString();
    // Create business object data key with partition value set to the generated UUID.
    BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey(uploadSingleInitiationRequest.getSourceBusinessObjectFormatKey().getNamespace(), uploadSingleInitiationRequest.getSourceBusinessObjectFormatKey().getBusinessObjectDefinitionName(), uploadSingleInitiationRequest.getSourceBusinessObjectFormatKey().getBusinessObjectFormatUsage(), uploadSingleInitiationRequest.getSourceBusinessObjectFormatKey().getBusinessObjectFormatFileType(), uploadSingleInitiationRequest.getSourceBusinessObjectFormatKey().getBusinessObjectFormatVersion(), uuid, null, BusinessObjectDataEntity.BUSINESS_OBJECT_DATA_INITIAL_VERSION);
    // Get a file upload specific S3 key prefix for the source storage based on the generated UUID.
    String sourceStorageDirectoryPath = s3KeyPrefixHelper.buildS3KeyPrefix(sourceStorageEntity, sourceBusinessObjectFormatEntity, businessObjectDataKey);
    String sourceStorageFilePath = String.format("%s/%s", sourceStorageDirectoryPath, uploadSingleInitiationRequest.getFile().getFileName());
    // Create a business object data create request.
    BusinessObjectDataCreateRequest sourceBusinessObjectDataCreateRequest = businessObjectDataHelper.createBusinessObjectDataCreateRequest(sourceBusinessObjectFormatEntity, uuid, BusinessObjectDataStatusEntity.UPLOADING, uploadSingleInitiationRequest.getBusinessObjectDataAttributes(), sourceStorageEntity, sourceStorageDirectoryPath, sourceStorageFilePath, uploadSingleInitiationRequest.getFile().getFileSizeBytes(), null);
    // Create a new business object data instance. Set the flag to false, since for the file upload service the file size value is optional.
    BusinessObjectData sourceBusinessObjectData = businessObjectDataDaoHelper.createBusinessObjectData(sourceBusinessObjectDataCreateRequest, false);
    // Get a file upload specific S3 key prefix for the target storage based on the generated UUID.
    String targetStorageDirectoryPath = s3KeyPrefixHelper.buildS3KeyPrefix(targetStorageEntity, targetBusinessObjectFormatEntity, businessObjectDataKey);
    String targetStorageFilePath = String.format("%s/%s", targetStorageDirectoryPath, uploadSingleInitiationRequest.getFile().getFileName());
    uploadDownloadHelperService.assertS3ObjectKeyDoesNotExist(storageHelper.getStorageBucketName(targetStorageEntity), targetStorageFilePath);
    // Create a target business object data based on the source business object data and target business object format.
    BusinessObjectDataCreateRequest targetBusinessObjectDataCreateRequest = businessObjectDataHelper.createBusinessObjectDataCreateRequest(targetBusinessObjectFormatEntity, uuid, BusinessObjectDataStatusEntity.UPLOADING, uploadSingleInitiationRequest.getBusinessObjectDataAttributes(), targetStorageEntity, targetStorageDirectoryPath, targetStorageFilePath, uploadSingleInitiationRequest.getFile().getFileSizeBytes(), null);
    // Create a target business object data instance. Set the flag to false, since for the file upload service the file size value is optional.
    BusinessObjectData targetBusinessObjectData = businessObjectDataDaoHelper.createBusinessObjectData(targetBusinessObjectDataCreateRequest, false);
    // Get decrypted AWS ARN of the role that is required to provide access to S3_MANAGED_LOADING_DOCK storage.
    String awsRoleArn = getStorageUploadRoleArn(sourceStorageEntity);
    // Get expiration interval for the pre-signed URL to be generated.
    Integer awsRoleDurationSeconds = getStorageUploadSessionDuration(sourceStorageEntity);
    String awsKmsKeyId = storageHelper.getStorageKmsKeyId(sourceStorageEntity);
    // Get the temporary security credentials to access S3_MANAGED_STORAGE.
    Credentials assumedSessionCredentials = stsDao.getTemporarySecurityCredentials(awsHelper.getAwsParamsDto(), String.valueOf(sourceBusinessObjectData.getId()), awsRoleArn, awsRoleDurationSeconds, createUploaderPolicy(s3BucketName, sourceStorageFilePath, awsKmsKeyId));
    // Create the response.
    UploadSingleInitiationResponse response = new UploadSingleInitiationResponse();
    response.setSourceBusinessObjectData(sourceBusinessObjectData);
    response.setTargetBusinessObjectData(targetBusinessObjectData);
    response.setFile(uploadSingleInitiationRequest.getFile());
    response.setUuid(uuid);
    response.setAwsAccessKey(assumedSessionCredentials.getAccessKeyId());
    response.setAwsSecretKey(assumedSessionCredentials.getSecretAccessKey());
    response.setAwsSessionToken(assumedSessionCredentials.getSessionToken());
    response.setAwsSessionExpirationTime(HerdDateUtils.getXMLGregorianCalendarValue(assumedSessionCredentials.getExpiration()));
    response.setAwsKmsKeyId(awsKmsKeyId);
    response.setTargetStorageName(targetStorageName);
    return response;
}
Also used : BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) BusinessObjectDataCreateRequest(org.finra.herd.model.api.xml.BusinessObjectDataCreateRequest) StorageEntity(org.finra.herd.model.jpa.StorageEntity) UploadSingleInitiationResponse(org.finra.herd.model.api.xml.UploadSingleInitiationResponse) BusinessObjectFormatEntity(org.finra.herd.model.jpa.BusinessObjectFormatEntity) BusinessObjectDataKey(org.finra.herd.model.api.xml.BusinessObjectDataKey) Credentials(com.amazonaws.services.securitytoken.model.Credentials) PublishNotificationMessages(org.finra.herd.model.annotation.PublishNotificationMessages) NamespacePermission(org.finra.herd.model.annotation.NamespacePermission)

Aggregations

NamespacePermission (org.finra.herd.model.annotation.NamespacePermission)3 PublishNotificationMessages (org.finra.herd.model.annotation.PublishNotificationMessages)3 BusinessObjectFormatEntity (org.finra.herd.model.jpa.BusinessObjectFormatEntity)3 BusinessObjectFormat (org.finra.herd.model.api.xml.BusinessObjectFormat)2 Credentials (com.amazonaws.services.securitytoken.model.Credentials)1 BusinessObjectData (org.finra.herd.model.api.xml.BusinessObjectData)1 BusinessObjectDataCreateRequest (org.finra.herd.model.api.xml.BusinessObjectDataCreateRequest)1 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)1 BusinessObjectDefinitionKey (org.finra.herd.model.api.xml.BusinessObjectDefinitionKey)1 BusinessObjectFormatKey (org.finra.herd.model.api.xml.BusinessObjectFormatKey)1 UploadSingleInitiationResponse (org.finra.herd.model.api.xml.UploadSingleInitiationResponse)1 BusinessObjectDefinitionEntity (org.finra.herd.model.jpa.BusinessObjectDefinitionEntity)1 FileTypeEntity (org.finra.herd.model.jpa.FileTypeEntity)1 StorageEntity (org.finra.herd.model.jpa.StorageEntity)1