use of org.finra.herd.model.api.xml.AttributeDefinition in project herd by FINRAOS.
the class BusinessObjectFormatHelper method createBusinessObjectFormatFromEntity.
/**
* Creates the business object format from the persisted entity.
*
* @param businessObjectFormatEntity the newly persisted business object format entity.
*
* @param checkLatestVersion need to check latest version
*
* @return the business object format.
*/
public BusinessObjectFormat createBusinessObjectFormatFromEntity(BusinessObjectFormatEntity businessObjectFormatEntity, Boolean checkLatestVersion) {
BusinessObjectFormat businessObjectFormat = new BusinessObjectFormat();
businessObjectFormat.setId(businessObjectFormatEntity.getId());
businessObjectFormat.setNamespace(businessObjectFormatEntity.getBusinessObjectDefinition().getNamespace().getCode());
businessObjectFormat.setBusinessObjectDefinitionName(businessObjectFormatEntity.getBusinessObjectDefinition().getName());
businessObjectFormat.setBusinessObjectFormatUsage(businessObjectFormatEntity.getUsage());
businessObjectFormat.setBusinessObjectFormatFileType(businessObjectFormatEntity.getFileType().getCode());
businessObjectFormat.setBusinessObjectFormatVersion(businessObjectFormatEntity.getBusinessObjectFormatVersion());
businessObjectFormat.setLatestVersion(businessObjectFormatEntity.getLatestVersion());
businessObjectFormat.setPartitionKey(businessObjectFormatEntity.getPartitionKey());
businessObjectFormat.setDescription(businessObjectFormatEntity.getDescription());
// Add in the attributes.
List<Attribute> attributes = new ArrayList<>();
businessObjectFormat.setAttributes(attributes);
for (BusinessObjectFormatAttributeEntity attributeEntity : businessObjectFormatEntity.getAttributes()) {
Attribute attribute = new Attribute();
attributes.add(attribute);
attribute.setName(attributeEntity.getName());
attribute.setValue(attributeEntity.getValue());
}
// Add in the attribute definitions.
List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
businessObjectFormat.setAttributeDefinitions(attributeDefinitions);
for (BusinessObjectDataAttributeDefinitionEntity attributeDefinitionEntity : businessObjectFormatEntity.getAttributeDefinitions()) {
AttributeDefinition attributeDefinition = new AttributeDefinition();
attributeDefinitions.add(attributeDefinition);
attributeDefinition.setName(attributeDefinitionEntity.getName());
attributeDefinition.setPublish(attributeDefinitionEntity.getPublish());
}
// Only add schema information if this format has any schema columns defined.
if (!businessObjectFormatEntity.getSchemaColumns().isEmpty()) {
Schema schema = new Schema();
businessObjectFormat.setSchema(schema);
schema.setNullValue(businessObjectFormatEntity.getNullValue());
schema.setDelimiter(businessObjectFormatEntity.getDelimiter());
schema.setEscapeCharacter(businessObjectFormatEntity.getEscapeCharacter());
schema.setPartitionKeyGroup(businessObjectFormatEntity.getPartitionKeyGroup() != null ? businessObjectFormatEntity.getPartitionKeyGroup().getPartitionKeyGroupName() : null);
// Create two lists of schema column entities: one for the data columns and one for the partition columns.
List<SchemaColumnEntity> dataSchemaColumns = new ArrayList<>();
List<SchemaColumnEntity> partitionSchemaColumns = new ArrayList<>();
for (SchemaColumnEntity schemaColumnEntity : businessObjectFormatEntity.getSchemaColumns()) {
// We can determine which list (or both) a column entity belongs to depending on whether it has a position and/or partition level set.
if (schemaColumnEntity.getPosition() != null) {
dataSchemaColumns.add(schemaColumnEntity);
}
if (schemaColumnEntity.getPartitionLevel() != null) {
partitionSchemaColumns.add(schemaColumnEntity);
}
}
// Sort the data schema columns on the position.
Collections.sort(dataSchemaColumns, new SchemaColumnPositionComparator());
// Sort the partition schema columns on the partition level.
Collections.sort(partitionSchemaColumns, new SchemaColumnPartitionLevelComparator());
// Add in the data schema columns.
List<SchemaColumn> schemaColumns = new ArrayList<>();
schema.setColumns(schemaColumns);
for (SchemaColumnEntity schemaColumnEntity : dataSchemaColumns) {
schemaColumns.add(createSchemaColumn(schemaColumnEntity));
}
// columns which isn't valid from an XSD standpoint.
if (partitionSchemaColumns.size() > 0) {
schemaColumns = new ArrayList<>();
schema.setPartitions(schemaColumns);
for (SchemaColumnEntity schemaColumnEntity : partitionSchemaColumns) {
schemaColumns.add(createSchemaColumn(schemaColumnEntity));
}
}
}
BusinessObjectFormatEntity latestVersionBusinessObjectFormatEntity = businessObjectFormatEntity;
// use the latest version if it is not
if (checkLatestVersion) {
BusinessObjectFormatKey businessObjectFormatKey = getBusinessObjectFormatKey(businessObjectFormatEntity);
businessObjectFormatKey.setBusinessObjectFormatVersion(null);
latestVersionBusinessObjectFormatEntity = businessObjectFormatDao.getBusinessObjectFormatByAltKey(businessObjectFormatKey);
}
// add business object format parent
List<BusinessObjectFormatKey> businessObjectFormatParents = new ArrayList();
businessObjectFormat.setBusinessObjectFormatParents(businessObjectFormatParents);
for (BusinessObjectFormatEntity businessObjectFormatEntityParent : latestVersionBusinessObjectFormatEntity.getBusinessObjectFormatParents()) {
BusinessObjectFormatKey businessObjectFormatParent = getBusinessObjectFormatKey(businessObjectFormatEntityParent);
businessObjectFormatParent.setBusinessObjectFormatVersion(null);
businessObjectFormatParents.add(businessObjectFormatParent);
}
// add business object format children
List<BusinessObjectFormatKey> businessObjectFormatChildren = new ArrayList();
businessObjectFormat.setBusinessObjectFormatChildren(businessObjectFormatChildren);
for (BusinessObjectFormatEntity businessObjectFormatEntityChild : latestVersionBusinessObjectFormatEntity.getBusinessObjectFormatChildren()) {
BusinessObjectFormatKey businessObjectFormatChild = getBusinessObjectFormatKey(businessObjectFormatEntityChild);
businessObjectFormatChild.setBusinessObjectFormatVersion(null);
businessObjectFormatChildren.add(businessObjectFormatChild);
}
// add retention information
businessObjectFormat.setRecordFlag(latestVersionBusinessObjectFormatEntity.isRecordFlag());
businessObjectFormat.setRetentionPeriodInDays(latestVersionBusinessObjectFormatEntity.getRetentionPeriodInDays());
if (latestVersionBusinessObjectFormatEntity.getRetentionType() != null) {
businessObjectFormat.setRetentionType(latestVersionBusinessObjectFormatEntity.getRetentionType().getCode());
}
return businessObjectFormat;
}
use of org.finra.herd.model.api.xml.AttributeDefinition in project herd by FINRAOS.
the class BusinessObjectFormatServiceImpl method validateAndTrimBusinessObjectFormatAttributeDefinitionsHelper.
/**
* Validates business object format attribute definitions
*
* @param attributeDefinitions the attribute definitions
*/
private void validateAndTrimBusinessObjectFormatAttributeDefinitionsHelper(List<AttributeDefinition> attributeDefinitions) {
Assert.notNull(attributeDefinitions, "A business object format attribute definitions list is required.");
Map<String, AttributeDefinition> attributeDefinitionNameValidationMap = new HashMap<>();
for (AttributeDefinition attributeDefinition : attributeDefinitions) {
Assert.hasText(attributeDefinition.getName(), "An attribute definition name must be specified.");
attributeDefinition.setName(attributeDefinition.getName().trim());
// Ensure the attribute defination key isn't a duplicate by using a map with a "lowercase" name as the key for case insensitivity.
String lowercaseAttributeDefinitionName = attributeDefinition.getName().toLowerCase();
if (attributeDefinitionNameValidationMap.containsKey(lowercaseAttributeDefinitionName)) {
throw new IllegalArgumentException(String.format("Duplicate attribute definition name \"%s\" found.", attributeDefinition.getName()));
}
attributeDefinitionNameValidationMap.put(lowercaseAttributeDefinitionName, attributeDefinition);
}
}
use of org.finra.herd.model.api.xml.AttributeDefinition in project herd by FINRAOS.
the class BusinessObjectFormatServiceImpl method updateBusinessObjectFormatAttributeDefinitionsHelper.
/**
* Updates business object format attribute definitions
*
* @param businessObjectFormatEntity the business object format entity
* @param attributeDefinitions the attributes
*/
private void updateBusinessObjectFormatAttributeDefinitionsHelper(BusinessObjectFormatEntity businessObjectFormatEntity, List<AttributeDefinition> attributeDefinitions) {
// Update the attribute definitions.
// Load all existing attribute definition entities in a map with a "lowercase" attribute definition name as the key for case insensitivity.
Map<String, BusinessObjectDataAttributeDefinitionEntity> existingAttributeDefinitionEntities = businessObjectFormatEntity.getAttributeDefinitions().stream().collect(Collectors.toMap(attributeDefinition -> attributeDefinition.getName().toLowerCase(), attributeDefinition -> attributeDefinition));
// Process the list of attribute definitions to determine that business object definition attribute entities should be created, updated, or deleted.
List<BusinessObjectDataAttributeDefinitionEntity> createdAttributeDefinitionEntities = new ArrayList<>();
List<BusinessObjectDataAttributeDefinitionEntity> retainedAttributeDefinitionEntities = new ArrayList<>();
for (AttributeDefinition attributeDefinition : attributeDefinitions) {
// Use a "lowercase" attribute name for case insensitivity.
String lowercaseAttributeName = attributeDefinition.getName().toLowerCase();
if (existingAttributeDefinitionEntities.containsKey(lowercaseAttributeName)) {
// Check if the attribute definition value needs to be updated.
BusinessObjectDataAttributeDefinitionEntity businessObjectDataAttributeDefinitionEntity = existingAttributeDefinitionEntities.get(lowercaseAttributeName);
if (!attributeDefinition.isPublish().equals(businessObjectDataAttributeDefinitionEntity.getPublish())) {
// Update the business object attribute entity.
businessObjectDataAttributeDefinitionEntity.setPublish(attributeDefinition.isPublish());
}
// Add this entity to the list of business object definition attribute entities to be retained.
retainedAttributeDefinitionEntities.add(businessObjectDataAttributeDefinitionEntity);
} else {
// Create a new business object attribute entity.
BusinessObjectDataAttributeDefinitionEntity businessObjectDataAttributeDefinitionEntity = new BusinessObjectDataAttributeDefinitionEntity();
businessObjectFormatEntity.getAttributeDefinitions().add(businessObjectDataAttributeDefinitionEntity);
businessObjectDataAttributeDefinitionEntity.setBusinessObjectFormat(businessObjectFormatEntity);
businessObjectDataAttributeDefinitionEntity.setName(attributeDefinition.getName());
businessObjectDataAttributeDefinitionEntity.setPublish(BooleanUtils.isTrue(attributeDefinition.isPublish()));
// Add this entity to the list of the newly created business object definition attribute entities.
createdAttributeDefinitionEntities.add(businessObjectDataAttributeDefinitionEntity);
}
}
// Remove any of the currently existing attribute entities that did not get onto the retained entities list.
businessObjectFormatEntity.getAttributeDefinitions().retainAll(retainedAttributeDefinitionEntities);
// Add all of the newly created business object definition attribute entities.
businessObjectFormatEntity.getAttributeDefinitions().addAll(createdAttributeDefinitionEntities);
}
use of org.finra.herd.model.api.xml.AttributeDefinition in project herd by FINRAOS.
the class BusinessObjectFormatServiceImpl method updateBusinessObjectFormatAttributeDefinitions.
@NamespacePermission(fields = "#businessObjectFormatKey.namespace", permissions = NamespacePermissionEnum.WRITE)
@Override
public BusinessObjectFormat updateBusinessObjectFormatAttributeDefinitions(BusinessObjectFormatKey businessObjectFormatKey, BusinessObjectFormatAttributeDefinitionsUpdateRequest businessObjectFormatAttributeDefinitionsUpdateRequest) {
// Perform validation and trim the alternate key parameters.
businessObjectFormatHelper.validateBusinessObjectFormatKey(businessObjectFormatKey);
Assert.notNull(businessObjectFormatAttributeDefinitionsUpdateRequest, "A business object format attribute definitions update request is required.");
List<AttributeDefinition> attributeDefinitions = businessObjectFormatAttributeDefinitionsUpdateRequest.getAttributeDefinitions();
// Validate and trim optional attribute definitions. This is also going to trim the attribute definition names.
validateAndTrimBusinessObjectFormatAttributeDefinitionsHelper(attributeDefinitions);
// Retrieve and ensure that a business object format exists.
BusinessObjectFormatEntity businessObjectFormatEntity = businessObjectFormatDaoHelper.getBusinessObjectFormatEntity(businessObjectFormatKey);
// Update the business object format attributes
updateBusinessObjectFormatAttributeDefinitionsHelper(businessObjectFormatEntity, attributeDefinitions);
// Persist and refresh the entity.
businessObjectFormatEntity = businessObjectFormatDao.saveAndRefresh(businessObjectFormatEntity);
// Create and return the business object format object from the persisted entity.
return businessObjectFormatHelper.createBusinessObjectFormatFromEntity(businessObjectFormatEntity);
}
use of org.finra.herd.model.api.xml.AttributeDefinition in project herd by FINRAOS.
the class BusinessObjectFormatServiceTest method testUpdateBusinessObjectFormatAttributeDefinitions.
@Test
public void testUpdateBusinessObjectFormatAttributeDefinitions() {
List<Attribute> attributes = businessObjectDefinitionServiceTestHelper.getNewAttributes();
// Create an initial version of a business object format with format description and schema information.
// Attributes are passed rather attribute definations as this method also set attribute definition by default and no need to create another method
BusinessObjectFormat originalBusinessObjectFormat = businessObjectFormatServiceTestHelper.createTestBusinessObjectFormat(attributes);
List<AttributeDefinition> attributeDefinitions = businessObjectFormatServiceTestHelper.getTestAttributeDefinitions2();
BusinessObjectFormatAttributeDefinitionsUpdateRequest request = new BusinessObjectFormatAttributeDefinitionsUpdateRequest(attributeDefinitions);
// Perform an update by changing the description and schema.
BusinessObjectFormat updatedBusinessObjectFormat = businessObjectFormatService.updateBusinessObjectFormatAttributeDefinitions(new BusinessObjectFormatKey(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, INITIAL_FORMAT_VERSION), request);
// Validate the returned object.
businessObjectFormatServiceTestHelper.validateBusinessObjectFormat(originalBusinessObjectFormat.getId(), NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, INITIAL_FORMAT_VERSION, LATEST_VERSION_FLAG_SET, PARTITION_KEY, FORMAT_DESCRIPTION, attributes, attributeDefinitions, businessObjectFormatServiceTestHelper.getTestSchema(), updatedBusinessObjectFormat);
}
Aggregations