Search in sources :

Example 31 with DataValue

use of org.hisp.dhis.dxf2.datavalue.DataValue in project dhis2-core by dhis2.

the class DataValueSetServiceTest method testImportDataValuesInvalidAttributeOptionComboDates.

@Test
public void testImportDataValuesInvalidAttributeOptionComboDates() throws Exception {
    categoryOptionA.setStartDate(peB.getStartDate());
    categoryOptionA.setEndDate(peB.getEndDate());
    categoryService.updateDataElementCategoryOption(categoryOptionA);
    in = new ClassPathResource("datavalueset/dataValueSetH.xml").getInputStream();
    ImportSummary summary = dataValueSetService.saveDataValueSet(in);
    assertEquals(summary.getConflicts().toString(), 2, summary.getConflicts().size());
    assertEquals(1, summary.getImportCount().getImported());
    assertEquals(0, summary.getImportCount().getUpdated());
    assertEquals(0, summary.getImportCount().getDeleted());
    assertEquals(2, summary.getImportCount().getIgnored());
    assertEquals(ImportStatus.WARNING, summary.getStatus());
    Collection<DataValue> dataValues = mockDataValueBatchHandler.getInserts();
    assertNotNull(dataValues);
    assertEquals(1, dataValues.size());
    assertTrue(dataValues.contains(new DataValue(deB, peB, ouB, ocDef, ocA)));
}
Also used : DataValue(org.hisp.dhis.datavalue.DataValue) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 32 with DataValue

use of org.hisp.dhis.dxf2.datavalue.DataValue in project dhis2-core by dhis2.

the class DataValueController method deleteDataValue.

