Skip to content

Commit

Permalink
feat: support csv
Browse files Browse the repository at this point in the history
  • Loading branch information
teleivo committed Jan 27, 2025
1 parent 0f5228b commit da1fe8c
Showing 1 changed file with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,17 @@ ResponseEntity<Page<ObjectNode>> getTrackedEntities(
@GetMapping(produces = {CONTENT_TYPE_CSV, CONTENT_TYPE_TEXT_CSV})
void getTrackedEntitiesAsCsv(
TrackedEntityRequestParams requestParams,
TrackerIdSchemeParams idSchemeParams,
HttpServletResponse response,
@RequestParam(required = false, defaultValue = "false") boolean skipHeader,
@CurrentUser UserDetails currentUser)
throws IOException, BadRequestException, ForbiddenException, NotFoundException {
List<TrackedEntity> trackedEntities = getTrackedEntitiesForCsv(requestParams, currentUser);
throws IOException,
BadRequestException,
ForbiddenException,
NotFoundException,
WebMessageException {
List<TrackedEntity> trackedEntities =
getTrackedEntitiesForCsv(requestParams, idSchemeParams, currentUser);

ResponseHeader.addContentDispositionAttachment(response, TE_CSV_FILE);
ResponseHeader.addContentTransferEncodingBinary(response);
Expand All @@ -228,11 +234,17 @@ void getTrackedEntitiesAsCsv(
@GetMapping(produces = {CONTENT_TYPE_CSV_ZIP})
void getTrackedEntitiesAsCsvZip(
TrackedEntityRequestParams requestParams,
TrackerIdSchemeParams idSchemeParams,
HttpServletResponse response,
@RequestParam(required = false, defaultValue = "false") boolean skipHeader,
@CurrentUser UserDetails currentUser)
throws IOException, BadRequestException, ForbiddenException, NotFoundException {
List<TrackedEntity> trackedEntities = getTrackedEntitiesForCsv(requestParams, currentUser);
throws IOException,
BadRequestException,
ForbiddenException,
NotFoundException,
WebMessageException {
List<TrackedEntity> trackedEntities =
getTrackedEntitiesForCsv(requestParams, idSchemeParams, currentUser);

ResponseHeader.addContentDispositionAttachment(response, TE_CSV_FILE + ZIP_EXT);
ResponseHeader.addContentTransferEncodingBinary(response);
Expand All @@ -245,11 +257,17 @@ void getTrackedEntitiesAsCsvZip(
@GetMapping(produces = {CONTENT_TYPE_CSV_GZIP})
void getTrackedEntitiesAsCsvGZip(
TrackedEntityRequestParams requestParams,
TrackerIdSchemeParams idSchemeParams,
HttpServletResponse response,
@RequestParam(required = false, defaultValue = "false") boolean skipHeader,
@CurrentUser UserDetails currentUser)
throws IOException, BadRequestException, ForbiddenException, NotFoundException {
List<TrackedEntity> trackedEntities = getTrackedEntitiesForCsv(requestParams, currentUser);
throws IOException,
BadRequestException,
ForbiddenException,
NotFoundException,
WebMessageException {
List<TrackedEntity> trackedEntities =
getTrackedEntitiesForCsv(requestParams, idSchemeParams, currentUser);

ResponseHeader.addContentDispositionAttachment(response, TE_CSV_FILE + GZIP_EXT);
ResponseHeader.addContentTransferEncodingBinary(response);
Expand All @@ -259,17 +277,20 @@ void getTrackedEntitiesAsCsvGZip(
}

private List<TrackedEntity> getTrackedEntitiesForCsv(
TrackedEntityRequestParams requestParams, UserDetails currentUser)
throws BadRequestException, ForbiddenException, NotFoundException {
TrackedEntityRequestParams requestParams,
TrackerIdSchemeParams idSchemeParams,
UserDetails currentUser)
throws BadRequestException, ForbiddenException, NotFoundException, WebMessageException {
TrackedEntityOperationParams operationParams =
paramsMapper.map(requestParams, CSV_FIELDS, currentUser);

// only supports idScheme=UID
TrackerIdSchemeParams idSchemeParams = TrackerIdSchemeParams.builder().build();
MappingErrors errors = new MappingErrors(idSchemeParams);
return trackedEntityService.getTrackedEntities(operationParams).stream()
.map(te -> TRACKED_ENTITY_MAPPER.map(idSchemeParams, errors, te))
.toList();
List<TrackedEntity> result =
trackedEntityService.getTrackedEntities(operationParams).stream()
.map(te -> TRACKED_ENTITY_MAPPER.map(idSchemeParams, errors, te))
.toList();
ensureNoMappingErrors(errors);
return result;
}

@OpenApi.Response(OpenApi.EntityType.class)
Expand Down Expand Up @@ -300,20 +321,24 @@ ResponseEntity<ObjectNode> getTrackedEntityByUid(
produces = {CONTENT_TYPE_CSV, CONTENT_TYPE_TEXT_CSV})
void getTrackedEntityByUidAsCsv(
@PathVariable UID uid,
TrackerIdSchemeParams idSchemeParams,
HttpServletResponse response,
@RequestParam(required = false, defaultValue = "false") boolean skipHeader,
@OpenApi.Param({UID.class, Program.class}) @RequestParam(required = false) UID program)
throws IOException, ForbiddenException, NotFoundException, BadRequestException {
throws IOException,
ForbiddenException,
NotFoundException,
BadRequestException,
WebMessageException {
TrackedEntityParams trackedEntityParams = fieldsMapper.map(CSV_FIELDS);

// only supports idScheme=UID
TrackerIdSchemeParams idSchemeParams = TrackerIdSchemeParams.builder().build();
MappingErrors errors = new MappingErrors(idSchemeParams);
TrackedEntity trackedEntity =
TRACKED_ENTITY_MAPPER.map(
idSchemeParams,
errors,
trackedEntityService.getTrackedEntity(uid, program, trackedEntityParams));
ensureNoMappingErrors(errors);

OutputStream outputStream = response.getOutputStream();
response.setContentType(CONTENT_TYPE_CSV);
Expand Down

0 comments on commit da1fe8c

Please sign in to comment.