-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes ##101
- Loading branch information
Showing
1 changed file
with
97 additions
and
0 deletions.
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"; | ||
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)); | ||
} | ||
} |