use of org.finra.herd.model.jpa.BusinessObjectFormatAttributeEntity 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.jpa.BusinessObjectFormatAttributeEntity in project herd by FINRAOS.
the class BusinessObjectFormatDaoTestHelper method createBusinessObjectFormatEntity.
/**
* Creates and persists a new business object format entity.
*
* @return the newly created business object format entity.
*/
public BusinessObjectFormatEntity createBusinessObjectFormatEntity(BusinessObjectDefinitionEntity businessObjectDefinitionEntity, String businessObjectFormatUsage, FileTypeEntity fileTypeEntity, Integer businessObjectFormatVersion, String businessObjectFormatDescription, Boolean businessObjectFormatLatestVersion, String businessObjectFormatPartitionKey, PartitionKeyGroupEntity partitionKeyGroupEntity, List<Attribute> attributes, String schemaDelimiterCharacter, String schemaEscapeCharacter, String schemaNullValue, List<SchemaColumn> schemaColumns, List<SchemaColumn> partitionColumns) {
BusinessObjectFormatEntity businessObjectFormatEntity = new BusinessObjectFormatEntity();
businessObjectFormatEntity.setBusinessObjectDefinition(businessObjectDefinitionEntity);
businessObjectFormatEntity.setDescription(businessObjectFormatDescription);
businessObjectFormatEntity.setFileType(fileTypeEntity);
businessObjectFormatEntity.setBusinessObjectFormatVersion(businessObjectFormatVersion);
businessObjectFormatEntity.setLatestVersion(businessObjectFormatLatestVersion);
businessObjectFormatEntity.setUsage(businessObjectFormatUsage);
businessObjectFormatEntity.setPartitionKey(businessObjectFormatPartitionKey);
businessObjectFormatEntity.setPartitionKeyGroup(partitionKeyGroupEntity);
// Create the attributes if they are specified.
if (!CollectionUtils.isEmpty(attributes)) {
List<BusinessObjectFormatAttributeEntity> attributeEntities = new ArrayList<>();
businessObjectFormatEntity.setAttributes(attributeEntities);
for (Attribute attribute : attributes) {
BusinessObjectFormatAttributeEntity attributeEntity = new BusinessObjectFormatAttributeEntity();
attributeEntities.add(attributeEntity);
attributeEntity.setBusinessObjectFormat(businessObjectFormatEntity);
attributeEntity.setName(attribute.getName());
attributeEntity.setValue(attribute.getValue());
}
}
if (schemaColumns != null && !schemaColumns.isEmpty()) {
businessObjectFormatEntity.setDelimiter(schemaDelimiterCharacter);
businessObjectFormatEntity.setEscapeCharacter(schemaEscapeCharacter);
businessObjectFormatEntity.setNullValue(schemaNullValue);
List<SchemaColumnEntity> schemaColumnEntities = new ArrayList<>();
businessObjectFormatEntity.setSchemaColumns(schemaColumnEntities);
int columnPosition = 1;
for (SchemaColumn schemaColumn : schemaColumns) {
SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity();
schemaColumnEntities.add(schemaColumnEntity);
schemaColumnEntity.setBusinessObjectFormat(businessObjectFormatEntity);
schemaColumnEntity.setPosition(columnPosition);
schemaColumnEntity.setPartitionLevel(null);
schemaColumnEntity.setName(schemaColumn.getName());
schemaColumnEntity.setType(schemaColumn.getType());
schemaColumnEntity.setSize(schemaColumn.getSize());
schemaColumnEntity.setDescription(schemaColumn.getDescription());
schemaColumnEntity.setRequired(schemaColumn.isRequired());
schemaColumnEntity.setDefaultValue(schemaColumn.getDefaultValue());
columnPosition++;
}
if (partitionColumns != null && !partitionColumns.isEmpty()) {
int partitionLevel = 1;
for (SchemaColumn schemaColumn : partitionColumns) {
// Check if this partition column belongs to the list of regular schema columns.
int schemaColumnIndex = schemaColumns.indexOf(schemaColumn);
if (schemaColumnIndex >= 0) {
// Retrieve the relative column entity and set its partition level.
schemaColumnEntities.get(schemaColumnIndex).setPartitionLevel(partitionLevel);
} else {
// Add this partition column as a new schema column entity.
SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity();
schemaColumnEntities.add(schemaColumnEntity);
schemaColumnEntity.setBusinessObjectFormat(businessObjectFormatEntity);
schemaColumnEntity.setPosition(null);
schemaColumnEntity.setPartitionLevel(partitionLevel);
schemaColumnEntity.setName(schemaColumn.getName());
schemaColumnEntity.setType(schemaColumn.getType());
schemaColumnEntity.setSize(schemaColumn.getSize());
schemaColumnEntity.setDescription(schemaColumn.getDescription());
schemaColumnEntity.setRequired(schemaColumn.isRequired());
schemaColumnEntity.setDefaultValue(schemaColumn.getDefaultValue());
}
partitionLevel++;
}
}
}
return businessObjectFormatDao.saveAndRefresh(businessObjectFormatEntity);
}
use of org.finra.herd.model.jpa.BusinessObjectFormatAttributeEntity in project herd by FINRAOS.
the class BusinessObjectFormatServiceImpl method createBusinessObjectFormatEntity.
/**
* Creates and persists a new business object format entity from the request information.
*
* @param request the request.
* @param businessObjectFormatEntityParents parent business object format entity
*
* @return the newly created business object format entity.
*/
private BusinessObjectFormatEntity createBusinessObjectFormatEntity(BusinessObjectFormatCreateRequest request, BusinessObjectDefinitionEntity businessObjectDefinitionEntity, FileTypeEntity fileTypeEntity, Integer businessObjectFormatVersion, List<BusinessObjectFormatEntity> businessObjectFormatEntityParents) {
BusinessObjectFormatEntity businessObjectFormatEntity = new BusinessObjectFormatEntity();
businessObjectFormatEntity.setBusinessObjectDefinition(businessObjectDefinitionEntity);
businessObjectFormatEntity.setUsage(request.getBusinessObjectFormatUsage());
businessObjectFormatEntity.setFileType(fileTypeEntity);
businessObjectFormatEntity.setBusinessObjectFormatVersion(businessObjectFormatVersion);
businessObjectFormatEntity.setLatestVersion(Boolean.TRUE);
businessObjectFormatEntity.setPartitionKey(request.getPartitionKey());
businessObjectFormatEntity.setDescription(request.getDescription());
// Create the attributes if they are specified.
if (!CollectionUtils.isEmpty(request.getAttributes())) {
List<BusinessObjectFormatAttributeEntity> attributeEntities = new ArrayList<>();
businessObjectFormatEntity.setAttributes(attributeEntities);
for (Attribute attribute : request.getAttributes()) {
BusinessObjectFormatAttributeEntity attributeEntity = new BusinessObjectFormatAttributeEntity();
attributeEntities.add(attributeEntity);
attributeEntity.setBusinessObjectFormat(businessObjectFormatEntity);
attributeEntity.setName(attribute.getName());
attributeEntity.setValue(attribute.getValue());
}
}
businessObjectFormatEntity.setAttributeDefinitions(createAttributeDefinitionEntities(request.getAttributeDefinitions(), businessObjectFormatEntity));
// Add optional schema information.
populateBusinessObjectFormatSchema(businessObjectFormatEntity, request.getSchema());
// set the parents
businessObjectFormatEntity.setBusinessObjectFormatParents(businessObjectFormatEntityParents);
// Persist and return the new entity.
return businessObjectFormatDao.saveAndRefresh(businessObjectFormatEntity);
}
use of org.finra.herd.model.jpa.BusinessObjectFormatAttributeEntity in project herd by FINRAOS.
the class BusinessObjectFormatServiceImpl method updateBusinessObjectFormatAttributesHelper.
/**
* Updates business object format attributes
*
* @param businessObjectFormatEntity the business object format entity
* @param attributes the attributes
*/
private void updateBusinessObjectFormatAttributesHelper(BusinessObjectFormatEntity businessObjectFormatEntity, List<Attribute> attributes) {
// Update the attributes.
// Load all existing attribute entities in a map with a "lowercase" attribute name as the key for case insensitivity.
Map<String, BusinessObjectFormatAttributeEntity> existingAttributeEntities = new HashMap<>();
for (BusinessObjectFormatAttributeEntity attributeEntity : businessObjectFormatEntity.getAttributes()) {
String mapKey = attributeEntity.getName().toLowerCase();
if (existingAttributeEntities.containsKey(mapKey)) {
throw new IllegalStateException(String.format("Found duplicate attribute with name \"%s\" for business object format {%s}.", mapKey, businessObjectFormatHelper.businessObjectFormatEntityAltKeyToString(businessObjectFormatEntity)));
}
existingAttributeEntities.put(mapKey, attributeEntity);
}
// Process the list of attributes to determine that business object definition attribute entities should be created, updated, or deleted.
List<BusinessObjectFormatAttributeEntity> createdAttributeEntities = new ArrayList<>();
List<BusinessObjectFormatAttributeEntity> retainedAttributeEntities = new ArrayList<>();
if (!CollectionUtils.isEmpty(attributes)) {
for (Attribute attribute : attributes) {
// Use a "lowercase" attribute name for case insensitivity.
String lowercaseAttributeName = attribute.getName().toLowerCase();
if (existingAttributeEntities.containsKey(lowercaseAttributeName)) {
// Check if the attribute value needs to be updated.
BusinessObjectFormatAttributeEntity attributeEntity = existingAttributeEntities.get(lowercaseAttributeName);
if (!StringUtils.equals(attribute.getValue(), attributeEntity.getValue())) {
// Update the business object attribute entity.
attributeEntity.setValue(attribute.getValue());
}
// Add this entity to the list of business object definition attribute entities to be retained.
retainedAttributeEntities.add(attributeEntity);
} else {
// Create a new business object attribute entity.
BusinessObjectFormatAttributeEntity attributeEntity = new BusinessObjectFormatAttributeEntity();
businessObjectFormatEntity.getAttributes().add(attributeEntity);
attributeEntity.setBusinessObjectFormat(businessObjectFormatEntity);
attributeEntity.setName(attribute.getName());
attributeEntity.setValue(attribute.getValue());
// Add this entity to the list of the newly created business object definition attribute entities.
createdAttributeEntities.add(attributeEntity);
}
}
}
// Remove any of the currently existing attribute entities that did not get onto the retained entities list.
businessObjectFormatEntity.getAttributes().retainAll(retainedAttributeEntities);
// Add all of the newly created business object definition attribute entities.
businessObjectFormatEntity.getAttributes().addAll(createdAttributeEntities);
}
Aggregations