// ---------------------------------------------------------------------
// DELETE
// ---------------------------------------------------------------------
@PreAuthorize("hasRole('ALL') or hasRole('F_DATAVALUE_DELETE')")
@RequestMapping(method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteDataValue(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, HttpServletResponse response) throws WebMessageException {
    // ---------------------------------------------------------------------
    // Input validation
    // ---------------------------------------------------------------------
    DataElement dataElement = getAndValidateDataElement(de);
    DataElementCategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo(co, false);
    DataElementCategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo(cc, cp);
    Period period = getAndValidatePeriod(pe);
    OrganisationUnit organisationUnit = getAndValidateOrganisationUnit(ou);
    // ---------------------------------------------------------------------
    // Locking validation
    // ---------------------------------------------------------------------
    validateDataSetNotLocked(dataElement, period, organisationUnit, attributeOptionCombo);
    // ---------------------------------------------------------------------
    // Period validation
    // ---------------------------------------------------------------------
    validateDataInputPeriodForDataElementAndPeriod(dataElement, period);
    // ---------------------------------------------------------------------
    // Delete data value
    // ---------------------------------------------------------------------
    DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
    if (dataValue == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Data value cannot be deleted because it does not exist"));
    }
    dataValueService.deleteDataValue(dataValue);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataValue(org.hisp.dhis.datavalue.DataValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Period(org.hisp.dhis.period.Period) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 33 with DataValue

use of org.hisp.dhis.dxf2.datavalue.DataValue in project dhis2-core by dhis2.

the class DataValueController method getDataValue.

// ---------------------------------------------------------------------
// GET
// ---------------------------------------------------------------------
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public List<String> getDataValue(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, Model model, HttpServletResponse response) throws WebMessageException {
    // ---------------------------------------------------------------------
    // Input validation
    // ---------------------------------------------------------------------
    DataElement dataElement = getAndValidateDataElement(de);
    DataElementCategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo(co, false);
    DataElementCategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo(cc, cp);
    Period period = getAndValidatePeriod(pe);
    OrganisationUnit organisationUnit = getAndValidateOrganisationUnit(ou);
    // ---------------------------------------------------------------------
    // Get data value
    // ---------------------------------------------------------------------
    DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
    if (dataValue == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Data value does not exist"));
    }
    List<String> value = new ArrayList<>();
    value.add(dataValue.getValue());
    return value;
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataValue(org.hisp.dhis.datavalue.DataValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 34 with DataValue

use of org.hisp.dhis.dxf2.datavalue.DataValue in project dhis2-core by dhis2.

the class DataValueController method getDataValueFile.

// ---------------------------------------------------------------------
// GET file
// ---------------------------------------------------------------------
@RequestMapping(value = "/files", method = RequestMethod.GET)
public void getDataValueFile(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
    // ---------------------------------------------------------------------
    // Input validation
    // ---------------------------------------------------------------------
    DataElement dataElement = getAndValidateDataElement(de);
    if (!dataElement.isFileType()) {
        throw new WebMessageException(WebMessageUtils.conflict("DataElement must be of type file"));
    }
    DataElementCategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo(co, false);
    DataElementCategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo(cc, cp);
    Period period = getAndValidatePeriod(pe);
    OrganisationUnit organisationUnit = getAndValidateOrganisationUnit(ou);
    // ---------------------------------------------------------------------
    // Get data value
    // ---------------------------------------------------------------------
    DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
    if (dataValue == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Data value does not exist"));
    }
    // ---------------------------------------------------------------------
    // Get file resource
    // ---------------------------------------------------------------------
    String uid = dataValue.getValue();
    FileResource fileResource = fileResourceService.getFileResource(uid);
    if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
        throw new WebMessageException(WebMessageUtils.notFound("A data value file resource with id " + uid + " does not exist."));
    }
    FileResourceStorageStatus storageStatus = fileResource.getStorageStatus();
    if (storageStatus != FileResourceStorageStatus.STORED) {
        // Special case:
        //  The FileResource exists and has been tied to this DataValue, however, the underlying file
        //  content is still not stored to the (most likely external) file store provider.
        // HTTP 409, for lack of a more suitable status code
        WebMessage webMessage = WebMessageUtils.conflict("The content is being processed and is not available yet. Try again later.", "The content requested is in transit to the file store and will be available at a later time.");
        webMessage.setResponse(new FileResourceWebMessageResponse(fileResource));
        throw new WebMessageException(webMessage);
    }
    ByteSource content = fileResourceService.getFileResourceContent(fileResource);
    if (content == null) {
        throw new WebMessageException(WebMessageUtils.notFound("The referenced file could not be found"));
    }
    // ---------------------------------------------------------------------
    // Attempt to build signed URL request for content and redirect
    // ---------------------------------------------------------------------
    URI signedGetUri = fileResourceService.getSignedGetFileResourceContentUri(uid);
    if (signedGetUri != null) {
        response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
        response.setHeader(HttpHeaders.LOCATION, signedGetUri.toASCIIString());
        return;
    }
    // ---------------------------------------------------------------------
    // Build response and return
    // ---------------------------------------------------------------------
    response.setContentType(fileResource.getContentType());
    response.setContentLength(new Long(fileResource.getContentLength()).intValue());
    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName());
    // ---------------------------------------------------------------------
    // Request signing is not available, stream content back to client
    // ---------------------------------------------------------------------
    InputStream inputStream = null;
    try {
        inputStream = content.openStream();
        IOUtils.copy(inputStream, response.getOutputStream());
    } catch (IOException e) {
        throw new WebMessageException(WebMessageUtils.error("Failed fetching the file from storage", "There was an exception when trying to fetch the file from the storage backend. " + "Depending on the provider the root cause could be network or file system related."));
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) DataValue(org.hisp.dhis.datavalue.DataValue) InputStream(java.io.InputStream) FileResourceStorageStatus(org.hisp.dhis.fileresource.FileResourceStorageStatus) FileResource(org.hisp.dhis.fileresource.FileResource) Period(org.hisp.dhis.period.Period) IOException(java.io.IOException) URI(java.net.URI) DataElement(org.hisp.dhis.dataelement.DataElement) FileResourceWebMessageResponse(org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse) ByteSource(com.google.common.io.ByteSource) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 35 with DataValue

use of org.hisp.dhis.dxf2.datavalue.DataValue in project dhis2-core by dhis2.

the class EventAnalyticsServiceTest method parseEventData.

// -------------------------------------------------------------------------
// Internal Logic
// -------------------------------------------------------------------------
private void parseEventData(ArrayList<String[]> lines) {
    String storedBy = "johndoe";
    for (String[] line : lines) {
        Event event = new Event();
        event.setProgram(line[0]);
        event.setProgramStage(line[1]);
        DataValue dataValue = new DataValue();
        dataValue.setDataElement(line[2]);
        dataValue.setValue(line[6]);
        dataValue.setStoredBy(storedBy);
        event.setEventDate(line[3]);
        event.setOrgUnit(line[4]);
        event.setDataValues(Lists.newArrayList(dataValue));
        event.setCompletedDate(line[3]);
        event.setTrackedEntityInstance(line[5]);
        event.setStatus(EventStatus.COMPLETED);
    }
}
Also used : DataValue(org.hisp.dhis.dxf2.events.event.DataValue) Event(org.hisp.dhis.dxf2.events.event.Event)

Aggregations

DataValue (org.hisp.dhis.datavalue.DataValue)22 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)20 DhisSpringTest (org.hisp.dhis.DhisSpringTest)17 Test (org.junit.Test)17 ClassPathResource (org.springframework.core.io.ClassPathResource)16 DataElement (org.hisp.dhis.dataelement.DataElement)8 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)7 DataValue (org.hisp.dhis.dxf2.events.event.DataValue)7 Event (org.hisp.dhis.dxf2.events.event.Event)7 Period (org.hisp.dhis.period.Period)7 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)6 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 DataValueAudit (org.hisp.dhis.datavalue.DataValueAudit)4 IdScheme (org.hisp.dhis.common.IdScheme)3 IdSchemes (org.hisp.dhis.common.IdSchemes)3 CompleteDataSetRegistration (org.hisp.dhis.dataset.CompleteDataSetRegistration)3