Search in sources :

Example 6 with ExpectedPartitionValueEntity

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

the class ExpectedPartitionValueDaoTest method testGetExpectedPartitionValue.

@Test
public void testGetExpectedPartitionValue() {
    // Create and persist a partition key group entity.
    PartitionKeyGroupEntity partitionKeyGroupEntity = partitionKeyGroupDaoTestHelper.createPartitionKeyGroupEntity(PARTITION_KEY_GROUP);
    // Create and persist a list of test expected partition values.
    expectedPartitionValueDaoTestHelper.createExpectedPartitionValueEntities(partitionKeyGroupEntity, expectedPartitionValueDaoTestHelper.getTestUnsortedExpectedPartitionValues());
    // Get expected partition value for different offset values.
    List<String> testSortedExpectedPartitionValues = expectedPartitionValueDaoTestHelper.getTestSortedExpectedPartitionValues();
    int testExpectedPartitionValueIndex = 3;
    for (Integer offset : Arrays.asList(-2, 0, 2)) {
        ExpectedPartitionValueEntity resultExpectedPartitionValueEntity = expectedPartitionValueDao.getExpectedPartitionValue(new ExpectedPartitionValueKey(PARTITION_KEY_GROUP, testSortedExpectedPartitionValues.get(testExpectedPartitionValueIndex)), offset);
        // Validate the returned object.
        resultExpectedPartitionValueEntity.getPartitionValue().equals(testSortedExpectedPartitionValues.get(testExpectedPartitionValueIndex + offset));
    }
}
Also used : ExpectedPartitionValueEntity(org.finra.herd.model.jpa.ExpectedPartitionValueEntity) PartitionKeyGroupEntity(org.finra.herd.model.jpa.PartitionKeyGroupEntity) ExpectedPartitionValueKey(org.finra.herd.model.api.xml.ExpectedPartitionValueKey) Test(org.junit.Test)

Example 7 with ExpectedPartitionValueEntity

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

the class ExpectedPartitionValueDaoTestHelper method createExpectedPartitionValueEntities.

/**
 * Creates and persists expected partition value entities.
 *
 * @param partitionKeyGroupEntity the partition key group entity
 * @param expectedPartitionValues the list of expected partition value entities
 */
public void createExpectedPartitionValueEntities(PartitionKeyGroupEntity partitionKeyGroupEntity, List<String> expectedPartitionValues) {
    for (String expectedPartitionValue : expectedPartitionValues) {
        ExpectedPartitionValueEntity expectedPartitionValueEntity = new ExpectedPartitionValueEntity();
        expectedPartitionValueEntity.setPartitionKeyGroup(partitionKeyGroupEntity);
        expectedPartitionValueEntity.setPartitionValue(expectedPartitionValue);
        expectedPartitionValueDao.saveAndRefresh(expectedPartitionValueEntity);
    }
    partitionKeyGroupDao.saveAndRefresh(partitionKeyGroupEntity);
    assertEquals(expectedPartitionValues.size(), partitionKeyGroupEntity.getExpectedPartitionValues().size());
}
Also used : ExpectedPartitionValueEntity(org.finra.herd.model.jpa.ExpectedPartitionValueEntity)

Example 8 with ExpectedPartitionValueEntity

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

the class ExpectedPartitionValueDaoTest method testGetExpectedPartitionValuesByGroupAndNoRange.

/**
 * Test DAO method to retrieve expected partition values with no range (specified 2 ways). In the month of April, 2014, the number of values (i.e.
 * non-weekend days) is 22.
 */
@Test
public void testGetExpectedPartitionValuesByGroupAndNoRange() {
    expectedPartitionValueDaoTestHelper.createExpectedPartitionValueProcessDatesForApril2014(PARTITION_KEY_GROUP);
    // Null range.
    List<ExpectedPartitionValueEntity> expectedPartitionValueEntities = expectedPartitionValueDao.getExpectedPartitionValuesByGroupAndRange(PARTITION_KEY_GROUP, null);
    assertEquals(expectedPartitionValueEntities.size(), 22, expectedPartitionValueEntities.size());
    // Range with no start or end.
    PartitionValueRange partitionValueRange = new PartitionValueRange();
    expectedPartitionValueEntities = expectedPartitionValueDao.getExpectedPartitionValuesByGroupAndRange(PARTITION_KEY_GROUP, partitionValueRange);
    assertEquals(expectedPartitionValueEntities.size(), 22, expectedPartitionValueEntities.size());
}
Also used : PartitionValueRange(org.finra.herd.model.api.xml.PartitionValueRange) ExpectedPartitionValueEntity(org.finra.herd.model.jpa.ExpectedPartitionValueEntity) Test(org.junit.Test)

Example 9 with ExpectedPartitionValueEntity

use of org.finra.herd.model.jpa.ExpectedPartitionValueEntity 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 10 with ExpectedPartitionValueEntity

use of org.finra.herd.model.jpa.ExpectedPartitionValueEntity 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)

Aggregations

ExpectedPartitionValueEntity (org.finra.herd.model.jpa.ExpectedPartitionValueEntity)13 PartitionKeyGroupEntity (org.finra.herd.model.jpa.PartitionKeyGroupEntity)7 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Order (javax.persistence.criteria.Order)2 Predicate (javax.persistence.criteria.Predicate)2 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)2 PartitionValueRange (org.finra.herd.model.api.xml.PartitionValueRange)2 AlreadyExistsException (org.finra.herd.model.AlreadyExistsException)1 ExpectedPartitionValueKey (org.finra.herd.model.api.xml.ExpectedPartitionValueKey)1 ExpectedPartitionValuesInformation (org.finra.herd.model.api.xml.ExpectedPartitionValuesInformation)1 PartitionKeyGroupKey (org.finra.herd.model.api.xml.PartitionKeyGroupKey)1