Search in sources :

Example 1 with XmlElement

use of org.opendatakit.briefcase.export.XmlElement in project briefcase by opendatakit.

the class FormMetadata method from.

public static FormMetadata from(Path storageRoot, Path formFile) {
    XmlElement root = XmlElement.from(formFile);
    assert root.getName().equals("html");
    String name = root.findElements("head", "title").get(0).maybeValue().orElseThrow(BriefcaseException::new);
    XmlElement mainInstance = root.findElements("head", "model", "instance").stream().filter(FormMetadata::isTheMainInstance).findFirst().orElseThrow(BriefcaseException::new);
    String id = mainInstance.childrenOf().get(0).getAttributeValue("id").orElseThrow(BriefcaseException::new);
    FormKey key = FormKey.of(name, id);
    return new FormMetadata(key, storageRoot, formFile.getParent(), true, Cursor.empty(), Optional.empty(), new HashSet<>());
}
Also used : BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) XmlElement(org.opendatakit.briefcase.export.XmlElement)

Example 2 with XmlElement

use of org.opendatakit.briefcase.export.XmlElement in project briefcase by opendatakit.

the class PullFromCentral method pull.

/**
 * Pulls a form completely, writing the form file, form attachments,
 * submission files and their attachments to the local filesystem
 * under the Briefcase Storage directory.
 */
