Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit f55bba4

Browse files
SORMAS-Foundation#2724 extract method for getting task assignee in TaskService
1 parent 8bc22bb commit f55bba4

4 files changed

Lines changed: 165 additions & 42 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package de.symeda.sormas.backend.common;
2+
3+
@SuppressWarnings("serial")
4+
public class FollowUpTaskCreationException extends Exception {
5+
6+
public FollowUpTaskCreationException(String message) {
7+
super(message);
8+
}
9+
}

sormas-backend/src/main/java/de/symeda/sormas/backend/contact/ContactFacadeEjb.java

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.List;
3131
import java.util.Map;
3232
import java.util.Optional;
33-
import java.util.Random;
3433
import java.util.function.Function;
3534
import java.util.stream.Collectors;
3635
import java.util.stream.Stream;
@@ -117,6 +116,7 @@
117116
import de.symeda.sormas.backend.clinicalcourse.ClinicalCourseFacadeEjb;
118117
import de.symeda.sormas.backend.common.AbstractAdoService;
119118
import de.symeda.sormas.backend.common.AbstractDomainObject;
119+
import de.symeda.sormas.backend.common.FollowUpTaskCreationException;
120120
import de.symeda.sormas.backend.epidata.EpiData;
121121
import de.symeda.sormas.backend.epidata.EpiDataFacadeEjb;
122122
import de.symeda.sormas.backend.epidata.EpiDataFacadeEjb.EpiDataFacadeEjbLocal;
@@ -1232,47 +1232,12 @@ public void generateContactFollowUpTasks() {
12321232
continue;
12331233
}
12341234

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-
}
1235+
User assignee;
1236+
try {
1237+
assignee = taskService.getTaskAssignee(contact);
1238+
} catch (FollowUpTaskCreationException e) {
1239+
logger.warn(e.getMessage());
1240+
continue;
12761241
}
12771242

12781243
// find already existing tasks

sormas-backend/src/main/java/de/symeda/sormas/backend/task/TaskService.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
*******************************************************************************/
1818
package de.symeda.sormas.backend.task;
1919

20+
import java.util.ArrayList;
2021
import java.util.Date;
2122
import java.util.List;
23+
import java.util.Random;
2224

