Search in sources :

Example 11 with Instance

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));
            }
        }
    }
}
Also used : InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) ProjectKeys(org.odk.collect.settings.keys.ProjectKeys) SettingsProvider(org.odk.collect.settings.SettingsProvider) Instance(org.odk.collect.forms.instances.Instance) AsyncTask(android.os.AsyncTask) InstanceUploaderListener(org.odk.collect.android.listeners.InstanceUploaderListener) InstanceUploaderUtils.shouldFormBeDeleted(org.odk.collect.android.utilities.InstanceUploaderUtils.shouldFormBeDeleted) Uri(android.net.Uri) Set(java.util.Set) HashMap(java.util.HashMap) InstancesRepositoryProvider(org.odk.collect.android.utilities.InstancesRepositoryProvider) FormsRepository(org.odk.collect.forms.FormsRepository) Stream(java.util.stream.Stream) Collect(org.odk.collect.android.application.Collect) InstancesRepositoryProvider(org.odk.collect.android.utilities.InstancesRepositoryProvider) Instance(org.odk.collect.forms.instances.Instance)

Example 12 with Instance

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));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstanceUtils.buildInstance(org.odk.collect.formstest.InstanceUtils.buildInstance) Test(org.junit.Test)

Example 13 with Instance

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));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstanceUtils.buildInstance(org.odk.collect.formstest.InstanceUtils.buildInstance) Test(org.junit.Test)

Example 14 with Instance

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;
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InMemInstancesRepository(org.odk.collect.formstest.InMemInstancesRepository)

Example 15 with Instance

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();
}
Also used : InstancesRepositoryProvider(org.odk.collect.android.utilities.InstancesRepositoryProvider) Instance(org.odk.collect.forms.instances.Instance) Intent(android.content.Intent) Uri(android.net.Uri)

Aggregations

Instance (org.odk.collect.forms.instances.Instance)62 Test (org.junit.Test)28 InstancesRepository (org.odk.collect.forms.instances.InstancesRepository)23 InstancesRepositoryProvider (org.odk.collect.android.utilities.InstancesRepositoryProvider)10 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)9 File (java.io.File)7 DatabaseObjectMapper.getValuesFromInstance (org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromInstance)7 Uri (android.net.Uri)6 Form (org.odk.collect.forms.Form)6 InstanceUtils.buildInstance (org.odk.collect.formstest.InstanceUtils.buildInstance)6 Pair (android.util.Pair)5 ArrayList (java.util.ArrayList)5 FormsRepositoryProvider (org.odk.collect.android.utilities.FormsRepositoryProvider)5 Date (java.util.Date)4 FormController (org.odk.collect.android.javarosawrapper.FormController)4 MapPoint (org.odk.collect.geo.maps.MapPoint)4 ContentValues (android.content.ContentValues)3 Intent (android.content.Intent)3 Cursor (android.database.Cursor)3 AsyncTask (android.os.AsyncTask)2