Search in sources :

Example 11 with InstancesRepository

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

the class InstancesRepositoryTest method getAllNotDeleted_returnsUndeletedInstances.

@Test
public void getAllNotDeleted_returnsUndeletedInstances() {
    InstancesRepository instancesRepository = buildSubject();
    instancesRepository.save(InstanceUtils.buildInstance("deleted", "1", getInstancesDir()).status(Instance.STATUS_COMPLETE).deletedDate(System.currentTimeMillis()).build());
    instancesRepository.save(InstanceUtils.buildInstance("undeleted", "1", getInstancesDir()).status(Instance.STATUS_COMPLETE).build());
    List<Instance> allNotDeleted = instancesRepository.getAllNotDeleted();
    assertThat(allNotDeleted.size(), is(1));
    assertThat(allNotDeleted.get(0).getFormId(), is("undeleted"));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) Test(org.junit.Test)

Example 12 with InstancesRepository

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

the class InstancesRepositoryTest method getAllByFormIdAndVersionNotDeleted_excludesDeleted.

@Test
public void getAllByFormIdAndVersionNotDeleted_excludesDeleted() {
    InstancesRepository instancesRepository = buildSubject();
    instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).build());
    instancesRepository.save(InstanceUtils.buildInstance("formid", "1", "display", Instance.STATUS_COMPLETE, null, getInstancesDir()).build());
    instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).build());
    instancesRepository.save(InstanceUtils.buildInstance("formid", "1", "display", Instance.STATUS_COMPLETE, System.currentTimeMillis(), getInstancesDir()).build());
    instancesRepository.save(InstanceUtils.buildInstance("formid2", "1", "display", Instance.STATUS_COMPLETE, null, getInstancesDir()).build());
    List<Instance> instances = instancesRepository.getAllNotDeletedByFormIdAndVersion("formid", "1");
    assertThat(instances.size(), is(3));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) Test(org.junit.Test)

Example 13 with InstancesRepository

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

the class InstancesRepositoryTest method save_whenStatusIsNull_usesIncomplete.

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

Example 14 with InstancesRepository

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

the class InstancesRepositoryTest method save_whenInstanceHasId_updatesExisting.

@Test
public void save_whenInstanceHasId_updatesExisting() {
    InstancesRepository instancesRepository = buildSubject();
    Instance originalInstance = instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).displayName("Blah").build());
    instancesRepository.save(new Instance.Builder(originalInstance).displayName("A different blah").build());
    assertThat(instancesRepository.get(originalInstance.getDbId()).getDisplayName(), is("A different blah"));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) Test(org.junit.Test)

Example 15 with InstancesRepository

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

the class InstanceProvider method update.

@Override
public int update(@NonNull Uri uri, ContentValues values, String where, String[] whereArgs) {
    DaggerUtils.getComponent(getContext()).inject(this);
    String projectId = getProjectId(uri);
    logServerEvent(projectId, AnalyticsEvents.INSTANCE_PROVIDER_UPDATE);
    InstancesRepository instancesRepository = instancesRepositoryProvider.get(projectId);
    String instancesPath = storagePathProvider.getOdkDirPath(StorageSubdirectory.INSTANCES, projectId);
    int count;
    switch(URI_MATCHER.match(uri)) {
        case INSTANCES:
            try (Cursor cursor = dbQuery(projectId, null, where, whereArgs, null)) {
                while (cursor.moveToNext()) {
                    Instance instance = getInstanceFromCurrentCursorPosition(cursor, instancesPath);
                    ContentValues existingValues = getValuesFromInstance(instance, instancesPath);
                    existingValues.putAll(values);
                    instancesRepository.save(getInstanceFromValues(existingValues));
                }
                count = cursor.getCount();
            }
            break;
        case INSTANCE_ID:
            long instanceId = ContentUriHelper.getIdFromUri(uri);
            if (whereArgs == null || whereArgs.length == 0) {
                Instance instance = instancesRepository.get(instanceId);
                ContentValues existingValues = getValuesFromInstance(instance, instancesPath);
                existingValues.putAll(values);
                instancesRepository.save(getInstanceFromValues(existingValues));
                count = 1;
            } else {
                try (Cursor cursor = dbQuery(projectId, new String[] { _ID }, where, whereArgs, null)) {
                    while (cursor.moveToNext()) {
                        if (cursor.getLong(cursor.getColumnIndex(_ID)) == instanceId) {
                            Instance instance = getInstanceFromCurrentCursorPosition(cursor, instancesPath);
                            ContentValues existingValues = getValuesFromInstance(instance, instancesPath);
                            existingValues.putAll(values);
                            instancesRepository.save(getInstanceFromValues(existingValues));
                            break;
                        }
                    }
                }
                count = 1;
            }
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return count;
}
Also used : ContentValues(android.content.ContentValues) Instance(org.odk.collect.forms.instances.Instance) DatabaseObjectMapper.getValuesFromInstance(org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromInstance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) DatabaseInstancesRepository(org.odk.collect.android.database.instances.DatabaseInstancesRepository) Cursor(android.database.Cursor)

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