Search in sources :

Example 6 with DocumentDescription

use of org.opendatakit.briefcase.model.DocumentDescription 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)

Aggregations

DocumentDescription (org.opendatakit.briefcase.model.DocumentDescription)6 FormStatusEvent (org.opendatakit.briefcase.model.FormStatusEvent)5 File (java.io.File)4 ArrayList (java.util.ArrayList)3 ParsingException (org.opendatakit.briefcase.model.ParsingException)3 XmlDocumentFetchException (org.opendatakit.briefcase.model.XmlDocumentFetchException)3 HashMap (java.util.HashMap)2 TransmissionException (org.opendatakit.briefcase.model.TransmissionException)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 SQLException (java.sql.SQLException)1 ExecutionException (java.util.concurrent.ExecutionException)1 FileSystemException (org.opendatakit.briefcase.model.FileSystemException)1 RemoteFormDefinition (org.opendatakit.briefcase.model.RemoteFormDefinition)1 DocumentFetchResult (org.opendatakit.briefcase.util.AggregateUtils.DocumentFetchResult)1 SubmissionChunk (org.opendatakit.briefcase.util.ServerFetcher.SubmissionChunk)1