Search in sources :

Example 51 with Form

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

the class FormEntryActivity method createLanguageDialog.

/**
 * Creates and displays a dialog allowing the user to set the language for
 * the form.
 */
private void createLanguageDialog() {
    FormController formController = getFormController();
    final String[] languages = formController.getLanguages();
    int selected = -1;
    if (languages != null) {
        String language = formController.getLanguage();
        for (int i = 0; i < languages.length; i++) {
            if (language.equals(languages[i])) {
                selected = i;
            }
        }
    }
    alertDialog = new MaterialAlertDialogBuilder(this).setSingleChoiceItems(languages, selected, (dialog, whichButton) -> {
        Form form = formsRepository.getOneByPath(formPath);
        if (form != null) {
            formsRepository.save(new Form.Builder(form).language(languages[whichButton]).build());
        }
        getFormController().setLanguage(languages[whichButton]);
        dialog.dismiss();
        if (getFormController().currentPromptIsQuestion()) {
            saveAnswersForCurrentScreen(DO_NOT_EVALUATE_CONSTRAINTS);
        }
        onScreenRefresh();
    }).setTitle(getString(R.string.change_language)).setNegativeButton(getString(R.string.do_not_change), null).create();
    alertDialog.show();
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController) Form(org.odk.collect.forms.Form) MaterialAlertDialogBuilder(com.google.android.material.dialog.MaterialAlertDialogBuilder) FailedConstraint(org.odk.collect.android.javarosawrapper.FormController.FailedConstraint)

Example 52 with Form

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

the class Collect method getFormIdentifierHash.

/**
 * Gets a unique, privacy-preserving identifier for a form based on its id and version.
 *
 * @param formId      id of a form
 * @param formVersion version of a form
 * @return md5 hash of the form title, a space, the form ID
 */
public static String getFormIdentifierHash(String formId, String formVersion) {
    Form form = new FormsRepositoryProvider(Collect.getInstance()).get().getLatestByFormIdAndVersion(formId, formVersion);
    String formTitle = form != null ? form.getDisplayName() : "";
    String formIdentifier = formTitle + " " + formId;
    return Md5.getMd5Hash(new ByteArrayInputStream(formIdentifier.getBytes()));
}
Also used : Form(org.odk.collect.forms.Form) ByteArrayInputStream(java.io.ByteArrayInputStream) FormsRepositoryProvider(org.odk.collect.android.utilities.FormsRepositoryProvider)

Example 53 with Form

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

the class InstanceListCursorAdapter method getView.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    ImageView imageView = view.findViewById(R.id.image);
    setImageFromStatus(imageView);
    setUpSubtext(view);
    // Update: This only seems to be the case in Edit Saved Forms and it's not clear why...
    if (!shouldCheckDisabled) {
        return view;
    }
    boolean formExists = false;
    boolean isFormEncrypted = false;
    String formId = getCursor().getString(getCursor().getColumnIndex(DatabaseInstanceColumns.JR_FORM_ID));
    String formVersion = getCursor().getString(getCursor().getColumnIndex(DatabaseInstanceColumns.JR_VERSION));
    Form form = new FormsRepositoryProvider(context.getApplicationContext()).get().getLatestByFormIdAndVersion(formId, formVersion);
    if (form != null) {
        String base64RSAPublicKey = form.getBASE64RSAPublicKey();
        formExists = true;
        isFormEncrypted = base64RSAPublicKey != null;
    }
    long date = getCursor().getLong(getCursor().getColumnIndex(DatabaseInstanceColumns.DELETED_DATE));
    if (date != 0 || !formExists || isFormEncrypted) {
        String disabledMessage;
        if (date != 0) {
            try {
                String deletedTime = context.getString(R.string.deleted_on_date_at_time);
                disabledMessage = new SimpleDateFormat(deletedTime, Locale.getDefault()).format(new Date(date));
            } catch (IllegalArgumentException e) {
                Timber.e(e);
                disabledMessage = context.getString(R.string.submission_deleted);
            }
        } else if (!formExists) {
            disabledMessage = context.getString(R.string.deleted_form);
        } else {
            disabledMessage = context.getString(R.string.encrypted_form);
        }
        setDisabled(view, disabledMessage);
    } else {
        setEnabled(view);
    }
    return view;
}
Also used : Form(org.odk.collect.forms.Form) ImageView(android.widget.ImageView) FormsRepositoryProvider(org.odk.collect.android.utilities.FormsRepositoryProvider) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 54 with Form

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

the class InstanceGoogleSheetsUploader method uploadOneSubmission.

@Override
public String uploadOneSubmission(Instance instance, String spreadsheetUrl) throws UploadException {
    File instanceFile = new File(instance.getInstanceFilePath());
    if (!instanceFile.exists()) {
        throw new UploadException(FAIL + "instance XML file does not exist!");
    }
    // Get corresponding blank form and verify there is exactly 1
    List<Form> forms = new FormsRepositoryProvider(Collect.getInstance()).get().getAllByFormIdAndVersion(instance.getFormId(), instance.getFormVersion());
    try {
        if (forms.size() != 1) {
            throw new UploadException(getLocalizedString(Collect.getInstance(), R.string.not_exactly_one_blank_form_for_this_form_id));
        }
        Form form = forms.get(0);
        if (form.getBASE64RSAPublicKey() != null) {
            submissionComplete(instance, false);
            throw new UploadException(getLocalizedString(Collect.getInstance(), R.string.google_sheets_encrypted_message));
        }
        String formFilePath = PathUtils.getAbsoluteFilePath(new StoragePathProvider().getOdkDirPath(StorageSubdirectory.FORMS), form.getFormFilePath());
        TreeElement instanceElement = getInstanceElement(formFilePath, instanceFile);
        setUpSpreadsheet(spreadsheetUrl);
        sheetsHelper.updateSpreadsheetLocaleForNewSpreadsheet(spreadsheet.getSpreadsheetId(), spreadsheet.getSheets().get(0).getProperties().getTitle());
        if (hasRepeatableGroups(instanceElement)) {
            createSheetsIfNeeded(instanceElement);
        }
        String key = getInstanceID(getChildElements(instanceElement, false));
        if (key == null) {
            key = PropertyUtils.genUUID();
        }
        insertRows(instance, instanceElement, null, key, instanceFile, spreadsheet.getSheets().get(0).getProperties().getTitle());
    } catch (UploadException e) {
        submissionComplete(instance, false);
        throw e;
    } catch (GoogleJsonResponseException e) {
        submissionComplete(instance, false);
        throw new UploadException(getErrorMessageFromGoogleJsonResponseException(e));
    }
    submissionComplete(instance, true);
    // Google Sheets can't provide a custom success message
    return null;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) StoragePathProvider(org.odk.collect.android.storage.StoragePathProvider) Form(org.odk.collect.forms.Form) UploadException(org.odk.collect.android.upload.UploadException) FormsRepositoryProvider(org.odk.collect.android.utilities.FormsRepositoryProvider) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString) File(java.io.File) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 55 with Form

use of org.odk.collect.forms.Form 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)

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