Search in sources :

Example 26 with PartitionKeyGroupEntity

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

the class ExpectedPartitionValueDaoTest method testGetExpectedPartitionValueWithOffsetExpectedPartitionValueNoExists.

@Test
public void testGetExpectedPartitionValueWithOffsetExpectedPartitionValueNoExists() {
    // Create and persist a partition key group entity.
    PartitionKeyGroupEntity partitionKeyGroupEntity = partitionKeyGroupDaoTestHelper.createPartitionKeyGroupEntity(PARTITION_KEY_GROUP);
    // Create and persist a single test expected partition value.
    expectedPartitionValueDaoTestHelper.createExpectedPartitionValueEntities(partitionKeyGroupEntity, Arrays.asList(PARTITION_VALUE));
    // Validate that we get null back when passing an existing expected partition value but giving an invalid offset.
    for (Integer offset : Arrays.asList(-1, 1)) {
        assertNull(expectedPartitionValueDao.getExpectedPartitionValue(new ExpectedPartitionValueKey(PARTITION_KEY_GROUP, PARTITION_VALUE), offset));
    }
}
Also used : PartitionKeyGroupEntity(org.finra.herd.model.jpa.PartitionKeyGroupEntity) ExpectedPartitionValueKey(org.finra.herd.model.api.xml.ExpectedPartitionValueKey) Test(org.junit.Test)

Example 27 with PartitionKeyGroupEntity

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

the class ExpectedPartitionValueDaoImpl method getExpectedPartitionValue.

@Override
public ExpectedPartitionValueEntity getExpectedPartitionValue(ExpectedPartitionValueKey expectedPartitionValueKey, int offset) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ExpectedPartitionValueEntity> criteria = builder.createQuery(ExpectedPartitionValueEntity.class);
    // The criteria root is the expected partition value.
    Root<ExpectedPartitionValueEntity> expectedPartitionValueEntity = criteria.from(ExpectedPartitionValueEntity.class);
    // Join to the other tables we can filter on.
    Join<ExpectedPartitionValueEntity, PartitionKeyGroupEntity> partitionKeyGroupEntity = expectedPartitionValueEntity.join(ExpectedPartitionValueEntity_.partitionKeyGroup);
    // Add a restriction to filter case insensitive groups that match the user specified group.
    Predicate whereRestriction = builder.equal(builder.upper(partitionKeyGroupEntity.get(PartitionKeyGroupEntity_.partitionKeyGroupName)), expectedPartitionValueKey.getPartitionKeyGroupName().toUpperCase());
    // Depending on the offset, we might need to order the records in the query.
    Order orderByExpectedPartitionValue = null;
    // Add additional restrictions to handle expected partition value and an optional offset.
    if (offset == 0) {
        // Since there is no offset, we need to match the expected partition value exactly.
        whereRestriction = builder.and(whereRestriction, builder.equal(expectedPartitionValueEntity.get(ExpectedPartitionValueEntity_.partitionValue), expectedPartitionValueKey.getExpectedPartitionValue()));
    } else if (offset > 0) {
        // For a positive offset value, add a restriction to filter expected partition values that are >= the user specified expected partition value.
        whereRestriction = builder.and(whereRestriction, builder.greaterThanOrEqualTo(expectedPartitionValueEntity.get(ExpectedPartitionValueEntity_.partitionValue), expectedPartitionValueKey.getExpectedPartitionValue()));
        // Order by expected partition value in ascending order.
        orderByExpectedPartitionValue = builder.asc(expectedPartitionValueEntity.get(ExpectedPartitionValueEntity_.partitionValue));
    } else {
        // For a negative offset value, add a restriction to filter expected partition values that are <= the user specified expected partition value.
        whereRestriction = builder.and(whereRestriction, builder.lessThanOrEqualTo(expectedPartitionValueEntity.get(ExpectedPartitionValueEntity_.partitionValue), expectedPartitionValueKey.getExpectedPartitionValue()));
        // Order by expected partition value in descending order.
        orderByExpectedPartitionValue = builder.desc(expectedPartitionValueEntity.get(ExpectedPartitionValueEntity_.partitionValue));
    }
    // Add the clauses for the query and execute the query.
    if (offset == 0) {
        criteria.select(expectedPartitionValueEntity).where(whereRestriction);
        return executeSingleResultQuery(criteria, String.format("Found more than one expected partition value with parameters {partitionKeyGroupName=\"%s\", expectedPartitionValue=\"%s\"}.", expectedPartitionValueKey.getPartitionKeyGroupName(), expectedPartitionValueKey.getExpectedPartitionValue()));
    } else {
        criteria.select(expectedPartitionValueEntity).where(whereRestriction).orderBy(orderByExpectedPartitionValue);
        List<ExpectedPartitionValueEntity> resultList = entityManager.createQuery(criteria).setFirstResult(Math.abs(offset)).setMaxResults(1).getResultList();
        return resultList.size() > 0 ? resultList.get(0) : null;
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) ExpectedPartitionValueEntity(org.finra.herd.model.jpa.ExpectedPartitionValueEntity) PartitionKeyGroupEntity(org.finra.herd.model.jpa.PartitionKeyGroupEntity) Predicate(javax.persistence.criteria.Predicate)

