Search in sources :

Example 26 with CsvSchema

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema in project dhis2-core by dhis2.

the class SystemController method getUidCsv.

@GetMapping(value = { "/uid", "/id" }, produces = "application/csv")
public void getUidCsv(@RequestParam(required = false, defaultValue = "1") Integer limit, HttpServletResponse response) throws IOException {
    CodeList codeList = generateCodeList(Math.min(limit, 10000), CodeGenerator::generateUid);
    CsvSchema schema = CsvSchema.builder().addColumn("uid").setUseHeader(true).build();
    CsvGenerator csvGenerator = CSV_FACTORY.createGenerator(response.getOutputStream());
    csvGenerator.setSchema(schema);
    for (String code : codeList.getCodes()) {
        csvGenerator.writeStartObject();
        csvGenerator.writeStringField("uid", code);
        csvGenerator.writeEndObject();
    }
    csvGenerator.flush();
}
Also used : CodeList(org.hisp.dhis.webapi.webdomain.CodeList) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) CodeGenerator(org.hisp.dhis.common.CodeGenerator) CsvGenerator(com.fasterxml.jackson.dataformat.csv.CsvGenerator) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 27 with CsvSchema

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema in project dhis2-core by dhis2.

the class TrackerCsvEventService method readEvents.

@Override
public List<Event> readEvents(InputStream inputStream, boolean skipFirst) throws IOException, ParseException {
    final CsvSchema csvSchema = CSV_MAPPER.schemaFor(CsvEventDataValue.class).withUseHeader(skipFirst).withColumnReordering(true);
    List<Event> events = Lists.newArrayList();
    ObjectReader reader = CSV_MAPPER.readerFor(CsvEventDataValue.class).with(csvSchema);
    MappingIterator<CsvEventDataValue> iterator = reader.readValues(inputStream);
    Event event = new Event();
    while (iterator.hasNext()) {
        CsvEventDataValue dataValue = iterator.next();
        if (!Objects.equals(event.getEvent(), dataValue.getEvent())) {
            event = new Event();
            event.setEvent(dataValue.getEvent());
            event.setStatus(StringUtils.isEmpty(dataValue.getStatus()) ? EventStatus.ACTIVE : Enum.valueOf(EventStatus.class, dataValue.getStatus()));
            event.setProgram(dataValue.getProgram());
            event.setProgramStage(dataValue.getProgramStage());
            event.setEnrollment(dataValue.getEnrollment());
            event.setOrgUnit(dataValue.getOrgUnit());
            event.setCreatedAt(DateUtils.instantFromDateAsString(dataValue.getCreatedAt()));
            event.setCreatedAtClient(DateUtils.instantFromDateAsString(dataValue.getCreatedAtClient()));
            event.setUpdatedAt(DateUtils.instantFromDateAsString(dataValue.getUpdatedAt()));
            event.setUpdatedAtClient(DateUtils.instantFromDateAsString(dataValue.getUpdatedAtClient()));
            event.setOccurredAt(DateUtils.instantFromDateAsString(dataValue.getOccurredAt()));
            event.setScheduledAt(DateUtils.instantFromDateAsString(dataValue.getScheduledAt()));
            event.setCompletedAt(DateUtils.instantFromDateAsString(dataValue.getCompletedAt()));
            event.setCompletedBy(dataValue.getCompletedBy());
            event.setStoredBy(dataValue.getStoredBy());
            event.setAttributeOptionCombo(dataValue.getAttributeOptionCombo());
            event.setAttributeCategoryOptions(dataValue.getAttributeCategoryOptions());
            event.setAssignedUser(dataValue.getAssignedUser());
            if (dataValue.getGeometry() != null) {
                event.setGeometry(new WKTReader().read(dataValue.getGeometry()));
            } else if (dataValue.getLongitude() != null && dataValue.getLatitude() != null) {
                event.setGeometry(new WKTReader().read("Point(" + dataValue.getLongitude() + " " + dataValue.getLatitude() + ")"));
            }
            events.add(event);
        }
        if (ObjectUtils.anyNotNull(dataValue.getProvidedElsewhere(), dataValue.getDataElement(), dataValue.getValue(), dataValue.getCreatedAtDataValue(), dataValue.getUpdatedAtDataValue(), dataValue.getStoredByDataValue())) {
            DataValue value = new DataValue();
            value.setProvidedElsewhere(dataValue.getProvidedElsewhere() != null && dataValue.getProvidedElsewhere());
            value.setDataElement(dataValue.getDataElement());
            value.setValue(dataValue.getValue());
            value.setCreatedAt(DateUtils.instantFromDateAsString(dataValue.getCreatedAtDataValue()));
            value.setUpdatedAt(DateUtils.instantFromDateAsString(dataValue.getUpdatedAtDataValue()));
            value.setStoredBy(dataValue.getStoredByDataValue());
            event.getDataValues().add(value);
        }
    }
    return events;
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) DataValue(org.hisp.dhis.tracker.domain.DataValue) Event(org.hisp.dhis.tracker.domain.Event) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) WKTReader(org.locationtech.jts.io.WKTReader)

