Search in sources :

Example 1 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)

Example 2 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 3 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 4 with Form

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

the class FormDeleter method delete.

public void delete(Long id) {
    Form form = formsRepository.get(id);
    List<Instance> instancesForVersion = instancesRepository.getAllNotDeletedByFormIdAndVersion(form.getFormId(), form.getVersion());
    // If there's more than one form with the same formid/version, trust the user that they want to truly delete this one
    // because otherwise it may not ever be removed (instance deletion only deletes one corresponding form).
    List<Form> formsWithSameFormIdVersion = formsRepository.getAllByFormIdAndVersion(form.getFormId(), form.getVersion());
    if (instancesForVersion.isEmpty() || formsWithSameFormIdVersion.size() > 1) {
        formsRepository.delete(id);
    } else {
        formsRepository.softDelete(form.getDbId());
    }
}
Also used : Form(org.odk.collect.forms.Form) Instance(org.odk.collect.forms.instances.Instance)

Example 5 with Form

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

the class ServerFormDownloader method downloadXform.

/**
 * Takes the formName and the URL and attempts to download the specified file. Returns a file
 * object representing the downloaded file.
 */
private FileResult downloadXform(String formName, String url, FormDownloaderListener stateListener, File tempDir, String formsDirPath) throws FormSourceException, IOException, FormDownloadException.DownloadingInterrupted {
    InputStream xform = formSource.fetchForm(url);
    String fileName = getFormFileName(formName, formsDirPath);
    File tempFormFile = new File(tempDir + File.separator + fileName);
    writeFile(xform, tempFormFile, tempDir, stateListener);
    // we've downloaded the file, and we may have renamed it
    // make sure it's not the same as a file we already have
    Form form = formsRepository.getOneByMd5Hash(Md5.getMd5Hash(tempFormFile));
    if (form != null) {
        // delete the file we just downloaded, because it's a duplicate
        FileUtils.deleteAndReport(tempFormFile);
        // set the file returned to the file we already had
        return new FileResult(new File(form.getFormFilePath()), false);
    } else {
        return new FileResult(tempFormFile, true);
    }
}
Also used : Form(org.odk.collect.forms.Form) InputStream(java.io.InputStream) MediaFile(org.odk.collect.forms.MediaFile) File(java.io.File)

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