Search in sources :

Example 1 with EventExtended

use of org.eyeseetea.malariacare.data.sync.importer.models.EventExtended in project pictureapp by EyeSeeTea.

the class SurveyChecker method checkAllQuarantineSurveys.

/**
     * Download the related events. and checks all the quarantine surveys.
     * If a survey is in the server, the survey should be set as sent. Else, the survey should be
     * set as completed and it will be resend.
     */
public static void checkAllQuarantineSurveys() {
    List<Program> programs = Program.getAllPrograms();
    for (Program program : programs) {
        for (OrgUnit orgUnit : Survey.getQuarantineOrgUnits(program.getId_program())) {
            List<Survey> quarantineSurveys = Survey.getAllQuarantineSurveysByProgramAndOrgUnit(program, orgUnit);
            if (quarantineSurveys.size() == 0) {
                continue;
            }
            Date minDate = Survey.getMinQuarantineCompletionDateByProgramAndOrgUnit(program, orgUnit);
            Date maxDate = Survey.getMaxQuarantineEventDateByProgramAndOrgUnit(program, orgUnit);
            List<EventExtended> events = getEvents(program.getUid(), orgUnit.getUid(), minDate, maxDate);
            if (events != null && events.size() > 0) {
                for (Survey survey : quarantineSurveys) {
                    updateQuarantineSurveysStatus(events, survey);
                }
            }
        }
    }
}
Also used : OrgUnit(org.eyeseetea.malariacare.data.database.model.OrgUnit) Survey(org.eyeseetea.malariacare.data.database.model.Survey) Program(org.eyeseetea.malariacare.data.database.model.Program) EventExtended(org.eyeseetea.malariacare.data.sync.importer.models.EventExtended) Date(java.util.Date)

Example 2 with EventExtended

use of org.eyeseetea.malariacare.data.sync.importer.models.EventExtended in project pictureapp by EyeSeeTea.

the class SurveyChecker method updateQuarantineSurveysStatus.

public static void updateQuarantineSurveysStatus(List<EventExtended> events, Survey survey) {
    boolean isSent = false;
    for (EventExtended event : events) {
        isSent = surveyDateExistsInEventTimeCaptureControlDE(survey, event);
        if (isSent) {
            break;
        }
    }
    if (isSent) {
        Log.d(TAG, "Set quarantine survey as sent" + survey.getId_survey());
        survey.setStatus(Constants.SURVEY_SENT);
    } else {
        //When the completion date for a survey is not present in the server, this survey is
        // not in the server.
        //This survey is set as "completed" and will be send in the future.
        Log.d(TAG, "Set quarantine survey as completed" + survey.getId_survey());
        survey.setStatus(Constants.SURVEY_COMPLETED);
    }
    survey.save();
}
Also used : EventExtended(org.eyeseetea.malariacare.data.sync.importer.models.EventExtended)

Example 3 with EventExtended

use of org.eyeseetea.malariacare.data.sync.importer.models.EventExtended in project pictureapp by EyeSeeTea.

the class ConvertToSDKVisitor method saveSurveyStatus.

/**
     * Saves changes in the survey (supposedly after a successful push)
     */
