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

Directly Assign Pharmacy #24

Merged
merged 8 commits into from
Jun 19, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[submodule "src/main/resources/META-INF/resources/frontend"]
path = src/main/resources/META-INF/resources/frontend
url = https://github.com/ere-health/front-end-ere.health.git
branch = ERE-381---Demo-Erixa
54 changes: 54 additions & 0 deletions src/main/java/health/ere/ps/config/UserConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package health.ere.ps.config;


import health.ere.ps.service.extractor.TemplateProfile;
import health.ere.ps.service.extractor.SVGExtractorConfiguration;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.enterprise.context.ApplicationScoped;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;

@ApplicationScoped
public class UserConfig {

private final Logger log = Logger.getLogger(UserConfig.class.getName());

private final Properties erixaProperties;

@ConfigProperty(name = "muster16.template.configuration", defaultValue = "DENS")
String muster16TemplateConfiguration;

public UserConfig() {
erixaProperties = erixaProperties();
}

public String getErixaHotfolder() {
return erixaProperties.getProperty("erixa.hotfolder");
}

public String getErixaReceiverEmail() {
return erixaProperties.getProperty("erixa.receiver.email");
}

public SVGExtractorConfiguration getMuster16TemplateConfiguration() {
TemplateProfile profile;
try {
profile = TemplateProfile.valueOf(muster16TemplateConfiguration);
} catch (IllegalArgumentException ignored) {
profile = TemplateProfile.DENS;
}
return profile.configuration;
}

private Properties erixaProperties() {
final Properties properties = new Properties();
try {
properties.load(UserConfig.class.getResourceAsStream("/eRiXa.properties"));
} catch (IOException e) {
log.warning("Failed to load eRiXa.properties");
}
return properties;
}
}
16 changes: 16 additions & 0 deletions src/main/java/health/ere/ps/event/ErixaEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package health.ere.ps.event;


import javax.json.JsonObject;


public class ErixaEvent {

public final String processType;
public final JsonObject payload;

public ErixaEvent(JsonObject jsonObject) {
payload = jsonObject.getJsonObject("payload");
processType = payload.getString("processType");
}
}
12 changes: 12 additions & 0 deletions src/main/java/health/ere/ps/event/ErixaSyncEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package health.ere.ps.event;

import health.ere.ps.model.erixa.ErixaSyncLoad;

public class ErixaSyncEvent {

public final ErixaSyncLoad load;

public ErixaSyncEvent(ErixaSyncLoad load) {
this.load = load;
}
}
31 changes: 31 additions & 0 deletions src/main/java/health/ere/ps/model/erixa/ErixaSyncLoad.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package health.ere.ps.model.erixa;

public class ErixaSyncLoad {

private String document;
private String patient;

public ErixaSyncLoad() {
}

public ErixaSyncLoad(String document, String patient) {
this.document = document;
this.patient = patient;
}

public void setDocument(String document) {
this.document = document;
}

public void setPatient(String patient) {
this.patient = patient;
}

public String getDocument() {
return document;
}

public String getPatient() {
return patient;
}
}
28 changes: 28 additions & 0 deletions src/main/java/health/ere/ps/resource/erixa/ErixaResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package health.ere.ps.resource.erixa;


import health.ere.ps.event.ErixaSyncEvent;
import health.ere.ps.model.erixa.ErixaSyncLoad;

import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;


@Path("erixa")
public class ErixaResource {


@Inject
Event<ErixaSyncEvent> erixaEvent;

@POST
@Path("/sync")
public Response handleSync(ErixaSyncLoad load) {
ErixaSyncEvent event = new ErixaSyncEvent(load);
erixaEvent.fireAsync(event);
return Response.ok().build();
}
}
30 changes: 30 additions & 0 deletions src/main/java/health/ere/ps/service/erixa/ErixaService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package health.ere.ps.service.erixa;

import health.ere.ps.event.ErixaEvent;
import health.ere.ps.event.ErixaSyncEvent;
import health.ere.ps.model.erixa.ErixaSyncLoad;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Event;
import javax.enterprise.event.ObservesAsync;
import javax.inject.Inject;
import java.util.logging.Logger;

