Search in sources :

Example 1 with Task

use of de.symeda.sormas.app.backend.task.Task in project SORMAS-Project by hzi-braunschweig.

the class EventDao method deleteEventAndAllDependingEntities.

public void deleteEventAndAllDependingEntities(String eventUuid) throws SQLException {
    Event event = queryUuidWithEmbedded(eventUuid);
    // Cancel if not in local database
    if (event == null) {
        return;
    }
    // Delete event tasks
    List<Task> tasks = DatabaseHelper.getTaskDao().queryByEvent(event);
    for (Task task : tasks) {
        DatabaseHelper.getTaskDao().deleteCascade(task);
    }
    // Delete event participants
    List<EventParticipant> eventParticipants = DatabaseHelper.getEventParticipantDao().getByEvent(event);
    for (EventParticipant eventParticipant : eventParticipants) {
        DatabaseHelper.getEventParticipantDao().deleteCascade(eventParticipant);
    }
    // Delete event
    deleteCascade(event);
}
Also used : Task(de.symeda.sormas.app.backend.task.Task)

Example 2 with Task

use of de.symeda.sormas.app.backend.task.Task in project SORMAS-Project by hzi-braunschweig.

the class TestEntityCreator method createCaseTask.

public static Task createCaseTask(Case caze, TaskStatus taskStatus, User user) {
    TaskDto taskDto = new TaskDto();
    Task task = new TaskDtoHelper().fillOrCreateFromDto(null, taskDto);
    task.setUuid(DataHelper.createUuid());
    task.setCreationDate(new Date());
    task.setChangeDate(new Date());
    task.setTaskContext(TaskContext.CASE);
    task.setTaskType(TaskType.CASE_INVESTIGATION);
    task.setTaskStatus(taskStatus);
    task.setCaze(caze);
    task.setAssigneeUser(user);
    try {
        DatabaseHelper.getTaskDao().saveAndSnapshot(task);
        DatabaseHelper.getTaskDao().accept(task);
    } catch (DaoException e) {
        throw new RuntimeException(e);
    }
    return DatabaseHelper.getTaskDao().queryForIdWithEmbedded(task.getId());
}
Also used : Task(de.symeda.sormas.app.backend.task.Task) TaskDtoHelper(de.symeda.sormas.app.backend.task.TaskDtoHelper) DaoException(de.symeda.sormas.app.backend.common.DaoException) TaskDto(de.symeda.sormas.api.task.TaskDto) Date(java.util.Date)

Example 3 with Task

use of de.symeda.sormas.app.backend.task.Task in project SORMAS-Project by hzi-braunschweig.

the class CaseBackendTest method testTaskReassignmentAfterChangedCaseDistrict.

