use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.
the class FormsProvider method update.
@Override
public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
deferDaggerInit();
String projectId = getProjectId(uri);
logServerEvent(projectId, AnalyticsEvents.FORMS_PROVIDER_UPDATE);
FormsRepository formsRepository = getFormsRepository(projectId);
String formsPath = storagePathProvider.getOdkDirPath(StorageSubdirectory.FORMS, projectId);
String cachePath = storagePathProvider.getOdkDirPath(StorageSubdirectory.CACHE, projectId);
int count;
switch(URI_MATCHER.match(uri)) {
case FORMS:
try (Cursor cursor = databaseQuery(projectId, null, where, whereArgs, null, null, null)) {
while (cursor.moveToNext()) {
Form form = getFormFromCurrentCursorPosition(cursor, formsPath, cachePath);
ContentValues existingValues = getValuesFromForm(form, formsPath);
existingValues.putAll(values);
formsRepository.save(getFormFromValues(existingValues, formsPath, cachePath));
}
count = cursor.getCount();
}
break;
case FORM_ID:
Form form = formsRepository.get(ContentUriHelper.getIdFromUri(uri));
if (form != null) {
ContentValues existingValues = getValuesFromForm(form, formsPath);
existingValues.putAll(values);
formsRepository.save(getFormFromValues(existingValues, formsPath, cachePath));
count = 1;
} else {
count = 0;
}
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
use of org.odk.collect.forms.FormsRepository 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.FormsRepository in project collect by opendatakit.
the class FormMapActivityTest method setUpActivity.
@Before
public void setUpActivity() {
CollectHelpers.setupDemoProject();
AppDependencyComponent component = CollectHelpers.overrideAppDependencyModule(new AppDependencyModule() {
@Override
public MapProvider providesMapProvider() {
MapProvider mapProvider = mock(MapProvider.class);
when(mapProvider.createMapFragment(ArgumentMatchers.any())).thenReturn(map);
return mapProvider;
}
});
FormsRepository formsRepository = component.formsRepositoryProvider().get();
instancesRepository = component.instancesRepositoryProvider().get();
form = formsRepository.save(testForm);
Arrays.stream(testInstances).forEach(instancesRepository::save);
Intent intent = new Intent();
intent.putExtra(FormMapActivity.EXTRA_FORM_ID, form.getDbId());
activityController = CollectHelpers.buildThemedActivity(FormMapActivity.class, intent);
activity = (FormMapActivity) activityController.get();
activityController.setup();
}
use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.
the class FormsRepositoryTest method getAllNotDeletedByFormIdAndVersion_onlyReturnsNotDeletedFormsThatMatchVersion.
@Test
public void getAllNotDeletedByFormIdAndVersion_onlyReturnsNotDeletedFormsThatMatchVersion() {
FormsRepository formsRepository = buildSubject();
formsRepository.save(FormUtils.buildForm("id", "1", getFormFilesPath()).deleted(true).build());
formsRepository.save(FormUtils.buildForm("id", "1", getFormFilesPath()).deleted(false).build());
formsRepository.save(FormUtils.buildForm("id", "2", getFormFilesPath()).deleted(true).build());
formsRepository.save(FormUtils.buildForm("id", "2", getFormFilesPath()).deleted(false).build());
List<Form> forms = formsRepository.getAllNotDeletedByFormIdAndVersion("id", "2");
assertThat(forms.size(), is(1));
assertThat(forms.get(0).getVersion(), equalTo("2"));
}
use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.
the class FormsRepositoryTest method save_addsHashBasedOnFormFile.
@Test
public void save_addsHashBasedOnFormFile() {
FormsRepository formsRepository = buildSubject();
Form form = FormUtils.buildForm("id", "version", getFormFilesPath()).build();
assertThat(form.getMD5Hash(), equalTo(null));
formsRepository.save(form);
String expectedHash = Md5.getMd5Hash(new File(form.getFormFilePath()));
assertThat(formsRepository.get(1L).getMD5Hash(), equalTo(expectedHash));
}
Aggregations