Search in sources :

Example 51 with FormController

use of org.odk.collect.android.javarosawrapper.FormController 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 52 with FormController

use of org.odk.collect.android.javarosawrapper.FormController in project collect by opendatakit.

the class RankingWidget method onButtonClick.

@Override
public void onButtonClick(int buttonId) {
    FormController formController = Collect.getInstance().getFormController();
    if (formController != null) {
        formController.setIndexWaitingForData(getFormEntryPrompt().getIndex());
    }
    RankingWidgetDialog rankingWidgetDialog = new RankingWidgetDialog(savedItems == null ? items : savedItems, getFormEntryPrompt().getIndex());
    rankingWidgetDialog.show(((FormEntryActivity) getContext()).getSupportFragmentManager(), "RankingDialog");
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController) RankingWidgetDialog(org.odk.collect.android.fragments.dialogs.RankingWidgetDialog)

Example 53 with FormController

use of org.odk.collect.android.javarosawrapper.FormController in project collect by opendatakit.

the class FormControllerWaitingForDataRegistry method isWaitingForData.

@Override
public boolean isWaitingForData(FormIndex index) {
    Collect collect = Collect.getInstance();
    if (collect == null) {
        throw new IllegalStateException("Collect application instance is null.");
    }
    FormController formController = collect.getFormController();
    if (formController == null) {
        return false;
    }
    return index.equals(formController.getIndexWaitingForData());
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController) Collect(org.odk.collect.android.application.Collect)

Aggregations

FormController (org.odk.collect.android.javarosawrapper.FormController)53 FormIndex (org.javarosa.core.model.FormIndex)11 File (java.io.File)9 JavaRosaException (org.odk.collect.android.exception.JavaRosaException)9 FormDef (org.javarosa.core.model.FormDef)4 FormEntryPrompt (org.javarosa.form.api.FormEntryPrompt)4 Collect (org.odk.collect.android.application.Collect)4 RepeatsInFieldListException (org.odk.collect.android.javarosawrapper.RepeatsInFieldListException)4 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)4 TextView (android.widget.TextView)3 ArrayList (java.util.ArrayList)3 FormInstance (org.javarosa.core.model.instance.FormInstance)3 TreeReference (org.javarosa.core.model.instance.TreeReference)3 FormEntryCaption (org.javarosa.form.api.FormEntryCaption)3 Before (org.junit.Before)3 Test (org.junit.Test)3 Form (org.odk.collect.forms.Form)3 Intent (android.content.Intent)2 View (android.view.View)2 WebView (android.webkit.WebView)2