33import static de .netzwerk_universitaetsmedizin .codex .processes .data_transfer .ConstantsDataTransfer .BPMN_EXECUTION_VARIABLE_EXPORT_FROM ;
44import static de .netzwerk_universitaetsmedizin .codex .processes .data_transfer .ConstantsDataTransfer .BPMN_EXECUTION_VARIABLE_EXPORT_FROM_PRECISION ;
55import static de .netzwerk_universitaetsmedizin .codex .processes .data_transfer .ConstantsDataTransfer .BPMN_EXECUTION_VARIABLE_EXPORT_TO ;
6+ import static de .netzwerk_universitaetsmedizin .codex .processes .data_transfer .ConstantsDataTransfer .BPMN_EXECUTION_VARIABLE_LAST_EXPORT_TO ;
67import static de .netzwerk_universitaetsmedizin .codex .processes .data_transfer .ConstantsDataTransfer .BPMN_EXECUTION_VARIABLE_PSEUDONYMS_LIST ;
8+ import static de .netzwerk_universitaetsmedizin .codex .processes .data_transfer .ConstantsDataTransfer .CODESYSTEM_NUM_CODEX_DATA_TRANSFER ;
9+ import static de .netzwerk_universitaetsmedizin .codex .processes .data_transfer .ConstantsDataTransfer .CODESYSTEM_NUM_CODEX_DATA_TRANSFER_VALUE_EXPORT_FROM ;
710import static org .highmed .dsf .bpe .ConstantsBase .BPMN_EXECUTION_VARIABLE_TARGET ;
811
912import java .util .Arrays ;
1013import java .util .Date ;
11- import java .util .List ;
1214import java .util .Objects ;
15+ import java .util .Optional ;
16+ import java .util .stream .Stream ;
1317
1418import org .camunda .bpm .engine .delegate .BpmnError ;
1519import org .camunda .bpm .engine .delegate .DelegateExecution ;
1620import org .camunda .bpm .engine .variable .Variables ;
17- import org .camunda .bpm .engine .variable .Variables .SerializationDataFormats ;
1821import org .highmed .dsf .bpe .delegate .AbstractServiceDelegate ;
1922import org .highmed .dsf .fhir .client .FhirWebserviceClientProvider ;
2023import org .highmed .dsf .fhir .organization .OrganizationProvider ;
2124import org .highmed .dsf .fhir .task .TaskHelper ;
2225import org .highmed .dsf .fhir .variables .Target ;
2326import org .highmed .dsf .fhir .variables .TargetValues ;
27+ import org .hl7 .fhir .r4 .model .DateTimeType ;
28+ import org .hl7 .fhir .r4 .model .Task ;
29+ import org .hl7 .fhir .r4 .model .Type ;
2430import org .slf4j .Logger ;
2531import org .slf4j .LoggerFactory ;
2632import org .springframework .beans .factory .InitializingBean ;
2733
2834import ca .uhn .fhir .model .api .TemporalPrecisionEnum ;
2935import de .netzwerk_universitaetsmedizin .codex .processes .data_transfer .client .FhirClientFactory ;
36+ import de .netzwerk_universitaetsmedizin .codex .processes .data_transfer .variables .PseudonymList ;
37+ import de .netzwerk_universitaetsmedizin .codex .processes .data_transfer .variables .PseudonymListValues ;
3038
3139public class FindNewData extends AbstractServiceDelegate implements InitializingBean
3240{
41+ @ SuppressWarnings ("serial" )
42+ private static class DateWithPrecision extends Date
43+ {
44+ private final TemporalPrecisionEnum precision ;
45+
46+ DateWithPrecision (Date exportFrom , TemporalPrecisionEnum precision )
47+ {
48+ super (exportFrom .getTime ());
49+ this .precision = precision ;
50+ }
51+ }
52+
3353 private static final Logger logger = LoggerFactory .getLogger (FindNewData .class );
3454
3555 private final OrganizationProvider organizationProvider ;
@@ -56,40 +76,56 @@ public void afterPropertiesSet() throws Exception
5676 @ Override
5777 protected void doExecute (DelegateExecution execution ) throws BpmnError , Exception
5878 {
59- Date exportFrom = (Date ) execution .getVariable (BPMN_EXECUTION_VARIABLE_EXPORT_FROM );
60- String exportFromPrecisionStr = (String ) execution .getVariable (BPMN_EXECUTION_VARIABLE_EXPORT_FROM_PRECISION );
61- TemporalPrecisionEnum exportFromPrecision = exportFromPrecisionStr == null ? null
62- : TemporalPrecisionEnum .valueOf (exportFromPrecisionStr );
63- Date exportTo = (Date ) execution .getVariable (BPMN_EXECUTION_VARIABLE_EXPORT_TO );
79+ Optional <DateWithPrecision > exportFrom = getExportFrom (execution );
80+ Date exportTo = new Date ();
6481
65- List < String > pseudonyms = searchForPseudonymsWithNewData (exportFrom , exportFromPrecision , exportTo );
82+ PseudonymList pseudonyms = searchForPseudonymsWithNewData (exportFrom . orElse ( null ) , exportTo );
6683
67- execution .setVariable (BPMN_EXECUTION_VARIABLE_PSEUDONYMS_LIST ,
68- Variables .objectValue (pseudonyms ).serializationDataFormat (SerializationDataFormats .JSON ).create ());
84+ execution .setVariable (BPMN_EXECUTION_VARIABLE_EXPORT_FROM , Variables .dateValue (exportFrom .orElse (null )));
85+ execution .setVariable (BPMN_EXECUTION_VARIABLE_EXPORT_FROM_PRECISION ,
86+ Variables .stringValue (exportFrom .map (p -> p .precision ).map (Enum ::name ).orElse (null )));
87+ execution .setVariable (BPMN_EXECUTION_VARIABLE_EXPORT_TO , Variables .dateValue (exportTo ));
88+ execution .setVariable (BPMN_EXECUTION_VARIABLE_LAST_EXPORT_TO , Variables .dateValue (exportTo ));
89+ execution .setVariable (BPMN_EXECUTION_VARIABLE_PSEUDONYMS_LIST , PseudonymListValues .create (pseudonyms ));
6990 execution .setVariable (BPMN_EXECUTION_VARIABLE_TARGET ,
7091 TargetValues .create (Target .createUniDirectionalTarget (organizationProvider .getLocalIdentifierValue ())));
7192 }
7293
73- /**
74- * @param exportFrom
75- * may be <code>null</code>
76- * @param exportFromPrecision
77- * may be <code>null</code>
78- * @param exportTo
79- * not <code>null</code>
80- * @return
81- */
82- private List <String > searchForPseudonymsWithNewData (Date exportFrom , TemporalPrecisionEnum exportFromPrecision ,
83- Date exportTo )
94+ protected Optional <DateWithPrecision > getExportFrom (DelegateExecution execution )
95+ {
96+ Date lastExportTo = (Date ) execution .getVariable (BPMN_EXECUTION_VARIABLE_LAST_EXPORT_TO );
97+
98+ if (lastExportTo != null )
99+ return Optional .of (new DateWithPrecision (lastExportTo , TemporalPrecisionEnum .MILLI ));
100+
101+ Optional <DateTimeType > exportFromInput = getExportFromInput (getCurrentTaskFromExecutionVariables ());
102+ return exportFromInput .map (d -> new DateWithPrecision (d .getValue (), d .getPrecision ()));
103+ }
104+
105+ private Optional <DateTimeType > getExportFromInput (Task task )
106+ {
107+ return getInputParameterValues (task , CODESYSTEM_NUM_CODEX_DATA_TRANSFER ,
108+ CODESYSTEM_NUM_CODEX_DATA_TRANSFER_VALUE_EXPORT_FROM , DateTimeType .class ).findFirst ();
109+ }
110+
111+ private <T extends Type > Stream <T > getInputParameterValues (Task task , String system , String code , Class <T > type )
112+ {
113+ return task .getInput ().stream ().filter (c -> type .isInstance (c .getValue ()))
114+ .filter (c -> c .getType ().getCoding ().stream ()
115+ .anyMatch (co -> system .equals (co .getSystem ()) && code .equals (co .getCode ())))
116+ .map (c -> type .cast (c .getValue ()));
117+ }
118+
119+ protected PseudonymList searchForPseudonymsWithNewData (DateWithPrecision exportFrom , Date exportTo )
84120 {
85121 logger .debug ("Searching for new data to transfer from {} with precision {} to {}" , exportFrom ,
86- exportFromPrecision , exportTo );
122+ exportFrom . precision , exportTo );
87123
88124 // TODO implement FHIR search
89125
90126 // IGenericClient client = localFhirStoreClientFactory.createClient();
91127 // client.search().forResource(Patient.class).withProfile("profile").lastUpdated(new DateRangeParam().set)
92128
93- return Arrays .asList ("1234567890" );
129+ return new PseudonymList ( Arrays .asList ("source/original" ) );
94130 }
95131}
0 commit comments