public Job<Void> pull(FormStatus form) {
    FormKey key = FormKey.from(form);
    PullFromCentralTracker tracker = new PullFromCentralTracker(form, onEventCallback);
    return run(rs -> tracker.trackStart()).thenRun(allOf(supply(runnerStatus -> getSubmissionIds(form, token, runnerStatus, tracker)), run(runnerStatus -> {
        downloadForm(form, token, runnerStatus, tracker);
        List<CentralAttachment> attachments = getFormAttachments(form, token, runnerStatus, tracker);
        int totalAttachments = attachments.size();
        AtomicInteger attachmentNumber = new AtomicInteger(1);
        attachments.parallelStream().forEach(attachment -> downloadFormAttachment(form, attachment, token, runnerStatus, tracker, attachmentNumber.getAndIncrement(), totalAttachments));
    }))).thenAccept((runnerStatus, pair) -> withDb(form.getFormDir(briefcaseDir), db -> {
        List<String> submissions = pair.getLeft();
        int totalSubmissions = submissions.size();
        AtomicInteger submissionNumber = new AtomicInteger(1);
        Set<String> submissionVersions = new HashSet<>();
        if (submissions.isEmpty())
            tracker.trackNoSubmissions();
        submissions.stream().map(instanceId -> Pair.of(submissionNumber.getAndIncrement(), instanceId)).forEach(submissionNumberId -> {
            int currentSubmissionNumber = submissionNumberId.getLeft();
            String instanceId = submissionNumberId.getRight();
            boolean inDb = db.hasRecordedInstance(instanceId) != null;
            Path downloadedSubmissionPath = form.getSubmissionFile(briefcaseDir, instanceId);
            if (!inDb || !downloadedSubmissionPath.toFile().exists()) {
                downloadSubmission(form, instanceId, token, runnerStatus, tracker, currentSubmissionNumber, totalSubmissions);
                if (downloadedSubmissionPath.toFile().exists()) {
                    XmlElement root = XmlElement.from(new String(readAllBytes(downloadedSubmissionPath)));
                    SubmissionMetaData metaData = new SubmissionMetaData(root);
                    metaData.getVersion().ifPresent(submissionVersions::add);
                }
            } else {
                tracker.trackSubmissionAlreadyDownloaded(currentSubmissionNumber, totalSubmissions);
            }
            List<CentralAttachment> attachments = getSubmissionAttachmentList(form, instanceId, token, runnerStatus, tracker, currentSubmissionNumber, totalSubmissions);
            int totalAttachments = attachments.size();
            AtomicInteger attachmentNumber = new AtomicInteger(1);
            attachments.stream().filter(attachment -> !inDb || !form.getSubmissionMediaFile(briefcaseDir, instanceId, attachment.getName()).toFile().exists()).forEach(attachment -> downloadSubmissionAttachment(form, instanceId, attachment, token, runnerStatus, tracker, currentSubmissionNumber, totalSubmissions, attachmentNumber.getAndIncrement(), totalAttachments));
            if (!runnerStatus.isCancelled() && !inDb) {
                db.putRecordedInstanceDirectory(instanceId, form.getSubmissionDir(briefcaseDir, instanceId).toFile());
            }
        });
        tracker.trackEnd();
        formMetadataPort.execute(updateAsPulled(key, briefcaseDir, form.getFormDir(briefcaseDir), submissionVersions));
        EventBus.publish(PullEvent.Success.of(form, server));
    }));
}
Also used : Job.run(org.opendatakit.briefcase.reused.job.Job.run) CentralAttachment(org.opendatakit.briefcase.reused.transfer.CentralAttachment) Response(org.opendatakit.briefcase.reused.http.response.Response) Job.supply(org.opendatakit.briefcase.reused.job.Job.supply) SubmissionMetaData(org.opendatakit.briefcase.export.SubmissionMetaData) HashSet(java.util.HashSet) FormMetadataPort(org.opendatakit.briefcase.model.form.FormMetadataPort) PullEvent(org.opendatakit.briefcase.pull.PullEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UncheckedFiles.readAllBytes(org.opendatakit.briefcase.reused.UncheckedFiles.readAllBytes) EventBus(org.bushe.swing.event.EventBus) Pair(org.opendatakit.briefcase.reused.Pair) DatabaseUtils.withDb(org.opendatakit.briefcase.util.DatabaseUtils.withDb) Path(java.nio.file.Path) FormKey(org.opendatakit.briefcase.model.form.FormKey) FormStatus(org.opendatakit.briefcase.model.FormStatus) FormStatusEvent(org.opendatakit.briefcase.model.FormStatusEvent) CentralServer(org.opendatakit.briefcase.reused.transfer.CentralServer) Job.allOf(org.opendatakit.briefcase.reused.job.Job.allOf) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) RunnerStatus(org.opendatakit.briefcase.reused.job.RunnerStatus) FormMetadataCommands.updateAsPulled(org.opendatakit.briefcase.model.form.FormMetadataCommands.updateAsPulled) Http(org.opendatakit.briefcase.reused.http.Http) UncheckedFiles.createDirectories(org.opendatakit.briefcase.reused.UncheckedFiles.createDirectories) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Job(org.opendatakit.briefcase.reused.job.Job) XmlElement(org.opendatakit.briefcase.export.XmlElement) Path(java.nio.file.Path) SubmissionMetaData(org.opendatakit.briefcase.export.SubmissionMetaData) HashSet(java.util.HashSet) Set(java.util.Set) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) XmlElement(org.opendatakit.briefcase.export.XmlElement) Collections.emptyList(java.util.Collections.emptyList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) FormKey(org.opendatakit.briefcase.model.form.FormKey)

Example 3 with XmlElement

use of org.opendatakit.briefcase.export.XmlElement in project briefcase by opendatakit.

the class AggregateCursor method from.

/**
 * Parses the provided cursor xml document and returns a new Cursor instance.
 */
public static Cursor from(String cursorXml) {
    if (cursorXml.isEmpty() || // Ona compatibility
    cursorXml.equals("0"))
        return AggregateCursor.empty();
    XmlElement root = XmlElement.from(cursorXml);
    Optional<OffsetDateTime> lastUpdate = root.findElement("attributeValue").flatMap(XmlElement::maybeValue).map(Iso8601Helpers::parseDateTime);
    Optional<String> lastReturnedValue = root.findElement("uriLastReturnedValue").flatMap(XmlElement::maybeValue);
    return new AggregateCursor(cursorXml, lastUpdate, lastReturnedValue);
}
Also used : OffsetDateTime(java.time.OffsetDateTime) Iso8601Helpers(org.opendatakit.briefcase.reused.Iso8601Helpers) XmlElement(org.opendatakit.briefcase.export.XmlElement)

