use of org.odk.collect.android.utilities.WebCredentialsUtils in project collect by opendatakit.
the class InstanceSubmitter method submitInstances.
public Pair<Boolean, String> submitInstances(List<Instance> toUpload) throws SubmitException {
if (toUpload.isEmpty()) {
throw new SubmitException(Type.NOTHING_TO_SUBMIT);
}
String protocol = generalSettings.getString(ProjectKeys.KEY_PROTOCOL);
InstanceUploader uploader;
Map<String, String> resultMessagesByInstanceId = new HashMap<>();
String deviceId = null;
boolean anyFailure = false;
if (protocol.equals(ProjectKeys.PROTOCOL_GOOGLE_SHEETS)) {
if (permissionsProvider.isGetAccountsPermissionGranted()) {
String googleUsername = googleAccountsManager.getLastSelectedAccountIfValid();
if (googleUsername.isEmpty()) {
throw new SubmitException(Type.GOOGLE_ACCOUNT_NOT_SET);
}
googleAccountsManager.selectAccount(googleUsername);
uploader = new InstanceGoogleSheetsUploader(googleApiProvider.getDriveApi(googleUsername), googleApiProvider.getSheetsApi(googleUsername));
} else {
throw new SubmitException(Type.GOOGLE_ACCOUNT_NOT_PERMITTED);
}
} else {
OpenRosaHttpInterface httpInterface = Collect.getInstance().getComponent().openRosaHttpInterface();
uploader = new InstanceServerUploader(httpInterface, new WebCredentialsUtils(generalSettings), new HashMap<>(), generalSettings);
deviceId = new PropertyManager().getSingularProperty(PropertyManager.PROPMGR_DEVICE_ID);
}
for (Instance instance : toUpload) {
try {
String destinationUrl;
if (protocol.equals(ProjectKeys.PROTOCOL_GOOGLE_SHEETS)) {
destinationUrl = uploader.getUrlToSubmitTo(instance, null, null, generalSettings.getString(KEY_GOOGLE_SHEETS_URL));
if (!InstanceUploaderUtils.doesUrlRefersToGoogleSheetsFile(destinationUrl)) {
anyFailure = true;
resultMessagesByInstanceId.put(instance.getDbId().toString(), SPREADSHEET_UPLOADED_TO_GOOGLE_DRIVE);
continue;
}
} else {
destinationUrl = uploader.getUrlToSubmitTo(instance, deviceId, null, null);
}
String customMessage = uploader.uploadOneSubmission(instance, destinationUrl);
resultMessagesByInstanceId.put(instance.getDbId().toString(), customMessage != null ? customMessage : getLocalizedString(Collect.getInstance(), R.string.success));
// communicated to the user. Maybe successful delete should also be communicated?
if (InstanceUploaderUtils.shouldFormBeDeleted(formsRepository, instance.getFormId(), instance.getFormVersion(), generalSettings.getBoolean(ProjectKeys.KEY_DELETE_AFTER_SEND))) {
new InstanceDeleter(new InstancesRepositoryProvider(Collect.getInstance()).get(), new FormsRepositoryProvider(Collect.getInstance()).get()).delete(instance.getDbId());
}
String action = protocol.equals(ProjectKeys.PROTOCOL_GOOGLE_SHEETS) ? "HTTP-Sheets auto" : "HTTP auto";
String label = Collect.getFormIdentifierHash(instance.getFormId(), instance.getFormVersion());
analytics.logEvent(SUBMISSION, action, label);
} catch (UploadException e) {
Timber.d(e);
anyFailure = true;
resultMessagesByInstanceId.put(instance.getDbId().toString(), e.getDisplayMessage());
}
}
return new Pair<>(anyFailure, InstanceUploaderUtils.getUploadResultMessage(instancesRepository, Collect.getInstance(), resultMessagesByInstanceId));
}
use of org.odk.collect.android.utilities.WebCredentialsUtils in project collect by opendatakit.
the class DownloadFormListTaskTest method whenAlternateCredentialsAreSet_shouldServerFormsDetailsFetcherBeUpdated.
@Test
public void whenAlternateCredentialsAreSet_shouldServerFormsDetailsFetcherBeUpdated() {
ServerFormsDetailsFetcher serverFormsDetailsFetcher = mock(ServerFormsDetailsFetcher.class);
WebCredentialsUtils webCredentialsUtils = mock(WebCredentialsUtils.class);
DownloadFormListTask task = new DownloadFormListTask(serverFormsDetailsFetcher);
task.setAlternateCredentials(webCredentialsUtils, "https://test-server.com", "testUser", "testPassword");
verify(serverFormsDetailsFetcher).updateUrl("https://test-server.com");
verify(serverFormsDetailsFetcher).updateCredentials(webCredentialsUtils);
}
use of org.odk.collect.android.utilities.WebCredentialsUtils in project collect by opendatakit.
the class DownloadFormListTaskTest method whenAlternateCredentialsDoNotContainUrl_shouldNotUrlBeUpdated.
@Test
public void whenAlternateCredentialsDoNotContainUrl_shouldNotUrlBeUpdated() {
ServerFormsDetailsFetcher serverFormsDetailsFetcher = mock(ServerFormsDetailsFetcher.class);
WebCredentialsUtils webCredentialsUtils = mock(WebCredentialsUtils.class);
DownloadFormListTask task = new DownloadFormListTask(serverFormsDetailsFetcher);
task.setAlternateCredentials(webCredentialsUtils, null, "testUser", "testPassword");
verify(serverFormsDetailsFetcher, never()).updateUrl(anyString());
verify(serverFormsDetailsFetcher).updateCredentials(webCredentialsUtils);
task.setAlternateCredentials(webCredentialsUtils, "", "testUser", "testPassword");
verify(serverFormsDetailsFetcher, never()).updateUrl(anyString());
verify(serverFormsDetailsFetcher, times(2)).updateCredentials(webCredentialsUtils);
}
Aggregations