Example 28 with PartitionKeyGroupEntity

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

the class ExpectedPartitionValueDaoImpl method getExpectedPartitionValuesByGroupAndRange.

@Override
public List<ExpectedPartitionValueEntity> getExpectedPartitionValuesByGroupAndRange(String partitionKeyGroupName, PartitionValueRange partitionValueRange) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ExpectedPartitionValueEntity> criteria = builder.createQuery(ExpectedPartitionValueEntity.class);
    // The criteria root is the expected partition value.
    Root<ExpectedPartitionValueEntity> expectedPartitionValueEntity = criteria.from(ExpectedPartitionValueEntity.class);
    // Join to the other tables we can filter on.
    Join<ExpectedPartitionValueEntity, PartitionKeyGroupEntity> partitionKeyGroupEntity = expectedPartitionValueEntity.join(ExpectedPartitionValueEntity_.partitionKeyGroup);
    // Add a restriction to filter case insensitive groups that match the user specified group.
    Predicate whereRestriction = builder.equal(builder.upper(partitionKeyGroupEntity.get(PartitionKeyGroupEntity_.partitionKeyGroupName)), partitionKeyGroupName.toUpperCase());
    // If we have a possible partition value range, we need to add additional restrictions.
    if (partitionValueRange != null) {
        // Add a restriction to filter values that are >= the user specified range start value.
        if (StringUtils.isNotBlank(partitionValueRange.getStartPartitionValue())) {
            whereRestriction = builder.and(whereRestriction, builder.greaterThanOrEqualTo(expectedPartitionValueEntity.get(ExpectedPartitionValueEntity_.partitionValue), partitionValueRange.getStartPartitionValue()));
        }
        // Add a restriction to filter values that are <= the user specified range end value.
        if (StringUtils.isNotBlank(partitionValueRange.getEndPartitionValue())) {
            whereRestriction = builder.and(whereRestriction, builder.lessThanOrEqualTo(expectedPartitionValueEntity.get(ExpectedPartitionValueEntity_.partitionValue), partitionValueRange.getEndPartitionValue()));
        }
    }
    // Order the results by partition value.
    Order orderByValue = builder.asc(expectedPartitionValueEntity.get(ExpectedPartitionValueEntity_.partitionValue));
    // Add the clauses for the query.
    criteria.select(expectedPartitionValueEntity).where(whereRestriction).orderBy(orderByValue);
    // Execute the query and return the results.
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) ExpectedPartitionValueEntity(org.finra.herd.model.jpa.ExpectedPartitionValueEntity) PartitionKeyGroupEntity(org.finra.herd.model.jpa.PartitionKeyGroupEntity) Predicate(javax.persistence.criteria.Predicate)

Example 29 with PartitionKeyGroupEntity

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

the class PartitionKeyGroupDaoImpl method getPartitionKeyGroups.

