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();
}
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()));
}
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;
}
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;
}
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;
}
Aggregations