copy method

  1. @override
E copy()
override

Copies the entity (oid, code, attributes and neighbors). It is not a deep copy.

Implementation

@override
E copy() {
  if (_concept == null) {
    throw new ConceptException('Entity concept is not defined.');
  }

  Entity<E> entity = newEntity();

  var beforeUpdateOid = entity.concept.updateOid;
  entity.concept.updateOid = true;
  entity.oid = _oid;
  entity.concept.updateOid = beforeUpdateOid;

  if (_code != null) {
    var beforeUpdateCode = entity.concept.updateCode;
    entity.concept.updateCode = true;
    entity.code = _code;
    entity.concept.updateCode = beforeUpdateCode;
  }

  var beforeUpdateWhen = concept.updateWhen;
  concept.updateWhen = true;
  if (_whenAdded != null) {
    entity.whenAdded = _whenAdded;
  }
  if (_whenSet != null) {
    entity.whenSet = _whenSet;
  }
  if (_whenRemoved != null) {
    entity.whenRemoved = _whenRemoved;
  }
  concept.updateWhen = beforeUpdateWhen;

  for (Attribute attribute in _concept!.attributes.whereType<Attribute>()) {
    if (attribute.identifier) {
      var beforeUpdate = attribute.update;
      attribute.update = true;
      entity.setAttribute(attribute.code!, _attributeMap[attribute.code!]);
      attribute.update = beforeUpdate;
    } else {
      entity.setAttribute(attribute.code!, _attributeMap[attribute.code!]);
    }
  }

  for (Parent parent in _concept!.parents.whereType<Parent>()) {
    if (parent.identifier) {
      var beforeUpdate = parent.update;
      parent.update = true;
      entity.setParent(parent.code!, _parentMap[parent.code!]);
      parent.update = beforeUpdate;
    } else if (_parentMap[parent.code!] != null) {
      entity.setParent(parent.code!, _parentMap[parent.code!]);
    }
  }

  for (Child child in _concept!.children.whereType<Child>()) {
    entity.setChild(child.code!, _childMap[child.code!]!);
  }

  return entity as E;
}