Search in sources :

Example 16 with Form

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

the class DatabaseFormsRepository method deleteForms.

private void deleteForms(String selection, String[] selectionArgs) {
    List<Form> forms = queryForForms(selection, selectionArgs);
    for (Form form : forms) {
        deleteFilesForForm(form);
    }
    SQLiteDatabase writeableDatabase = databaseConnection.getWriteableDatabase();
    writeableDatabase.delete(FORMS_TABLE_NAME, selection, selectionArgs);
}
Also used : Form(org.odk.collect.forms.Form) DatabaseObjectMapper.getValuesFromForm(org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromForm) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 17 with Form

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

the class ServerFormDownloaderTest method whenFormIsSoftDeleted_unDeletesForm.

// region Undelete on re-download
@Test
public void whenFormIsSoftDeleted_unDeletesForm() throws Exception {
    String xform = createXFormBody("deleted-form", "version");
    Form form = buildForm("deleted-form", "version", getFormFilesPath(), xform).deleted(true).build();
    formsRepository.save(form);
    ServerFormDetails serverFormDetails = new ServerFormDetails(form.getDisplayName(), "http://downloadUrl", form.getFormId(), form.getVersion(), Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, null);
    FormSource formSource = mock(FormSource.class);
    when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
    ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), new FormMetadataParser(), mock(Analytics.class));
    downloader.downloadForm(serverFormDetails, null, null);
    assertThat(formsRepository.get(1L).isDeleted(), is(false));
}
Also used : Form(org.odk.collect.forms.Form) FormUtils.buildForm(org.odk.collect.formstest.FormUtils.buildForm) ByteArrayInputStream(java.io.ByteArrayInputStream) FormSource(org.odk.collect.forms.FormSource) Analytics(org.odk.collect.analytics.Analytics) Test(org.junit.Test)

Example 18 with Form

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

the class ServerFormDownloaderTest method whenFormToDownloadIsUpdate_savesNewVersionAlongsideOldVersion.

@Test
public void whenFormToDownloadIsUpdate_savesNewVersionAlongsideOldVersion() throws Exception {
    String xform = createXFormBody("id", "version");
    ServerFormDetails serverFormDetails = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, null);
    FormSource formSource = mock(FormSource.class);
    when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
    ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), new FormMetadataParser(), mock(Analytics.class));
    downloader.downloadForm(serverFormDetails, null, null);
    String xformUpdate = createXFormBody("id", "updated");
    ServerFormDetails serverFormDetailsUpdated = new ServerFormDetails("Form", "http://downloadUpdatedUrl", "id", "updated", Md5.getMd5Hash(new ByteArrayInputStream(xformUpdate.getBytes())), true, false, null);
    when(formSource.fetchForm("http://downloadUpdatedUrl")).thenReturn(new ByteArrayInputStream(xformUpdate.getBytes()));
    downloader.downloadForm(serverFormDetailsUpdated, null, null);
    List<Form> allForms = formsRepository.getAll();
    assertThat(allForms.size(), is(2));
    allForms.forEach(f -> {
        File formFile = new File(getAbsoluteFilePath(formsDir.getAbsolutePath(), f.getFormFilePath()));
        assertThat(formFile.exists(), is(true));
    });
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Form(org.odk.collect.forms.Form) FormUtils.buildForm(org.odk.collect.formstest.FormUtils.buildForm) FormSource(org.odk.collect.forms.FormSource) MediaFile(org.odk.collect.forms.MediaFile) File(java.io.File) ManifestFile(org.odk.collect.forms.ManifestFile) Analytics(org.odk.collect.analytics.Analytics) Test(org.junit.Test)

Example 19 with Form

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

the class ServerFormDownloaderTest method whenFormAlreadyDownloaded_andFormHasNewMediaFiles_updatesMediaFiles.

