setChild method

  1. @override
bool setChild(
  1. String name,
  2. Object entities
)
override

Updates a child relationship name with entities. If child.update is false, we throw an UpdateException. If policies fail, we revert.

Implementation

@override
bool setChild(String name, Object entities) {
  if (_concept == null) {
    throw ConceptException('Entity concept is not defined.');
  }

  Child? child = _concept?.children.singleWhereCode(name) as Child?;
  if (child == null) {
    String msg =
        '${_concept?.code}.$name is not correct child entities name.';
    throw UpdateException(msg);
  }

  if (child.update) {
    _childMap.update(name, (value) => entities);
    if (child.internal) {
      _internalChildMap[name] = entities;
    }

    // Evaluate policies after child change.
    var policyResult = evaluatePolicies();
    if (!policyResult.success) {
      _childMap.remove(name);
      if (_internalChildMap.containsKey(name)) {
        _internalChildMap.remove(name);
      }
      throw PolicyViolationException(policyResult.violations);
    }

    // Then model policies.
    var modelPolicyResult = concept.model.evaluateModelPolicies(this);
    if (!modelPolicyResult) {
      _childMap.remove(name);
      if (_internalChildMap.containsKey(name)) {
        _internalChildMap.remove(name);
      }
      throw PolicyViolationException(modelPolicyResult.violations);
    }

    return true;
  } else {
    return false;
    // or throw new UpdateException
  }
}