Search in sources :

Example 11 with FormStatusEvent

use of org.opendatakit.briefcase.model.FormStatusEvent in project briefcase by opendatakit.

the class ServerFetcher method downloadSubmission.

private void downloadSubmission(File formInstancesDir, DatabaseUtils formDatabase, BriefcaseFormDefinition lfd, FormStatus fs, String uri) throws Exception {
    File instanceFolder = formDatabase.hasRecordedInstance(uri);
    if (instanceFolder != null) {
        // check if the submission file is present in the folder before skipping
        File instance = new File(instanceFolder, "submission.xml");
        File instanceEncrypted = new File(instanceFolder, "submission.xml.enc");
        if (instance.exists() || instanceEncrypted.exists()) {
            log.info("already present - skipping fetch: " + uri);
            return;
        }
    }
    String formId = lfd.getSubmissionKey(uri);
    if (isCancelled()) {
        fs.setStatusString("aborting fetch of submission...", true);
        EventBus.publish(new FormStatusEvent(fs));
        throw new SubmissionDownloadException("Transfer cancelled by user.");
    }
    String baseUrl = serverInfo.getUrl() + "/view/downloadSubmission";
    Map<String, String> params = new HashMap<>();
    params.put("formId", formId);
    String fullUrl = WebUtils.createLinkWithProperties(baseUrl, params);
    AggregateUtils.DocumentFetchResult result;
    try {
        DocumentDescription submissionDescription = new DocumentDescription("Fetch of a submission failed.  Detailed error: ", "Fetch of a submission failed.", "submission", terminationFuture);
        result = AggregateUtils.getXmlDocument(fullUrl, serverInfo, false, submissionDescription, null);
    } catch (XmlDocumentFetchException e) {
        throw new SubmissionDownloadException(e.getMessage());
    }
    // and parse the document...
    SubmissionManifest submissionManifest;
    try {
        submissionManifest = XmlManipulationUtils.parseDownloadSubmissionResponse(result.doc);
    } catch (ParsingException e) {
        throw new SubmissionDownloadException(e.getMessage());
    }
    String msg = "Fetched instanceID=" + submissionManifest.instanceID;
    log.info(msg);
    if (FileSystemUtils.hasFormSubmissionDirectory(formInstancesDir, submissionManifest.instanceID)) {
        // create instance directory...
        File instanceDir = FileSystemUtils.assertFormSubmissionDirectory(formInstancesDir, submissionManifest.instanceID);
        // fetch attachments
        for (MediaFile m : submissionManifest.attachmentList) {
            downloadMediaFileIfChanged(instanceDir, m, fs);
        }
        // write submission file -- we rely on instanceId being unique...
        File submissionFile = new File(instanceDir, "submission.xml");
        OutputStreamWriter fo = new OutputStreamWriter(new FileOutputStream(submissionFile), "UTF-8");
        fo.write(submissionManifest.submissionXml);
        fo.close();
        // if we get here, we know that this is a completed submission
        // (because it was in /view/submissionList) and that we safely
        // copied it into the storage area (because we didn't get any
        // exceptions).
        formDatabase.assertRecordedInstanceDirectory(uri, instanceDir);
    } else {
        // create instance directory...
        File instanceDir = FileSystemUtils.assertFormSubmissionDirectory(formInstancesDir, submissionManifest.instanceID);
        // fetch attachments
        for (MediaFile m : submissionManifest.attachmentList) {
            downloadMediaFileIfChanged(instanceDir, m, fs);
        }
        // write submission file
        File submissionFile = new File(instanceDir, "submission.xml");
        OutputStreamWriter fo = new OutputStreamWriter(new FileOutputStream(submissionFile), "UTF-8");
        fo.write(submissionManifest.submissionXml);
        fo.close();
        // if we get here, we know that this is a completed submission
        // (because it was in /view/submissionList) and that we safely
        // copied it into the storage area (because we didn't get any
        // exceptions).
        formDatabase.assertRecordedInstanceDirectory(uri, instanceDir);
    }
}
Also used : HashMap(java.util.HashMap) FormStatusEvent(org.opendatakit.briefcase.model.FormStatusEvent) XmlDocumentFetchException(org.opendatakit.briefcase.model.XmlDocumentFetchException) DocumentDescription(org.opendatakit.briefcase.model.DocumentDescription) ParsingException(org.opendatakit.briefcase.model.ParsingException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Example 12 with FormStatusEvent

use of org.opendatakit.briefcase.model.FormStatusEvent in project briefcase by opendatakit.

the class ServerFetcher method downloadFormAndSubmissionFiles.

public boolean downloadFormAndSubmissionFiles(List<FormStatus> formsToTransfer) {
    boolean allSuccessful = true;
    // boolean error = false;
    int total = formsToTransfer.size();
    for (int i = 0; i < total; i++) {
        FormStatus fs = formsToTransfer.get(i);
        if (isCancelled()) {
            fs.setStatusString("Aborted. Skipping fetch of form and submissions...", true);
            EventBus.publish(new FormStatusEvent(fs));
            return false;
        }
        RemoteFormDefinition fd = getRemoteFormDefinition(fs);
        fs.setStatusString("Fetching form definition", true);
        EventBus.publish(new FormStatusEvent(fs));
        try {
            File tmpdl = FileSystemUtils.getTempFormDefinitionFile();
            AggregateUtils.commonDownloadFile(serverInfo, tmpdl, fd.getDownloadUrl());
            fs.setStatusString("resolving against briefcase form definitions", true);
            EventBus.publish(new FormStatusEvent(fs));
            boolean successful = false;
            BriefcaseFormDefinition briefcaseLfd;
            DatabaseUtils formDatabase = null;
            try {
                try {
                    briefcaseLfd = BriefcaseFormDefinition.resolveAgainstBriefcaseDefn(tmpdl);
                    if (briefcaseLfd.needsMediaUpdate()) {
                        if (fd.getManifestUrl() != null) {
                            File mediaDir = FileSystemUtils.getMediaDirectory(briefcaseLfd.getFormDirectory());
                            String error = downloadManifestAndMediaFiles(mediaDir, fs);
                            if (error != null) {
                                allSuccessful = false;
                                fs.setStatusString("Error fetching form definition: " + error, false);
                                EventBus.publish(new FormStatusEvent(fs));
                                continue;
                            }
                        }
                    }
                    formDatabase = DatabaseUtils.newInstance(briefcaseLfd.getFormDirectory());
                } catch (BadFormDefinition e) {
                    allSuccessful = false;
                    String msg = "Error parsing form definition";
                    log.error(msg, e);
                    fs.setStatusString(msg + ": " + e.getMessage(), false);
                    EventBus.publish(new FormStatusEvent(fs));
                    continue;
                }
                fs.setStatusString("preparing to retrieve instance data", true);
                EventBus.publish(new FormStatusEvent(fs));
                File formInstancesDir = FileSystemUtils.getFormInstancesDirectory(briefcaseLfd.getFormDirectory());
                // this will publish events
                successful = downloadAllSubmissionsForForm(formInstancesDir, formDatabase, briefcaseLfd, fs);
            } catch (SQLException | FileSystemException e) {
                allSuccessful = false;
                String msg = "unable to open form database";
                log.error(msg, e);
                fs.setStatusString(msg + ": " + e.getMessage(), false);
                EventBus.publish(new FormStatusEvent(fs));
                continue;
            } finally {
                if (formDatabase != null) {
                    try {
                        formDatabase.close();
                    } catch (SQLException e) {
                        allSuccessful = false;
                        String msg = "unable to close form database";
                        log.error(msg, e);
                        fs.setStatusString(msg + ": " + e.getMessage(), false);
                        EventBus.publish(new FormStatusEvent(fs));
                        continue;
                    }
                }
            }
            allSuccessful = allSuccessful && successful;
            // on success, we haven't actually set a success event (because we don't know we're done)
            if (successful) {
                fs.setStatusString(SUCCESS_STATUS, true);
                EventBus.publish(new FormStatusEvent(fs));
            } else {
                fs.setStatusString(FAILED_STATUS, true);
                EventBus.publish(new FormStatusEvent(fs));
            }
        } catch (SocketTimeoutException se) {
            allSuccessful = false;
            log.error("error accessing URL", se);
            fs.setStatusString("Communications to the server timed out. Detailed message: " + se.getLocalizedMessage() + " while accessing: " + fd.getDownloadUrl() + " A network login screen may be interfering with the transmission to the server.", false);
            EventBus.publish(new FormStatusEvent(fs));
        } catch (IOException e) {
            allSuccessful = false;
            log.error("error accessing form download URL", e);
            fs.setStatusString("Unexpected error: " + e.getLocalizedMessage() + " while accessing: " + fd.getDownloadUrl() + " A network login screen may be interfering with the transmission to the server.", false);
            EventBus.publish(new FormStatusEvent(fs));
        } catch (FileSystemException | TransmissionException | URISyntaxException e) {
            allSuccessful = false;
            log.error("error accessing form download URL", e);
            fs.setStatusString("Unexpected error: " + e.getLocalizedMessage() + " while accessing: " + fd.getDownloadUrl(), false);
            EventBus.publish(new FormStatusEvent(fs));
        }
    }
    return allSuccessful;
}
Also used : SQLException(java.sql.SQLException) FormStatusEvent(org.opendatakit.briefcase.model.FormStatusEvent) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) FileSystemException(org.opendatakit.briefcase.model.FileSystemException) SocketTimeoutException(java.net.SocketTimeoutException) RemoteFormDefinition(org.opendatakit.briefcase.model.RemoteFormDefinition) TransmissionException(org.opendatakit.briefcase.model.TransmissionException) FormStatus(org.opendatakit.briefcase.model.FormStatus) BriefcaseFormDefinition(org.opendatakit.briefcase.model.BriefcaseFormDefinition) File(java.io.File)

Aggregations

FormStatusEvent (org.opendatakit.briefcase.model.FormStatusEvent)12 File (java.io.File)10 IOException (java.io.IOException)5 SQLException (java.sql.SQLException)5 DocumentDescription (org.opendatakit.briefcase.model.DocumentDescription)5 FileSystemException (org.opendatakit.briefcase.model.FileSystemException)5 ParsingException (org.opendatakit.briefcase.model.ParsingException)5 TransmissionException (org.opendatakit.briefcase.model.TransmissionException)5 BriefcaseFormDefinition (org.opendatakit.briefcase.model.BriefcaseFormDefinition)4 XmlDocumentFetchException (org.opendatakit.briefcase.model.XmlDocumentFetchException)4 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3 FormStatus (org.opendatakit.briefcase.model.FormStatus)3 RemoteFormDefinition (org.opendatakit.briefcase.model.RemoteFormDefinition)3 SocketTimeoutException (java.net.SocketTimeoutException)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 OdkCollectFormDefinition (org.opendatakit.briefcase.model.OdkCollectFormDefinition)2 FileOutputStream (java.io.FileOutputStream)1