Search in sources :

Example 1 with TaskCreationException

use of de.symeda.sormas.backend.common.TaskCreationException in project SORMAS-Project by hzi-braunschweig.

the class TaskService method getTaskAssignee.

public User getTaskAssignee(Contact contact) throws TaskCreationException {
    User assignee = null;
    if (contact.getContactOfficer() != null) {
        // 1) The contact officer that is responsible for the contact
        assignee = contact.getContactOfficer();
    } else {
        // 2) A random user with user right CONTACT_RESPONSIBLE from the contact's, contact person's or contact case's district
        Function<District, User> lookupByDistrict = district -> userService.getRandomDistrictUser(district, UserRight.CONTACT_RESPONSIBLE);
        if (contact.getDistrict() != null) {
            assignee = lookupByDistrict.apply(contact.getDistrict());
        }
        if (assignee == null && contact.getPerson().getAddress().getDistrict() != null) {
            assignee = lookupByDistrict.apply(contact.getPerson().getAddress().getDistrict());
        }
        Case contactCase = contact.getCaze();
        if (assignee == null && contactCase != null) {
            assignee = lookupByDistrict.apply(contactCase.getResponsibleDistrict());
            if (assignee == null && contactCase.getDistrict() != null) {
                assignee = lookupByDistrict.apply(contactCase.getDistrict());
            }
        }
    }
    if (assignee == null) {
        // 3) Assign a random user with user right CONTACT_RESPONSIBLE from the contact's, contact person's or contact case's region
        Function<Region, User> lookupByRegion = region -> userService.getRandomRegionUser(region, UserRight.CONTACT_RESPONSIBLE);
        if (contact.getRegion() != null) {
            assignee = lookupByRegion.apply(contact.getRegion());
        }
        if (assignee == null && contact.getPerson().getAddress().getRegion() != null) {
            assignee = lookupByRegion.apply(contact.getPerson().getAddress().getRegion());
        }
        Case contactCase = contact.getCaze();
        if (assignee == null && contactCase != null) {
            assignee = lookupByRegion.apply(contactCase.getResponsibleRegion());
            if (assignee == null && contactCase.getRegion() != null) {
                assignee = lookupByRegion.apply(contactCase.getRegion());
            }
        }
        if (assignee == null) {
            throw new TaskCreationException("Contact has not contact officer and no region - can't create follow-up task: " + contact.getUuid());
        }
    }
    return assignee;
}
Also used : TaskJurisdictionFlagsDto(de.symeda.sormas.api.task.TaskJurisdictionFlagsDto) EventService(de.symeda.sormas.backend.event.EventService) Arrays(java.util.Arrays) Join(javax.persistence.criteria.Join) Date(java.util.Date) NoResultException(javax.persistence.NoResultException) TravelEntryQueryContext(de.symeda.sormas.backend.travelentry.TravelEntryQueryContext) TransactionAttributeType(javax.ejb.TransactionAttributeType) Predicate(javax.persistence.criteria.Predicate) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) UserRole(de.symeda.sormas.api.user.UserRole) From(javax.persistence.criteria.From) Contact(de.symeda.sormas.backend.contact.Contact) Stateless(javax.ejb.Stateless) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) CaseService(de.symeda.sormas.backend.caze.CaseService) Person(de.symeda.sormas.backend.person.Person) Timestamp(java.sql.Timestamp) Region(de.symeda.sormas.backend.infrastructure.region.Region) District(de.symeda.sormas.backend.infrastructure.district.District) TaskPriority(de.symeda.sormas.api.task.TaskPriority) Instant(java.time.Instant) ContactQueryContext(de.symeda.sormas.backend.contact.ContactQueryContext) List(java.util.List) UserService(de.symeda.sormas.backend.user.UserService) User(de.symeda.sormas.backend.user.User) JurisdictionHelper(de.symeda.sormas.backend.util.JurisdictionHelper) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) Selection(javax.persistence.criteria.Selection) EntityRelevanceStatus(de.symeda.sormas.api.EntityRelevanceStatus) Function(java.util.function.Function) EventQueryContext(de.symeda.sormas.backend.event.EventQueryContext) Case(de.symeda.sormas.backend.caze.Case) CriteriaUpdate(javax.persistence.criteria.CriteriaUpdate) AdoServiceWithUserFilter(de.symeda.sormas.backend.common.AdoServiceWithUserFilter) TransactionAttribute(javax.ejb.TransactionAttribute) TaskStatus(de.symeda.sormas.api.task.TaskStatus) LocalBean(javax.ejb.LocalBean) CaseQueryContext(de.symeda.sormas.backend.caze.CaseQueryContext) CriteriaBuilderHelper(de.symeda.sormas.backend.common.CriteriaBuilderHelper) EJB(javax.ejb.EJB) Root(javax.persistence.criteria.Root) TaskCriteria(de.symeda.sormas.api.task.TaskCriteria) TaskContext(de.symeda.sormas.api.task.TaskContext) DataHelper(de.symeda.sormas.api.utils.DataHelper) TravelEntryService(de.symeda.sormas.backend.travelentry.services.TravelEntryService) TaskCreationException(de.symeda.sormas.backend.common.TaskCreationException) Event(de.symeda.sormas.backend.event.Event) UserRight(de.symeda.sormas.api.user.UserRight) ContactService(de.symeda.sormas.backend.contact.ContactService) TravelEntry(de.symeda.sormas.backend.travelentry.TravelEntry) User(de.symeda.sormas.backend.user.User) Region(de.symeda.sormas.backend.infrastructure.region.Region) TaskCreationException(de.symeda.sormas.backend.common.TaskCreationException) District(de.symeda.sormas.backend.infrastructure.district.District) Case(de.symeda.sormas.backend.caze.Case)