@Test
public void whenFormAlreadyDownloaded_andFormHasNewMediaFiles_updatesMediaFiles() throws Exception {
    String xform = createXFormBody("id", "version");
    ServerFormDetails serverFormDetails = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, new ManifestFile("", asList(new MediaFile("file1", Md5.getMd5Hash(new ByteArrayInputStream("contents".getBytes())), "http://file1"))));
    FormSource formSource = mock(FormSource.class);
    when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
    when(formSource.fetchMediaFile("http://file1")).thenReturn(new ByteArrayInputStream("contents".getBytes()));
    ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), new FormMetadataParser(), mock(Analytics.class));
    // Initial download
    downloader.downloadForm(serverFormDetails, null, null);
    ServerFormDetails serverFormDetailsUpdatedMediaFile = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), false, false, new ManifestFile("", asList(new MediaFile("file1", Md5.getMd5Hash(new ByteArrayInputStream("contents-updated".getBytes())), "http://file1"))));
    when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
    when(formSource.fetchMediaFile("http://file1")).thenReturn(new ByteArrayInputStream("contents-updated".getBytes()));
    // Second download
    downloader.downloadForm(serverFormDetailsUpdatedMediaFile, null, null);
    List<Form> allForms = formsRepository.getAll();
    assertThat(allForms.size(), is(1));
    Form form = allForms.get(0);
    assertThat(form.getFormId(), is("id"));
    File formFile = new File(getAbsoluteFilePath(formsDir.getAbsolutePath(), form.getFormFilePath()));
    assertThat(formFile.exists(), is(true));
    assertThat(new String(read(formFile)), is(xform));
    File mediaFile1 = new File(form.getFormMediaPath() + "/file1");
    assertThat(mediaFile1.exists(), is(true));
    assertThat(new String(read(mediaFile1)), is("contents-updated"));
}
Also used : MediaFile(org.odk.collect.forms.MediaFile) ByteArrayInputStream(java.io.ByteArrayInputStream) Form(org.odk.collect.forms.Form) FormUtils.buildForm(org.odk.collect.formstest.FormUtils.buildForm) FormSource(org.odk.collect.forms.FormSource) MediaFile(org.odk.collect.forms.MediaFile) File(java.io.File) ManifestFile(org.odk.collect.forms.ManifestFile) ManifestFile(org.odk.collect.forms.ManifestFile) Analytics(org.odk.collect.analytics.Analytics) Test(org.junit.Test)

Example 20 with Form

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

the class ServerFormDownloaderTest method whenFormToDownloadIsUpdate_withSameFormIdAndVersion_savesNewVersionAlongsideOldVersion.

@Test
public void whenFormToDownloadIsUpdate_withSameFormIdAndVersion_savesNewVersionAlongsideOldVersion() throws Exception {
    String xform = createXFormBody("id", "version");
    ServerFormDetails serverFormDetails = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, null);
    FormSource formSource = mock(FormSource.class);
    when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
    ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), new FormMetadataParser(), mock(Analytics.class));
    downloader.downloadForm(serverFormDetails, null, null);
    String xformUpdate = FormUtils.createXFormBody("id", "version", "A different title");
    ServerFormDetails serverFormDetailsUpdated = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xformUpdate.getBytes())), true, false, null);
    when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xformUpdate.getBytes()));
    downloader.downloadForm(serverFormDetailsUpdated, null, null);
    List<Form> allForms = formsRepository.getAllByFormIdAndVersion("id", "version");
    assertThat(allForms.size(), is(2));
    allForms.forEach(f -> {
        File formFile = new File(getAbsoluteFilePath(formsDir.getAbsolutePath(), f.getFormFilePath()));
        assertThat(formFile.exists(), is(true));
    });
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Form(org.odk.collect.forms.Form) FormUtils.buildForm(org.odk.collect.formstest.FormUtils.buildForm) FormSource(org.odk.collect.forms.FormSource) MediaFile(org.odk.collect.forms.MediaFile) File(java.io.File) ManifestFile(org.odk.collect.forms.ManifestFile) Analytics(org.odk.collect.analytics.Analytics) Test(org.junit.Test)

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