Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete data on FHIR #104

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package org.smartregister.command;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.*;


import org.apache.http.HttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.client.methods.HttpDelete;

import org.smartregister.util.FctUtils;
import picocli.CommandLine;

@CommandLine.Command(name = "delete")
public class DeleteDataCommand implements Runnable {
@CommandLine.Option(
names = {"-u", "--baseUrl"},
description =
"FHIR base URL example http://localhost:8888/fhir",
required = true)
String baseUrl;

@CommandLine.Option(
names = {"-r", "--resourceType"},
description = "Resource to be deleted",
required = true)
String resourceType;

@CommandLine.Option(
names = {"-id", "--identifiers"},
description = "Resource Identifiers if deleting a single resource",
required = true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is required, but the description makes it sound like it is optional

String identifiers;

@CommandLine.Option(
names = {"-at", "--access-token"},
description = "access token for fhir server")
String accessToken;

@Override
public void run() {
String url = baseUrl + "/" + resourceType + "?_id=" + identifiers + "&_cascade=delete";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the cascades delete optional? maybe make it default not, considering the risk involved in data loss, so the person running the command has to actually opt in

FctUtils.printInfo("Starting deletion");

FctUtils.printInfo(String.format("URL: \u001b[35m%s\u001b[0m", url));

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpDelete httpDelete = new HttpDelete(url);
httpDelete.setHeader("Content-Type", "application/fhir+json");
httpDelete.setHeader("Authorization", "Bearer " + accessToken);
try {
HttpResponse response = httpClient.execute(httpDelete);
int statusCode = response.getStatusLine().getStatusCode();
FctUtils.printToConsole("Response Status: " + statusCode);

if (statusCode == 200) {
handleSuccessfulResponse(response);
} else {
handleErrorResponse(response);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
httpClient.close();
} catch (IOException e) {
FctUtils.printError(e.getMessage());
}
}
}
private void handleSuccessfulResponse(HttpResponse response) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(response.getEntity().getContent());

if (rootNode.has("issue")) {
ArrayNode issues = (ArrayNode) rootNode.get("issue");
for (JsonNode issue : issues) {
if (issue.has("diagnostics")) {
FctUtils.printToConsole("Diagnostics: " + issue.get("diagnostics").asText());
}
}
}
}

private void handleErrorResponse(HttpResponse response) throws IOException {
ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
JsonNode rootNode = objectMapper.readTree(response.getEntity().getContent());
FctUtils.printToConsole("Error Response:");
FctUtils.printToConsole(objectMapper.writeValueAsString(rootNode));
}
}