Search in sources :

Example 11 with Form

use of org.odk.collect.forms.Form in project collect by opendatakit.

the class FormsRepositoryTest method getAllNotDeletedByFormId_doesNotReturnDeletedForms.

@Test
public void getAllNotDeletedByFormId_doesNotReturnDeletedForms() {
    FormsRepository formsRepository = buildSubject();
    formsRepository.save(FormUtils.buildForm("1", "deleted", getFormFilesPath()).deleted(true).build());
    formsRepository.save(FormUtils.buildForm("1", "not-deleted", getFormFilesPath()).deleted(false).build());
    List<Form> forms = formsRepository.getAllNotDeletedByFormId("1");
    assertThat(forms.size(), is(1));
    assertThat(forms.get(0).getVersion(), equalTo("not-deleted"));
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) Test(org.junit.Test)

Example 12 with Form

use of org.odk.collect.forms.Form in project collect by opendatakit.

the class FormsRepositoryTest method save_addsId.

@Test
public void save_addsId() {
    FormsRepository formsRepository = buildSubject();
    Form form = FormUtils.buildForm("id", "version", getFormFilesPath()).build();
    formsRepository.save(form);
    assertThat(formsRepository.getAll().get(0).getDbId(), notNullValue());
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) Test(org.junit.Test)

Example 13 with Form

use of org.odk.collect.forms.Form in project collect by opendatakit.

the class FormsRepositoryTest method save_whenFormHasId_updatesExisting.

@Test
public void save_whenFormHasId_updatesExisting() {
    FormsRepository formsRepository = buildSubject();
    Form originalForm = formsRepository.save(FormUtils.buildForm("id", "version", getFormFilesPath()).displayName("original").build());
    formsRepository.save(new Form.Builder(originalForm).displayName("changed").build());
    assertThat(formsRepository.get(originalForm.getDbId()).getDisplayName(), is("changed"));
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) Test(org.junit.Test)

Example 14 with Form

use of org.odk.collect.forms.Form 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;
}
Also used : ContentValues(android.content.ContentValues) DatabaseFormsRepository(org.odk.collect.android.database.forms.DatabaseFormsRepository) FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) DatabaseObjectMapper.getValuesFromForm(org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromForm) Cursor(android.database.Cursor)

Example 15 with Form

use of org.odk.collect.forms.Form in project collect by opendatakit.

the class DatabaseFormsRepository method getFormsFromCursor.

@NotNull
private static List<Form> getFormsFromCursor(Cursor cursor, String formsPath, String cachePath) {
    List<Form> forms = new ArrayList<>();
    if (cursor != null) {
        cursor.moveToPosition(-1);
        while (cursor.moveToNext()) {
            Form form = getFormFromCurrentCursorPosition(cursor, formsPath, cachePath);
            forms.add(form);
        }
    }
    return forms;
}
Also used : Form(org.odk.collect.forms.Form) DatabaseObjectMapper.getValuesFromForm(org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromForm) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Form (org.odk.collect.forms.Form)62 Test (org.junit.Test)35 File (java.io.File)22 FormsRepository (org.odk.collect.forms.FormsRepository)21 ByteArrayInputStream (java.io.ByteArrayInputStream)13 Analytics (org.odk.collect.analytics.Analytics)13 FormSource (org.odk.collect.forms.FormSource)12 FormUtils.buildForm (org.odk.collect.formstest.FormUtils.buildForm)12 MediaFile (org.odk.collect.forms.MediaFile)9 FormsRepositoryProvider (org.odk.collect.android.utilities.FormsRepositoryProvider)7 ManifestFile (org.odk.collect.forms.ManifestFile)7 Instance (org.odk.collect.forms.instances.Instance)7 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)7 ArrayList (java.util.ArrayList)5 FormController (org.odk.collect.android.javarosawrapper.FormController)4 ViewModelProvider (androidx.lifecycle.ViewModelProvider)3 DatabaseObjectMapper.getValuesFromForm (org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromForm)3 Intent (android.content.Intent)2 View (android.view.View)2 TextView (android.widget.TextView)2