-
Notifications
You must be signed in to change notification settings - Fork 2
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
Publish binary resources #194
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,16 +12,12 @@ | |
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.TimeZone; | ||
import java.util.UUID; | ||
import java.util.*; | ||
import net.jimblackler.jsonschemafriend.GenerationException; | ||
import net.jimblackler.jsonschemafriend.ValidationException; | ||
import org.apache.http.HttpResponse; | ||
|
@@ -98,6 +94,12 @@ public class PublishFhirResourcesCommand implements Runnable { | |
required = false) | ||
String validateResource = "true"; | ||
|
||
@CommandLine.Option( | ||
names = {"-c", "--composition"}, | ||
description = "path of the composition configuration file", | ||
required = false) | ||
String compositionFilePath; | ||
|
||
@Override | ||
public void run() { | ||
long start = System.currentTimeMillis(); | ||
|
@@ -111,7 +113,11 @@ public void run() { | |
} | ||
} | ||
try { | ||
publishResources(); | ||
if (compositionFilePath != null) { | ||
ArrayList<JSONObject> resourceObjects = buildBinaries(compositionFilePath, projectFolder); | ||
buildBundle(resourceObjects); | ||
} | ||
buildResources(); | ||
stateManagement(); | ||
} catch (IOException | ValidationException | GenerationException e) { | ||
throw new RuntimeException(e); | ||
|
@@ -171,32 +177,98 @@ void setProperties(Properties properties) { | |
} | ||
} | ||
|
||
void publishResources() throws IOException, ValidationException, GenerationException { | ||
ArrayList<String> resourceFiles = getResourceFiles(projectFolder); | ||
String getFileName(String name) { | ||
if ((name.endsWith("Register")) || (name.endsWith("Profile"))) { | ||
String regex = "([a-z])([A-Z]+)"; | ||
String replacer = "$1_$2"; | ||
name = name.replaceAll(regex, replacer).toLowerCase(); | ||
} | ||
if (name.startsWith("strings")) { | ||
name = name + "_config.properties"; | ||
} else { | ||
name = name + "_config.json"; | ||
} | ||
return name; | ||
} | ||
|
||
HashMap<String, String> getDetails(JSONObject jsonObject) { | ||
JSONObject focus = jsonObject.getJSONObject("focus"); | ||
String reference = focus.getString("reference"); | ||
JSONObject identifier = focus.getJSONObject("identifier"); | ||
String name = identifier.getString("value"); | ||
|
||
HashMap<String, String> map = new HashMap<>(); | ||
map.put("reference", reference); | ||
map.put("name", getFileName(name)); | ||
return map; | ||
} | ||
|
||
String getBinaryContent(String fileName, String projectFolder) throws IOException { | ||
String pathToFile; | ||
if (fileName.contains("register")) { | ||
pathToFile = projectFolder + "/registers/" + fileName; | ||
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. assuming we're always using this agreed on file/folder structure 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. yea seems reasonable, on that note, should we to the validation checker (and later the app content from template creator) something to verify the repo follows file structure? 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. sure, created an issue here #195 |
||
} else if (fileName.contains("profile")) { | ||
pathToFile = projectFolder + "/profiles/" + fileName; | ||
} else if (fileName.startsWith("strings_")) { | ||
pathToFile = projectFolder + "/translations/" + fileName; | ||
} else { | ||
pathToFile = projectFolder + "/" + fileName; | ||
} | ||
|
||
String fileContent = FctUtils.readFile(pathToFile).getContent(); | ||
return Base64.getEncoder().encodeToString(fileContent.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
ArrayList<JSONObject> buildBinaries(String compositionFilePath, String projectFolder) | ||
throws IOException { | ||
FctFile compositionFile = FctUtils.readFile(compositionFilePath); | ||
JSONObject compositionResource = new JSONObject(compositionFile.getContent()); | ||
List<Map<String, String>> mapList = new ArrayList<>(); | ||
Map<String, String> detailsMap = new HashMap<>(); | ||
ArrayList<JSONObject> resourceObjects = new ArrayList<>(); | ||
boolean validateResourceBoolean = Boolean.parseBoolean(validateResource); | ||
|
||
for (String f : resourceFiles) { | ||
if (validateResourceBoolean) { | ||
FctUtils.printInfo(String.format("Validating file \u001b[35m%s\u001b[0m", f)); | ||
ValidateFhirResourcesCommand.validateFhirResources(f); | ||
} else { | ||
FctUtils.printInfo(String.format("Publishing \u001b[35m%s\u001b[0m Without Validation", f)); | ||
if (compositionResource.has("section")) { | ||
JSONArray compositionObjects = compositionResource.getJSONArray("section"); | ||
|
||
for (Object obj : compositionObjects) { | ||
JSONObject jsonObject = new JSONObject(obj.toString()); | ||
if (jsonObject.has("section")) { | ||
JSONArray section = jsonObject.getJSONArray("section"); | ||
for (Object subObj : section) { | ||
JSONObject jo = new JSONObject(subObj.toString()); | ||
detailsMap = getDetails(jo); | ||
} | ||
} else { | ||
detailsMap = getDetails(jsonObject); | ||
} | ||
mapList.add(detailsMap); | ||
} | ||
|
||
FctFile inputFile = FctUtils.readFile(f); | ||
JSONObject resourceObject = buildResourceObject(inputFile); | ||
resourceObjects.add(resourceObject); | ||
} | ||
for (Map<String, String> e : mapList) { | ||
String filename = e.get("name"); | ||
String binaryContent = getBinaryContent(filename, projectFolder); | ||
String contentType; | ||
|
||
// build the bundle | ||
JSONObject bundle = new JSONObject(); | ||
bundle.put("resourceType", "Bundle"); | ||
bundle.put("type", "transaction"); | ||
bundle.put("entry", resourceObjects); | ||
FctUtils.printToConsole("Full Payload to POST: "); | ||
FctUtils.printToConsole(bundle.toString()); | ||
if (filename.startsWith("strings_")) { | ||
contentType = "text/plain"; | ||
} else { | ||
contentType = "application/json"; | ||
} | ||
|
||
JSONObject binaryResourceObject = new JSONObject(); | ||
binaryResourceObject.put("resourceType", "Binary"); | ||
binaryResourceObject.put("id", e.get("reference").substring(7)); | ||
binaryResourceObject.put("contentType", contentType); | ||
binaryResourceObject.put("data", binaryContent); | ||
|
||
JSONObject finalResourceObject = buildResourceObject(binaryResourceObject.toString()); | ||
resourceObjects.add(finalResourceObject); | ||
} | ||
} | ||
return resourceObjects; | ||
} | ||
|
||
String getToken() { | ||
if (accessToken == null || accessToken.isBlank()) { | ||
if (clientId == null || clientId.isBlank()) { | ||
throw new IllegalArgumentException( | ||
|
@@ -217,7 +289,38 @@ void publishResources() throws IOException, ValidationException, GenerationExcep | |
accessToken = | ||
getAccessToken(clientId, clientSecret, accessTokenUrl, grantType, username, password); | ||
} | ||
postRequest(bundle.toString(), accessToken); | ||
return accessToken; | ||
} | ||
|
||
void buildBundle(ArrayList<JSONObject> resourceObjects) throws IOException { | ||
JSONObject bundle = new JSONObject(); | ||
bundle.put("resourceType", "Bundle"); | ||
bundle.put("type", "transaction"); | ||
bundle.put("entry", resourceObjects); | ||
FctUtils.printToConsole("Full Payload to POST: "); | ||
FctUtils.printToConsole(bundle.toString()); | ||
|
||
postRequest(bundle.toString(), getToken()); | ||
} | ||
|
||
void buildResources() throws IOException, ValidationException, GenerationException { | ||
ArrayList<String> resourceFiles = getResourceFiles(projectFolder); | ||
ArrayList<JSONObject> resourceObjects = new ArrayList<>(); | ||
boolean validateResourceBoolean = Boolean.parseBoolean(validateResource); | ||
|
||
for (String f : resourceFiles) { | ||
if (validateResourceBoolean) { | ||
FctUtils.printInfo(String.format("Validating file \u001b[35m%s\u001b[0m", f)); | ||
ValidateFhirResourcesCommand.validateFhirResources(f); | ||
} else { | ||
FctUtils.printInfo(String.format("Publishing \u001b[35m%s\u001b[0m Without Validation", f)); | ||
} | ||
|
||
FctFile inputFile = FctUtils.readFile(f); | ||
JSONObject resourceObject = buildResourceObject(inputFile.getContent()); | ||
resourceObjects.add(resourceObject); | ||
} | ||
buildBundle(resourceObjects); | ||
} | ||
|
||
static ArrayList<String> getResourceFiles(String pathToFolder) throws IOException { | ||
|
@@ -259,8 +362,8 @@ private static void addFhirResource(String filePath, List<String> filesArray) { | |
} | ||
} | ||
|
||
JSONObject buildResourceObject(FctFile inputFile) { | ||
JSONObject resource = new JSONObject(inputFile.getContent()); | ||
JSONObject buildResourceObject(String fileContent) { | ||
JSONObject resource = new JSONObject(fileContent); | ||
String resourceType = null; | ||
String resourceID; | ||
if (resource.has("resourceType")) { | ||
|
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.
assuming here that we are always converting from camel case
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.
can you add that to a docstring for this function?