Search in sources :

Example 6 with FormsRepositoryProvider

use of org.odk.collect.android.utilities.FormsRepositoryProvider in project collect by opendatakit.

the class InstanceGoogleSheetsUploaderTask method doInBackground.

@Override
protected Outcome doInBackground(Long... instanceIdsToUpload) {
    String account = settingsProvider.getUnprotectedSettings().getString(ProjectKeys.KEY_SELECTED_GOOGLE_ACCOUNT);
    InstanceGoogleSheetsUploader uploader = new InstanceGoogleSheetsUploader(googleApiProvider.getDriveApi(account), googleApiProvider.getSheetsApi(account));
    final Outcome outcome = new Outcome();
    List<Instance> instancesToUpload = uploader.getInstancesFromIds(instanceIdsToUpload);
    for (int i = 0; i < instancesToUpload.size(); i++) {
        Instance instance = instancesToUpload.get(i);
        if (isCancelled()) {
            outcome.messagesByInstanceId.put(instance.getDbId().toString(), getLocalizedString(Collect.getInstance(), R.string.instance_upload_cancelled));
            return outcome;
        }
        publishProgress(i + 1, instancesToUpload.size());
        // Get corresponding blank form and verify there is exactly 1
        List<Form> forms = new FormsRepositoryProvider(Collect.getInstance()).get().getAllByFormIdAndVersion(instance.getFormId(), instance.getFormVersion());
        if (forms.size() != 1) {
            outcome.messagesByInstanceId.put(instance.getDbId().toString(), getLocalizedString(Collect.getInstance(), R.string.not_exactly_one_blank_form_for_this_form_id));
        } else {
            try {
                String destinationUrl = uploader.getUrlToSubmitTo(instance, null, null, settingsProvider.getUnprotectedSettings().getString(KEY_GOOGLE_SHEETS_URL));
                if (InstanceUploaderUtils.doesUrlRefersToGoogleSheetsFile(destinationUrl)) {
                    uploader.uploadOneSubmission(instance, destinationUrl);
                    outcome.messagesByInstanceId.put(instance.getDbId().toString(), DEFAULT_SUCCESSFUL_TEXT);
                    analytics.logEvent(SUBMISSION, "HTTP-Sheets", Collect.getFormIdentifierHash(instance.getFormId(), instance.getFormVersion()));
                } else {
                    outcome.messagesByInstanceId.put(instance.getDbId().toString(), SPREADSHEET_UPLOADED_TO_GOOGLE_DRIVE);
                }
            } catch (UploadException e) {
                Timber.d(e);
                outcome.messagesByInstanceId.put(instance.getDbId().toString(), e.getDisplayMessage());
            }
        }
    }
    return outcome;
}
Also used : Instance(org.odk.collect.forms.instances.Instance) Form(org.odk.collect.forms.Form) UploadException(org.odk.collect.android.upload.UploadException) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString) FormsRepositoryProvider(org.odk.collect.android.utilities.FormsRepositoryProvider)

Example 7 with FormsRepositoryProvider

use of org.odk.collect.android.utilities.FormsRepositoryProvider 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)

Aggregations

FormsRepositoryProvider (org.odk.collect.android.utilities.FormsRepositoryProvider)7 Form (org.odk.collect.forms.Form)6 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)5 Instance (org.odk.collect.forms.instances.Instance)4 UploadException (org.odk.collect.android.upload.UploadException)3 File (java.io.File)2 InstancesRepositoryProvider (org.odk.collect.android.utilities.InstancesRepositoryProvider)2 Pair (android.util.Pair)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)1 FormInstance (org.javarosa.core.model.instance.FormInstance)1