public void saveSurveyStatus(Map<String, ImportSummary> importSummaryMap, final IPushController.IPushControllerCallback callback) {
    Log.d(TAG, String.format("ImportSummary %d surveys savedSurveyStatus", surveys.size()));
    for (int i = 0; i < surveys.size(); i++) {
        Survey iSurvey = surveys.get(i);
        iSurvey.setStatus(Constants.SURVEY_QUARANTINE);
        Log.d(TAG, "saveSurveyStatus: Starting saving survey Set Survey status as QUARANTINE" + iSurvey.getId_survey() + " eventuid: " + iSurvey.getEventUid());
        iSurvey.save();
        if (importSummaryMap == null) {
            continue;
        }
        EventExtended iEvent = new EventExtended(events.get(iSurvey.getId_survey()));
        ImportSummary importSummary = importSummaryMap.get(iEvent.getEvent().getUId());
        List<Conflict> conflicts = importSummary.getConflicts();
        // never resend, the survey is saved as survey in conflict.
        if (conflicts != null && conflicts.size() > 0) {
            for (Conflict conflict : conflicts) {
                Log.d(TAG, "saveSurveyStatus: Faileditem not null " + iSurvey.getId_survey());
                if (conflict.getObject() != null) {
                    Log.d(TAG, "saveSurveyStatus: PUSH process...Conflict in " + conflict.getObject() + " with error " + conflict.getValue() + " dataelement pushing survey: " + iSurvey.getId_survey());
                    callback.onError(new ImportSummaryErrorException(String.format(context.getString(R.string.error_conflict_message), iEvent.getEvent().getUId(), conflict.getObject(), conflict.getValue()) + ""));
                    iSurvey.setStatus(Constants.SURVEY_CONFLICT);
                }
            }
            iSurvey.save();
            continue;
        } else if (importSummary != null && importSummary.getStatus() == ImportSummary.Status.ERROR) {
            Log.d(TAG, "saveSurveyStatus: PUSH error process..." + importSummary.getDescription() + " dataelement pushing survey: " + iSurvey.getId_survey());
            callback.onError(new ImportSummaryErrorException(importSummary.getDescription() + ""));
            iSurvey.setStatus(Constants.SURVEY_CONFLICT);
            iSurvey.save();
        }
        if (importSummary == null) {
            Log.d(TAG, "saveSurveyStatus: importSummary null " + iSurvey.getId_survey());
            //Saved as quarantine
            continue;
        } else {
            Log.d(TAG, "saveSurveyStatus: " + importSummary.toString());
        }
        //No errors -> Save and next
        if (!hasImportSummaryErrors(importSummary)) {
            Log.d(TAG, "saveSurveyStatus: importSummary without errors and status ok " + iSurvey.getId_survey());
            saveSurveyFromImportSummary(iSurvey);
            continue;
        }
        //Generated event must be remove too
        iEvent.delete();
    }
}
Also used : Survey(org.eyeseetea.malariacare.data.database.model.Survey) EventExtended(org.eyeseetea.malariacare.data.sync.importer.models.EventExtended) Conflict(org.hisp.dhis.client.sdk.models.common.importsummary.Conflict) ImportSummaryErrorException(org.eyeseetea.malariacare.domain.exception.ImportSummaryErrorException) ImportSummary(org.hisp.dhis.client.sdk.models.common.importsummary.ImportSummary)

Example 4 with EventExtended

use of org.eyeseetea.malariacare.data.sync.importer.models.EventExtended in project pictureapp by EyeSeeTea.

the class ConvertToSDKVisitor method visit.

@Override
public void visit(Survey survey) throws Exception {
    EventExtended event = null;
    try {
        //Precondition
        if (isEmpty(survey)) {
            survey.delete();
            return;
        }
        if (Survey.countSurveysByCompletiondate(survey.getCompletionDate()) > 1) {
            Log.d(TAG, String.format("Delete repeated survey", survey.toString()));
            survey.delete();
            return;
        }
        Log.d(TAG, String.format("Creating event for survey (%d) ...", survey.getId_survey()));
        event = buildEvent(survey);
        survey.setEventUid(event.getUid());
        survey.save();
        //Turn question values into dataValues
        Log.d(TAG, "Creating datavalues from questions...");
        for (Value value : survey.getValuesFromDB()) {
            if (value.getQuestion().hasDataElement()) {
                buildAndSaveDataValue(value.getQuestion().getUid(), value.getValue(), event);
            }
        }
        Log.d(TAG, "Saving control dataelements");
        buildControlDataElements(survey, event);
        if (Survey.countSurveysByCompletiondate(survey.getCompletionDate()) > 1) {
            Log.d(TAG, String.format("Delete repeated survey", survey.toString()));
            survey.delete();
            event.delete();
            return;
        }
        //Annotate both objects to update its state once the process is over
        annotateSurveyAndEvent(survey, event);
    } catch (Exception e) {
        e.printStackTrace();
        //If the conversion fails the survey is wrong and will be delete.
        removeSurveyAndEvent(survey, event);
    }
}
Also used : EventExtended(org.eyeseetea.malariacare.data.sync.importer.models.EventExtended) Value(org.eyeseetea.malariacare.data.database.model.Value) ImportSummaryErrorException(org.eyeseetea.malariacare.domain.exception.ImportSummaryErrorException)

