use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.
the class InstanceUploaderTask method onPostExecute.
@Override
protected void onPostExecute(Outcome outcome) {
synchronized (this) {
if (outcome != null && stateListener != null) {
if (outcome.authRequestingServer != null) {
stateListener.authRequest(outcome.authRequestingServer, outcome.messagesByInstanceId);
} else {
stateListener.uploadingComplete(outcome.messagesByInstanceId);
// Delete instances that were successfully sent and that need to be deleted
// either because app-level auto-delete is enabled or because the form
// specifies it.
Set<String> instanceIds = outcome.messagesByInstanceId.keySet();
boolean isFormAutoDeleteOptionEnabled;
// the app preferences set for delete after submission
if (deleteInstanceAfterSubmission != null) {
isFormAutoDeleteOptionEnabled = deleteInstanceAfterSubmission;
} else {
isFormAutoDeleteOptionEnabled = settingsProvider.getUnprotectedSettings().getBoolean(ProjectKeys.KEY_DELETE_AFTER_SEND);
}
Stream<Instance> instancesToDelete = instanceIds.stream().map(id -> new InstancesRepositoryProvider(Collect.getInstance()).get().get(Long.parseLong(id))).filter(instance -> instance.getStatus().equals(Instance.STATUS_SUBMITTED)).filter(instance -> shouldFormBeDeleted(formsRepository, instance.getFormId(), instance.getFormVersion(), isFormAutoDeleteOptionEnabled));
DeleteInstancesTask dit = new DeleteInstancesTask(instancesRepository, formsRepository);
dit.execute(instancesToDelete.map(Instance::getDbId).toArray(Long[]::new));
}
}
}
}
use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.
the class InstanceDeleterTest method whenFormForInstanceIsSoftDeleted_andThereAreNoOtherInstances_deletesForm.
@Test
public void whenFormForInstanceIsSoftDeleted_andThereAreNoOtherInstances_deletesForm() {
formsRepository.save(new Form.Builder().formId("1").version("version").deleted(true).formFilePath(FormUtils.createXFormFile("1", "version").getAbsolutePath()).build());
Instance instanceToDelete = instancesRepository.save(new Instance.Builder().formId("1").formVersion("version").instanceFilePath(TempFiles.createTempDir().getAbsolutePath()).build());
instanceDeleter.delete(instanceToDelete.getDbId());
assertThat(formsRepository.getAll().isEmpty(), is(true));
}
use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.
the class InstanceDeleterTest method whenFormForInstanceIsSoftDeleted_andThereIsAnotherInstaceWithDeletedDate_deletesForm.
@Test
public void whenFormForInstanceIsSoftDeleted_andThereIsAnotherInstaceWithDeletedDate_deletesForm() {
formsRepository.save(new Form.Builder().formId("1").version("version").deleted(true).formFilePath(FormUtils.createXFormFile("1", "version").getAbsolutePath()).build());
instancesRepository.save(new Instance.Builder().formId("1").deletedDate(0L).formVersion("version").build());
Instance instanceToDelete = instancesRepository.save(new Instance.Builder().formId("1").formVersion("version").instanceFilePath(TempFiles.createTempDir().getAbsolutePath()).build());
instanceDeleter.delete(instanceToDelete.getDbId());
assertThat(formsRepository.getAll().size(), is(0));
}
use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.
the class InstanceUploaderUtilsTest method getTestInstancesRepository.
private InMemInstancesRepository getTestInstancesRepository() {
InMemInstancesRepository instancesRepository = new InMemInstancesRepository();
for (int i = 1; i <= NUMBER_OF_INSTANCES_TO_SEND; i++) {
long time = System.currentTimeMillis();
Instance instance = new Instance.Builder().dbId((long) i).displayName("InstanceTest").formId("instanceTest").status(Instance.STATUS_COMPLETE).lastStatusChangeDate(time).build();
instancesRepository.save(instance);
}
return instancesRepository;
}
use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.
the class FormEntryActivity method finishAndReturnInstance.
/**
* Returns the instance that was just filled out to the calling activity, if
* requested.
*/
private void finishAndReturnInstance() {
String action = getIntent().getAction();
if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_EDIT.equals(action)) {
// caller is waiting on a picked form
Uri uri = null;
String path = getAbsoluteInstancePath();
if (path != null) {
Instance instance = new InstancesRepositoryProvider(this).get().getOneByPath(path);
if (instance != null) {
uri = InstancesContract.getUri(currentProjectProvider.getCurrentProject().getUuid(), instance.getDbId());
}
}
if (uri != null) {
setResult(RESULT_OK, new Intent().setData(uri));
}
}
finish();
}
Aggregations