use of org.finra.herd.model.jpa.ExpectedPartitionValueEntity in project herd by FINRAOS.
the class ExpectedPartitionValueServiceImpl method deleteExpectedPartitionValues.
/**
* Deletes specified expected partition values from an existing partition key group which is identified by name.
*
* @param expectedPartitionValuesDeleteRequest the information needed to delete the expected partition values
*
* @return the expected partition values that got deleted
*/
@Override
public ExpectedPartitionValuesInformation deleteExpectedPartitionValues(ExpectedPartitionValuesDeleteRequest expectedPartitionValuesDeleteRequest) {
// Perform request validation and trim request parameters.
validateExpectedPartitionValuesDeleteRequest(expectedPartitionValuesDeleteRequest);
// Retrieve and ensure that a partition key group exists with the specified name.
PartitionKeyGroupEntity partitionKeyGroupEntity = partitionKeyGroupDaoHelper.getPartitionKeyGroupEntity(expectedPartitionValuesDeleteRequest.getPartitionKeyGroupKey());
// Load all existing expected partition value entities into a map for quick access.
Map<String, ExpectedPartitionValueEntity> expectedPartitionValueEntityMap = getExpectedPartitionValueEntityMap(partitionKeyGroupEntity.getExpectedPartitionValues());
// Build a list of all expected partition value entities to be deleted.
Collection<ExpectedPartitionValueEntity> deletedExpectedPartitionValueEntities = new ArrayList<>();
for (String expectedPartitionValue : expectedPartitionValuesDeleteRequest.getExpectedPartitionValues()) {
// Find the relative expected partition entity.
ExpectedPartitionValueEntity expectedPartitionValueEntity = expectedPartitionValueEntityMap.get(expectedPartitionValue);
if (expectedPartitionValueEntity != null) {
deletedExpectedPartitionValueEntities.add(expectedPartitionValueEntity);
} else {
throw new ObjectNotFoundException(String.format("Expected partition value \"%s\" doesn't exist in \"%s\" partition key group.", expectedPartitionValue, partitionKeyGroupEntity.getPartitionKeyGroupName()));
}
}
// Perform the actual deletion.
for (ExpectedPartitionValueEntity expectedPartitionValueEntity : deletedExpectedPartitionValueEntities) {
partitionKeyGroupEntity.getExpectedPartitionValues().remove(expectedPartitionValueEntity);
expectedPartitionValueDao.delete(expectedPartitionValueEntity);
}
return createExpectedPartitionValuesInformationFromEntities(partitionKeyGroupEntity, deletedExpectedPartitionValueEntities);
}
use of org.finra.herd.model.jpa.ExpectedPartitionValueEntity in project herd by FINRAOS.
the class ExpectedPartitionValueServiceImpl method getExpectedPartitionValues.
/**
* Retrieves a range of existing expected partition values.
*
* @param partitionKeyGroupKey the partition key group key
* @param partitionValueRange the partition value range
*
* @return the expected partition values
*/
@Override
public ExpectedPartitionValuesInformation getExpectedPartitionValues(PartitionKeyGroupKey partitionKeyGroupKey, PartitionValueRange partitionValueRange) {
// Perform validation and trim of the input parameters.
partitionKeyGroupHelper.validatePartitionKeyGroupKey(partitionKeyGroupKey);
// Trim the start expected partition value for the expected partition value range.
if (StringUtils.isNotBlank(partitionValueRange.getStartPartitionValue())) {
partitionValueRange.setStartPartitionValue(partitionValueRange.getStartPartitionValue().trim());
}
// Trim the end expected partition value for the expected partition value range.
if (StringUtils.isNotBlank(partitionValueRange.getEndPartitionValue())) {
partitionValueRange.setEndPartitionValue(partitionValueRange.getEndPartitionValue().trim());
}
// Validate that we are not missing both start and end expected partition values for the range.
if (StringUtils.isBlank(partitionValueRange.getStartPartitionValue()) && StringUtils.isBlank(partitionValueRange.getEndPartitionValue())) {
throw new IllegalArgumentException("At least one start or end expected partition value must be specified.");
}
// Using string compare, validate that start expected partition value is less than or equal to end expected partition value.
if (StringUtils.isNotBlank(partitionValueRange.getStartPartitionValue()) && StringUtils.isNotBlank(partitionValueRange.getEndPartitionValue()) && partitionValueRange.getStartPartitionValue().compareTo(partitionValueRange.getEndPartitionValue()) > 0) {
throw new IllegalArgumentException(String.format("The start expected partition value \"%s\" cannot be greater than the end expected partition value \"%s\".", partitionValueRange.getStartPartitionValue(), partitionValueRange.getEndPartitionValue()));
}
// Retrieve and ensure that a partition key group exists with the specified name.
PartitionKeyGroupEntity partitionKeyGroupEntity = partitionKeyGroupDaoHelper.getPartitionKeyGroupEntity(partitionKeyGroupKey);
// Retrieve a list of expected partition values.
Collection<ExpectedPartitionValueEntity> expectedPartitionValueEntities = expectedPartitionValueDao.getExpectedPartitionValuesByGroupAndRange(partitionKeyGroupKey.getPartitionKeyGroupName(), partitionValueRange);
return createExpectedPartitionValuesInformationFromEntities(partitionKeyGroupEntity, expectedPartitionValueEntities);
}
use of org.finra.herd.model.jpa.ExpectedPartitionValueEntity in project herd by FINRAOS.
the class ExpectedPartitionValueServiceImpl method createExpectedPartitionValuesInformationFromEntities.
/**
* Creates the expected partition values information from the persisted entities.
*
* @param partitionKeyGroupEntity the partition key group entity
* @param expectedPartitionValueEntities the list of expected partition value entities
*
* @return the expected partition values information
*/
private ExpectedPartitionValuesInformation createExpectedPartitionValuesInformationFromEntities(PartitionKeyGroupEntity partitionKeyGroupEntity, Collection<ExpectedPartitionValueEntity> expectedPartitionValueEntities) {
// Create an expected partition values information instance.
ExpectedPartitionValuesInformation expectedPartitionValuesInformation = new ExpectedPartitionValuesInformation();
// Add the partition key group key.
PartitionKeyGroupKey partitionKeyGroupKey = new PartitionKeyGroupKey();
expectedPartitionValuesInformation.setPartitionKeyGroupKey(partitionKeyGroupKey);
partitionKeyGroupKey.setPartitionKeyGroupName(partitionKeyGroupEntity.getPartitionKeyGroupName());
// Add the expected partition values.
List<String> expectedPartitionValues = new ArrayList<>();
expectedPartitionValuesInformation.setExpectedPartitionValues(expectedPartitionValues);
for (ExpectedPartitionValueEntity expectedPartitionValueEntity : expectedPartitionValueEntities) {
expectedPartitionValues.add(expectedPartitionValueEntity.getPartitionValue());
}
return expectedPartitionValuesInformation;
}
use of org.finra.herd.model.jpa.ExpectedPartitionValueEntity in project herd by FINRAOS.
the class ExpectedPartitionValueDaoTestHelper method createExpectedPartitionValueEntities.
/**
* Creates and persists specified partition value entities. This method also creates and persists a partition key group entity, if it does not exist.
*
* @param partitionKeyGroupName the partition key group name
* @param expectedPartitionValues the list of expected partition values
*
* @return the list of expected partition value entities
*/
public List<ExpectedPartitionValueEntity> createExpectedPartitionValueEntities(String partitionKeyGroupName, List<String> expectedPartitionValues) {
// Create partition key group if it does not exist.
PartitionKeyGroupEntity partitionKeyGroupEntity = partitionKeyGroupDao.getPartitionKeyGroupByName(partitionKeyGroupName);
if (partitionKeyGroupEntity == null) {
partitionKeyGroupEntity = partitionKeyGroupDaoTestHelper.createPartitionKeyGroupEntity(partitionKeyGroupName);
}
// Initialize the return list.
List<ExpectedPartitionValueEntity> expectedPartitionValueEntities = new ArrayList<>();
// infinite loop in case the end date is before the start date.
for (String expectedPartitionValue : expectedPartitionValues) {
ExpectedPartitionValueEntity expectedPartitionValueEntity = new ExpectedPartitionValueEntity();
expectedPartitionValueEntity.setPartitionKeyGroup(partitionKeyGroupEntity);
expectedPartitionValueEntity.setPartitionValue(expectedPartitionValue);
expectedPartitionValueEntities.add(expectedPartitionValueDao.saveAndRefresh(expectedPartitionValueEntity));
}
// Return the list of entities.
return expectedPartitionValueEntities;
}
use of org.finra.herd.model.jpa.ExpectedPartitionValueEntity in project herd by FINRAOS.
the class ExpectedPartitionValueDaoTest method testGetExpectedPartitionValuesByGroupAndRange.
/**
* Test DAO method to retrieve expected partition values by range.
*/
@Test
public void testGetExpectedPartitionValuesByGroupAndRange() {
expectedPartitionValueDaoTestHelper.createExpectedPartitionValueProcessDatesForApril2014(PARTITION_KEY_GROUP);
PartitionValueRange partitionValueRange = new PartitionValueRange();
partitionValueRange.setStartPartitionValue(getDateAsString(2014, 3, 11));
partitionValueRange.setEndPartitionValue(getDateAsString(2014, 3, 17));
List<ExpectedPartitionValueEntity> expectedPartitionValueEntities = expectedPartitionValueDao.getExpectedPartitionValuesByGroupAndRange(PARTITION_KEY_GROUP, partitionValueRange);
assertEquals(expectedPartitionValueEntities.size(), 5, expectedPartitionValueEntities.size());
assertEquals(expectedPartitionValueEntities.get(0).getPartitionValue(), getDateAsString(2014, 3, 11));
assertEquals(expectedPartitionValueEntities.get(1).getPartitionValue(), getDateAsString(2014, 3, 14));
assertEquals(expectedPartitionValueEntities.get(2).getPartitionValue(), getDateAsString(2014, 3, 15));
assertEquals(expectedPartitionValueEntities.get(3).getPartitionValue(), getDateAsString(2014, 3, 16));
assertEquals(expectedPartitionValueEntities.get(4).getPartitionValue(), getDateAsString(2014, 3, 17));
}
Aggregations