4646import de .symeda .sormas .api .externalmessage .processing .labmessage .LabMessageProcessingHelper ;
4747import de .symeda .sormas .api .externalmessage .processing .labmessage .SampleAndPathogenTests ;
4848import de .symeda .sormas .api .feature .FeatureType ;
49- import de .symeda .sormas .api .infrastructure .country .CountryReferenceDto ;
5049import de .symeda .sormas .api .infrastructure .facility .FacilityDto ;
5150import de .symeda .sormas .api .infrastructure .facility .FacilityReferenceDto ;
5251import de .symeda .sormas .api .infrastructure .facility .FacilityType ;
53- import de .symeda .sormas .api .location .LocationDto ;
54- import de .symeda .sormas .api .person .PersonContactDetailDto ;
55- import de .symeda .sormas .api .person .PersonContactDetailType ;
5652import de .symeda .sormas .api .person .PersonDto ;
5753import de .symeda .sormas .api .person .PersonReferenceDto ;
58- import de .symeda .sormas .api .person .PhoneNumberType ;
5954import de .symeda .sormas .api .sample .PathogenTestDto ;
6055import de .symeda .sormas .api .sample .SampleCriteria ;
6156import de .symeda .sormas .api .sample .SampleDto ;
@@ -1051,12 +1046,18 @@ protected abstract void markExternalMessageAsProcessed(
10511046 ProcessingResult <ExternalMessageProcessingResult > result ,
10521047 SurveillanceReportDto surveillanceReport );
10531048
1054- protected void doPersonUpdates (EntitySelection <PersonDto > personSelection ) {
1055- // requested for #13589
1056- // TODO: we need to find a better way to handle this
1049+ @ Override
1050+ protected CompletionStage <ProcessingResult <ExternalMessageProcessingResult >> pickOrCreatePerson (ExternalMessageProcessingResult previousResult ) {
1051+ return super .pickOrCreatePerson (previousResult ).thenCompose (result -> {
1052+ if (!result .getStatus ().isCanceled () && result .getData () != null ) {
1053+ mergePerson (result .getData ().getSelectedPerson ());
1054+ }
1055+ return result .asCompletedFuture ();
1056+ });
1057+ }
10571058
1058- if ( personSelection . isNew () ) {
1059- // no updates for new persons
1059+ protected void mergePerson ( EntitySelection < PersonDto > personSelection ) {
1060+ if ( personSelection == null ) {
10601061 return ;
10611062 }
10621063
@@ -1065,124 +1066,14 @@ protected void doPersonUpdates(EntitySelection<PersonDto> personSelection) {
10651066 return ;
10661067 }
10671068
1068- boolean doUpdate = false ;
1069-
1070- final LocationDto personAddress = person .getAddress ();
1071-
1072- if (personAddress != null ) {
1073- final String houseNumber = getExternalMessage ().getPersonHouseNumber ();
1074- if (houseNumber != null ) {
1075- personAddress .setHouseNumber (houseNumber );
1076- }
1077- final String street = getExternalMessage ().getPersonStreet ();
1078- if (street != null ) {
1079- personAddress .setStreet (street );
1080- }
1081- final String city = getExternalMessage ().getPersonCity ();
1082- if (city != null ) {
1083- personAddress .setCity (city );
1084- }
1085- final String postalCode = getExternalMessage ().getPersonPostalCode ();
1086- if (postalCode != null ) {
1087- personAddress .setPostalCode (postalCode );
1088- }
1089- final CountryReferenceDto country = getExternalMessage ().getPersonCountry ();
1090- if (country != null ) {
1091- personAddress .setCountry (country );
1092- }
1093-
1094- doUpdate = true ;
1095- }
1096-
1097- final List <PersonContactDetailDto > personContactDetails = person .getPersonContactDetails ();
1098-
1099- final String phoneNumber = getExternalMessage ().getPersonPhone ();
1100- final PhoneNumberType phoneNumberType = getExternalMessage ().getPersonPhoneNumberType ();
1101-
1102- if (phoneNumber != null && !phoneNumber .isBlank ()) {
1103- final PersonContactDetailDto primaryPhone = personContactDetails .stream ()
1104- .filter (pdc -> pdc .getPersonContactDetailType () == PersonContactDetailType .PHONE && !pdc .isThirdParty () && pdc .isPrimaryContact ())
1105- .findFirst ()
1106- .orElse (null );
1107-
1108- final PersonContactDetailDto existingPhone = personContactDetails .stream ()
1109- .filter (pdc -> pdc .getPersonContactDetailType () == PersonContactDetailType .PHONE && !pdc .isThirdParty () && phoneNumber .equals (pdc .getContactInformation ()))
1110- .findFirst ()
1111- .orElse (null );
1112-
1113- if (existingPhone != null ) {
1114- // if we have a existing phone number maybe it is not the new one
1115- // make the primary phone not primary anymore and set the primary on the existing one
1116- // coincidentally the existing one may be the primary one so set it to false first just in case
1117- if (primaryPhone != null ) {
1118- primaryPhone .setPrimaryContact (false );
1119- }
1120- existingPhone .setPrimaryContact (true );
1121- } else {
1122- // we do not have the new phone number in the list so we need to create a new one
1123- final PersonContactDetailDto personContactDetail = new PersonContactDetailDto ();
1124- personContactDetail .setPerson (person .toReference ());
1125- personContactDetail .setPrimaryContact (true );
1126- personContactDetail .setPersonContactDetailType (PersonContactDetailType .PHONE );
1127- personContactDetail .setPrimaryContact (true );
1128- personContactDetail .setPhoneNumberType (phoneNumberType );
1129- personContactDetail .setContactInformation (phoneNumber );
1130- personContactDetail .setThirdParty (false );
1131- personContactDetails .add (personContactDetail );
1132-
1133- // we need to set the old primary to false
1134- if (primaryPhone != null ) {
1135- primaryPhone .setPrimaryContact (false );
1136- }
1137- }
1138-
1139- doUpdate = true ;
1140- }
1141-
1142- final String emailAddress = getExternalMessage ().getPersonEmail ();
1143-
1144- if (emailAddress != null && !emailAddress .isBlank ()) {
1145- final PersonContactDetailDto primaryEmail = personContactDetails .stream ()
1146- .filter (pdc -> pdc .getPersonContactDetailType () == PersonContactDetailType .EMAIL && !pdc .isThirdParty () && pdc .isPrimaryContact ())
1147- .findFirst ()
1148- .orElse (null );
1149-
1150- final PersonContactDetailDto existingEmail = personContactDetails .stream ()
1151- .filter (pdc -> pdc .getPersonContactDetailType () == PersonContactDetailType .EMAIL && !pdc .isThirdParty () && emailAddress .equals (pdc .getContactInformation ()))
1152- .findFirst ()
1153- .orElse (null );
1154-
1155- if (existingEmail != null ) {
1156- // if we have a existing email address maybe it is not the new one
1157- // make the primary email not primary anymore and set the primary on the existing one
1158- // coincidentally the existing one may be the primary one so set it to false first just in case
1159- if (primaryEmail != null ) {
1160- primaryEmail .setPrimaryContact (false );
1161- }
1162- existingEmail .setPrimaryContact (true );
1163- } else {
1164- // we do not have the new email address in the list so we need to create a new one
1165- final PersonContactDetailDto personContactDetail = new PersonContactDetailDto ();
1166- personContactDetail .setPerson (person .toReference ());
1167- personContactDetail .setPrimaryContact (true );
1168- personContactDetail .setPersonContactDetailType (PersonContactDetailType .EMAIL );
1169- personContactDetail .setPrimaryContact (true );
1170- personContactDetail .setContactInformation (emailAddress );
1171- personContactDetail .setThirdParty (false );
1172- personContactDetails .add (personContactDetail );
1173-
1174- // we need to set the old primary to false
1175- if (primaryEmail != null ) {
1176- primaryEmail .setPrimaryContact (false );
1177- }
1178- }
1179-
1180- doUpdate = true ;
1069+ if (personSelection .isNew ()) {
1070+ // no merges for new person
1071+ // additional contacts will be handled by {@link AbstractProcessingFlow#buildPerson()}
1072+ return ;
11811073 }
11821074
1183- if (doUpdate ) {
1184- getExternalMessageProcessingFacade ().updatePerson (person );
1185- }
1075+ getMapper ().mergePersonAddress (person );
1076+ getMapper ().mergePersonContactDetails (person );
11861077 }
11871078
11881079 /**
0 commit comments