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