use of org.odk.collect.android.upload.UploadException in project collect by opendatakit.
the class InstanceGoogleSheetsUploader method setUpSpreadsheet.
private void setUpSpreadsheet(String urlString) throws UploadException, GoogleJsonResponseException {
if (spreadsheet == null || spreadsheet.getSpreadsheetUrl() == null || !urlString.equals(spreadsheet.getSpreadsheetUrl())) {
try {
spreadsheet = sheetsHelper.getSpreadsheet(UrlUtils.getSpreadsheetID(urlString));
spreadsheet.setSpreadsheetUrl(urlString);
} catch (GoogleJsonResponseException e) {
Timber.i(e);
throw e;
} catch (IOException | BadUrlException e) {
Timber.i(e);
throw new UploadException(e);
}
}
}
use of org.odk.collect.android.upload.UploadException in project collect by opendatakit.
the class InstanceGoogleSheetsUploader method uploadMediaFile.
private String uploadMediaFile(Instance instance, String fileName) throws UploadException {
File instanceFile = new File(instance.getInstanceFilePath());
String filePath = instanceFile.getParentFile() + "/" + fileName;
File toUpload = new File(filePath);
if (!new File(filePath).exists()) {
throw new UploadException(Collect.getInstance().getString(R.string.media_upload_error, filePath));
}
String folderId;
try {
folderId = driveHelper.createOrGetIDOfSubmissionsFolder();
} catch (IOException | MultipleFoldersFoundException e) {
Timber.e(e);
throw new UploadException(e);
}
String uploadedFileId;
// file is ready to be uploaded
try {
uploadedFileId = driveHelper.uploadFileToDrive(filePath, folderId, toUpload);
} catch (IOException e) {
Timber.e(e, "Exception thrown while uploading the file to drive");
throw new UploadException(e);
}
// checking if file was successfully uploaded
if (uploadedFileId == null) {
throw new UploadException("Unable to upload the media files. Try again");
}
return UPLOADED_MEDIA_URL + uploadedFileId;
}
use of org.odk.collect.android.upload.UploadException 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