Search in sources :

Example 1 with SchemaColumnEntity

use of org.finra.herd.model.jpa.SchemaColumnEntity 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;
}
Also used : Attribute(org.finra.herd.model.api.xml.Attribute) SchemaColumnEntity(org.finra.herd.model.jpa.SchemaColumnEntity) Schema(org.finra.herd.model.api.xml.Schema) ArrayList(java.util.ArrayList) SchemaColumn(org.finra.herd.model.api.xml.SchemaColumn) AttributeDefinition(org.finra.herd.model.api.xml.AttributeDefinition) BusinessObjectFormat(org.finra.herd.model.api.xml.BusinessObjectFormat) BusinessObjectFormatEntity(org.finra.herd.model.jpa.BusinessObjectFormatEntity) BusinessObjectDataAttributeDefinitionEntity(org.finra.herd.model.jpa.BusinessObjectDataAttributeDefinitionEntity) BusinessObjectFormatKey(org.finra.herd.model.api.xml.BusinessObjectFormatKey) BusinessObjectFormatAttributeEntity(org.finra.herd.model.jpa.BusinessObjectFormatAttributeEntity)

Example 2 with SchemaColumnEntity

use of org.finra.herd.model.jpa.SchemaColumnEntity in project herd by FINRAOS.

the class BusinessObjectFormatServiceImpl method createSchemaColumnEntity.

/**
 * Creates a new schema column entity from the schema column.
 *
 * @param schemaColumn the schema column.
 * @param businessObjectFormatEntity the business object format entity to associated each newly created schema column entity with.
 *
 * @return the newly created schema column entity.
 */
private SchemaColumnEntity createSchemaColumnEntity(SchemaColumn schemaColumn, BusinessObjectFormatEntity businessObjectFormatEntity) {
    SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity();
    schemaColumnEntity.setBusinessObjectFormat(businessObjectFormatEntity);
    schemaColumnEntity.setName(schemaColumn.getName());
    schemaColumnEntity.setType(schemaColumn.getType());
    schemaColumnEntity.setSize(schemaColumn.getSize());
    schemaColumnEntity.setRequired(schemaColumn.isRequired());
    schemaColumnEntity.setDefaultValue(schemaColumn.getDefaultValue());
    schemaColumnEntity.setDescription(schemaColumn.getDescription());
    return schemaColumnEntity;
}
Also used : SchemaColumnEntity(org.finra.herd.model.jpa.SchemaColumnEntity)

Example 3 with SchemaColumnEntity

use of org.finra.herd.model.jpa.SchemaColumnEntity in project herd by FINRAOS.

the class BusinessObjectFormatServiceImpl method populateBusinessObjectFormatSchema.

/**
 * Adds business object schema information to the business object format entity.
 *
 * @param businessObjectFormatEntity the business object format entity
 * @param schema the schema from the business object format create request
 */
private void populateBusinessObjectFormatSchema(BusinessObjectFormatEntity businessObjectFormatEntity, Schema schema) {
    // Add optional schema information.
    if (schema != null) {
        // If optional partition key group value is specified, get the partition key group entity and ensure it exists.
        PartitionKeyGroupEntity partitionKeyGroupEntity = null;
        if (StringUtils.isNotBlank(schema.getPartitionKeyGroup())) {
            partitionKeyGroupEntity = partitionKeyGroupDaoHelper.getPartitionKeyGroupEntity(schema.getPartitionKeyGroup());
        }
        businessObjectFormatEntity.setNullValue(schema.getNullValue());
        businessObjectFormatEntity.setDelimiter(schema.getDelimiter());
        businessObjectFormatEntity.setEscapeCharacter(schema.getEscapeCharacter());
        businessObjectFormatEntity.setPartitionKeyGroup(partitionKeyGroupEntity);
        // Create a schema column entities collection, if needed.
        Collection<SchemaColumnEntity> schemaColumnEntities = businessObjectFormatEntity.getSchemaColumns();
        if (schemaColumnEntities == null) {
            schemaColumnEntities = new ArrayList<>();
            businessObjectFormatEntity.setSchemaColumns(schemaColumnEntities);
        }
        // Create a map that will easily let us keep track of schema column entities we're creating by their column name.
        Map<String, SchemaColumnEntity> schemaColumnMap = new HashMap<>();
        // Create the schema columns for both the data columns and then the partition columns.
        // Since both lists share the same schema column entity list, we will use a common method to process them in the same way.
        createSchemaColumnEntities(schema.getColumns(), false, schemaColumnEntities, schemaColumnMap, businessObjectFormatEntity);
        createSchemaColumnEntities(schema.getPartitions(), true, schemaColumnEntities, schemaColumnMap, businessObjectFormatEntity);
    }
}
Also used : SchemaColumnEntity(org.finra.herd.model.jpa.SchemaColumnEntity) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PartitionKeyGroupEntity(org.finra.herd.model.jpa.PartitionKeyGroupEntity)

Example 4 with SchemaColumnEntity

use of org.finra.herd.model.jpa.SchemaColumnEntity in project herd by FINRAOS.

the class BusinessObjectDataDaoImpl method createPartitionValueFilters.

