Search in sources :

Example 16 with InstancesRepository

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

the class SaveFormToDisk method updateInstanceDatabase.

/**
 * Updates the status and editability for the database row corresponding to the instance that is
 * currently managed by the {@link FormController}. There are three cases:
 * - the instance was opened for edit so its database row already exists
 * - a new instance was just created so its database row doesn't exist and needs to be created
 * - a new instance was created at the start of this editing session but the user has already
 * saved it so its database row already exists
 * <p>
 * Post-condition: the uri field is set to the URI of the instance database row that matches
 * the instance currently managed by the {@link FormController}.
 */
private void updateInstanceDatabase(boolean incomplete, boolean canEditAfterCompleted) {
    FormController formController = Collect.getInstance().getFormController();
    FormInstance formInstance = formController.getFormDef().getInstance();
    String instancePath = formController.getInstanceFile().getAbsolutePath();
    InstancesRepository instances = new InstancesRepositoryProvider(Collect.getInstance()).get();
    Instance instance = instances.getOneByPath(instancePath);
    Instance.Builder instanceBuilder;
    if (instance != null) {
        instanceBuilder = new Instance.Builder(instance);
    } else {
        instanceBuilder = new Instance.Builder();
    }
    if (instanceName != null) {
        instanceBuilder.displayName(instanceName);
    }
    if (incomplete || !shouldFinalize) {
        instanceBuilder.status(Instance.STATUS_INCOMPLETE);
    } else {
        instanceBuilder.status(Instance.STATUS_COMPLETE);
    }
    instanceBuilder.canEditWhenComplete(canEditAfterCompleted);
    if (instance != null) {
        String geometryXpath = getGeometryXpathForInstance(instance);
        Pair<String, String> geometryContentValues = extractGeometryContentValues(formInstance, geometryXpath);
        if (geometryContentValues != null) {
            instanceBuilder.geometryType(geometryContentValues.first);
            instanceBuilder.geometry(geometryContentValues.second);
        }
        Instance newInstance = new InstancesRepositoryProvider(Collect.getInstance()).get().save(instanceBuilder.build());
        uri = InstancesContract.getUri(currentProjectId, newInstance.getDbId());
    } else {
        Timber.i("No instance found, creating");
        Form form = new FormsRepositoryProvider(Collect.getInstance()).get().get(ContentUriHelper.getIdFromUri(uri));
        // add missing fields into values
        instanceBuilder.instanceFilePath(instancePath);
        instanceBuilder.submissionUri(form.getSubmissionUri());
        if (instanceName != null) {
            instanceBuilder.displayName(instanceName);
        } else {
            instanceBuilder.displayName(form.getDisplayName());
        }
        instanceBuilder.formId(form.getFormId());
        instanceBuilder.formVersion(form.getVersion());
        Pair<String, String> geometryContentValues = extractGeometryContentValues(formInstance, form.getGeometryXpath());
        if (geometryContentValues != null) {
            instanceBuilder.geometryType(geometryContentValues.first);
            instanceBuilder.geometry(geometryContentValues.second);
        }
    }
    Instance newInstance = new InstancesRepositoryProvider(Collect.getInstance()).get().save(instanceBuilder.build());
    uri = InstancesContract.getUri(currentProjectId, newInstance.getDbId());
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController) InstancesRepositoryProvider(org.odk.collect.android.utilities.InstancesRepositoryProvider) FormInstance(org.javarosa.core.model.instance.FormInstance) Instance(org.odk.collect.forms.instances.Instance) Form(org.odk.collect.forms.Form) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString) FormsRepositoryProvider(org.odk.collect.android.utilities.FormsRepositoryProvider) FormInstance(org.javarosa.core.model.instance.FormInstance)

Example 17 with InstancesRepository

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

the class InstancesRepositoryTest method getAllByStatus_withMultipleStatus_returnsMatchingInstances.

