1818package de .symeda .sormas .backend .contact ;
1919
2020import static de .symeda .sormas .backend .visit .VisitLogic .getVisitResult ;
21+ import static java .time .temporal .ChronoUnit .DAYS ;
2122
2223import java .math .BigInteger ;
2324import java .sql .Timestamp ;
3031import java .util .List ;
3132import java .util .Map ;
3233import java .util .Optional ;
33- import java .util .Random ;
3434import java .util .function .Function ;
3535import java .util .stream .Collectors ;
3636import java .util .stream .Stream ;
5858import org .slf4j .Logger ;
5959import org .slf4j .LoggerFactory ;
6060
61+ import de .symeda .sormas .api .CountryHelper ;
6162import de .symeda .sormas .api .Disease ;
6263import de .symeda .sormas .api .Language ;
6364import de .symeda .sormas .api .caze .CaseReferenceDto ;
117118import de .symeda .sormas .backend .clinicalcourse .ClinicalCourseFacadeEjb ;
118119import de .symeda .sormas .backend .common .AbstractAdoService ;
119120import de .symeda .sormas .backend .common .AbstractDomainObject ;
121+ import de .symeda .sormas .backend .common .ConfigFacadeEjb ;
122+ import de .symeda .sormas .backend .common .TaskCreationException ;
120123import de .symeda .sormas .backend .epidata .EpiData ;
121124import de .symeda .sormas .backend .epidata .EpiDataFacadeEjb ;
122125import de .symeda .sormas .backend .epidata .EpiDataFacadeEjb .EpiDataFacadeEjbLocal ;
@@ -193,6 +196,8 @@ public class ContactFacadeEjb implements ContactFacade {
193196 private EpiDataFacadeEjbLocal epiDataFacade ;
194197 @ EJB
195198 private ClinicalCourseFacadeEjb .ClinicalCourseFacadeEjbLocal clinicalCourseFacade ;
199+ @ EJB
200+ private ConfigFacadeEjb .ConfigFacadeEjbLocal configFacade ;
196201
197202 @ Override
198203 public List <String > getAllActiveUuids () {
@@ -274,9 +279,10 @@ public ContactDto saveContact(ContactDto dto, boolean handleChanges) {
274279 // }
275280
276281 Contact entity = fromDto (dto );
277-
278282 contactService .ensurePersisted (entity );
279283
284+ createInvestigationTask (entity );
285+
280286 if (handleChanges ) {
281287 updateContactVisitAssociations (existingContactDto , entity );
282288
@@ -291,6 +297,22 @@ public ContactDto saveContact(ContactDto dto, boolean handleChanges) {
291297 return toDto (entity );
292298 }
293299
300+ private void createInvestigationTask (Contact entity ) {
301+ LocalDate now = LocalDate .now ();
302+ LocalDate reportDate = DateHelper8 .toLocalDate (entity .getReportDateTime ());
303+ if (DAYS .between (reportDate , now ) <= 30 ) {
304+ try {
305+ User assignee = taskService .getTaskAssignee (entity );
306+ LocalDateTime fromDateTime = LocalDate .now ().atStartOfDay ();
307+ LocalDateTime toDateTime = fromDateTime .plusDays (1 );
308+ Task task = createContactTask (TaskType .CONTACT_INVESTIGATION , fromDateTime , toDateTime , entity , assignee );
309+ taskService .ensurePersisted (task );
310+ } catch (TaskCreationException e ) {
311+ logger .warn (e .getMessage ());
312+ }
313+ }
314+ }
315+
294316 private void updateContactVisitAssociations (ContactDto existingContact , Contact contact ) {
295317
296318 if (existingContact != null
@@ -1232,47 +1254,12 @@ public void generateContactFollowUpTasks() {
12321254 continue ;
12331255 }
12341256
1235- User assignee = null ;
1236- if (contact .getContactOfficer () != null ) {
1237- // 1) The contact officer that is responsible for the contact
1238- assignee = contact .getContactOfficer ();
1239- } else {
1240- // 2) A random contact officer from the contact's, contact person's or contact case's district
1241- List <User > officers = new ArrayList <>();
1242- if (contact .getDistrict () != null ) {
1243- officers = userService .getAllByDistrict (contact .getDistrict (), false , UserRole .CONTACT_OFFICER );
1244- }
1245- if (officers .isEmpty () && contact .getPerson ().getAddress ().getDistrict () != null ) {
1246- officers = userService .getAllByDistrict (contact .getPerson ().getAddress ().getDistrict (), false , UserRole .CONTACT_OFFICER );
1247- }
1248- if (officers .isEmpty () && contact .getCaze () != null && contact .getCaze ().getDistrict () != null ) {
1249- officers = userService .getAllByDistrict (contact .getCaze ().getDistrict (), false , UserRole .CONTACT_OFFICER );
1250- }
1251- if (!officers .isEmpty ()) {
1252- Random rand = new Random ();
1253- assignee = officers .get (rand .nextInt (officers .size ()));
1254- }
1255- }
1256-
1257- if (assignee == null ) {
1258- // 3) Assign a random contact supervisor from the contact's, contact person's or contact case's region
1259- List <User > supervisors = new ArrayList <>();
1260- if (contact .getRegion () != null ) {
1261- supervisors = userService .getAllByRegionAndUserRoles (contact .getRegion (), UserRole .CONTACT_SUPERVISOR );
1262- }
1263- if (supervisors .isEmpty () && contact .getPerson ().getAddress ().getRegion () != null ) {
1264- supervisors = userService .getAllByRegionAndUserRoles (contact .getPerson ().getAddress ().getRegion (), UserRole .CONTACT_SUPERVISOR );
1265- }
1266- if (supervisors .isEmpty ()) {
1267- supervisors = userService .getAllByRegionAndUserRoles (contact .getCaze ().getRegion (), UserRole .CONTACT_SUPERVISOR );
1268- }
1269- if (!supervisors .isEmpty ()) {
1270- Random rand = new Random ();
1271- assignee = supervisors .get (rand .nextInt (supervisors .size ()));
1272- } else {
1273- logger .warn ("Contact has not contact officer and no region - can't create follow-up task: " + contact .getUuid ());
1274- continue ;
1275- }
1257+ User assignee ;
1258+ try {
1259+ assignee = taskService .getTaskAssignee (contact );
1260+ } catch (TaskCreationException e ) {
1261+ logger .warn (e .getMessage ());
1262+ continue ;
12761263 }
12771264
12781265 // find already existing tasks
@@ -1298,22 +1285,26 @@ public void generateContactFollowUpTasks() {
12981285 }
12991286
13001287 // none found -> create the task
1301- Task task = taskService .buildTask (null );
1302- task .setTaskContext (TaskContext .CONTACT );
1303- task .setContact (contact );
1304- task .setTaskType (TaskType .CONTACT_FOLLOW_UP );
1305- task .setSuggestedStart (DateHelper8 .toDate (fromDateTime ));
1306- task .setDueDate (DateHelper8 .toDate (toDateTime .minusMinutes (1 )));
1307- task .setAssigneeUser (assignee );
1308-
1309- if (contact .isHighPriority ()) {
1310- task .setPriority (TaskPriority .HIGH );
1311- }
1312-
1288+ Task task = createContactTask (TaskType .CONTACT_FOLLOW_UP , fromDateTime , toDateTime , contact , assignee );
13131289 taskService .ensurePersisted (task );
13141290 }
13151291 }
13161292
1293+ private Task createContactTask (TaskType taskType , LocalDateTime fromDateTime , LocalDateTime toDateTime , Contact contact , User assignee ) {
1294+ Task task = taskService .buildTask (null );
1295+ task .setTaskContext (TaskContext .CONTACT );
1296+ task .setContact (contact );
1297+ task .setTaskType (taskType );
1298+ task .setSuggestedStart (DateHelper8 .toDate (fromDateTime ));
1299+ task .setDueDate (DateHelper8 .toDate (toDateTime .minusMinutes (1 )));
1300+ task .setAssigneeUser (assignee );
1301+
1302+ if (contact .isHighPriority ()) {
1303+ task .setPriority (TaskPriority .HIGH );
1304+ }
1305+ return task ;
1306+ }
1307+
13171308 @ Override
13181309 public void validate (ContactDto contact ) throws ValidationRuntimeException {
13191310
0 commit comments