Skip to content

Commit

Permalink
added launch-local launch point for kicking off a local-resource base…
Browse files Browse the repository at this point in the history
…d launch. includes functionality to present a list of resource bundles in a predefined local folder to the user, and to collect the name of one of them from the user in order to kick off a launch. for issue #31
  • Loading branch information
OHSUCMP committed Feb 2, 2023
1 parent 6d528c5 commit 1b21093
Show file tree
Hide file tree
Showing 6 changed files with 4,439 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/java/edu/ohsu/cmp/coach/controller/SessionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import edu.ohsu.cmp.coach.model.fhir.FHIRCredentialsWithClient;
import edu.ohsu.cmp.coach.model.recommendation.Audience;
import edu.ohsu.cmp.coach.util.FhirUtil;
import edu.ohsu.cmp.coach.util.FileUtil;
import edu.ohsu.cmp.coach.workspace.UserWorkspace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -18,6 +19,8 @@
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class SessionController extends BaseController {
Expand All @@ -26,6 +29,23 @@ public class SessionController extends BaseController {
@Value("${socket.timeout:300000}")
private Integer socketTimeout;

@Value("${security.permit-load-local}")
private Boolean permitLoadLocal;

@Value("${local-test-resources-path}")
private String localTestResourcesPath;

@GetMapping("launch-local")
public String launchLocal(Model model) throws IOException {
model.addAttribute("applicationName", applicationName);
if (permitLoadLocal) {
model.addAttribute("permitLoadLocal", true);
List<String> bundleItems = FileUtil.getFilenameList(localTestResourcesPath);
model.addAttribute("bundleItems", bundleItems);
}
return "launch-local";
}

@GetMapping("launch-ehr")
public String launchEHR(Model model) {
model.addAttribute("applicationName", applicationName);
Expand All @@ -45,6 +65,15 @@ public String launchPatient(Model model) {
return "launch-patient";
}

@PostMapping("prepare-detached-session")
public ResponseEntity<?> prepareDetachedSession(HttpSession session,
@RequestParam("bundleName") String bundleName) {

logger.info("preparing local detached session for bundle=" + bundleName);

return ResponseEntity.ok("session configured successfully");
}

@PostMapping("prepare-session")
public ResponseEntity<?> prepareSession(HttpSession session,
@RequestParam("clientId") String clientId,
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/edu/ohsu/cmp/coach/util/FileUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package edu.ohsu.cmp.coach.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {
private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);

public static List<String> getResourceFilenameList(String resourcePath) throws IOException {
List<String> list = new ArrayList<>();

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try (InputStream in = classLoader.getResourceAsStream(resourcePath)) {
if (in != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String resource;
while ((resource = reader.readLine()) != null) {
list.add(resource);
}
} else {
logger.error("couldn't obtain resource as stream for resourcePath=" + resourcePath);
}
}

return list;
}

public static List<String> getFilenameList(String path) throws IOException {
List<String> list = new ArrayList<>();

File dir = new File(path);
File[] fileArr = dir.listFiles();
if (fileArr != null) {
for (File file : fileArr) {
list.add(file.getName());
}

} else {
throw new IOException(path + " is not a directory");
}

return list;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ server.port=8082
security.salt=CHANGE_THIS_BEFORE_RUNNING_IN_PRODUCTION
security.show-dev-errors=false
security.browser.cache-credentials=false
security.permit-load-local=false

local-test-resources-path=c://git//coach//test-resource-bundles/

feature.button.clear-supplemental-data.show=false

Expand Down
65 changes: 65 additions & 0 deletions src/main/resources/templates/launch-local.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html lang="en">
{{#permitLoadLocal}}
<head>
<meta charset="UTF-8">
<title>{{applicationName}} - Load Local Patient</title>
<script type="text/javascript" src="/node_modules/jquery/dist/jquery.js"></script>
<script type="text/javascript">
const REDIRECT_URL = "/";
function prepareSession(bundleName) {
let data = {
bundleName: bundleName
};
// alert("useful development breakpoint");
$.ajax({
method: "POST",
url: "/prepare-detached-session",
data: data
}).done(function(cards, textStatus, jqXHR) {
if (jqXHR.status === 200) {
$('body').html("Handshake completed, redirecting ...<br/>If you are not redirected, please <a href='" + REDIRECT_URL + "'>click here</a>.")
window.location.href = REDIRECT_URL;
} else {
$('body').html("Error - received response " + response.status + " status");
}
});
}
$(document).on('click', '#launchButton', function() {
let bundleName = $('#bundleName').val();
prepareSession(bundleName);
});
$(document).on('change', '#bundleName', function() {
$('#launchButton').prop('disabled', $(this).val() === '');
});
</script>
</head>
<body>
Select a local FHIR Resource Bundle:
<form action="/prepare-detached-session">
<select id="bundleName">
<option value="">-- Select a Local Bundle --</option>
{{#bundleItems}}
<option value="{{.}}">{{.}}</option>
{{/bundleItems}}
</select>
<input id="launchButton" type="button" value="Launch Detached" disabled />
</form>
</body>
{{/permitLoadLocal}}
{{^permitLoadLocal}}
<head>
<meta charset="UTF-8">
<title>{{applicationName}} - Operation Not Supported</title>
</head>
<body>
This operation is not currently supported.
</body>
{{/permitLoadLocal}}
</html>
Loading

0 comments on commit 1b21093

Please sign in to comment.