Search in sources :

Example 56 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class InstancesDaoHelper method isInstanceComplete.

/**
 * Checks the database to determine if the current instance being edited has
 * already been 'marked completed'. A form can be 'unmarked' complete and
 * then resaved.
 *
 * @return true if form has been marked completed, false otherwise.
 * <p>
 * TODO: replace with method in {@link InstancesRepository}
 * that returns an {@link Instance} object from a path.
 */
public static boolean isInstanceComplete(boolean end, boolean completedByDefault) {
    // default to false if we're mid form
    boolean complete = false;
    FormController formController = Collect.getInstance().getFormController();
    if (formController != null && formController.getInstanceFile() != null) {
        // First check if we're at the end of the form, then check the preferences
        complete = end && completedByDefault;
        // Then see if we've already marked this form as complete before
        String path = formController.getInstanceFile().getAbsolutePath();
        Instance instance = new InstancesRepositoryProvider(Collect.getInstance()).get().getOneByPath(path);
        if (instance != null && instance.getStatus().equals(Instance.STATUS_COMPLETE)) {
            complete = true;
        }
    } else {
        Timber.w("FormController or its instanceFile field has a null value");
    }
    return complete;
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController) InstancesRepositoryProvider(org.odk.collect.android.utilities.InstancesRepositoryProvider) Instance(org.odk.collect.forms.instances.Instance)

Example 57 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class DatabaseInstancesRepository method getOneByPath.

@Override
public Instance getOneByPath(String instancePath) {
    String selection = INSTANCE_FILE_PATH + "=?";
    String[] args = { getRelativeFilePath(instancesPath, instancePath) };
    try (Cursor cursor = query(null, selection, args, null)) {
        List<Instance> instances = getInstancesFromCursor(cursor, instancesPath);
        if (instances.size() == 1) {
            return instances.get(0);
        } else {
            return null;
        }
    }
}
Also used : Instance(org.odk.collect.forms.instances.Instance) DatabaseObjectMapper.getValuesFromInstance(org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromInstance) Cursor(android.database.Cursor)

Example 58 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class DatabaseInstancesRepository method get.

@Override
public Instance get(Long databaseId) {
    String selection = _ID + "=?";
    String[] selectionArgs = { Long.toString(databaseId) };
    try (Cursor cursor = query(null, selection, selectionArgs, null)) {
        List<Instance> result = getInstancesFromCursor(cursor, instancesPath);
        return !result.isEmpty() ? result.get(0) : null;
    }
}
Also used : Instance(org.odk.collect.forms.instances.Instance) DatabaseObjectMapper.getValuesFromInstance(org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromInstance) Cursor(android.database.Cursor)

Example 59 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class DatabaseInstancesRepository method getInstancesFromCursor.

private static List<Instance> getInstancesFromCursor(Cursor cursor, String instancesPath) {
    List<Instance> instances = new ArrayList<>();
    cursor.moveToPosition(-1);
    while (cursor.moveToNext()) {
        Instance instance = getInstanceFromCurrentCursorPosition(cursor, instancesPath);
        instances.add(instance);
    }
    return instances;
}
Also used : Instance(org.odk.collect.forms.instances.Instance) DatabaseObjectMapper.getValuesFromInstance(org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromInstance) ArrayList(java.util.ArrayList)

Example 60 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class DatabaseInstancesRepository method deleteAll.

@Override
public void deleteAll() {
    List<Instance> instances = getAll();
    databaseConnection.getWriteableDatabase().delete(INSTANCES_TABLE_NAME, null, null);
    for (Instance instance : instances) {
        deleteInstanceFiles(instance);
    }
}
Also used : Instance(org.odk.collect.forms.instances.Instance) DatabaseObjectMapper.getValuesFromInstance(org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromInstance)

Aggregations

Instance (org.odk.collect.forms.instances.Instance)62 Test (org.junit.Test)28 InstancesRepository (org.odk.collect.forms.instances.InstancesRepository)23 InstancesRepositoryProvider (org.odk.collect.android.utilities.InstancesRepositoryProvider)10 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)9 File (java.io.File)7 DatabaseObjectMapper.getValuesFromInstance (org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromInstance)7 Uri (android.net.Uri)6 Form (org.odk.collect.forms.Form)6 InstanceUtils.buildInstance (org.odk.collect.formstest.InstanceUtils.buildInstance)6 Pair (android.util.Pair)5 ArrayList (java.util.ArrayList)5 FormsRepositoryProvider (org.odk.collect.android.utilities.FormsRepositoryProvider)5 Date (java.util.Date)4 FormController (org.odk.collect.android.javarosawrapper.FormController)4 MapPoint (org.odk.collect.geo.maps.MapPoint)4 ContentValues (android.content.ContentValues)3 Intent (android.content.Intent)3 Cursor (android.database.Cursor)3 AsyncTask (android.os.AsyncTask)2