Search in sources :

Example 1 with ReserveValueException

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

Aggregations

Date (java.util.Date)1 List (java.util.List)1 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)1 ReserveValueException (org.hisp.dhis.reservedvalue.ReserveValueException)1 ReservedValue (org.hisp.dhis.reservedvalue.ReservedValue)1 TextPatternGenerationException (org.hisp.dhis.textpattern.TextPatternGenerationException)1 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)1