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"));
}
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));
}
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));
}
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"));
}
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;
}
Aggregations