Search in sources :

Example 6 with ReservedValue

use of org.hisp.dhis.reservedvalue.ReservedValue 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 7 with ReservedValue

use of org.hisp.dhis.reservedvalue.ReservedValue in project dhis2-core by dhis2.

the class HibernateReservedValueStoreTest method isReservedShouldBeTrue.

@Test
void isReservedShouldBeTrue() {
    ReservedValue rv = reservedValue.value(prog001).build();
    reservedValueStore.save(rv);
    assertTrue(reservedValueStore.isReserved(rv.getOwnerObject(), rv.getOwnerUid(), prog001));
}
Also used : ReservedValue(org.hisp.dhis.reservedvalue.ReservedValue) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 8 with ReservedValue

use of org.hisp.dhis.reservedvalue.ReservedValue in project dhis2-core by dhis2.

the class HibernateReservedValueStoreTest method getAvailableValuesWhenAlreadyUsed.

@Test
void getAvailableValuesWhenAlreadyUsed() throws TextPatternParser.TextPatternParsingException, IllegalAccessException {
    OrganisationUnit ou = createOrganisationUnit("OU");
    organisationUnitStore.save(ou);
    TrackedEntityInstance tei = createTrackedEntityInstance(ou);
    trackedEntityInstanceStore.save(tei);
    TrackedEntityAttribute tea = createTrackedEntityAttribute('Y');
    TextPattern textPattern = TextPatternParser.parse(key);
    textPattern.setOwnerObject(Objects.fromClass(tea.getClass()));
    textPattern.setOwnerUid(tea.getUid());
    tea.setTextPattern(textPattern);
    tea.setUid(teaUid);
    trackedEntityAttributeStore.save(tea);
    TrackedEntityAttributeValue teav = createTrackedEntityAttributeValue('Z', tei, tea);
    teav.setValue(prog001);
    trackedEntityAttributeValueStore.save(teav);
    ReservedValue rv = reservedValue.value(prog001).build();
    rv.setTrackedEntityAttributeId(teav.getAttribute().getId());
    assertEquals(1, trackedEntityAttributeValueStore.getAll().size());
    assertEquals(0, reservedValueStore.getAll().size());
    List<ReservedValue> res = reservedValueStore.getAvailableValues(rv, Lists.newArrayList(prog001, prog002), rv.getOwnerObject());
    assertFalse(res.stream().anyMatch(r -> r.getValue().equals(prog001)));
    assertTrue(res.stream().anyMatch(r -> r.getValue().equals(prog002)));
    assertEquals(1, res.size());
}
Also used : Date(java.util.Date) TextPattern(org.hisp.dhis.textpattern.TextPattern) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) ReservedValue(org.hisp.dhis.reservedvalue.ReservedValue) Autowired(org.springframework.beans.factory.annotation.Autowired) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstanceStore(org.hisp.dhis.trackedentity.TrackedEntityInstanceStore) ArrayList(java.util.ArrayList) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) ReservedValueStore(org.hisp.dhis.reservedvalue.ReservedValueStore) TextPatternParser(org.hisp.dhis.textpattern.TextPatternParser) Calendar(java.util.Calendar) Lists(com.google.common.collect.Lists) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) DhisSpringTest(org.hisp.dhis.DhisSpringTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Objects(org.hisp.dhis.common.Objects) OrganisationUnitStore(org.hisp.dhis.organisationunit.OrganisationUnitStore) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttributeStore(org.hisp.dhis.trackedentity.TrackedEntityAttributeStore) TrackedEntityAttributeValueStore(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueStore) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TextPattern(org.hisp.dhis.textpattern.TextPattern) ReservedValue(org.hisp.dhis.reservedvalue.ReservedValue) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 9 with ReservedValue

use of org.hisp.dhis.reservedvalue.ReservedValue in project dhis2-core by dhis2.

the class HibernateReservedValueStoreTest method reserveValuesSingleValue.

@Test
void reserveValuesSingleValue() {
    reservedValueStore.save(reservedValue.value(prog001).build());
    int count = reservedValueStore.getCount();
    ReservedValue rv = reservedValue.value(prog002).build();
    List<ReservedValue> res = reservedValueStore.reserveValuesJpa(rv, Lists.newArrayList(rv.getValue()));
    assertEquals(1, res.size());
    assertEquals(reservedValueStore.getCount(), count + 1);
}
Also used : ReservedValue(org.hisp.dhis.reservedvalue.ReservedValue) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 10 with ReservedValue

use of org.hisp.dhis.reservedvalue.ReservedValue in project dhis2-core by dhis2.

the class HibernateReservedValueStoreTest method getAvailableValuesWhenNotReserved.

@Test
void getAvailableValuesWhenNotReserved() {
    ReservedValue rv = reservedValue.value(prog001).build();
    reservedValueStore.save(rv);
    assertEquals(1, reservedValueStore.getAll().size());
    List<ReservedValue> res = reservedValueStore.getAvailableValues(rv, Lists.newArrayList(prog001, prog002), rv.getOwnerObject());
    assertEquals(1, res.size());
    assertTrue(res.stream().anyMatch(r -> r.getValue().equals(prog002)));
}
Also used : Date(java.util.Date) TextPattern(org.hisp.dhis.textpattern.TextPattern) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) ReservedValue(org.hisp.dhis.reservedvalue.ReservedValue) Autowired(org.springframework.beans.factory.annotation.Autowired) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstanceStore(org.hisp.dhis.trackedentity.TrackedEntityInstanceStore) ArrayList(java.util.ArrayList) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) ReservedValueStore(org.hisp.dhis.reservedvalue.ReservedValueStore) TextPatternParser(org.hisp.dhis.textpattern.TextPatternParser) Calendar(java.util.Calendar) Lists(com.google.common.collect.Lists) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) DhisSpringTest(org.hisp.dhis.DhisSpringTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Objects(org.hisp.dhis.common.Objects) OrganisationUnitStore(org.hisp.dhis.organisationunit.OrganisationUnitStore) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttributeStore(org.hisp.dhis.trackedentity.TrackedEntityAttributeStore) TrackedEntityAttributeValueStore(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueStore) ReservedValue(org.hisp.dhis.reservedvalue.ReservedValue) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

ReservedValue (org.hisp.dhis.reservedvalue.ReservedValue)14 DhisSpringTest (org.hisp.dhis.DhisSpringTest)13 Test (org.junit.jupiter.api.Test)13 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)6 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)5 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)5 Calendar (java.util.Calendar)4 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 List (java.util.List)3 Lists (com.google.common.collect.Lists)2 Objects (org.hisp.dhis.common.Objects)2 OrganisationUnitStore (org.hisp.dhis.organisationunit.OrganisationUnitStore)2 ReservedValueStore (org.hisp.dhis.reservedvalue.ReservedValueStore)2 TextPattern (org.hisp.dhis.textpattern.TextPattern)2 TextPatternParser (org.hisp.dhis.textpattern.TextPatternParser)2 TrackedEntityAttributeStore (org.hisp.dhis.trackedentity.TrackedEntityAttributeStore)2 TrackedEntityInstanceStore (org.hisp.dhis.trackedentity.TrackedEntityInstanceStore)2 TrackedEntityAttributeValueStore (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueStore)2