@ApplicationScoped
public class ErixaService {

private final Logger log = Logger.getLogger(ErixaService.class.getName());

@Inject
Event<ErixaSyncEvent> erixaSyncEvent;

public void generatePrescriptionBundle(@ObservesAsync ErixaEvent event) {

if ("sync".equals(event.processType)) {
ErixaSyncLoad load = new ErixaSyncLoad(event.payload.getString("document"), event.payload.getString("patient"));
ErixaSyncEvent syncEvent = new ErixaSyncEvent(load);
erixaSyncEvent.fireAsync(syncEvent);
}
}
}

69 changes: 69 additions & 0 deletions src/main/java/health/ere/ps/service/erixa/ErixaSyncService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package health.ere.ps.service.erixa;

import health.ere.ps.config.UserConfig;
import health.ere.ps.event.ErixaSyncEvent;
import health.ere.ps.model.erixa.ErixaSyncLoad;
import org.apache.pdfbox.pdmodel.PDDocument;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.ObservesAsync;
import javax.inject.Inject;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.logging.Logger;

@ApplicationScoped
public class ErixaSyncService {

private final Logger log = Logger.getLogger(ErixaSyncService.class.getName());

private final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyyMMdd");

@Inject
UserConfig userConfig;

public void syncToHotfolder(@ObservesAsync ErixaSyncEvent event) {
log.info("ErixaSyncEvent");
try {
final ErixaSyncLoad load = event.load;
PDDocument document = parseDocument(load.getDocument());
Path path = buildFilePath(load.getPatient());
saveDocument(document, path);
} catch (IOException e) {
log.severe("Unable to parse document");
}
}

private PDDocument parseDocument(String document) throws IOException {
byte[] decodedString = Base64.getDecoder().decode(document.getBytes(StandardCharsets.UTF_8));
return PDDocument.load(decodedString);
}

private Path buildFilePath(String patientName) {
return getHotfolderPath().resolve(buildFileName(patientName));
}

private Path getHotfolderPath() {
return Path.of(userConfig.getErixaHotfolder());
}

private String buildFileName(String patientName) {
return String.format("[%s] [%s] [%s].pdf", getDateString(), patientName, getReceiverEmail());
}

private String getDateString() {
return LocalDate.now().format(dateFormat);
}

private String getReceiverEmail() {
return userConfig.getErixaReceiverEmail();
}

private void saveDocument(PDDocument document, Path path) throws IOException {
document.save(path.toFile());
}
}
47 changes: 30 additions & 17 deletions src/main/java/health/ere/ps/service/extractor/SVGExtractor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package health.ere.ps.service.extractor;