@Test
public void testTaskReassignmentAfterChangedCaseDistrict() throws DaoException {
    CaseDao caseDao = DatabaseHelper.getCaseDao();
    Case caze = TestEntityCreator.createCase();
    caze.setRegion(caze.getResponsibleRegion());
    caze.setDistrict(caze.getResponsibleDistrict());
    caze.setCommunity(caze.getResponsibleCommunity());
    caseDao.saveAndSnapshot(caze);
    User user = ConfigProvider.getUser();
    UserRole userRole = UserRole.SURVEILLANCE_OFFICER;
    Set<UserRole> userRoles = new HashSet<>();
    userRoles.add(userRole);
    user.setUserRoles(userRoles);
    DatabaseHelper.getUserDao().saveAndSnapshot(user);
    TaskDao taskDao = DatabaseHelper.getTaskDao();
    Task task = TestEntityCreator.createCaseTask(caze, TaskStatus.PENDING, user);
    assertEquals(caze.getResponsibleRegion().getUuid(), TestHelper.REGION_UUID);
    assertEquals(caze.getResponsibleDistrict().getUuid(), TestHelper.DISTRICT_UUID);
    assertEquals(caze.getResponsibleCommunity().getUuid(), TestHelper.COMMUNITY_UUID);
    assertEquals(caze.getRegion().getUuid(), TestHelper.REGION_UUID);
    assertEquals(caze.getDistrict().getUuid(), TestHelper.DISTRICT_UUID);
    assertEquals(caze.getCommunity().getUuid(), TestHelper.COMMUNITY_UUID);
    assertEquals(caze.getHealthFacility().getUuid(), TestHelper.FACILITY_UUID);
    task = taskDao.queryUuid(task.getUuid());
    assertEquals(TestHelper.USER_UUID, task.getAssigneeUser().getUuid());
    // ResponsibleDistrict changed, but District still in user's jurisdiction
    District secondDistrict = DatabaseHelper.getDistrictDao().queryUuid(TestHelper.SECOND_DISTRICT_UUID);
    Community secondCommunity = DatabaseHelper.getCommunityDao().queryUuid(TestHelper.SECOND_COMMUNITY_UUID);
    caze.setResponsibleDistrict(secondDistrict);
    caze.setResponsibleCommunity(secondCommunity);
    caseDao.saveAndSnapshot(caze);
    task = taskDao.queryUuid(task.getUuid());
    assertEquals(TestHelper.USER_UUID, task.getAssigneeUser().getUuid());
    // Case not in user's jurisdiction anymore
    caze.setDistrict(secondDistrict);
    caze.setCommunity(null);
    caseDao.saveAndSnapshot(caze);
    task = taskDao.queryUuid(task.getUuid());
    assertEquals(TestHelper.SECOND_USER_UUID, task.getAssigneeUser().getUuid());
}
Also used : Task(de.symeda.sormas.app.backend.task.Task) User(de.symeda.sormas.app.backend.user.User) UserRole(de.symeda.sormas.api.user.UserRole) CaseDao(de.symeda.sormas.app.backend.caze.CaseDao) TaskDao(de.symeda.sormas.app.backend.task.TaskDao) District(de.symeda.sormas.app.backend.region.District) Community(de.symeda.sormas.app.backend.region.Community) Case(de.symeda.sormas.app.backend.caze.Case) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with Task

use of de.symeda.sormas.app.backend.task.Task in project SORMAS-Project by hzi-braunschweig.

the class CaseDao method deleteCaseAndAllDependingEntities.

public void deleteCaseAndAllDependingEntities(String caseUuid) throws SQLException {
    Case caze = queryUuidWithEmbedded(caseUuid);
    // Cancel if not in local database
    if (caze == null) {
        return;
    }
    // Delete contacts, contact tasks and visits
    List<Contact> contacts = DatabaseHelper.getContactDao().getByCase(caze);
    for (Contact contact : contacts) {
        DatabaseHelper.getContactDao().deleteContactAndAllDependingEntities(contact);
    }
    // Delete samples, pathogen tests and additional tests
    List<Sample> samples = DatabaseHelper.getSampleDao().queryByCase(caze);
    for (Sample sample : samples) {
        DatabaseHelper.getSampleDao().deleteSampleAndAllDependingEntities(sample);
    }
    // Delete case tasks
    List<Task> tasks = DatabaseHelper.getTaskDao().queryByCase(caze);
    for (Task task : tasks) {
        DatabaseHelper.getTaskDao().deleteCascade(task);
    }
    // Delete treatments and prescriptions
    if (caze.getTherapy() != null) {
        for (Treatment treatment : DatabaseHelper.getTreatmentDao().findBy(new TreatmentCriteria().therapy(caze.getTherapy()))) {
            DatabaseHelper.getTreatmentDao().delete(treatment);
        }
        for (Prescription prescription : DatabaseHelper.getPrescriptionDao().findBy(new PrescriptionCriteria().therapy(caze.getTherapy()))) {
            DatabaseHelper.getPrescriptionDao().delete(prescription);
        }
    }
    // Delete clinical visits
    if (caze.getClinicalCourse() != null) {
        for (ClinicalVisit clinicalVisit : DatabaseHelper.getClinicalVisitDao().findBy(new ClinicalVisitCriteria().clinicalCourse(caze.getClinicalCourse()))) {
            DatabaseHelper.getClinicalVisitDao().delete(clinicalVisit);
        }
    }
    // Remove events linked to case by removing case_id from event participants - delete event participant and
    List<EventParticipant> eventParticipants = DatabaseHelper.getEventParticipantDao().getByCase(caze);
    for (EventParticipant eventParticipant : eventParticipants) {
        DatabaseHelper.getEventParticipantDao().deleteEventParticipant(eventParticipant);
    }
    // Remove events outside jurisdiction which were pulled in due to linking with an accessible case
    EventCriteria eventCriteria = new EventCriteria();
    eventCriteria.caze(caze);
    List<Event> eventList = DatabaseHelper.getEventDao().queryByCriteria(eventCriteria, 0, 0);
    for (Event event : eventList) {
        List<EventParticipant> eventParticipantByEventList = DatabaseHelper.getEventParticipantDao().getByEvent(event);
        if (eventParticipantByEventList.isEmpty()) {
            Boolean isEventInJurisdiction = EventEditAuthorization.isEventEditAllowed(event);
            if (!isEventInJurisdiction) {
                DatabaseHelper.getEventDao().delete(event);
            }
        }
    }
    // Delete case
    deleteCascade(caze);
}
Also used : Prescription(de.symeda.sormas.app.backend.therapy.Prescription) PrescriptionCriteria(de.symeda.sormas.app.backend.therapy.PrescriptionCriteria) Task(de.symeda.sormas.app.backend.task.Task) Sample(de.symeda.sormas.app.backend.sample.Sample) ClinicalVisit(de.symeda.sormas.app.backend.clinicalcourse.ClinicalVisit) ClinicalVisitCriteria(de.symeda.sormas.app.backend.clinicalcourse.ClinicalVisitCriteria) EventCriteria(de.symeda.sormas.app.backend.event.EventCriteria) ActivityAsCase(de.symeda.sormas.app.backend.activityascase.ActivityAsCase) Contact(de.symeda.sormas.app.backend.contact.Contact) Treatment(de.symeda.sormas.app.backend.therapy.Treatment) TreatmentCriteria(de.symeda.sormas.app.backend.therapy.TreatmentCriteria) Event(de.symeda.sormas.app.backend.event.Event) EventParticipant(de.symeda.sormas.app.backend.event.EventParticipant)

