5151import de .symeda .sormas .api .infrastructure .facility .FacilityReferenceDto ;
5252import de .symeda .sormas .api .infrastructure .facility .FacilityType ;
5353import de .symeda .sormas .api .location .LocationDto ;
54+ import de .symeda .sormas .api .person .PersonContactDetailDto ;
55+ import de .symeda .sormas .api .person .PersonContactDetailType ;
5456import de .symeda .sormas .api .person .PersonDto ;
5557import de .symeda .sormas .api .person .PersonReferenceDto ;
58+ import de .symeda .sormas .api .person .PhoneNumberType ;
5659import de .symeda .sormas .api .sample .PathogenTestDto ;
5760import de .symeda .sormas .api .sample .SampleCriteria ;
5861import de .symeda .sormas .api .sample .SampleDto ;
@@ -691,7 +694,7 @@ protected SurveillanceReportDto createSurveillanceReport(ExternalMessageDto exte
691694 return surveillanceReport ;
692695 }
693696
694- /**
697+ /**
695698 * Updates the additional data of the surveillance report.
696699 *
697700 * @param surveillanceReport
@@ -701,7 +704,10 @@ protected SurveillanceReportDto createSurveillanceReport(ExternalMessageDto exte
701704 * @param caze
702705 * the case
703706 */
704- protected void updateSurveillanceReportAdditionalData (SurveillanceReportDto surveillanceReport , ExternalMessageDto externalMessage , CaseDataDto caze ) {
707+ protected void updateSurveillanceReportAdditionalData (
708+ SurveillanceReportDto surveillanceReport ,
709+ ExternalMessageDto externalMessage ,
710+ CaseDataDto caze ) {
705711 // no additional data to update for default implementation
706712 }
707713
@@ -1059,6 +1065,8 @@ protected void doPersonUpdates(EntitySelection<PersonDto> personSelection) {
10591065 return ;
10601066 }
10611067
1068+ boolean doUpdate = false ;
1069+
10621070 final LocationDto personAddress = person .getAddress ();
10631071
10641072 if (personAddress != null ) {
@@ -1083,6 +1091,96 @@ protected void doPersonUpdates(EntitySelection<PersonDto> personSelection) {
10831091 personAddress .setCountry (country );
10841092 }
10851093
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 ;
1181+ }
1182+
1183+ if (doUpdate ) {
10861184 getExternalMessageProcessingFacade ().updatePerson (person );
10871185 }
10881186 }
0 commit comments