Example 5 with EventExtended

use of org.eyeseetea.malariacare.data.sync.importer.models.EventExtended in project pictureapp by EyeSeeTea.

the class DataConverter method convert.

public void convert(IPullController.Callback callback, ConvertFromSDKVisitor converter) {
    String orgUnitName = PreferencesState.getInstance().getOrgUnit();
    List<OrgUnit> orgUnits = converter.getOrgUnits();
    for (OrgUnit orgUnit : orgUnits) {
        //Only events for the right ORGUNIT are loaded
        if (!orgUnitName.isEmpty() && orgUnit.getName() != null && !orgUnit.getName().equals(orgUnitName)) {
            continue;
        }
        List<Program> programs = Program.getAllPrograms();
        for (Program program : programs) {
            List<EventExtended> events = EventExtended.getExtendedList(SdkQueries.getEvents(orgUnit.getUid(), program.getUid()));
            callback.onStep(BUILDING_SURVEYS);
            for (EventExtended event : events) {
                event.accept(converter);
            }
            callback.onStep(BUILDING_VALUES);
            for (EventExtended event : events) {
                List<DataValueExtended> dataValues = DataValueExtended.getExtendedList(SdkQueries.getDataValues(event.getUid()));
                for (DataValueExtended dataValueExtended : dataValues) {
                    dataValueExtended.accept(converter);
                }
                try {
                    dataConverterStrategy.convert(converter, event);
                } catch (QuestionNotFoundException e) {
                    callback.onError(e);
                }
            }
        }
    }
    saveConvertedSurveys(callback, converter);
}
Also used : OrgUnit(org.eyeseetea.malariacare.data.database.model.OrgUnit) Program(org.eyeseetea.malariacare.data.database.model.Program) EventExtended(org.eyeseetea.malariacare.data.sync.importer.models.EventExtended) QuestionNotFoundException(org.eyeseetea.malariacare.domain.exception.QuestionNotFoundException) DataValueExtended(org.eyeseetea.malariacare.data.sync.importer.models.DataValueExtended)

Aggregations

EventExtended (org.eyeseetea.malariacare.data.sync.importer.models.EventExtended)7 OrgUnit (org.eyeseetea.malariacare.data.database.model.OrgUnit)2 Program (org.eyeseetea.malariacare.data.database.model.Program)2 Survey (org.eyeseetea.malariacare.data.database.model.Survey)2 ImportSummaryErrorException (org.eyeseetea.malariacare.domain.exception.ImportSummaryErrorException)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JodaModule (com.fasterxml.jackson.datatype.joda.JodaModule)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 Value (org.eyeseetea.malariacare.data.database.model.Value)1 DataValueExtended (org.eyeseetea.malariacare.data.sync.importer.models.DataValueExtended)1 QuestionNotFoundException (org.eyeseetea.malariacare.domain.exception.QuestionNotFoundException)1 EventFlow (org.hisp.dhis.client.sdk.android.api.persistence.flow.EventFlow)1 TrackedEntityDataValueFlow (org.hisp.dhis.client.sdk.android.api.persistence.flow.TrackedEntityDataValueFlow)1 Conflict (org.hisp.dhis.client.sdk.models.common.importsummary.Conflict)1 ImportSummary (org.hisp.dhis.client.sdk.models.common.importsummary.ImportSummary)1 Event (org.hisp.dhis.client.sdk.models.event.Event)1