2325
import javax.ejb.EJB;
2426
import javax.ejb.LocalBean;
@@ -41,6 +43,7 @@
4143
import de.symeda.sormas.backend.caze.Case;
4244
import de.symeda.sormas.backend.caze.CaseService;
4345
import de.symeda.sormas.backend.common.AbstractAdoService;
46+
import de.symeda.sormas.backend.common.FollowUpTaskCreationException;
4447
import de.symeda.sormas.backend.contact.Contact;
4548
import de.symeda.sormas.backend.contact.ContactService;
4649
import de.symeda.sormas.backend.event.Event;
@@ -287,4 +290,51 @@ public Task buildTask(User creatorUser) {
287290
task.setTaskStatus(TaskStatus.PENDING);
288291
return task;
289292
}
293+
294+
public User getTaskAssignee(Contact contact) throws FollowUpTaskCreationException {
295+
User assignee = null;
296+
297+
if (contact.getContactOfficer() != null) {
298+
// 1) The contact officer that is responsible for the contact
299+
assignee = contact.getContactOfficer();
300+
} else {
301+
// 2) A random contact officer from the contact's, contact person's or contact case's district
302+
List<User> officers = new ArrayList<>();
303+
if (contact.getDistrict() != null) {
304+
officers = userService.getAllByDistrict(contact.getDistrict(), false, UserRole.CONTACT_OFFICER);
305+
}
306+
if (officers.isEmpty() && contact.getPerson().getAddress().getDistrict() != null) {
307+
officers = userService.getAllByDistrict(contact.getPerson().getAddress().getDistrict(), false, UserRole.CONTACT_OFFICER);
308+
}
309+
if (officers.isEmpty() && contact.getCaze() != null && contact.getCaze().getDistrict() != null) {
310+
officers = userService.getAllByDistrict(contact.getCaze().getDistrict(), false, UserRole.CONTACT_OFFICER);
311+
}
312+
if (!officers.isEmpty()) {
313+
Random rand = new Random();
314+
assignee = officers.get(rand.nextInt(officers.size()));
315+
}
316+
}
317+
318+
if (assignee == null) {
319+
// 3) Assign a random contact supervisor from the contact's, contact person's or contact case's region
320+
List<User> supervisors = new ArrayList<>();
321+
if (contact.getRegion() != null) {
322+
supervisors = userService.getAllByRegionAndUserRoles(contact.getRegion(), UserRole.CONTACT_SUPERVISOR);
323+
}
324+
if (supervisors.isEmpty() && contact.getPerson().getAddress().getRegion() != null) {
325+
supervisors = userService.getAllByRegionAndUserRoles(contact.getPerson().getAddress().getRegion(), UserRole.CONTACT_SUPERVISOR);
326+
}
327+
if (supervisors.isEmpty()) {
328+
supervisors = userService.getAllByRegionAndUserRoles(contact.getCaze().getRegion(), UserRole.CONTACT_SUPERVISOR);
329+
}
330+
if (!supervisors.isEmpty()) {
331+
Random rand = new Random();
332+
assignee = supervisors.get(rand.nextInt(supervisors.size()));
333+
} else {
334+
throw new FollowUpTaskCreationException("Contact has not contact officer and no region - can't create follow-up task: " + contact.getUuid());
335+
}
336+
}
337+
338+
return assignee;
339+
}
290340
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package de.symeda.sormas.backend.task;
2+
3+
import static junit.framework.TestCase.assertEquals;
4+
import static org.mockito.Matchers.any;
5+
import static org.mockito.Matchers.anyBoolean;
6+
import static org.mockito.Matchers.anyObject;
7+
8+
import java.util.Collections;
9+
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.mockito.InjectMocks;
13+
import org.mockito.Mock;
14+
import org.mockito.Mockito;
15+
import org.mockito.MockitoAnnotations;
16+
17+
import de.symeda.sormas.backend.AbstractBeanTest;
18+
import de.symeda.sormas.backend.caze.Case;
19+
import de.symeda.sormas.backend.common.FollowUpTaskCreationException;
20+
import de.symeda.sormas.backend.contact.Contact;
21+
import de.symeda.sormas.backend.location.Location;
22+
import de.symeda.sormas.backend.person.Person;
23+
import de.symeda.sormas.backend.region.District;
24+
import de.symeda.sormas.backend.region.Region;
25+
import de.symeda.sormas.backend.user.User;
26+
import de.symeda.sormas.backend.user.UserService;
27+
28+
public class TaskServiceTest extends AbstractBeanTest {
29+
30+
@InjectMocks
31+
private TaskService taskService;
32+
33+
@Mock
34+
private UserService userService;
35+
36+
@Before
37+
public void setUp() {
38+
MockitoAnnotations.initMocks(this);
39+
}
40+
41+
@Test
42+
public void testGetTaskAssigneeFromContactWithContactOfficer() throws FollowUpTaskCreationException {
43+
User contactOfficer = new User();
44+
contactOfficer.setId(1L);
45+
Contact contact = new Contact();
46+
contact.setContactOfficer(contactOfficer);
47+
48+
User actualAssignee = taskService.getTaskAssignee(contact);
49+
assertEquals(actualAssignee.getId(), contactOfficer.getId());
50+
}
51+
52+
@Test
53+
public void testGetTaskAssigneeFromDistrictOfficers() throws FollowUpTaskCreationException {
54+
User contactOfficer = new User();
55+
contactOfficer.setId(1L);
56+
District district = new District();
57+
Contact contact = new Contact();
58+
contact.setDistrict(district);
59+
60+
Mockito.when(userService.getAllByDistrict(any(District.class), anyBoolean(), anyObject()))
61+
.thenReturn(Collections.singletonList(contactOfficer));
62+
63+
User actualAssignee = taskService.getTaskAssignee(contact);
64+
assertEquals(actualAssignee.getId(), contactOfficer.getId());
65+
}
66+
67+
@Test
68+
public void testGetTaskAssigneeFromRegionSupervisors() throws FollowUpTaskCreationException {
69+
User contactOfficer = new User();
70+
contactOfficer.setId(1L);
71+
Contact contact = new Contact();
72+
Location location = new Location();
73+
Person person = new Person();
74+
person.setAddress(location);
75+
contact.setPerson(person);
76+
Region region = new Region();
77+
contact.setRegion(region);
78+
79+
Mockito.when(userService.getAllByRegionAndUserRoles(any(Region.class), anyObject())).thenReturn(Collections.singletonList(contactOfficer));
80+
81+
User actualAssignee = taskService.getTaskAssignee(contact);
82+
assertEquals(actualAssignee.getId(), contactOfficer.getId());
83+
}
84+
85+
@Test(expected = FollowUpTaskCreationException.class)
86+
public void testGetTaskAssigneeException() throws FollowUpTaskCreationException {
87+
Contact contact = new Contact();
88+
Location location = new Location();
89+
Person person = new Person();
90+
person.setAddress(location);
91+
contact.setPerson(person);
92+
Case caze = new Case();
93+
contact.setCaze(caze);
94+
95+
Mockito.when(userService.getAllByRegionAndUserRoles(any(Region.class), anyObject())).thenReturn(Collections.emptyList());
96+
97+
taskService.getTaskAssignee(contact);
98+
}
99+
}

0 commit comments

Comments
 (0)