setParent method
Sets or updates a parent relationship named name with the given entity.
If parent is uninitialized or updatable, we store a reference.
If policies fail, we revert.
Implementation
@override
bool setParent(String name, entity) {
if (_concept == null) {
throw ConceptException('Entity concept is not defined.');
}
Parent? parent = _concept?.parents.singleWhereCode(name) as Parent?;
if (parent == null) {
String msg = '${_concept?.code}.$name is not correct parent entity name.';
throw UpdateException(msg);
}
if (entity != null && getParent(name) == null) {
var reference = Reference(entity.oid.toString(), entity.concept.code,
entity.concept.entryConcept.code);
_parentMap[name] = entity;
_referenceMap[name] = reference;
var policyResult = evaluatePolicies();
if (!policyResult.success) {
_parentMap.remove(name);
_referenceMap.remove(name);
throw PolicyViolationException(policyResult.violations);
}
return true;
} else if (entity != null && parent.update) {
var reference = Reference(entity.oid.toString(), entity.concept.code,
entity.concept.entryConcept.code);
_parentMap[name] = entity;
_referenceMap[name] = reference;
var policyResult = evaluatePolicies();
if (!policyResult.success) {
_parentMap.remove(name);
_referenceMap.remove(name);
throw PolicyViolationException(policyResult.violations);
}
var modelPolicyResult = concept.model.evaluateModelPolicies(this);
if (!modelPolicyResult.success) {
_parentMap.remove(name);
_referenceMap.remove(name);
throw PolicyViolationException(modelPolicyResult.violations);
}
return true;
} else {
String msg = '${_concept?.code}.${parent.code} is not updatable.';
throw UpdateException(msg);
}
}