Dart DocumentationdormEntity

Entity abstract class

abstract class Entity extends ObservableBase implements IExternalizable {
 
 //-----------------------------------
 //
 // Private properties
 //
 //-----------------------------------
 
 List<DormProxy> _proxies = <DormProxy>[];
 Map _source;
 EntityScan _scan;
 bool _isPointer, _isRegistered = false;
 int _uid;
 
 //-----------------------------------
 //
 // Public properties
 //
 //-----------------------------------
 
 //-----------------------------------
 // refClassName
 //-----------------------------------
 
 String get refClassName => null;
 
 //-----------------------------------
 //
 // Operator overloads
 //
 //-----------------------------------
 
 dynamic operator [](String propertyName) {
   _ProxyEntry entry;
   List<_ProxyEntry> proxies = _scan._proxies;
   int i = proxies.length;
   
   while (i > 0) {
     entry = proxies[--i];
     
     if (entry.property == propertyName) {
       return entry.proxy.value;
     }
   }
   
   return null;
 }
 
 void operator []=(String propertyName, dynamic propertyValue) {
   _ProxyEntry entry;
   List<_ProxyEntry> proxies = _scan._proxies;
   int i = proxies.length;
   
   while (i > 0) {
     entry = proxies[--i];
     
     if (entry.property == propertyName) {
       entry.proxy.value = notifyPropertyChange(
           entry.proxy.propertySymbol, 
           entry.proxy.value,
           propertyValue
       );
     }
   }
   
   return null;
 }
 
 //-----------------------------------
 //
 // Public methods
 //
 //-----------------------------------
 
 void validate() {
   List<_ProxyEntry> proxies = _scan._proxies;
   int i = proxies.length;
   
   while (i > 0) {
     proxies[--i].proxy.validate();
   }
 }
 
 bool isDirty() {
   _ProxyEntry entry;
   List<_ProxyEntry> proxies = _scan._proxies;
   int i = proxies.length;
   
   while (i > 0) {
     entry = proxies[--i];
     
     if (entry.proxy._value != entry.proxy._defaultValue) {
       return true;
     }
   }
   
   return false;
 }
 
 void readExternal(Map<String, dynamic> data, OnConflictFunction onConflict) {
   EntityFactory<Entity> factory = new EntityFactory(onConflict);
   _ProxyEntry entry;
   List<_ProxyEntry> proxies = _scan._proxies;
   Iterable<Map<String, dynamic>> spawnList = new List<Map<String, dynamic>>(1);
   DormProxy proxy;
   int i = proxies.length;
   dynamic entryValue;
   
   _isPointer = data.containsKey(SerializationType.POINTER);
   
   while (i > 0) {
     entry = proxies[--i];
     
     entryValue = data[entry.property];
     
     proxy = entry.proxy;
     
     if (entryValue is Map) {
       spawnList[0] = entryValue;
       
       proxy._initialValue = factory.spawn(spawnList).first;
     } else if (entryValue is Iterable) {
       proxy._initialValue = proxy.owner = new ObservableList.from(factory.spawn(entryValue));
     } else {
       proxy._initialValue = entryValue;
     }
   }
 }
 
 void writeExternal(Map<String, dynamic> data) {
   _writeExternalImpl(data, null);
 }
 
 String toJson({Map<String, Map<String, dynamic>> convertedEntities}) {
   Map<String, dynamic> jsonMap = new Map<String, dynamic>();
   
   writeExternal(jsonMap);
   
   return stringify(jsonMap);
 }
 
 String toString() {
   List<String> result = <String>[];
   
   _scan._proxies.forEach(
     (_ProxyEntry entry) {
       if (entry.proxy.isLabelField) {
         result.add(entry.proxy.value.toString());
       }
     }
   );
   
   return result.join(', ');
 }
 
 //---------------------------------
 //
 // Private methods
 //
 //---------------------------------
 
 void _identityKeyListener(List<ChangeRecord> changes) {
   changes.forEach(
       (ChangeRecord change) {
         if (change is PropertyChangeRecord) {
           _ProxyEntry result = _scan._identityProxies.firstWhere(
               (_ProxyEntry entry) => (entry.proxy.propertySymbol == change.field),
               orElse: () => null
           );
           
           if (
               (result != null) &&
               result.proxy.isId
           ) {
             _scan.buildKey();
           }
         }
       }
   );
 }
 
 void _writeExternalImpl(Map<String, dynamic> data, Map<int, Map<String, dynamic>> convertedEntities) {
   data[SerializationType.ENTITY_TYPE] = _scan.refClassName;
   data[SerializationType.UID] = _uid;
   
   if (convertedEntities == null) {
     convertedEntities = new Map<int, Map<String, dynamic>>();
   }
   
   convertedEntities[_uid] = data;
   
   _scan._proxies.forEach(
     (_ProxyEntry entry) {
       if (
           entry.proxy.isId ||
           (entry.proxy._value != entry.proxy._defaultValue)
       ) {
         if (entry.proxy.value is Entity) {
           Entity subEntity = entry.proxy.value;
           
           if (convertedEntities.containsKey(subEntity._uid)) {
             Map<String, dynamic> pointerMap = new Map<String, dynamic>();
             
             pointerMap[SerializationType.POINTER] = subEntity._uid;
             pointerMap[SerializationType.ENTITY_TYPE] = subEntity._scan.refClassName;
             
             subEntity._scan._proxies.forEach(
                 (_ProxyEntry subEntry) {
                   if (subEntry.proxy.isId) {
                     pointerMap[subEntry.property] = subEntry.proxy.value;
                   }
                 }
             );
             
             data[entry.property] = pointerMap;
           } else {
             data[entry.property] = new Map<String, dynamic>();
             
             subEntity._writeExternalImpl(data[entry.property], convertedEntities);
           }
         } else {
           data[entry.property] = entry.proxy.value;
         }
       }
     }
   );
 }
 
}

