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

[SEP] Send tasks #22

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.camunda.bpm.delegate;

import java.io.IOException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.impl.instance.DataObjectReferenceImpl;
import org.camunda.bpm.model.bpmn.instance.DataInputAssociation;
import org.camunda.bpm.model.bpmn.instance.Documentation;
import org.camunda.bpm.model.bpmn.instance.ItemAwareElement;
import org.camunda.bpm.model.bpmn.instance.SendTask;

import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;

public class SendTaskEntry implements JavaDelegate {
@Override
public void execute(DelegateExecution delegateExecution) throws Exception {
Properties properties = new Properties();
try {
properties.load(getClass().getClassLoader().getResourceAsStream("config.properties"));
} catch (IOException e) {
e.printStackTrace();
}

SendTask sendTask = null;
try {
sendTask = delegateExecution.getProcessEngine().getRepositoryService().getBpmnModelInstance(delegateExecution.getProcessDefinitionId()).getModelElementById(delegateExecution.getCurrentActivityId());
} catch (Exception e) {
e.printStackTrace();
}

String data = "";

try {
BpmnModelInstance bpmModel = delegateExecution.getProcessEngine().getRepositoryService().getBpmnModelInstance(delegateExecution.getProcessDefinitionId());
for (DataInputAssociation dataInputAssociation : sendTask.getDataInputAssociations()) {
for (ItemAwareElement source : dataInputAssociation.getSources()) {
DataObjectReferenceImpl dataReference = bpmModel.getModelElementById(source.getId());
for (Documentation documentation : dataReference.getDocumentations()) {
data = documentation.getRawTextContent();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

StringBuffer sb = new StringBuffer();
Matcher m = Pattern.compile("\\$\\((.*?)\\)").matcher(data);
int count = 1;
while (m.find()) {
String reqVariable = m.group(count);
m.appendReplacement(sb, (String) delegateExecution.getVariable(reqVariable));
count++;
}
m.appendTail(sb);

HttpResponse<JsonNode> httpResponse = Unirest.post(properties.getProperty("wfc.url") +
"/PostObservationValue/")
.body(sb.toString())
.asJson();
}
}
44 changes: 44 additions & 0 deletions new_scripts/build_all_dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
CURRENT_DIR=$(pwd)
CURRENT_DIR_LENGTH=${#CURRENT_DIR}
if [ "${CURRENT_DIR: -12}" != "/new_scripts" ]; then
echo ""
echo "You are not in the correct directory. Please run this script from the root directory of the project."
echo ""
exit 1
fi

echo "--------------- Now building apps... ---------------"
cd ..

echo ""
echo "---------- Camunda build-script starting ----------"
cd engine/camunda
mvn -B package --file pom.xml
cd ../..
echo "---------- Camunda build-script finished ----------"

echo ""
echo "---------- Caregiver app build-script starting ----------"
cd caregiver_application
pip install -r requirements.txt
cd ..
echo "---------- Caregiver app build-script finished ----------"

echo ""
echo "---------- Management Dashboard build-script starting ----------"
cd management_dashboard
npm install
cd ..
echo "---------- Management Dashboard build-script finished ----------"

echo ""
echo "---------- WFC build-script starting ----------"
cd workflow_capability_core
mvn -B package --file pom.xml
cd ..
echo "---------- WFC build-script finished ----------"

cd scripts
echo ""
echo "----------------- Apps built! -----------------"
58 changes: 58 additions & 0 deletions new_scripts/serve_dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

# Ask if demos should be served
read -p "Serve all of the demos? (y/n) [n]: " SERVE_DEMOS

echo "---- Removing docker containers ----"
docker container stop $(docker container ls -aq)
docker container rm $(docker container ls -aq)

echo "---- Starting Postgres ----"
docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=admin postgres
sleep 5

echo "---- Starting FHIR store ----"
docker run -d -p 8180:8080 -v "/$(pwd)/../fhir_jpa_config:/data" -e "--spring.config.location=file:///data/application.yaml" hapiproject/hapi:latest
# get container id of this container
CONTAINER_ID=$(docker ps --filter "ancestor=hapiproject/hapi:latest" --format "{{.ID}}")
sleep 5

echo "--- Wait until HAPI FHIR has started ---"
# We wait by checking the last line of the logs of the container for the string "Started Application in"
while true; do
CONTAINER_LOG_LAST_LINE=$(docker logs --tail 20 $CONTAINER_ID)
if [[ $CONTAINER_LOG_LAST_LINE == *"Started Application in"* ]]; then
break
fi
sleep 1
done

echo "-- STARTING CAMUNDA --"
cd ..
bash -eo pipefail -c "java -jar engine/camunda/target/camunda_engine-0.0.2-SNAPSHOT.jar | awk '{print \"[CAM] \"\$0}'" &
cd new_scripts

echo "-- STARTING CAREGIVER APP --"
cd ..
bash -eo pipefail -c "python caregiver_application/main.py | awk '{print \"[CGA] \"\$0}'" &
cd new_scripts

# echo "-- STARTING MANAGEMENT DASHBOARD --"
# cd ..
# cd management_dashboard
# npm run start &
# cd ..
# cd new_scripts

sleep 10

echo "-- STARTING WFC --"
cd ..
if [ "$SERVE_DEMOS" == "y" ]; then
bash -eo pipefail -c "java -jar workflow_capability_core/target/workflow_capability_core-0.0.2-SNAPSHOT.jar withAllDemos | awk '{print \"[WFC] \"\$0}'" &
else
bash -eo pipefail -c "java -jar workflow_capability_core/target/workflow_capability_core-0.0.2-SNAPSHOT.jar | awk '{print \"[WFC] \"\$0}'" &
fi
cd new_scripts

read -n 1 -s -r -p "Press any key to continue..."
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ private File addCamundaTags(File xmlFile) throws IOException, SAXException, Pars
modifier.addStartEventListener();
modifier.addUserTaskListener();
modifier.addReceiveTaskListener();
modifier.addSendTaskListener();
modifier.addEndEventListener();
return modifier.saveBPMNModel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,33 @@ private NodeList getReceiveTasks() {
return doc.getElementsByTagName("bpmn:receiveTask");
}

/**
* Adds the StartEventDelegate reference to the Send Task in the BPMN XML Definition
*/
public void addSendTaskListener() {
NodeList sendTasks = this.getSendTasks();
for (int i = 0; i < sendTasks.getLength(); i++) {
Node sendTask = sendTasks.item(i);
Boolean hasDataInput = false;
// Check if the SendTasks is requesting Data from FHIR
if (sendTask.hasChildNodes()) {
NodeList childNodes = sendTask.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
if (childNodes.item(j).getNodeName() == "bpmn:dataInputAssociation") {
hasDataInput = true;
}
}
}
if (hasDataInput) {
addListenerToNode(sendTask, "org.camunda.bpm.delegate.SendTaskEntry", "startExecutionListener");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expect the SendTaskExecutionListener here

}
}
}

private NodeList getSendTasks() {
return doc.getElementsByTagName("bpmn:sendTask");
}


private NodeList getUserTasks() {
return doc.getElementsByTagName("bpmn:userTask");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;

import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Observation;
import org.hl7.fhir.r4.model.Resource;
import org.hl7.fhir.r4.model.StringType;
import org.hl7.fhir.r4.model.Subscription;
Expand All @@ -32,6 +35,27 @@ public EngineQueryHandler() throws IOException {
pendingRequests = new HashMap<>();
}

public void postFhirResource(@NotNull String data) {
FhirContext ctx = FhirContext.forR4();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get cached FhirContext

try {
IParser parser = ctx.newJsonParser();
IBaseResource resource = parser.parseResource(data);
String fhirResource = resource.fhirType();

postFhirObject(fhirResource, data);
}
catch (Exception e) {
System.out.println("Incorrect data" + e);
}
}

private void postFhirObject(String fhirResource, String data) {
HttpResponse<JsonNode> httpResponse = Unirest.post(properties.get("config.fhirUrl") + "/fhir/" +
fhirResource)
.header("Content-Type", "application/json")
.body(data)
.asJson();
}

public Resource getFhirResource(@NotNull String query, String returnMessage, String processID, String variableName) {
FhirContext ctx = FhirContext.forR4();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ void informEngineAboutObservationValue(@PathVariable("processID") String process
}
}

/**
* @param processID
* @param returnMessage
* @param variableName
* @param query
* @throws IOException
*/
@RequestMapping(
value = "/PostObservationValue/",
method = RequestMethod.POST)
@Async
void postObservationValue(@RequestBody String data) throws IOException {
this.engineQueryHandler.postFhirResource(data);
}


/**
* @param carePlanInstanceID
Expand Down