Search in sources :

Example 6 with SchemaColumnEntity

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

the class SchemaColumnDaoImpl method getSchemaColumns.

@Override
public List<SchemaColumnEntity> getSchemaColumns(BusinessObjectDefinitionEntity businessObjectDefinitionEntity, String schemaColumnName) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<SchemaColumnEntity> criteria = builder.createQuery(SchemaColumnEntity.class);
    // The criteria root is the schema column.
    Root<SchemaColumnEntity> schemaColumnEntityRoot = criteria.from(SchemaColumnEntity.class);
    // Join to the other tables we can filter on.
    Join<SchemaColumnEntity, BusinessObjectFormatEntity> businessObjectFormatEntityJoin = schemaColumnEntityRoot.join(SchemaColumnEntity_.businessObjectFormat);
    // Create the standard restrictions (i.e. the standard where clauses).
    List<Predicate> predicates = new ArrayList<>();
    if (businessObjectDefinitionEntity.getDescriptiveBusinessObjectFormat() == null) {
        predicates.add(builder.equal(businessObjectFormatEntityJoin.get(BusinessObjectFormatEntity_.businessObjectDefinition), businessObjectDefinitionEntity));
    } else {
        predicates.add(builder.equal(businessObjectFormatEntityJoin, businessObjectDefinitionEntity.getDescriptiveBusinessObjectFormat()));
    }
    predicates.add(builder.equal(builder.upper(schemaColumnEntityRoot.get(SchemaColumnEntity_.name)), schemaColumnName.toUpperCase()));
    // Add the clauses for the query.
    criteria.select(schemaColumnEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()])));
    // Execute the query and return the results.
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) SchemaColumnEntity(org.finra.herd.model.jpa.SchemaColumnEntity) ArrayList(java.util.ArrayList) BusinessObjectFormatEntity(org.finra.herd.model.jpa.BusinessObjectFormatEntity) Predicate(javax.persistence.criteria.Predicate)

Example 7 with SchemaColumnEntity

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

the class Hive13DdlGeneratorTest method testGenerateReplaceColumnsStatement.

/**
 * Asserts that generateReplaceColumnsStatement() generates the correct DDL statement.
 */
@Test
public void testGenerateReplaceColumnsStatement() {
    BusinessObjectFormatDdlRequest businessObjectFormatDdlRequest = new BusinessObjectFormatDdlRequest();
    businessObjectFormatDdlRequest.setTableName(TABLE_NAME);
    BusinessObjectFormatEntity businessObjectFormatEntity = businessObjectFormatDaoTestHelper.createBusinessObjectFormatEntity(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, FORMAT_DESCRIPTION, true, PARTITION_KEY);
    {
        SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity();
        schemaColumnEntity.setPosition(0);
        schemaColumnEntity.setName("col1");
        schemaColumnEntity.setType("varchar");
        schemaColumnEntity.setSize("255");
        schemaColumnEntity.setDescription("lorem ipsum");
        businessObjectFormatEntity.getSchemaColumns().add(schemaColumnEntity);
    }
    {
        SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity();
        schemaColumnEntity.setPosition(1);
        schemaColumnEntity.setName("col2");
        schemaColumnEntity.setType("date");
        businessObjectFormatEntity.getSchemaColumns().add(schemaColumnEntity);
    }
    String actual = hive13DdlGenerator.generateReplaceColumnsStatement(businessObjectFormatDdlRequest, businessObjectFormatEntity);
    String expected = "ALTER TABLE `" + businessObjectFormatDdlRequest.getTableName() + "` REPLACE COLUMNS (\n" + "    `col1` VARCHAR(255) COMMENT 'lorem ipsum',\n" + "    `col2` DATE);";
    Assert.assertEquals("generated DDL", expected, actual);
}
Also used : SchemaColumnEntity(org.finra.herd.model.jpa.SchemaColumnEntity) BusinessObjectFormatDdlRequest(org.finra.herd.model.api.xml.BusinessObjectFormatDdlRequest) BusinessObjectFormatEntity(org.finra.herd.model.jpa.BusinessObjectFormatEntity) Test(org.junit.Test) AbstractServiceTest(org.finra.herd.service.AbstractServiceTest)