Extends

ObservableBase > Entity

Implements

IExternalizable

Properties

final Stream<List<ChangeRecord>> changes #

inherited from ObservableBase

The stream of change records to this object.

Changes should be delivered in asynchronous batches by calling queueChangeRecords.

deliverChangeRecords can be called to force delivery.

docs inherited from Observable
Stream<List<ChangeRecord>> get changes {
 if (_broadcastController == null) {
   _broadcastController =
       new StreamController<List<ChangeRecord>>.broadcast(sync: true);
 }
 return _broadcastController.stream;
}

final bool hasObservers #

inherited from ObservableBase

True if this object has any observers, and should call notifyPropertyChange for changes.

bool get hasObservers => _broadcastController != null &&
                        _broadcastController.hasListener;

final String refClassName #

String get refClassName => null;

Operators

dynamic operator [](String propertyName) #

dynamic operator [](String propertyName) {
 _ProxyEntry entry;
 List<_ProxyEntry> proxies = _scan._proxies;
 int i = proxies.length;
 
 while (i > 0) {
   entry = proxies[--i];
   
   if (entry.property == propertyName) {
     return entry.proxy.value;
   }
 }
 
 return null;
}

void operator []=(String propertyName, propertyValue) #

void operator []=(String propertyName, dynamic propertyValue) {
 _ProxyEntry entry;
 List<_ProxyEntry> proxies = _scan._proxies;
 int i = proxies.length;
 
 while (i > 0) {
   entry = proxies[--i];
   
   if (entry.property == propertyName) {
     entry.proxy.value = notifyPropertyChange(
         entry.proxy.propertySymbol, 
         entry.proxy.value,
         propertyValue
     );
   }
 }
 
 return null;
}

Methods

bool isDirty() #

bool isDirty() {
 _ProxyEntry entry;
 List<_ProxyEntry> proxies = _scan._proxies;
 int i = proxies.length;
 
 while (i > 0) {
   entry = proxies[--i];
   
   if (entry.proxy._value != entry.proxy._defaultValue) {
     return true;
   }
 }
 
 return false;
}

void notifyChange(ChangeRecord record) #

inherited from ObservableBase

Notify observers of a change. For most objects notifyPropertyChange is more convenient, but collections sometimes deliver other types of changes such as a ListChangeRecord.

void notifyChange(ChangeRecord record) {
 if (!hasObservers) return;

 if (_changes == null) {
   _changes = [];
   queueChangeRecords(_deliverChanges);
 }
 _changes.add(record);
}

dynamic notifyPropertyChange(Symbol field, Object oldValue, Object newValue) #

inherited from ObservableBase

Notify that the field name of this object has been changed.

The oldValue and newValue are also recorded. If the two values are identical, no change will be recorded.

For convenience this returns newValue. This makes it easy to use in a setter:

var _myField;
get myField => _myField;
set myField(value) {
  _myField = notifyPropertyChange(
      const Symbol('myField'), _myField, value);
}
notifyPropertyChange(Symbol field, Object oldValue, Object newValue) {
 if (hasObservers && !identical(oldValue, newValue)) {
   notifyChange(new PropertyChangeRecord(field));
 }
 return newValue;
}

void readExternal(Map<String, dynamic> data, OnConflictFunction onConflict) #

void readExternal(Map<String, dynamic> data, OnConflictFunction onConflict) {
 EntityFactory<Entity> factory = new EntityFactory(onConflict);
 _ProxyEntry entry;
 List<_ProxyEntry> proxies = _scan._proxies;
 Iterable<Map<String, dynamic>> spawnList = new List<Map<String, dynamic>>(1);
 DormProxy proxy;
 int i = proxies.length;
 dynamic entryValue;
 
 _isPointer = data.containsKey(SerializationType.POINTER);
 
 while (i > 0) {
   entry = proxies[--i];
   
   entryValue = data[entry.property];
   
   proxy = entry.proxy;
   
   if (entryValue is Map) {
     spawnList[0] = entryValue;
     
     proxy._initialValue = factory.spawn(spawnList).first;
   } else if (entryValue is Iterable) {
     proxy._initialValue = proxy.owner = new ObservableList.from(factory.spawn(entryValue));
   } else {
     proxy._initialValue = entryValue;
   }
 }
}

String toJson({Map<String, Map<String, dynamic>> convertedEntities}) #

String toJson({Map<String, Map<String, dynamic>> convertedEntities}) {
 Map<String, dynamic> jsonMap = new Map<String, dynamic>();
 
 writeExternal(jsonMap);
 
 return stringify(jsonMap);
}

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString() {
 List<String> result = <String>[];
 
 _scan._proxies.forEach(
   (_ProxyEntry entry) {
     if (entry.proxy.isLabelField) {
       result.add(entry.proxy.value.toString());
     }
   }
 );
 
 return result.join(', ');
}

void validate() #

void validate() {
 List<_ProxyEntry> proxies = _scan._proxies;
 int i = proxies.length;
 
 while (i > 0) {
   proxies[--i].proxy.validate();
 }
}

void writeExternal(Map<String, dynamic> data) #

void writeExternal(Map<String, dynamic> data) {
 _writeExternalImpl(data, null);
}