@Override
public List<PartitionKeyGroupKey> getPartitionKeyGroups() {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<PartitionKeyGroupEntity> criteria = builder.createQuery(PartitionKeyGroupEntity.class);
    // The criteria root is the partition key group.
    Root<PartitionKeyGroupEntity> partitionKeyGroupEntity = criteria.from(PartitionKeyGroupEntity.class);
    criteria.select(partitionKeyGroupEntity);
    List<PartitionKeyGroupKey> partitionKeyGroupKeys = new ArrayList<>();
    for (PartitionKeyGroupEntity entity : entityManager.createQuery(criteria).getResultList()) {
        PartitionKeyGroupKey partitionKeyGroupKey = new PartitionKeyGroupKey();
        partitionKeyGroupKey.setPartitionKeyGroupName(entity.getPartitionKeyGroupName());
        partitionKeyGroupKeys.add(partitionKeyGroupKey);
    }
    return partitionKeyGroupKeys;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ArrayList(java.util.ArrayList) PartitionKeyGroupEntity(org.finra.herd.model.jpa.PartitionKeyGroupEntity) PartitionKeyGroupKey(org.finra.herd.model.api.xml.PartitionKeyGroupKey)

Example 30 with PartitionKeyGroupEntity

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

the class ExpectedPartitionValueServiceTest method testGetExpectedPartitionValueOffsetExpectedPartitionValueNoExists.

@Test
public void testGetExpectedPartitionValueOffsetExpectedPartitionValueNoExists() {
    // Create and persist a partition key group entity.
    PartitionKeyGroupEntity partitionKeyGroupEntity = partitionKeyGroupDaoTestHelper.createPartitionKeyGroupEntity(PARTITION_KEY_GROUP);
    // Create and persist a single test expected partition value.
    expectedPartitionValueDaoTestHelper.createExpectedPartitionValueEntities(partitionKeyGroupEntity, Arrays.asList(PARTITION_VALUE));
    // Try to get a non-existing expected partition value by passing an existing expected partition value but giving an invalid offset.
    for (Integer offset : Arrays.asList(-1, 1)) {
        try {
            expectedPartitionValueService.getExpectedPartitionValue(new ExpectedPartitionValueKey(PARTITION_KEY_GROUP, PARTITION_VALUE), offset);
            fail("Should throw an IllegalArgumentException when the expected partition value does not exist.");
        } catch (ObjectNotFoundException e) {
            assertEquals(String.format("Expected partition value \"%s\" with offset %d doesn't exist in \"%s\" partition key group.", PARTITION_VALUE, offset, PARTITION_KEY_GROUP), e.getMessage());
        }
    }
}
Also used : ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) PartitionKeyGroupEntity(org.finra.herd.model.jpa.PartitionKeyGroupEntity) ExpectedPartitionValueKey(org.finra.herd.model.api.xml.ExpectedPartitionValueKey) Test(org.junit.Test)

Aggregations

PartitionKeyGroupEntity (org.finra.herd.model.jpa.PartitionKeyGroupEntity)40 Test (org.junit.Test)25 ExpectedPartitionValueKey (org.finra.herd.model.api.xml.ExpectedPartitionValueKey)9 PartitionKeyGroupKey (org.finra.herd.model.api.xml.PartitionKeyGroupKey)9 ExpectedPartitionValuesInformation (org.finra.herd.model.api.xml.ExpectedPartitionValuesInformation)8 ExpectedPartitionValueEntity (org.finra.herd.model.jpa.ExpectedPartitionValueEntity)7 ArrayList (java.util.ArrayList)6 ExpectedPartitionValueInformation (org.finra.herd.model.api.xml.ExpectedPartitionValueInformation)6 PartitionValueRange (org.finra.herd.model.api.xml.PartitionValueRange)6 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 HashMap (java.util.HashMap)3 Predicate (javax.persistence.criteria.Predicate)3 AlreadyExistsException (org.finra.herd.model.AlreadyExistsException)3 ExpectedPartitionValuesDeleteRequest (org.finra.herd.model.api.xml.ExpectedPartitionValuesDeleteRequest)3 Order (javax.persistence.criteria.Order)2 FieldExtension (org.activiti.bpmn.model.FieldExtension)2 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)2 ExpectedPartitionValuesCreateRequest (org.finra.herd.model.api.xml.ExpectedPartitionValuesCreateRequest)2 Parameter (org.finra.herd.model.api.xml.Parameter)2 BusinessObjectDefinitionEntity (org.finra.herd.model.jpa.BusinessObjectDefinitionEntity)2