Example 2 with TaskCreationException

use of de.symeda.sormas.backend.common.TaskCreationException in project SORMAS-Project by hzi-braunschweig.

the class ContactFacadeEjb method generateContactFollowUpTasks.

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@RolesAllowed(UserRight._SYSTEM)
public void generateContactFollowUpTasks() {
    // get all contacts that are followed up
    LocalDateTime fromDateTime = LocalDate.now().atStartOfDay();
    LocalDateTime toDateTime = fromDateTime.plusDays(1);
    List<Contact> contacts = service.getFollowUpBetween(DateHelper8.toDate(fromDateTime), DateHelper8.toDate(toDateTime));
    for (Contact contact : contacts) {
        // Only generate tasks for contacts that are under follow-up
        if (!(contact.getFollowUpStatus().equals(FollowUpStatus.FOLLOW_UP) || contact.getFollowUpStatus().equals(FollowUpStatus.LOST))) {
            continue;
        }
        User assignee;
        try {
            assignee = taskService.getTaskAssignee(contact);
        } catch (TaskCreationException e) {
            logger.warn(e.getMessage());
            continue;
        }
        // find already existing tasks
        TaskCriteria pendingUserTaskCriteria = new TaskCriteria().contact(contact.toReference()).taskType(TaskType.CONTACT_FOLLOW_UP).assigneeUser(assignee.toReference()).taskStatus(TaskStatus.PENDING);
        List<Task> pendingUserTasks = taskService.findBy(pendingUserTaskCriteria, true);
        if (!pendingUserTasks.isEmpty()) {
            // the user still has a pending task for this contact
            continue;
        }
        TaskCriteria dayTaskCriteria = new TaskCriteria().contact(contact.toReference()).taskType(TaskType.CONTACT_FOLLOW_UP).dueDateBetween(DateHelper8.toDate(fromDateTime), DateHelper8.toDate(toDateTime));
        List<Task> dayTasks = taskService.findBy(dayTaskCriteria, true);
        if (!dayTasks.isEmpty()) {
            // there is already a task for the exact day
            continue;
        }
        // none found -> create the task
        Task task = createContactTask(TaskType.CONTACT_FOLLOW_UP, fromDateTime, toDateTime, contact, assignee);
        taskService.ensurePersisted(task);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Task(de.symeda.sormas.backend.task.Task) User(de.symeda.sormas.backend.user.User) TaskCriteria(de.symeda.sormas.api.task.TaskCriteria) TaskCreationException(de.symeda.sormas.backend.common.TaskCreationException) RolesAllowed(javax.annotation.security.RolesAllowed) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 3 with TaskCreationException

use of de.symeda.sormas.backend.common.TaskCreationException in project SORMAS-Project by hzi-braunschweig.

the class ContactFacadeEjb method createInvestigationTask.

private void createInvestigationTask(Contact entity) {
    LocalDate now = LocalDate.now();
    LocalDate reportDate = DateHelper8.toLocalDate(entity.getReportDateTime());
    if (DAYS.between(reportDate, now) <= 30) {
        try {
            User assignee = taskService.getTaskAssignee(entity);
            LocalDateTime fromDateTime = LocalDate.now().atStartOfDay();
            LocalDateTime toDateTime = fromDateTime.plusDays(1);
            Task task = createContactTask(TaskType.CONTACT_INVESTIGATION, fromDateTime, toDateTime, entity, assignee);
            taskService.ensurePersisted(task);
        } catch (TaskCreationException e) {
            logger.warn(e.getMessage());
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Task(de.symeda.sormas.backend.task.Task) User(de.symeda.sormas.backend.user.User) TaskCreationException(de.symeda.sormas.backend.common.TaskCreationException) LocalDate(java.time.LocalDate)

Aggregations

TaskCreationException (de.symeda.sormas.backend.common.TaskCreationException)3 User (de.symeda.sormas.backend.user.User)3 TaskCriteria (de.symeda.sormas.api.task.TaskCriteria)2 Task (de.symeda.sormas.backend.task.Task)2 LocalDateTime (java.time.LocalDateTime)2 TransactionAttribute (javax.ejb.TransactionAttribute)2 EntityRelevanceStatus (de.symeda.sormas.api.EntityRelevanceStatus)1 TaskContext (de.symeda.sormas.api.task.TaskContext)1 TaskJurisdictionFlagsDto (de.symeda.sormas.api.task.TaskJurisdictionFlagsDto)1 TaskPriority (de.symeda.sormas.api.task.TaskPriority)1 TaskStatus (de.symeda.sormas.api.task.TaskStatus)1 JurisdictionLevel (de.symeda.sormas.api.user.JurisdictionLevel)1 UserRight (de.symeda.sormas.api.user.UserRight)1 UserRole (de.symeda.sormas.api.user.UserRole)1 DataHelper (de.symeda.sormas.api.utils.DataHelper)1 Case (de.symeda.sormas.backend.caze.Case)1 CaseQueryContext (de.symeda.sormas.backend.caze.CaseQueryContext)1 CaseService (de.symeda.sormas.backend.caze.CaseService)1 AdoServiceWithUserFilter (de.symeda.sormas.backend.common.AdoServiceWithUserFilter)1 CriteriaBuilderHelper (de.symeda.sormas.backend.common.CriteriaBuilderHelper)1