Search in sources :

Example 6 with UploadException

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);
        }
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) UploadException(org.odk.collect.android.upload.UploadException) IOException(java.io.IOException) BadUrlException(org.odk.collect.android.exception.BadUrlException)

Example 7 with UploadException

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;
}
Also used : UploadException(org.odk.collect.android.upload.UploadException) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString) IOException(java.io.IOException) File(java.io.File) MultipleFoldersFoundException(org.odk.collect.android.exception.MultipleFoldersFoundException)

Example 8 with UploadException

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;
}
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

UploadException (org.odk.collect.android.upload.UploadException)8 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)7 IOException (java.io.IOException)4 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)3 File (java.io.File)3 FormsRepositoryProvider (org.odk.collect.android.utilities.FormsRepositoryProvider)3 Instance (org.odk.collect.forms.instances.Instance)3 PropertyManager (org.odk.collect.android.logic.PropertyManager)2 InstanceServerUploader (org.odk.collect.android.upload.InstanceServerUploader)2 Form (org.odk.collect.forms.Form)2 Pair (android.util.Pair)1 ValueRange (com.google.api.services.sheets.v4.model.ValueRange)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 FormDef (org.javarosa.core.model.FormDef)1 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)1 TreeElement (org.javarosa.core.model.instance.TreeElement)1 FormEntryController (org.javarosa.form.api.FormEntryController)1 FormEntryModel (org.javarosa.form.api.FormEntryModel)1