Example 28 with CsvSchema

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema in project dhis2-core by dhis2.

the class TrackerCsvEventService method writeEvents.

@Override
public void writeEvents(OutputStream outputStream, List<Event> events, boolean withHeader) throws IOException {
    final CsvSchema csvSchema = CSV_MAPPER.schemaFor(CsvEventDataValue.class).withLineSeparator("\n").withUseHeader(withHeader);
    ObjectWriter writer = CSV_MAPPER.writer(csvSchema.withUseHeader(withHeader));
    List<CsvEventDataValue> dataValues = new ArrayList<>();
    for (Event event : events) {
        CsvEventDataValue templateDataValue = new CsvEventDataValue();
        templateDataValue.setEvent(event.getEvent());
        templateDataValue.setStatus(event.getStatus() != null ? event.getStatus().name() : null);
        templateDataValue.setProgram(event.getProgram());
        templateDataValue.setProgramStage(event.getProgramStage());
        templateDataValue.setEnrollment(event.getEnrollment());
        templateDataValue.setOrgUnit(event.getOrgUnit());
        templateDataValue.setOrgUnitName(event.getOrgUnitName());
        templateDataValue.setOccurredAt(event.getOccurredAt() == null ? null : event.getOccurredAt().toString());
        templateDataValue.setScheduledAt(event.getScheduledAt() == null ? null : event.getScheduledAt().toString());
        templateDataValue.setFollowup(event.isFollowup());
        templateDataValue.setDeleted(event.isDeleted());
        templateDataValue.setCreatedAt(event.getCreatedAt() == null ? null : event.getCreatedAt().toString());
        templateDataValue.setCreatedAtClient(event.getCreatedAtClient() == null ? null : event.getCreatedAtClient().toString());
        templateDataValue.setUpdatedAt(event.getUpdatedAt() == null ? null : event.getUpdatedAt().toString());
        templateDataValue.setUpdatedAtClient(event.getUpdatedAtClient() == null ? null : event.getUpdatedAtClient().toString());
        templateDataValue.setCompletedBy(event.getCompletedBy());
        templateDataValue.setCompletedAt(event.getCompletedAt() == null ? null : event.getCompletedAt().toString());
        templateDataValue.setUpdatedBy(event.getUpdatedBy());
        templateDataValue.setStoredBy(event.getStoredBy());
        templateDataValue.setCompletedAt(event.getCompletedAt() == null ? null : event.getCompletedAt().toString());
        templateDataValue.setCompletedBy(event.getCompletedBy());
        templateDataValue.setAttributeOptionCombo(event.getAttributeOptionCombo());
        templateDataValue.setAttributeCategoryOptions(event.getAttributeCategoryOptions());
        templateDataValue.setAssignedUser(event.getAssignedUser());
        if (event.getGeometry() != null) {
            templateDataValue.setGeometry(event.getGeometry().toText());
            if (event.getGeometry().getGeometryType().equals("Point")) {
                templateDataValue.setLongitude(event.getGeometry().getCoordinate().x);
                templateDataValue.setLatitude(event.getGeometry().getCoordinate().y);
            }
        }
        for (DataValue value : event.getDataValues()) {
            CsvEventDataValue dataValue = new CsvEventDataValue(templateDataValue);
            dataValue.setDataElement(value.getDataElement());
            dataValue.setValue(value.getValue());
            dataValue.setProvidedElsewhere(value.isProvidedElsewhere());
            dataValue.setCreatedAtDataValue(value.getCreatedAt() == null ? null : value.getCreatedAt().toString());
            dataValue.setUpdatedAtDataValue(value.getUpdatedAt() == null ? null : value.getUpdatedAt().toString());
            if (value.getStoredBy() != null) {
                dataValue.setStoredBy(value.getStoredBy());
            }
            dataValues.add(dataValue);
        }
    }
    writer.writeValue(outputStream, dataValues);
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) DataValue(org.hisp.dhis.tracker.domain.DataValue) ArrayList(java.util.ArrayList) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Event(org.hisp.dhis.tracker.domain.Event)

Aggregations

CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)17 CsvMapper (com.fasterxml.jackson.dataformat.csv.CsvMapper)11 CsvSchema (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)5 CsvMapper (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper)5 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)3 File (java.io.File)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 BulkWriter (org.apache.flink.api.common.serialization.BulkWriter)3 Converter (org.apache.flink.formats.common.Converter)3 Preconditions.checkNotNull (org.apache.flink.util.Preconditions.checkNotNull)3 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Collections (java.util.Collections)2 List (java.util.List)2 Set (java.util.Set)2