Search in sources :

Example 21 with InstancesRepository

use of org.odk.collect.forms.instances.InstancesRepository in project collect by opendatakit.

the class InstancesRepositoryTest method deleteWithLogging_setsDeletedDate.

@Test
public void deleteWithLogging_setsDeletedDate() {
    InstancesRepository instancesRepository = buildSubject();
    Instance instance = instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).build());
    instancesRepository.deleteWithLogging(instance.getDbId());
    assertThat(instancesRepository.get(instance.getDbId()).getDeletedDate(), is(notNullValue()));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) Test(org.junit.Test)

Example 22 with InstancesRepository

use of org.odk.collect.forms.instances.InstancesRepository in project collect by opendatakit.

the class InstancesRepositoryTest method deleteWithLogging_deletesInstanceDir.

@Test
public void deleteWithLogging_deletesInstanceDir() {
    InstancesRepository instancesRepository = buildSubject();
    Instance instance = instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).build());
    File instanceDir = new File(instance.getInstanceFilePath()).getParentFile();
    assertThat(instanceDir.exists(), is(true));
    assertThat(instanceDir.isDirectory(), is(true));
    instancesRepository.deleteWithLogging(instance.getDbId());
    assertThat(instanceDir.exists(), is(false));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) File(java.io.File) Test(org.junit.Test)

Example 23 with InstancesRepository

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

Example 24 with InstancesRepository

use of org.odk.collect.forms.instances.InstancesRepository in project collect by opendatakit.

the class InstancesRepositoryTest method save_whenLastStatusChangeDateIsNull_setsIt.

@Test
public void save_whenLastStatusChangeDateIsNull_setsIt() {
    InstancesRepository instancesRepository = buildSubject();
    Instance instance = instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).lastStatusChangeDate(null).build());
    assertThat(instancesRepository.get(instance.getDbId()).getLastStatusChangeDate(), is(notNullValue()));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) Test(org.junit.Test)

Example 25 with InstancesRepository

use of org.odk.collect.forms.instances.InstancesRepository in project collect by opendatakit.

the class InstanceAutoSender method autoSendInstances.

public boolean autoSendInstances(String projectId) {
    FormsRepository formsRepository = formsRepositoryProvider.get(projectId);
    InstancesRepository instancesRepository = instancesRepositoryProvider.get(projectId);
    Settings generalSettings = settingsProvider.getUnprotectedSettings(projectId);
    InstanceSubmitter instanceSubmitter = new InstanceSubmitter(analytics, formsRepository, instancesRepository, googleAccountsManager, googleApiProvider, permissionsProvider, generalSettings);
    return changeLockProvider.getInstanceLock(projectId).withLock(acquiredLock -> {
        if (acquiredLock) {
            try {
                List<Instance> toUpload = getInstancesToAutoSend(formsRepository, instancesRepository, generalSettings);
                Pair<Boolean, String> results = instanceSubmitter.submitInstances(toUpload);
                notifier.onSubmission(results.first, results.second, projectId);
            } catch (SubmitException e) {
                switch(e.getType()) {
                    case GOOGLE_ACCOUNT_NOT_SET:
                        notifier.onSubmission(true, context.getString(R.string.google_set_account), projectId);
                        break;
                    case GOOGLE_ACCOUNT_NOT_PERMITTED:
                        notifier.onSubmission(true, context.getString(R.string.odk_permissions_fail), projectId);
                        break;
                    case NOTHING_TO_SUBMIT:
                        break;
                }
            }
            instancesAppState.update();
            return true;
        } else {
            return false;
        }
    });
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) Settings(org.odk.collect.shared.settings.Settings)

Aggregations

InstancesRepository (org.odk.collect.forms.instances.InstancesRepository)25 Instance (org.odk.collect.forms.instances.Instance)23 Test (org.junit.Test)19 File (java.io.File)4 InstancesRepositoryProvider (org.odk.collect.android.utilities.InstancesRepositoryProvider)3 ContentValues (android.content.ContentValues)2 FormInstance (org.javarosa.core.model.instance.FormInstance)2 FormController (org.odk.collect.android.javarosawrapper.FormController)2 FormsRepository (org.odk.collect.forms.FormsRepository)2 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)2 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 AsyncTask (android.os.AsyncTask)1 RandomAccessFile (java.io.RandomAccessFile)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 Stream (java.util.stream.Stream)1 ByteArrayPayload (org.javarosa.core.services.transport.payload.ByteArrayPayload)1 Collect (org.odk.collect.android.application.Collect)1 DatabaseObjectMapper.getValuesFromInstance (org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromInstance)1