@Test
public void getAllByStatus_withMultipleStatus_returnsMatchingInstances() {
    InstancesRepository instancesRepository = buildSubject();
    instancesRepository.save(InstanceUtils.buildInstance("incomplete", "1", getInstancesDir()).status(Instance.STATUS_INCOMPLETE).build());
    instancesRepository.save(InstanceUtils.buildInstance("incomplete", "1", getInstancesDir()).status(Instance.STATUS_INCOMPLETE).build());
    instancesRepository.save(InstanceUtils.buildInstance("complete", "1", getInstancesDir()).status(Instance.STATUS_COMPLETE).build());
    instancesRepository.save(InstanceUtils.buildInstance("complete", "1", getInstancesDir()).status(Instance.STATUS_COMPLETE).build());
    instancesRepository.save(InstanceUtils.buildInstance("submitted", "1", getInstancesDir()).status(Instance.STATUS_SUBMITTED).build());
    instancesRepository.save(InstanceUtils.buildInstance("submitted", "1", getInstancesDir()).status(Instance.STATUS_SUBMITTED).build());
    List<Instance> incomplete = instancesRepository.getAllByStatus(Instance.STATUS_INCOMPLETE, Instance.STATUS_SUBMITTED);
    assertThat(incomplete.size(), is(4));
    assertThat(incomplete.get(0).getFormId(), is(not("complete")));
    assertThat(incomplete.get(1).getFormId(), is(not("complete")));
    assertThat(incomplete.get(2).getFormId(), is(not("complete")));
    assertThat(incomplete.get(3).getStatus(), is(not("complete")));
    // Check corresponding count method is also correct
    assertThat(instancesRepository.getCountByStatus(Instance.STATUS_INCOMPLETE, Instance.STATUS_SUBMITTED), is(4));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) Test(org.junit.Test)

Example 18 with InstancesRepository

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

the class InstancesRepositoryTest method deleteAll_deletesInstanceFiles.

@Test
public void deleteAll_deletesInstanceFiles() {
    InstancesRepository instancesRepository = buildSubject();
    Instance instance1 = instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).build());
    Instance instance2 = instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).build());
    instancesRepository.deleteAll();
    assertThat(new File(instance1.getInstanceFilePath()).exists(), is(false));
    assertThat(new File(instance2.getInstanceFilePath()).exists(), is(false));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) File(java.io.File) Test(org.junit.Test)

Example 19 with InstancesRepository

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

the class InstancesRepositoryTest method deleteAll_deletesAllInstances.

@Test
public void deleteAll_deletesAllInstances() {
    InstancesRepository instancesRepository = buildSubject();
    instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).build());
    instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).build());
    instancesRepository.deleteAll();
    assertThat(instancesRepository.getAll().size(), is(0));
}
Also used : InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) Test(org.junit.Test)

Example 20 with InstancesRepository

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

the class InstancesRepositoryTest method deleteWithLogging_clearsGeometryData.

@Test
public void deleteWithLogging_clearsGeometryData() {
    InstancesRepository instancesRepository = buildSubject();
    Instance instance = instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).geometry("blah").geometryType("blah").build());
    instancesRepository.deleteWithLogging(instance.getDbId());
    assertThat(instancesRepository.get(instance.getDbId()).getGeometry(), is(nullValue()));
    assertThat(instancesRepository.get(instance.getDbId()).getGeometryType(), is(nullValue()));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) Test(org.junit.Test)

Aggregations

InstancesRepository (org.odk.collect.forms.instances.InstancesRepository)25 Instance (org.odk.collect.forms.instances.Instance)23 Test (org.junit.Test)19 File (java.io.File)4 InstancesRepositoryProvider (org.odk.collect.android.utilities.InstancesRepositoryProvider)3 ContentValues (android.content.ContentValues)2 FormInstance (org.javarosa.core.model.instance.FormInstance)2 FormController (org.odk.collect.android.javarosawrapper.FormController)2 FormsRepository (org.odk.collect.forms.FormsRepository)2 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)2 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 AsyncTask (android.os.AsyncTask)1 RandomAccessFile (java.io.RandomAccessFile)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 Stream (java.util.stream.Stream)1 ByteArrayPayload (org.javarosa.core.services.transport.payload.ByteArrayPayload)1 Collect (org.odk.collect.android.application.Collect)1 DatabaseObjectMapper.getValuesFromInstance (org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromInstance)1