Search in sources :

Example 1 with SubmissionChunk

use of org.opendatakit.briefcase.util.ServerFetcher.SubmissionChunk in project briefcase by opendatakit.

the class XmlManipulationUtils method parseSubmissionDownloadListResponse.

public static final SubmissionChunk parseSubmissionDownloadListResponse(Document doc) throws ParsingException {
    List<String> uriList = new ArrayList<>();
    String websafeCursorString = "";
    // Attempt parsing
    Element idChunkElement = doc.getRootElement();
    if (!idChunkElement.getName().equals("idChunk")) {
        String msg = "Parsing submissionList reply -- root element is not <idChunk> :" + idChunkElement.getName();
        log.error(msg);
        throw new ParsingException(msg);
    }
    String namespace = idChunkElement.getNamespace();
    if (!namespace.equalsIgnoreCase(NAMESPACE_OPENDATAKIT_ORG_SUBMISSIONS)) {
        String msg = "Parsing submissionList reply -- root element namespace is incorrect:" + namespace;
        log.error(msg);
        throw new ParsingException(msg);
    }
    int nElements = idChunkElement.getChildCount();
    for (int i = 0; i < nElements; ++i) {
        if (idChunkElement.getType(i) != Element.ELEMENT) {
            // e.g., whitespace (text)
            continue;
        }
        Element subElement = (Element) idChunkElement.getElement(i);
        namespace = subElement.getNamespace();
        if (!namespace.equalsIgnoreCase(NAMESPACE_OPENDATAKIT_ORG_SUBMISSIONS)) {
            // someone else's extension?
            continue;
        }
        String name = subElement.getName();
        if (name.equalsIgnoreCase("idList")) {
            // parse the idList
            int nIdElements = subElement.getChildCount();
            for (int j = 0; j < nIdElements; ++j) {
                if (subElement.getType(j) != Element.ELEMENT) {
                    // e.g., whitespace (text)
                    continue;
                }
                Element idElement = (Element) subElement.getElement(j);
                namespace = idElement.getNamespace();
                if (!namespace.equalsIgnoreCase(NAMESPACE_OPENDATAKIT_ORG_SUBMISSIONS)) {
                    // someone else's extension?
                    continue;
                }
                name = idElement.getName();
                if (name.equalsIgnoreCase("id")) {
                    // gather the uri
                    String uri = XFormParser.getXMLText(idElement, true);
                    if (uri != null) {
                        uriList.add(uri);
                    }
                } else {
                    log.warn("Unrecognized tag inside idList: " + name);
                }
            }
        } else if (name.equalsIgnoreCase("resumptionCursor")) {
            // gather the resumptionCursor
            websafeCursorString = XFormParser.getXMLText(subElement, true);
            if (websafeCursorString == null) {
                websafeCursorString = "";
            }
        } else {
            log.warn("Unrecognized tag inside idChunk: " + name);
        }
    }
    return new SubmissionChunk(uriList, websafeCursorString);
}
Also used : Element(org.kxml2.kdom.Element) ParsingException(org.opendatakit.briefcase.model.ParsingException) ArrayList(java.util.ArrayList) SubmissionChunk(org.opendatakit.briefcase.util.ServerFetcher.SubmissionChunk)

Example 2 with SubmissionChunk

use of org.opendatakit.briefcase.util.ServerFetcher.SubmissionChunk 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)

Aggregations

ParsingException (org.opendatakit.briefcase.model.ParsingException)2 SubmissionChunk (org.opendatakit.briefcase.util.ServerFetcher.SubmissionChunk)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Element (org.kxml2.kdom.Element)1 DocumentDescription (org.opendatakit.briefcase.model.DocumentDescription)1 FormStatusEvent (org.opendatakit.briefcase.model.FormStatusEvent)1 XmlDocumentFetchException (org.opendatakit.briefcase.model.XmlDocumentFetchException)1 DocumentFetchResult (org.opendatakit.briefcase.util.AggregateUtils.DocumentFetchResult)1