Example 4 with XmlElement

use of org.opendatakit.briefcase.export.XmlElement in project briefcase by opendatakit.

the class FormKey method getFormNameAndId.

private static Pair<String, String> getFormNameAndId(FormStatus formStatus) {
    if (!(formStatus.getFormDefinition() instanceof BriefcaseFormDefinition))
        return Pair.of(formStatus.getFormName(), formStatus.getFormId());
    // We can't trust the form's title because JavaRosaParserWrapper strips illegal chars
    // from it for some reason and then we can't use it to match any stored form metadata
    BriefcaseFormDefinition formDef = (BriefcaseFormDefinition) formStatus.getFormDefinition();
    XmlElement root = XmlElement.from(formDef.getFormDefn().xml);
    String formName = root.findElement("head").flatMap(e -> e.findElement("title")).flatMap(XmlElement::maybeValue).orElseGet(formStatus::getFormName);
    XmlElement instance = root.findElements("head", "model", "instance").stream().filter(e -> !e.hasAttribute("id")).findFirst().orElseThrow(BriefcaseException::new);
    String formId = instance.childrenOf().get(0).getAttributeValue("id").orElseThrow(BriefcaseException::new);
    return Pair.of(formName, formId);
}
Also used : BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) XmlElement(org.opendatakit.briefcase.export.XmlElement) BriefcaseFormDefinition(org.opendatakit.briefcase.model.BriefcaseFormDefinition)

Example 5 with XmlElement

use of org.opendatakit.briefcase.export.XmlElement in project briefcase by opendatakit.

the class FileSystemFormMetadataAdapterTest method buildMetadataFrom.

private static FormMetadata buildMetadataFrom(Path storageRoot, Path formFile, Cursor cursor) {
    XmlElement root = readXml(newInputStream(formFile));
    XmlElement mainInstance = root.findElements("head", "model", "instance").stream().filter(e -> // It's not a secondary instance
    !e.hasAttribute("id") && // Just one child (sanity check: if there's a different number of children, we don't really know what's this element)
    e.childrenOf().size() == 1 && // The child element has an id (sanity check: we can't build form metadata without the form's id)
    e.childrenOf().get(0).hasAttribute("id")).findFirst().orElseThrow(RuntimeException::new).childrenOf().get(0);
    FormKey key = FormKey.of(root.findElements("head", "title").get(0).getValue(), mainInstance.getAttributeValue("id").orElseThrow(RuntimeException::new));
    return new FormMetadata(key, storageRoot, formFile.getParent(), cursor.isEmpty(), cursor, Optional.empty(), Collections.emptySet());
}
Also used : XmlElement(org.opendatakit.briefcase.export.XmlElement)

Aggregations

XmlElement (org.opendatakit.briefcase.export.XmlElement)7 Path (java.nio.file.Path)2 Collections.emptyList (java.util.Collections.emptyList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Consumer (java.util.function.Consumer)2 Collectors.toList (java.util.stream.Collectors.toList)2 EventBus (org.bushe.swing.event.EventBus)2 SubmissionMetaData (org.opendatakit.briefcase.export.SubmissionMetaData)2 FormStatus (org.opendatakit.briefcase.model.FormStatus)2 FormStatusEvent (org.opendatakit.briefcase.model.FormStatusEvent)2 FormKey (org.opendatakit.briefcase.model.form.FormKey)2 FormMetadataPort (org.opendatakit.briefcase.model.form.FormMetadataPort)2 BriefcaseException (org.opendatakit.briefcase.reused.BriefcaseException)2 UncheckedFiles.readAllBytes (org.opendatakit.briefcase.reused.UncheckedFiles.readAllBytes)2 Http (org.opendatakit.briefcase.reused.http.Http)2 Response (org.opendatakit.briefcase.reused.http.response.Response)2 Job (org.opendatakit.briefcase.reused.job.Job)2