import health.ere.ps.config.UserConfig;
import health.ere.ps.event.PDDocumentEvent;
import health.ere.ps.event.SVGExtractorResultEvent;
import org.apache.pdfbox.pdmodel.PDDocument;
Expand Down Expand Up @@ -32,39 +33,43 @@
public class SVGExtractor {

private static final Logger log = Logger.getLogger(SVGExtractor.class.getName());
private final SVGExtractorConfiguration configuration;
private final String DEFAULT_TEMPLATE = "/svg-extract-templates/Muster-16-Template.svg";

@Inject
Event<Exception> exceptionEvent;

@Inject
Event<SVGExtractorResultEvent> sVGExtractorResultEvent;

private String templatePath = "/svg-extract-templates/Muster-16-Template.svg";
private boolean debugRectangles = false;

private SVGExtractorConfiguration configuration;

public SVGExtractor() {
this(SVGExtractorConfiguration.DENS);
}

@Inject
public SVGExtractor(UserConfig userConfig) {
this(userConfig.getMuster16TemplateConfiguration());
}

public SVGExtractor(SVGExtractorConfiguration configuration) {
if (configuration.MUSTER_16_TEMPLATE != null && !configuration.MUSTER_16_TEMPLATE.isEmpty()) {
if (configuration.MUSTER_16_TEMPLATE != null && !configuration.MUSTER_16_TEMPLATE.equals(""))
log.log(Level.INFO, "Using muster 16 template: " + configuration.MUSTER_16_TEMPLATE);
this.templatePath = configuration.MUSTER_16_TEMPLATE;
}

this.configuration = configuration;
}

public SVGExtractor(SVGExtractorConfiguration configuration, boolean debugRectangles) {
this(configuration);
this.debugRectangles = debugRectangles;
this.setDebugRectangles(debugRectangles);
}

public void analyzeDocument(@ObservesAsync PDDocumentEvent pDDocumentEvent) {
log.info("SVGExtractor.analyzeDocument");
try {
Map<String, String> extractionResult = extract(pDDocumentEvent.getPDDocument());
sVGExtractorResultEvent.fireAsync(new SVGExtractorResultEvent(extractionResult));
Map<String, String> extractResult = extract(pDDocumentEvent.getPDDocument());
sVGExtractorResultEvent.fireAsync(new SVGExtractorResultEvent(extractResult));
} catch (Exception e) {
log.log(Level.SEVERE, "Could not extract results", e);
exceptionEvent.fireAsync(e);
Expand All @@ -76,18 +81,15 @@ public Map<String, String> extract(PDDocument document) throws IOException, XMLS
if (configuration.ROTATE_DEGREE != 0) {
page.setRotation(configuration.ROTATE_DEGREE);
}

Map<String, String> map = new HashMap<>();
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLEventReader reader = xmlInputFactory.createXMLEventReader(getTemplate());
boolean rectFetchMode = false;

while (reader.hasNext()) {
XMLEvent nextEvent = reader.nextEvent();
if (nextEvent.isStartElement()) {
StartElement startElement = nextEvent.asStartElement();
String localPart = startElement.getName().getLocalPart();

if ("g".equals(localPart)
&& "fields".equals(startElement.getAttributeByName(new QName("id")).getValue())) {
rectFetchMode = true;
Expand All @@ -97,8 +99,7 @@ public Map<String, String> extract(PDDocument document) throws IOException, XMLS
float y = java.lang.Float.parseFloat(startElement.getAttributeByName(new QName("y")).getValue()) * configuration.SCALE + configuration.Y_OFFSET;
float width = java.lang.Float.parseFloat(startElement.getAttributeByName(new QName("width")).getValue()) * configuration.SCALE;
float height = java.lang.Float.parseFloat(startElement.getAttributeByName(new QName("height")).getValue()) * configuration.SCALE;

if (debugRectangles) {
if (isDebugRectangles()) {
PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true);
if (configuration.ROTATE_DEGREE == 90)
contentStream.addRect(y, x, height, width);
Expand All @@ -114,12 +115,12 @@ public Map<String, String> extract(PDDocument document) throws IOException, XMLS
}
}
}
if (debugRectangles)
if (isDebugRectangles())
saveDebugFile(document);
return map;
}

private String extractTextAtPosition(PDDocument document, String id, float x, float y, float width, float height) throws IOException {
public String extractTextAtPosition(PDDocument document, String id, float x, float y, float width, float height) throws IOException {
PDFTextStripperByArea textStripper;
textStripper = new PDFTextStripperByArea();
PDPage docPage = document.getPage(0);
Expand All @@ -138,7 +139,19 @@ private void saveDebugFile(PDDocument document) throws IOException {
document.close();
}

private String getTemplatePath() {
return configuration.MUSTER_16_TEMPLATE != null ? configuration.MUSTER_16_TEMPLATE : DEFAULT_TEMPLATE;
}

private InputStream getTemplate() {
return SVGExtractor.class.getResourceAsStream(templatePath);
return SVGExtractor.class.getResourceAsStream(getTemplatePath());
}

public boolean isDebugRectangles() {
return debugRectangles;
}

public void setDebugRectangles(boolean debugRectangles) {
this.debugRectangles = debugRectangles;
}
}
Loading