Example 8 with SchemaColumnEntity

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

Example 9 with SchemaColumnEntity

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

the class SchemaColumnDaoTestHelper method createSchemaColumnEntity.

/**
 * Creates and persists a new schema column entity.
 *
 * @param businessObjectFormatEntity the business object format entity
 * @param columnName the name of the schema column
 * @param businessObjectDefinitionColumnEntity the business object definition column entity
 *
 * @return the newly created schema column entity
 */
public SchemaColumnEntity createSchemaColumnEntity(BusinessObjectFormatEntity businessObjectFormatEntity, String columnName, BusinessObjectDefinitionColumnEntity businessObjectDefinitionColumnEntity) {
    SchemaColumnEntity schemaColumnEntity = new SchemaColumnEntity();
    schemaColumnEntity.setBusinessObjectFormat(businessObjectFormatEntity);
    schemaColumnEntity.setName(columnName);
    schemaColumnEntity.setType(AbstractDaoTest.COLUMN_DATA_TYPE);
    schemaColumnEntity.setBusinessObjectDefinitionColumn(businessObjectDefinitionColumnEntity);
    return schemaColumnDao.saveAndRefresh(schemaColumnEntity);
}
Also used : SchemaColumnEntity(org.finra.herd.model.jpa.SchemaColumnEntity)

Example 10 with SchemaColumnEntity

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

the class BusinessObjectFormatServiceImpl method createSchemaColumnEntities.

/**
 * Creates the schema column entities.
 *
 * @param schemaColumns the list of schema columns (for either the data columns or the partition columns).
 * @param isPartitionList A flag to specify whether the list of schema columns is a data column or a partition column list.
 * @param schemaColumnEntityList the list of schema column entities we're creating. This method will add a new one to the list if it hasn't been created
 * before.
 * @param schemaColumnEntityMap a map of schema column entity names to the schema column entity. This is used so we don't have to keep searching the
 * schemaColumnEntityList which is less efficient.
 * @param businessObjectFormatEntity the business object format entity to associated each newly created schema column entity with.
 */
private void createSchemaColumnEntities(List<SchemaColumn> schemaColumns, boolean isPartitionList, Collection<SchemaColumnEntity> schemaColumnEntityList, Map<String, SchemaColumnEntity> schemaColumnEntityMap, BusinessObjectFormatEntity businessObjectFormatEntity) {
    // Create the relative database entities if schema columns are specified.
    if (!CollectionUtils.isEmpty(schemaColumns)) {
        // Initialize a position counter since the order we encounter the schema columns is the order we will set on the entity.
        int position = 1;
        // Loop through each schema column in the list.
        for (SchemaColumn schemaColumn : schemaColumns) {
            // See if we created the schema column already. If not, create it and add it to the map and the collection.
            SchemaColumnEntity schemaColumnEntity = schemaColumnEntityMap.get(schemaColumn.getName());
            if (schemaColumnEntity == null) {
                schemaColumnEntity = createSchemaColumnEntity(schemaColumn, businessObjectFormatEntity);
                schemaColumnEntityList.add(schemaColumnEntity);
                schemaColumnEntityMap.put(schemaColumn.getName(), schemaColumnEntity);
            }
            // Set the position or partition level depending on the type of object we're processing.
            if (isPartitionList) {
                schemaColumnEntity.setPartitionLevel(position++);
            } else {
                schemaColumnEntity.setPosition(position++);
            }
        }
    }
}
Also used : SchemaColumnEntity(org.finra.herd.model.jpa.SchemaColumnEntity) SchemaColumn(org.finra.herd.model.api.xml.SchemaColumn)

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