-
Notifications
You must be signed in to change notification settings - Fork 1
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
Closed
Delete data on FHIR #104
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
efsity/src/main/java/org/smartregister/command/DeleteDataCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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