From 23e593eb69f6d0beb197f33ae31b479c60d9316f Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Fri, 19 Dec 2014 00:29:55 +0100 Subject: added convenience function for document construction and tested them. --- src/core/model/Document.cpp | 104 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) (limited to 'src/core/model/Document.cpp') diff --git a/src/core/model/Document.cpp b/src/core/model/Document.cpp index 31b22e3..709981b 100644 --- a/src/core/model/Document.cpp +++ b/src/core/model/Document.cpp @@ -58,7 +58,7 @@ int DocumentEntity::getFieldDescriptorIndex(const std::string &fieldName) } void DocumentEntity::getField(ManagedVector &res, - const std::string &fieldName) + const std::string &fieldName) { int f = getFieldDescriptorIndex(fieldName); if (f < 0) { @@ -85,6 +85,108 @@ ManagedVector &DocumentEntity::getField( "The given FieldDescriptor is not specified in the Descriptor of this " "node."); } + +static Rooted resolveDescriptor( + std::vector> domains, const std::string &className) +{ + // iterate over all domains. + for (auto &d : domains) { + // use the actual resolve method. + std::vector> resolved = d->resolve(className); + // if we don't find anything, continue. + if (resolved.size() == 0) { + continue; + } + // Otherwise take the first valid result. + for (auto &r : resolved) { + Managed *m = &(*r); + StructuredClass *c = dynamic_cast(m); + if (c != nullptr) { + return Rooted(c); + } + } + } + return {nullptr}; +} + +Rooted StructuredEntity::buildRootEntity( + Handle document, std::vector> domains, + const std::string &className, Variant attributes, std::string name) +{ + // If the parent is not set, we can not build the entity. + if (document == nullptr) { + return {nullptr}; + } + // If we can not find the correct descriptor, we can not build the entity + // either. + Rooted descriptor = resolveDescriptor(domains, className); + if (descriptor == nullptr) { + return {nullptr}; + } + // Then construct the StructuredEntity itself. + Rooted root{ + new StructuredEntity(document->getManager(), document, descriptor, + attributes, std::move(name))}; + // append it to the document. + document->setRoot(root); + // and return it. + return root; +} + +Rooted StructuredEntity::buildEntity( + Handle parent, std::vector> domains, + const std::string &className, const std::string &fieldName, + Variant attributes, std::string name) +{ + // If the parent is not set, we can not build the entity. + if (parent == nullptr) { + return {nullptr}; + } + // If we can not find the correct descriptor, we can not build the entity + // either. + Rooted descriptor = resolveDescriptor(domains, className); + if (descriptor == nullptr) { + return {nullptr}; + } + // Then construct the StructuredEntity itself. + Rooted entity{new StructuredEntity( + parent->getManager(), parent, descriptor, attributes, std::move(name))}; + // if the field does not exist, return null handle as well. + if (!parent->hasField(fieldName)) { + return {nullptr}; + } + // append the new entity to the right field. + ManagedVector field{parent}; + parent->getField(field, fieldName); + field.push_back(entity); + + // and return it. + return entity; +} + +Rooted DocumentPrimitive::buildEntity( + Handle parent, Variant content, + const std::string &fieldName) +{ + // If the parent is not set, we can not build the entity. + if (parent == nullptr) { + return {nullptr}; + } + // Then construct the StructuredEntity itself. + Rooted entity{ + new DocumentPrimitive(parent->getManager(), parent, content)}; + // if the field does not exist, return null handle as well. + if (!parent->hasField(fieldName)) { + return {nullptr}; + } + // append the new entity to the right field. + ManagedVector field{parent}; + parent->getField(field, fieldName); + field.push_back(entity); + + // and return it. + return entity; +} } } -- cgit v1.2.3