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<>());
}
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));
}));
}
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);
}
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);
}
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());
}
Aggregations