use of org.hisp.dhis.util.Constants.RESERVED_VALUE_GENERATION_ATTEMPT in project dhis2-core by dhis2.
the class DefaultReservedValueService method reserve.
@Override
@Transactional
public List<ReservedValue> reserve(TrackedEntityAttribute trackedEntityAttribute, int numberOfReservations, Map<String, String> values, Date expires) throws ReserveValueException, TextPatternGenerationException {
long startTime = System.currentTimeMillis();
int attemptsLeft = RESERVED_VALUE_GENERATION_ATTEMPT;
List<ReservedValue> resultList = new ArrayList<>();
TextPattern textPattern = trackedEntityAttribute.getTextPattern();
TextPatternSegment generatedSegment = textPattern.getSegments().stream().filter((tp) -> tp.getMethod().isGenerated() && Boolean.TRUE.equals(trackedEntityAttribute.isGenerated())).findFirst().orElse(null);
String key = textPatternService.resolvePattern(textPattern, values);
// Used for searching value tables
String valueKey = Optional.ofNullable(generatedSegment).map(gs -> key.replaceAll(Pattern.quote(gs.getRawSegment()), "%")).orElse(key);
ReservedValue reservedValue = ReservedValue.builder().created(new Date()).ownerObject(textPattern.getOwnerObject().name()).ownerUid(textPattern.getOwnerUid()).key(key).value(valueKey).expiryDate(expires).build();
checkIfEnoughValues(numberOfReservations, generatedSegment, reservedValue);
if (generatedSegment == null) {
if (numberOfReservations == 1) {
List<ReservedValue> reservedValues = Collections.singletonList(reservedValue.toBuilder().value(key).build());
reservedValueStore.reserveValues(reservedValues);
return reservedValues;
}
} else {
int numberOfValuesLeftToGenerate = numberOfReservations;
boolean isPersistable = generatedSegment.getMethod().isPersistable();
reservedValue.setTrackedEntityAttributeId(trackedEntityAttribute.getId());
try {
List<String> generatedValues = new ArrayList<>();
while (attemptsLeft-- > 0 && numberOfValuesLeftToGenerate > 0) {
checkTimeout(startTime);
generatedValues.addAll(valueGeneratorService.generateValues(generatedSegment, textPattern, key, numberOfReservations - resultList.size()));
List<String> resolvedPatterns = getResolvedPatterns(values, textPattern, generatedSegment, generatedValues);
saveGeneratedValues(numberOfReservations, resultList, textPattern, reservedValue, isPersistable, resolvedPatterns);
numberOfValuesLeftToGenerate = numberOfReservations - resultList.size();
generatedValues = new ArrayList<>();
}
} catch (TimeoutException ex) {
log.warn(String.format("Generation and reservation of values for %s wih uid %s timed out. %s values was reserved. You might be running low on available values", textPattern.getOwnerObject().name(), textPattern.getOwnerUid(), resultList.size()));
} catch (ExecutionException e) {
log.error(String.format("Generation and reservation of values error %s : ", e.getMessage()));
} catch (InterruptedException e) {
log.error(String.format("Generation and reservation of values error %s : ", e.getMessage()));
Thread.currentThread().interrupt();
}
}
return resultList;
}
Aggregations