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);
}
}
}
}
}
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();
}
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();
}
}
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);
}
}
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);
}
Aggregations