/**
 * Creates a predicate for partition value filters.
 *
 * @param businessDataSearchKey businessDataSearchKey
 * @param businessObjectDataEntity businessObjectDataEntity
 * @param businessObjectFormatEntity businessObjectFormatEntity
 * @param builder builder
 * @param predicatePram predicate parameter
 *
 * @return the predicate
 */
private Predicate createPartitionValueFilters(BusinessObjectDataSearchKey businessDataSearchKey, Root<BusinessObjectDataEntity> businessObjectDataEntity, Join<BusinessObjectDataEntity, BusinessObjectFormatEntity> businessObjectFormatEntity, CriteriaBuilder builder, Predicate predicatePram) {
    Predicate predicate = predicatePram;
    if (businessDataSearchKey.getPartitionValueFilters() != null && !businessDataSearchKey.getPartitionValueFilters().isEmpty()) {
        for (PartitionValueFilter partitionFilter : businessDataSearchKey.getPartitionValueFilters()) {
            Join<BusinessObjectFormatEntity, SchemaColumnEntity> schemaEntity = businessObjectFormatEntity.join(BusinessObjectFormatEntity_.schemaColumns);
            List<String> partitionValues = partitionFilter.getPartitionValues();
            predicate = builder.and(predicate, builder.equal(builder.upper(schemaEntity.get(SchemaColumnEntity_.name)), partitionFilter.getPartitionKey().toUpperCase()));
            predicate = builder.and(predicate, builder.isNotNull(schemaEntity.get(SchemaColumnEntity_.partitionLevel)));
            if (partitionValues != null && !partitionValues.isEmpty()) {
                predicate = builder.and(predicate, builder.or(builder.and(builder.equal(schemaEntity.get(SchemaColumnEntity_.partitionLevel), 1), businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue).in(partitionValues)), builder.and(builder.equal(schemaEntity.get(SchemaColumnEntity_.partitionLevel), 2), businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue2).in(partitionValues)), builder.and(builder.equal(schemaEntity.get(SchemaColumnEntity_.partitionLevel), 3), businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue3).in(partitionValues)), builder.and(builder.equal(schemaEntity.get(SchemaColumnEntity_.partitionLevel), 4), businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue4).in(partitionValues)), builder.and(builder.equal(schemaEntity.get(SchemaColumnEntity_.partitionLevel), 5), businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue5).in(partitionValues))));
            } else if (partitionFilter.getPartitionValueRange() != null) {
                PartitionValueRange partitionRange = partitionFilter.getPartitionValueRange();
                String startPartitionValue = partitionRange.getStartPartitionValue();
                String endPartitionValue = partitionRange.getEndPartitionValue();
                predicate = builder.and(predicate, builder.or(builder.and(builder.equal(schemaEntity.get(SchemaColumnEntity_.partitionLevel), 1), builder.greaterThanOrEqualTo(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue), startPartitionValue), builder.lessThanOrEqualTo(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue), endPartitionValue)), builder.and(builder.equal(schemaEntity.get(SchemaColumnEntity_.partitionLevel), 2), builder.greaterThanOrEqualTo(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue2), startPartitionValue), builder.lessThanOrEqualTo(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue2), endPartitionValue)), builder.and(builder.equal(schemaEntity.get(SchemaColumnEntity_.partitionLevel), 3), builder.greaterThanOrEqualTo(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue3), startPartitionValue), builder.lessThanOrEqualTo(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue3), endPartitionValue)), builder.and(builder.equal(schemaEntity.get(SchemaColumnEntity_.partitionLevel), 4), builder.greaterThanOrEqualTo(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue4), startPartitionValue), builder.lessThanOrEqualTo(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue4), endPartitionValue)), builder.and(builder.equal(schemaEntity.get(SchemaColumnEntity_.partitionLevel), 5), builder.greaterThanOrEqualTo(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue5), startPartitionValue), builder.lessThanOrEqualTo(businessObjectDataEntity.get(BusinessObjectDataEntity_.partitionValue5), endPartitionValue))));
            }
        }
    }
    return predicate;
}
Also used : PartitionValueRange(org.finra.herd.model.api.xml.PartitionValueRange) SchemaColumnEntity(org.finra.herd.model.jpa.SchemaColumnEntity) BusinessObjectFormatEntity(org.finra.herd.model.jpa.BusinessObjectFormatEntity) PartitionValueFilter(org.finra.herd.model.api.xml.PartitionValueFilter) Predicate(javax.persistence.criteria.Predicate)

Example 5 with SchemaColumnEntity

use of org.finra.herd.model.jpa.SchemaColumnEntity in project herd by FINRAOS.

the class BusinessObjectFormatServiceTestHelper method createBusinessObjectFormat.

/**
 * Creates and persists {@link org.finra.herd.model.jpa.BusinessObjectFormatEntity} from the given request. Also creates and persists namespace, data
 * provider, bdef, and file type required for the format. If the request has sub-partitions, schema columns will be persisted. Otherwise, no schema will be
 * set for this format.
 *
 * @param request {@link org.finra.herd.model.api.xml.BusinessObjectDataInvalidateUnregisteredRequest} format alt key
 *
 * @return created {@link org.finra.herd.model.jpa.BusinessObjectFormatEntity}
 */
