Search in sources :

Example 1 with TextPatternGenerationException

use of org.hisp.dhis.textpattern.TextPatternGenerationException in project dhis2-core by dhis2.

the class ReservedValueServiceTest method shouldExitLoopWithExecutionException.

@Test
void shouldExitLoopWithExecutionException() throws TextPatternParser.TextPatternParsingException, TextPatternGenerationException, ReserveValueException, ExecutionException, InterruptedException {
    when(valueGeneratorService.generateValues(any(), any(), any(), anyInt())).thenThrow(new ExecutionException(new Exception("Execution exception")));
    assertEquals(0, reservedValueService.reserve(createTrackedEntityAttribute(Objects.TRACKEDENTITYATTRIBUTE, ownerUid, randomText), 2, new HashMap<>(), futureDate).size());
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) TextPatternGenerationException(org.hisp.dhis.textpattern.TextPatternGenerationException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 2 with TextPatternGenerationException

use of org.hisp.dhis.textpattern.TextPatternGenerationException in project dhis2-core by dhis2.

the class TrackedEntityAttributeController method reserve.

// Helpers
private List<ReservedValue> reserve(String id, int numberToReserve, int daysToLive) throws WebMessageException {
    if (numberToReserve > 1000 || numberToReserve < 1) {
        throw new WebMessageException(badRequest("You can only reserve between 1 and 1000 values in a single request."));
    }
    Map<String, List<String>> params = context.getParameterValuesMap();
    TrackedEntityAttribute attribute = trackedEntityAttributeService.getTrackedEntityAttribute(id);
    if (attribute == null) {
        throw new WebMessageException(notFound("No attribute found with id " + id));
    }
    if (attribute.getTextPattern() == null) {
        throw new WebMessageException(conflict("This attribute has no pattern"));
    }
    Map<String, String> values = getRequiredValues(attribute, params);
    Date expiration = DateUtils.getDateAfterAddition(new Date(), daysToLive);
    try {
        List<ReservedValue> result = reservedValueService.reserve(attribute, numberToReserve, values, expiration);
        if (result.isEmpty()) {
            throw new WebMessageException(conflict("Unable to reserve id. This may indicate that there are too few available ids left."));
        }
        return result;
    } catch (ReserveValueException ex) {
        throw new WebMessageException(conflict(ex.getMessage()));
    } catch (TextPatternGenerationException ex) {
        throw new WebMessageException(error(ex.getMessage()));
    }
}
Also used : ReservedValue(org.hisp.dhis.reservedvalue.ReservedValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TextPatternGenerationException(org.hisp.dhis.textpattern.TextPatternGenerationException) ReserveValueException(org.hisp.dhis.reservedvalue.ReserveValueException) List(java.util.List) Date(java.util.Date)

Example 3 with TextPatternGenerationException

use of org.hisp.dhis.textpattern.TextPatternGenerationException 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;
}
Also used : ImmutableMap(com.google.common.collect.ImmutableMap) Date(java.util.Date) RequiredArgsConstructor(lombok.RequiredArgsConstructor) TextPattern(org.hisp.dhis.textpattern.TextPattern) TimeoutException(java.util.concurrent.TimeoutException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) TextPatternValidationUtils(org.hisp.dhis.textpattern.TextPatternValidationUtils) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) TextPatternMethod(org.hisp.dhis.textpattern.TextPatternMethod) Service(org.springframework.stereotype.Service) Map(java.util.Map) TextPatternGenerationException(org.hisp.dhis.textpattern.TextPatternGenerationException) RESERVED_VALUE_GENERATION_ATTEMPT(org.hisp.dhis.util.Constants.RESERVED_VALUE_GENERATION_ATTEMPT) Optional(java.util.Optional) TextPatternService(org.hisp.dhis.textpattern.TextPatternService) Pattern(java.util.regex.Pattern) TextPatternSegment(org.hisp.dhis.textpattern.TextPatternSegment) Collections(java.util.Collections) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) RESERVED_VALUE_GENERATION_TIMEOUT(org.hisp.dhis.util.Constants.RESERVED_VALUE_GENERATION_TIMEOUT) Transactional(org.springframework.transaction.annotation.Transactional) TextPatternSegment(org.hisp.dhis.textpattern.TextPatternSegment) ArrayList(java.util.ArrayList) Date(java.util.Date) TextPattern(org.hisp.dhis.textpattern.TextPattern) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

TextPatternGenerationException (org.hisp.dhis.textpattern.TextPatternGenerationException)3 Date (java.util.Date)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Optional (java.util.Optional)1 TimeoutException (java.util.concurrent.TimeoutException)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)1 ReserveValueException (org.hisp.dhis.reservedvalue.ReserveValueException)1 ReservedValue (org.hisp.dhis.reservedvalue.ReservedValue)1 TextPattern (org.hisp.dhis.textpattern.TextPattern)1 TextPatternMethod (org.hisp.dhis.textpattern.TextPatternMethod)1