summaryrefslogtreecommitdiff
path: root/src/core/model/Document.cpp
diff options
context:
space:
mode:
authorBenjamin Paassen <bpaassen@techfak.uni-bielefeld.de>2014-12-19 00:29:55 +0100
committerBenjamin Paassen <bpaassen@techfak.uni-bielefeld.de>2014-12-19 00:29:55 +0100
commit23e593eb69f6d0beb197f33ae31b479c60d9316f (patch)
tree77046b5372b178ed08bdce398841b3eb0de11cea /src/core/model/Document.cpp
parent55d363bea503c0607c80604280c26c235a5d7ee1 (diff)
added convenience function for document construction and tested them.
Diffstat (limited to 'src/core/model/Document.cpp')
-rw-r--r--src/core/model/Document.cpp104
1 files changed, 103 insertions, 1 deletions
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<StructuredEntity> &res,
- const std::string &fieldName)
+ const std::string &fieldName)
{
int f = getFieldDescriptorIndex(fieldName);
if (f < 0) {
@@ -85,6 +85,108 @@ ManagedVector<StructuredEntity> &DocumentEntity::getField(
"The given FieldDescriptor is not specified in the Descriptor of this "
"node.");
}
+
+static Rooted<StructuredClass> resolveDescriptor(
+ std::vector<Handle<Domain>> domains, const std::string &className)
+{
+ // iterate over all domains.
+ for (auto &d : domains) {
+ // use the actual resolve method.
+ std::vector<Rooted<Managed>> 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<StructuredClass *>(m);
+ if (c != nullptr) {
+ return Rooted<StructuredClass>(c);
+ }
+ }
+ }
+ return {nullptr};
+}
+
+Rooted<StructuredEntity> StructuredEntity::buildRootEntity(
+ Handle<Document> document, std::vector<Handle<Domain>> 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<StructuredClass> descriptor = resolveDescriptor(domains, className);
+ if (descriptor == nullptr) {
+ return {nullptr};
+ }
+ // Then construct the StructuredEntity itself.
+ Rooted<StructuredEntity> 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> StructuredEntity::buildEntity(
+ Handle<DocumentEntity> parent, std::vector<Handle<Domain>> 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<StructuredClass> descriptor = resolveDescriptor(domains, className);
+ if (descriptor == nullptr) {
+ return {nullptr};
+ }
+ // Then construct the StructuredEntity itself.
+ Rooted<StructuredEntity> 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<StructuredEntity> field{parent};
+ parent->getField(field, fieldName);
+ field.push_back(entity);
+
+ // and return it.
+ return entity;
+}
+
+Rooted<DocumentPrimitive> DocumentPrimitive::buildEntity(
+ Handle<DocumentEntity> 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<DocumentPrimitive> 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<StructuredEntity> field{parent};
+ parent->getField(field, fieldName);
+ field.push_back(entity);
+
+ // and return it.
+ return entity;
+}
}
}