public BusinessObjectFormatEntity createBusinessObjectFormat(BusinessObjectDataInvalidateUnregisteredRequest request) {
    // Create namespace
    NamespaceEntity namespaceEntity = namespaceDaoTestHelper.createNamespaceEntity(request.getNamespace());
    // Create data provider with a name which is irrelevant for the test cases
    DataProviderEntity dataProviderEntity = dataProviderDaoTestHelper.createDataProviderEntity(AbstractServiceTest.DATA_PROVIDER_NAME);
    // Create business object definition
    BusinessObjectDefinitionEntity businessObjectDefinitionEntity = businessObjectDefinitionDaoTestHelper.createBusinessObjectDefinitionEntity(namespaceEntity, request.getBusinessObjectDefinitionName(), dataProviderEntity, AbstractServiceTest.NO_BDEF_DESCRIPTION, AbstractServiceTest.NO_BDEF_DISPLAY_NAME, AbstractServiceTest.NO_ATTRIBUTES, AbstractServiceTest.NO_SAMPLE_DATA_FILES);
    // Create file type
    FileTypeEntity fileTypeEntity = fileTypeDaoTestHelper.createFileTypeEntity(request.getBusinessObjectFormatFileType());
    // Manually creating format since it is easier than providing large amounts of params to existing method
    // Create format
    BusinessObjectFormatEntity businessObjectFormatEntity = new BusinessObjectFormatEntity();
    businessObjectFormatEntity.setBusinessObjectDefinition(businessObjectDefinitionEntity);
    businessObjectFormatEntity.setUsage(request.getBusinessObjectFormatUsage());
    businessObjectFormatEntity.setFileType(fileTypeEntity);
    businessObjectFormatEntity.setBusinessObjectFormatVersion(request.getBusinessObjectFormatVersion());
    // If sub-partition values exist in the request
    if (!CollectionUtils.isEmpty(request.getSubPartitionValues())) {
        // Create schema columns
        List<SchemaColumnEntity> schemaColumnEntities = new ArrayList<>();
        for (int partitionLevel = 0; partitionLevel < request.getSubPartitionValues().size() + 1; partitionLevel++) {
            SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity();
            schemaColumnEntity.setBusinessObjectFormat(businessObjectFormatEntity);
            schemaColumnEntity.setName(AbstractServiceTest.PARTITION_KEY + partitionLevel);
            schemaColumnEntity.setType("STRING");
            schemaColumnEntity.setPartitionLevel(partitionLevel);
            schemaColumnEntity.setPosition(partitionLevel);
            schemaColumnEntities.add(schemaColumnEntity);
        }
        businessObjectFormatEntity.setSchemaColumns(schemaColumnEntities);
        businessObjectFormatEntity.setPartitionKey(AbstractServiceTest.PARTITION_KEY + "0");
    } else // If sub-partition values do not exist in the request
    {
        businessObjectFormatEntity.setPartitionKey(AbstractServiceTest.PARTITION_KEY);
    }
    businessObjectFormatEntity.setLatestVersion(true);
    businessObjectFormatDao.saveAndRefresh(businessObjectFormatEntity);
    return businessObjectFormatEntity;
}
Also used : NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) DataProviderEntity(org.finra.herd.model.jpa.DataProviderEntity) FileTypeEntity(org.finra.herd.model.jpa.FileTypeEntity) SchemaColumnEntity(org.finra.herd.model.jpa.SchemaColumnEntity) BusinessObjectDefinitionEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionEntity) ArrayList(java.util.ArrayList) BusinessObjectFormatEntity(org.finra.herd.model.jpa.BusinessObjectFormatEntity)

Aggregations

SchemaColumnEntity (org.finra.herd.model.jpa.SchemaColumnEntity)14 BusinessObjectFormatEntity (org.finra.herd.model.jpa.BusinessObjectFormatEntity)8 ArrayList (java.util.ArrayList)4 SchemaColumn (org.finra.herd.model.api.xml.SchemaColumn)3 BusinessObjectDefinitionColumnEntity (org.finra.herd.model.jpa.BusinessObjectDefinitionColumnEntity)3 BusinessObjectDefinitionEntity (org.finra.herd.model.jpa.BusinessObjectDefinitionEntity)3 Test (org.junit.Test)3 Predicate (javax.persistence.criteria.Predicate)2 NamespacePermission (org.finra.herd.model.annotation.NamespacePermission)2 Attribute (org.finra.herd.model.api.xml.Attribute)2 BusinessObjectFormatDdlRequest (org.finra.herd.model.api.xml.BusinessObjectFormatDdlRequest)2 BusinessObjectFormatAttributeEntity (org.finra.herd.model.jpa.BusinessObjectFormatAttributeEntity)2 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 AlreadyExistsException (org.finra.herd.model.AlreadyExistsException)1 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)1 AttributeDefinition (org.finra.herd.model.api.xml.AttributeDefinition)1 BusinessObjectDefinitionColumn (org.finra.herd.model.api.xml.BusinessObjectDefinitionColumn)1