use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.
the class InstancesRepositoryTest method getAllByStatus_withOneStatus_returnsMatchingInstances.
@Test
public void getAllByStatus_withOneStatus_returnsMatchingInstances() {
InstancesRepository instancesRepository = buildSubject();
instancesRepository.save(InstanceUtils.buildInstance("incomplete", "1", getInstancesDir()).status(Instance.STATUS_INCOMPLETE).build());
instancesRepository.save(InstanceUtils.buildInstance("incomplete", "1", getInstancesDir()).status(Instance.STATUS_INCOMPLETE).build());
instancesRepository.save(InstanceUtils.buildInstance("complete", "1", getInstancesDir()).status(Instance.STATUS_COMPLETE).build());
instancesRepository.save(InstanceUtils.buildInstance("complete", "1", getInstancesDir()).status(Instance.STATUS_COMPLETE).build());
List<Instance> incomplete = instancesRepository.getAllByStatus(Instance.STATUS_INCOMPLETE);
assertThat(incomplete.size(), is(2));
assertThat(incomplete.get(0).getFormId(), is("incomplete"));
assertThat(incomplete.get(1).getStatus(), is("incomplete"));
// Check corresponding count method is also correct
assertThat(instancesRepository.getCountByStatus(Instance.STATUS_INCOMPLETE), is(2));
}
use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.
the class FormEntryActivity method loadFromIntent.
private void loadFromIntent(Intent intent) {
Uri uri = intent.getData();
String uriMimeType = null;
if (uri != null) {
uriMimeType = getContentResolver().getType(uri);
}
if (uriMimeType != null && uriMimeType.equals(InstancesContract.CONTENT_ITEM_TYPE)) {
Instance instance = new InstancesRepositoryProvider(Collect.getInstance()).get().get(ContentUriHelper.getIdFromUri(uri));
if (instance == null) {
createErrorDialog(getString(R.string.bad_uri, uri), true);
return;
}
instancePath = instance.getInstanceFilePath();
if (!new File(instancePath).exists()) {
analytics.logEvent(AnalyticsEvents.OPEN_DELETED_INSTANCE);
new InstanceDeleter(new InstancesRepositoryProvider(Collect.getInstance()).get(), formsRepository).delete(instance.getDbId());
createErrorDialog(getString(R.string.instance_deleted_message), true);
return;
}
List<Form> candidateForms = formsRepository.getAllByFormIdAndVersion(instance.getFormId(), instance.getFormVersion());
if (candidateForms.isEmpty()) {
createErrorDialog(getString(R.string.parent_form_not_present, instance.getFormId()) + ((instance.getFormVersion() == null) ? "" : "\n" + getString(R.string.version) + " " + instance.getFormVersion()), true);
return;
} else if (candidateForms.stream().filter(f -> !f.isDeleted()).count() > 1) {
createErrorDialog(getString(R.string.survey_multiple_forms_error), true);
return;
}
formPath = candidateForms.get(0).getFormFilePath();
} else if (uriMimeType != null && uriMimeType.equals(FormsContract.CONTENT_ITEM_TYPE)) {
Form form = formsRepositoryProvider.get().get(ContentUriHelper.getIdFromUri(uri));
if (form != null) {
formPath = form.getFormFilePath();
}
if (formPath == null) {
createErrorDialog(getString(R.string.bad_uri, uri), true);
return;
} else {
/**
* This is the fill-blank-form code path.See if there is a savepoint for this form
* that has never been explicitly saved by the user. If there is, open this savepoint(resume this filled-in form).
* Savepoints for forms that were explicitly saved will be recovered when that
* explicitly saved instance is edited via edit-saved-form.
*/
instancePath = loadSavePoint();
}
} else {
Timber.i("Unrecognized URI: %s", uri);
createErrorDialog(getString(R.string.unrecognized_uri, uri), true);
return;
}
formLoaderTask = new FormLoaderTask(instancePath, null, null);
showIfNotShowing(FormLoadingDialogFragment.class, getSupportFragmentManager());
formLoaderTask.execute(formPath);
}
use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.
the class FormMapViewModel method getMappableFormInstances.
private List<MappableFormInstance> getMappableFormInstances(List<Instance> allInstances) {
List<MappableFormInstance> mappableFormInstances = new ArrayList<>();
for (Instance instance : allInstances) {
if (instance.getGeometry() != null) {
try {
JSONObject geometry = new JSONObject(instance.getGeometry());
switch(instance.getGeometryType()) {
case "Point":
JSONArray coordinates = geometry.getJSONArray("coordinates");
// In GeoJSON, longitude comes before latitude.
Double lon = coordinates.getDouble(0);
Double lat = coordinates.getDouble(1);
mappableFormInstances.add(new MappableFormInstance(instance.getDbId(), lat, lon, instance.getDisplayName(), instance.getLastStatusChangeDate(), instance.getStatus(), getClickActionForInstance(instance)));
}
} catch (JSONException e) {
Timber.w("Invalid JSON in instances table: %s", instance.getGeometry());
}
}
}
return mappableFormInstances;
}
use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.
the class InstanceGoogleSheetsUploaderTask method doInBackground.
@Override
protected Outcome doInBackground(Long... instanceIdsToUpload) {
String account = settingsProvider.getUnprotectedSettings().getString(ProjectKeys.KEY_SELECTED_GOOGLE_ACCOUNT);
InstanceGoogleSheetsUploader uploader = new InstanceGoogleSheetsUploader(googleApiProvider.getDriveApi(account), googleApiProvider.getSheetsApi(account));
final Outcome outcome = new Outcome();
List<Instance> instancesToUpload = uploader.getInstancesFromIds(instanceIdsToUpload);
for (int i = 0; i < instancesToUpload.size(); i++) {
Instance instance = instancesToUpload.get(i);
if (isCancelled()) {
outcome.messagesByInstanceId.put(instance.getDbId().toString(), getLocalizedString(Collect.getInstance(), R.string.instance_upload_cancelled));
return outcome;
}
publishProgress(i + 1, instancesToUpload.size());
// Get corresponding blank form and verify there is exactly 1
List<Form> forms = new FormsRepositoryProvider(Collect.getInstance()).get().getAllByFormIdAndVersion(instance.getFormId(), instance.getFormVersion());
if (forms.size() != 1) {
outcome.messagesByInstanceId.put(instance.getDbId().toString(), getLocalizedString(Collect.getInstance(), R.string.not_exactly_one_blank_form_for_this_form_id));
} else {
try {
String destinationUrl = uploader.getUrlToSubmitTo(instance, null, null, settingsProvider.getUnprotectedSettings().getString(KEY_GOOGLE_SHEETS_URL));
if (InstanceUploaderUtils.doesUrlRefersToGoogleSheetsFile(destinationUrl)) {
uploader.uploadOneSubmission(instance, destinationUrl);
outcome.messagesByInstanceId.put(instance.getDbId().toString(), DEFAULT_SUCCESSFUL_TEXT);
analytics.logEvent(SUBMISSION, "HTTP-Sheets", Collect.getFormIdentifierHash(instance.getFormId(), instance.getFormVersion()));
} else {
outcome.messagesByInstanceId.put(instance.getDbId().toString(), SPREADSHEET_UPLOADED_TO_GOOGLE_DRIVE);
}
} catch (UploadException e) {
Timber.d(e);
outcome.messagesByInstanceId.put(instance.getDbId().toString(), e.getDisplayMessage());
}
}
}
return outcome;
}
use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.
the class InstanceAutoSender method getInstancesToAutoSend.
@NotNull
private List<Instance> getInstancesToAutoSend(FormsRepository formsRepository, InstancesRepository instancesRepository, Settings generalSettings) {
boolean isAutoSendAppSettingEnabled = !generalSettings.getString(ProjectKeys.KEY_AUTOSEND).equals("off");
List<Instance> toUpload = new ArrayList<>();
for (Instance instance : instancesRepository.getAllByStatus(Instance.STATUS_COMPLETE, Instance.STATUS_SUBMISSION_FAILED)) {
if (shouldFormBeSent(formsRepository, instance.getFormId(), instance.getFormVersion(), isAutoSendAppSettingEnabled)) {
toUpload.add(instance);
}
}
return toUpload;
}
Aggregations