setAttribute method

  1. @override
bool setAttribute(
  1. String name,
  2. Object? value
)
override

Implementation

@override
bool setAttribute(String name, Object? value) {
  bool updated = false;
  if (preSetAttribute(name, value)) {
    if (_concept == null) {
      throw new ConceptException('Entity concept is not defined.');
    }

    var attribute = _concept!.attributes.singleWhereCode(name);
    if (attribute == null) {
      String msg = '${_concept!.code!}.$name is not correct attribute name.';
      throw UpdateException(msg);
    }
    /*
     * validation done in Entities.preAdd
    if (value == null && attribute.minc != '0') {
      String msg = '${_concept!.code!}.$name cannot be null.';
      throw new UpdateException(msg);
    }
    */
    Object? beforeValue = _attributeMap[name];
    if (getAttribute(name) == null) {
      _attributeMap[name] = value;
      updated = true;
      //} else if (!attribute.derive && attribute.update) {
    } else if (attribute.update) {
      _attributeMap[name] = value;
      updated = true;
      _whenSet = DateTime.now();
    } else {
      String msg = '${_concept!.code!}.${attribute.code!} is not updatable.';
      throw UpdateException(msg);
    }
    if (postSetAttribute(name, value)) {
      updated = true;
    } else {
      var beforePre = pre;
      var beforePost = post;
      pre = false;
      post = false;
      if (beforeValue == null || !setAttribute(name, beforeValue)) {
        var msg = '${_concept!.code!}.${attribute.code!} '
            'was set to a new value, post was not successful, '
            'set to the before value was not successful';
        throw RemoveException(msg);
      } else {
        _whenSet = null;
      }
      pre = beforePre;
      post = beforePost;
    }
  }
  return updated;
}