diff options
Diffstat (limited to 'src/core/model/Document.cpp')
-rw-r--r-- | src/core/model/Document.cpp | 114 |
1 files changed, 103 insertions, 11 deletions
diff --git a/src/core/model/Document.cpp b/src/core/model/Document.cpp index 7552bd3..2ae9107 100644 --- a/src/core/model/Document.cpp +++ b/src/core/model/Document.cpp @@ -147,10 +147,14 @@ bool DocumentEntity::doValidate(Logger &logger) const // a constructor we can not check anything else. return false; } - // TODO: check the validated form of Attributes - // TODO: Check if descriptor is registered at the Document? - - bool valid = true; + // check the attribute primitive content. + bool valid; + if (descriptor->getAttributesDescriptor() == nullptr) { + valid = getAttributes() == nullptr; + } else { + valid = descriptor->getAttributesDescriptor()->isValid(getAttributes(), + logger); + } /* * generate the set of effective fields. This is trivial for * AnnotationEntities, but in the case of StructuredEntities we have to @@ -188,7 +192,18 @@ bool DocumentEntity::doValidate(Logger &logger) const continue; } // if we are here we know that exactly one child exists. - // TODO: Check the primitive type of the child. + if (!fields[f][0]->isa(RttiTypes::DocumentPrimitive)) { + logger.error(std::string("Primitive Field \"") + + fieldDescs[f]->getName() + + "\" has non primitive content!"); + valid = false; + } else { + Handle<DocumentPrimitive> primitive = + fields[f][0].cast<DocumentPrimitive>(); + valid = valid & + fieldDescs[f]->getPrimitiveType()->isValid( + primitive->getContent(), logger); + } continue; } @@ -320,13 +335,39 @@ void DocumentEntity::setAttributes(const Variant &a) void DocumentEntity::addStructureNode(Handle<StructureNode> s, const int &i) { - invalidateSubInstance(); - fields[i].push_back(s); - if (s->getParent() != subInst) { + // only add the new node if we don't have it already. + auto it = fields[i].find(s); + if (it == fields[i].end()) { + invalidateSubInstance(); + fields[i].push_back(s); + } + Handle<Managed> par = s->getParent(); + if (par != subInst) { + // if a previous parent existed, remove the StructureNode from it + if (par != nullptr) { + if (par->isa(RttiTypes::StructuredEntity)) { + par.cast<StructuredEntity>()->removeStructureNode(s); + } else { + par.cast<AnnotationEntity>()->removeStructureNode(s); + } + } s->setParent(subInst); } } +bool DocumentEntity::removeStructureNodeFromField(Handle<StructureNode> s, + const int &i) +{ + auto it = fields[i].find(s); + if (it != fields[i].end()) { + invalidateSubInstance(); + fields[i].erase(it); + s->setParent(nullptr); + return true; + } + return false; +} + void DocumentEntity::addStructureNode(Handle<StructureNode> s, const std::string &fieldName) { @@ -342,6 +383,13 @@ void DocumentEntity::addStructureNodes( } } +bool DocumentEntity::removeStructureNodeFromField( + Handle<StructureNode> s, const std::string &fieldName) +{ + return removeStructureNodeFromField( + s, getFieldDescriptorIndex(fieldName, true)); +} + void DocumentEntity::addStructureNode(Handle<StructureNode> s, Handle<FieldDescriptor> fieldDescriptor) { @@ -358,6 +406,28 @@ void DocumentEntity::addStructureNodes( } } +bool DocumentEntity::removeStructureNodeFromField( + Handle<StructureNode> s, Handle<FieldDescriptor> fieldDescriptor) +{ + return removeStructureNodeFromField( + s, getFieldDescriptorIndex(fieldDescriptor, true)); +} + +bool DocumentEntity::removeStructureNode(Handle<StructureNode> s) +{ + for (auto field : fields) { + auto it = field.find(s); + if (it != field.end()) { + invalidateSubInstance(); + field.erase(it); + s->setParent(nullptr); + return true; + } + } + + return false; +} + /* Class StructureNode */ bool StructureNode::doValidate(Logger &logger) const @@ -543,9 +613,17 @@ bool Document::doValidate(Logger &logger) const void Document::addAnnotation(Handle<AnnotationEntity> a) { - invalidate(); - annotations.push_back(a); - if (a->getParent() != this) { + // only add it if we need to. + if (annotations.find(a) == annotations.end()) { + invalidate(); + annotations.push_back(a); + } + Handle<Managed> par = a->getParent(); + if (par != this) { + if (par != nullptr) { + // remove the StructuredClass from the old parent. + par.cast<Document>()->removeAnnotation(a); + } a->setParent(this); } } @@ -557,6 +635,20 @@ void Document::addAnnotations(const std::vector<Handle<AnnotationEntity>> &as) } } + + +bool Document::removeAnnotation(Handle<AnnotationEntity> a) +{ + auto it = annotations.find(a); + if (it != annotations.end()) { + invalidate(); + annotations.erase(it); + a->setParent(nullptr); + return true; + } + return false; +} + bool Document::hasChild(Handle<StructureNode> s) const { Rooted<Managed> parent = s->getParent(); |