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));
}
}
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;
}
}
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();
}
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;
}
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());
}
}
}
Aggregations