use of org.hisp.dhis.reservedvalue.ReserveValueException 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()));
}
}
Aggregations