Example 5 with Task

use of de.symeda.sormas.app.backend.task.Task in project SORMAS-Project by hzi-braunschweig.

the class CaseDao method onCaseChanged.

private void onCaseChanged(Case existingCase, Case changedCase) {
    changedCase.setCompleteness(calculateCompleteness(changedCase));
    if (existingCase == null) {
        // If a new case is created, use the last available location to update its report latitude and longitude
        Location location = LocationService.instance().getLocation();
        if (location != null) {
            changedCase.setReportLat(location.getLatitude());
            changedCase.setReportLon(location.getLongitude());
            changedCase.setReportLatLonAccuracy(location.getAccuracy());
        }
    } else {
        // classification
        if (changedCase.getCaseClassification() != existingCase.getCaseClassification()) {
            changedCase.setClassificationDate(new Date());
            changedCase.setClassificationUser(ConfigProvider.getUser());
        }
        // change the disease of all contacts if the case disease or disease details have changed
        if (existingCase.getDisease() != changedCase.getDisease() || !StringUtils.equals(existingCase.getDiseaseDetails(), changedCase.getDiseaseDetails())) {
            for (Contact contact : DatabaseHelper.getContactDao().getByCase(changedCase)) {
                contact.setDisease(changedCase.getDisease());
                contact.setDiseaseDetails(changedCase.getDiseaseDetails());
                try {
                    DatabaseHelper.getContactDao().saveAndSnapshot(contact);
                } catch (DaoException e) {
                    Log.e(getTableName(), "Failed to save an updated contact in onCaseChanged");
                }
            }
        }
        boolean responsibleRegionChanged = !DataHelper.isSame(changedCase.getResponsibleRegion(), existingCase.getResponsibleRegion());
        boolean regionChanged = !DataHelper.isSame(changedCase.getRegion(), existingCase.getRegion());
        boolean responsibleDistrictChanged = !DataHelper.isSame(changedCase.getResponsibleDistrict(), existingCase.getResponsibleDistrict());
        boolean districtChanged = !DataHelper.isSame(changedCase.getDistrict(), existingCase.getDistrict());
        boolean responsibleCommunityChanged = !DataHelper.isSame(changedCase.getResponsibleCommunity(), existingCase.getResponsibleCommunity());
        boolean communityChanged = !DataHelper.isSame(changedCase.getCommunity(), existingCase.getCommunity());
        boolean facilityChanged = !DataHelper.isSame(changedCase.getHealthFacility(), existingCase.getHealthFacility());
        // If the case is moved from the surveillance officer's jurisdiction, assign a new surveillance officer
        if (DatabaseHelper.getFeatureConfigurationDao().isPropertyValueTrue(FeatureType.CASE_SURVEILANCE, FeatureTypeProperty.AUTOMATIC_RESPONSIBILITY_ASSIGNMENT) && changedCase.getSurveillanceOfficer() == null || ((responsibleDistrictChanged || districtChanged) && !DataHelper.isSame(changedCase.getResponsibleDistrict(), changedCase.getSurveillanceOfficer().getDistrict()) && !DataHelper.isSame(changedCase.getDistrict(), changedCase.getSurveillanceOfficer().getDistrict()))) {
            List<User> districtOfficers = DatabaseHelper.getUserDao().getByDistrictAndRole(changedCase.getResponsibleDistrict(), UserRole.SURVEILLANCE_OFFICER, User.UUID);
            if (districtOfficers.size() == 0 && changedCase.getDistrict() != null) {
                districtOfficers = DatabaseHelper.getUserDao().getByDistrictAndRole(changedCase.getDistrict(), UserRole.SURVEILLANCE_OFFICER, User.UUID);
            }
            changedCase.setSurveillanceOfficer(DataUtils.getRandomCandidate(districtOfficers));
        }
        // if the case's jurisdiction has changed, re-assign tasks
        if (responsibleRegionChanged || responsibleDistrictChanged || responsibleCommunityChanged || regionChanged || districtChanged || communityChanged || facilityChanged) {
            for (Task task : DatabaseHelper.getTaskDao().queryByCase(existingCase)) {
                if (task.getTaskStatus() != TaskStatus.PENDING) {
                    continue;
                }
                User assigneeUser = task.getAssigneeUser();
                if (assigneeUser != null && CaseJurisdictionBooleanValidator.of(JurisdictionHelper.createCaseJurisdictionDto(changedCase), JurisdictionHelper.createUserJurisdiction(assigneeUser)).isInJurisdiction()) {
                    continue;
                }
                assignOfficerOrSupervisorToTask(changedCase, task);
                try {
                    DatabaseHelper.getTaskDao().saveAndSnapshot(task);
                } catch (DaoException e) {
                    Log.e(getTableName(), "Failed to save an updated task in onCaseChanged");
                }
            }
        }
    }
}
Also used : Task(de.symeda.sormas.app.backend.task.Task) User(de.symeda.sormas.app.backend.user.User) DaoException(de.symeda.sormas.app.backend.common.DaoException) Date(java.util.Date) Location(android.location.Location) Contact(de.symeda.sormas.app.backend.contact.Contact)

Aggregations

Task (de.symeda.sormas.app.backend.task.Task)23 Case (de.symeda.sormas.app.backend.caze.Case)7 CaseDao (de.symeda.sormas.app.backend.caze.CaseDao)6 TaskDao (de.symeda.sormas.app.backend.task.TaskDao)6 User (de.symeda.sormas.app.backend.user.User)6 Contact (de.symeda.sormas.app.backend.contact.Contact)5 Event (de.symeda.sormas.app.backend.event.Event)4 Community (de.symeda.sormas.app.backend.region.Community)4 District (de.symeda.sormas.app.backend.region.District)4 AsyncTask (android.os.AsyncTask)3 UserRole (de.symeda.sormas.api.user.UserRole)3 SavingAsyncTask (de.symeda.sormas.app.core.async.SavingAsyncTask)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3 Test (org.junit.Test)3 TaskDto (de.symeda.sormas.api.task.TaskDto)2 ValidationException (de.symeda.sormas.api.utils.ValidationException)2 ActivityAsCase (de.symeda.sormas.app.backend.activityascase.ActivityAsCase)2 ClinicalVisit (de.symeda.sormas.app.backend.clinicalcourse.ClinicalVisit)2 DaoException (de.symeda.sormas.app.backend.common.DaoException)2