Search in sources :

Example 1 with DocumentDescription

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

the class ServerFetcher method fetchFormList.

public static final AggregateUtils.DocumentFetchResult fetchFormList(ServerConnectionInfo serverInfo, boolean alwaysResetCredentials, TerminationFuture terminationFuture) throws XmlDocumentFetchException {
    String urlString = serverInfo.getUrl();
    if (urlString.endsWith("/")) {
        urlString = urlString + "formList";
    } else {
        urlString = urlString + "/formList";
    }
    DocumentDescription formListDescription = new DocumentDescription("Unable to fetch formList: ", "Unable to fetch formList.", "form list", terminationFuture);
    AggregateUtils.DocumentFetchResult result = AggregateUtils.getXmlDocument(urlString, serverInfo, alwaysResetCredentials, formListDescription, null);
    return result;
}
Also used : DocumentDescription(org.opendatakit.briefcase.model.DocumentDescription)

Example 2 with DocumentDescription

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

the class ServerFetcher method downloadManifestAndMediaFiles.

private String downloadManifestAndMediaFiles(File mediaDir, FormStatus fs) {
    RemoteFormDefinition fd = getRemoteFormDefinition(fs);
    if (fd.getManifestUrl() == null)
        return null;
    fs.setStatusString("Fetching form manifest", true);
    EventBus.publish(new FormStatusEvent(fs));
    List<MediaFile> files = new ArrayList<>();
    AggregateUtils.DocumentFetchResult result;
    try {
        DocumentDescription formManifestDescription = new DocumentDescription("Fetch of manifest failed. Detailed reason: ", "Fetch of manifest failed ", "form manifest", terminationFuture);
        result = AggregateUtils.getXmlDocument(fd.getManifestUrl(), serverInfo, false, formManifestDescription, null);
    } catch (XmlDocumentFetchException e) {
        return e.getMessage();
    }
    try {
        files = XmlManipulationUtils.parseFormManifestResponse(result.isOpenRosaResponse, result.doc);
    } catch (ParsingException e) {
        return e.getMessage();
    }
    // OK we now have the full set of files to download...
    log.info("Downloading " + files.size() + " media files.");
    int mCount = 0;
    if (files.size() > 0) {
        for (MediaFile m : files) {
            ++mCount;
            fs.setStatusString(String.format(" (getting %1$d of %2$d media files)", mCount, files.size()), true);
            EventBus.publish(new FormStatusEvent(fs));
            try {
                downloadMediaFileIfChanged(mediaDir, m, fs);
            } catch (Exception e) {
                return e.getLocalizedMessage();
            }
        }
    }
    return null;
}
Also used : XmlDocumentFetchException(org.opendatakit.briefcase.model.XmlDocumentFetchException) DocumentDescription(org.opendatakit.briefcase.model.DocumentDescription) RemoteFormDefinition(org.opendatakit.briefcase.model.RemoteFormDefinition) FormStatusEvent(org.opendatakit.briefcase.model.FormStatusEvent) ParsingException(org.opendatakit.briefcase.model.ParsingException) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) FileSystemException(org.opendatakit.briefcase.model.FileSystemException) SQLException(java.sql.SQLException) SocketTimeoutException(java.net.SocketTimeoutException) TransmissionException(org.opendatakit.briefcase.model.TransmissionException) XmlDocumentFetchException(org.opendatakit.briefcase.model.XmlDocumentFetchException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ParsingException(org.opendatakit.briefcase.model.ParsingException)

Example 3 with DocumentDescription

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

the class ServerUploader method subtractServerInstances.

// remove any instances already completed on server
private void subtractServerInstances(FormStatus fs, DatabaseUtils formDatabase, Set<File> instancesToUpload) {
    /*
     * The /view/submissionList interface returns the list of COMPLETED submissions
     * on the server. Fetch this list and filter out the locally-held submissions
     * with the same instanceIds.  We know the server is already content with what
     * it has, so we don't need to send any of these to the server, as that POST
     * request will be treated as a no-op.
     */
    String baseUrl = serverInfo.getUrl() + "/view/submissionList";
    String oldWebsafeCursorString = "not-empty";
    String websafeCursorString = "";
    for (; !oldWebsafeCursorString.equals(websafeCursorString); ) {
        if (isCancelled()) {
            fs.setStatusString("aborting retrieval of instanceIds of submissions on server...", true);
            EventBus.publish(new FormStatusEvent(fs));
            return;
        }
        fs.setStatusString("retrieving next chunk of instanceIds from server...", true);
        EventBus.publish(new FormStatusEvent(fs));
        Map<String, String> params = new HashMap<>();
        params.put("numEntries", Integer.toString(MAX_ENTRIES));
        params.put("formId", fs.getFormDefinition().getFormId());
        params.put("cursor", websafeCursorString);
        String fullUrl = WebUtils.createLinkWithProperties(baseUrl, params);
        // remember what we had...
        oldWebsafeCursorString = websafeCursorString;
        AggregateUtils.DocumentFetchResult result;
        try {
            DocumentDescription submissionChunkDescription = new DocumentDescription("Fetch of instanceIds (submission download chunk) failed.  Detailed error: ", "Fetch of instanceIds (submission download chunk) failed.", "submission download chunk", terminationFuture);
            result = AggregateUtils.getXmlDocument(fullUrl, serverInfo, false, submissionChunkDescription, null);
        } catch (XmlDocumentFetchException e) {
            fs.setStatusString("Not all submissions retrieved: Error fetching list of instanceIds: " + e.getMessage(), false);
            EventBus.publish(new FormStatusEvent(fs));
            return;
        }
        SubmissionChunk chunk;
        try {
            chunk = XmlManipulationUtils.parseSubmissionDownloadListResponse(result.doc);
        } catch (ParsingException e) {
            fs.setStatusString("Not all instanceIds retrieved: Error parsing the submission download chunk: " + e.getMessage(), false);
            EventBus.publish(new FormStatusEvent(fs));
            return;
        }
        websafeCursorString = chunk.websafeCursorString;
        for (String uri : chunk.uriList) {
            File f = formDatabase.hasRecordedInstance(uri);
            if (f != null) {
                instancesToUpload.remove(f);
            }
        }
    }
}
Also used : XmlDocumentFetchException(org.opendatakit.briefcase.model.XmlDocumentFetchException) DocumentDescription(org.opendatakit.briefcase.model.DocumentDescription) HashMap(java.util.HashMap) FormStatusEvent(org.opendatakit.briefcase.model.FormStatusEvent) ParsingException(org.opendatakit.briefcase.model.ParsingException) DocumentFetchResult(org.opendatakit.briefcase.util.AggregateUtils.DocumentFetchResult) SubmissionChunk(org.opendatakit.briefcase.util.ServerFetcher.SubmissionChunk) File(java.io.File)

Example 4 with DocumentDescription

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

the class ServerUploader method uploadForm.

public boolean uploadForm(FormStatus formToTransfer, File briefcaseFormDefFile, File briefcaseFormMediaDir) {
    // very similar to upload submissions...
    URI u;
    try {
        u = AggregateUtils.testServerConnectionWithHeadRequest(serverInfo, "formUpload");
    } catch (TransmissionException e) {
        formToTransfer.setStatusString(e.getMessage(), false);
        EventBus.publish(new FormStatusEvent(formToTransfer));
        return false;
    }
    // try to send form...
    if (!briefcaseFormDefFile.exists()) {
        String msg = "Form definition file not found: " + briefcaseFormDefFile.getAbsolutePath();
        formToTransfer.setStatusString(msg, false);
        EventBus.publish(new FormStatusEvent(formToTransfer));
        return false;
    }
    // find all files in parent directory
    File[] allFiles = null;
    if (briefcaseFormMediaDir != null) {
        allFiles = briefcaseFormMediaDir.listFiles();
    }
    // clean up the list, removing anything that is suspicious
    // or that we won't attempt to upload. For OpenRosa servers,
    // we'll upload just about everything...
    List<File> files = new ArrayList<>();
    if (allFiles != null) {
        for (File f : allFiles) {
            String fileName = f.getName();
            if (fileName.startsWith(".")) {
                // potential Apple file attributes file -- ignore it
                continue;
            }
            files.add(f);
        }
    }
    DocumentDescription formDefinitionUploadDescription = new DocumentDescription("Form definition upload failed.  Detailed error: ", "Form definition upload failed.", "form definition", terminationFuture);
    return AggregateUtils.uploadFilesToServer(serverInfo, u, "form_def_file", briefcaseFormDefFile, files, formDefinitionUploadDescription, null, terminationFuture, formToTransfer);
}
Also used : DocumentDescription(org.opendatakit.briefcase.model.DocumentDescription) TransmissionException(org.opendatakit.briefcase.model.TransmissionException) FormStatusEvent(org.opendatakit.briefcase.model.FormStatusEvent) ArrayList(java.util.ArrayList) URI(java.net.URI) File(java.io.File)

Example 5 with DocumentDescription

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

the class ServerUploader method uploadSubmission.

private final boolean uploadSubmission(DatabaseUtils formDatabase, FormStatus formToTransfer, URI u, int count, int totalCount, File instanceDirectory) {
    // We have the actual server URL in u, possibly redirected to https.
    // We know we are talking to the server because the head request
    // succeeded and had a Location header field.
    // try to send instance
    // get instance file
    File file = new File(instanceDirectory, "submission.xml");
    String submissionFile = file.getName();
    if (!file.exists()) {
        String msg = "Submission file not found: " + file.getAbsolutePath();
        formToTransfer.setStatusString(msg, false);
        EventBus.publish(new FormStatusEvent(formToTransfer));
        return false;
    }
    // find all files in parent directory
    File[] allFiles = instanceDirectory.listFiles();
    // clean up the list, removing anything that is suspicious
    // or that we won't attempt to upload. For OpenRosa servers,
    // we'll upload just about everything...
    List<File> files = new ArrayList<>();
    for (File f : allFiles) {
        String fileName = f.getName();
        if (fileName.startsWith(".")) {
            // potential Apple file attributes file -- ignore it
            continue;
        }
        if (fileName.equals(submissionFile)) {
            // this is always added
            continue;
        } else {
            files.add(f);
        }
    }
    SubmissionResponseAction action = new SubmissionResponseAction(file);
    if (isCancelled()) {
        formToTransfer.setStatusString("aborting upload of submission...", true);
        EventBus.publish(new FormStatusEvent(formToTransfer));
        return false;
    }
    DocumentDescription submissionUploadDescription = new DocumentDescription("Submission upload failed.  Detailed error: ", "Submission upload failed.", "submission (" + count + " of " + totalCount + ")", terminationFuture);
    boolean outcome = AggregateUtils.uploadFilesToServer(serverInfo, u, "xml_submission_file", file, files, submissionUploadDescription, action, terminationFuture, formToTransfer);
    // and try to rename the instance directory to be its instanceID
    action.afterUpload(formToTransfer);
    return outcome;
}
Also used : DocumentDescription(org.opendatakit.briefcase.model.DocumentDescription) FormStatusEvent(org.opendatakit.briefcase.model.FormStatusEvent) ArrayList(java.util.ArrayList) 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