private ChangePacket createChangePacket(IModelObject obj, ModelChange change) { // Clear buffers keys.Clear(); values.Clear(); var p = new ChangePacket(); p.Change = change; p.Guid = getObjectGuid(obj); p.TypeFullName = TW.Data.TypeSerializer.Serialize(obj.GetType()); foreach (var att in ReflectionHelper.GetAllAttributes(obj.GetType())) { var data = att.GetData(obj); if (data != null && Attribute.GetCustomAttribute(data.GetType(), typeof(NoSyncAttribute)) != null) { continue; // Do not sync this type of object!!!! } keys.Add(att.Name); values.Add(serializeObject(data)); } p.Keys = keys.ToArray(); p.Values = values.ToArray(); return(p); }
private void synchronizeModelObject(IModelObject source, IModelObject target) { var properties = target.GetType().GetProperties().Where( info => Attribute.IsDefined(info, typeof(ModelObjectChangedAttribute))); foreach (var prop in properties) { prop.SetValue(target, prop.GetValue(source, null), null); } }
/// <summary> /// Reads and applies a single attribute /// </summary> private void readAttribute(IModelObject obj, SectionedStreamReader strm) { var name = strm.ReadLine(); var att = ReflectionHelper.GetAttributeByName(obj.GetType(), name); var serialized = readAttributeValue(strm); if (serialized == StringSerializer.Unknown) { return; //TODO: print zis shit } var deserialized = deserializeAttributeValue(serialized, att); att.SetData(obj, deserialized); }
private Guid getObjectGuid(IModelObject obj) { // Algorithm safety :D if (Attribute.GetCustomAttribute(obj.GetType(), typeof(NoSyncAttribute)) != null) { throw new InvalidOperationException("This is not allowed!!! EVAR"); } if (!guidMap.Contains(obj)) { guidMap.Add(obj, Guid.NewGuid()); } return(guidMap[obj]); }
public void SetItems(List <object> results) { Items = new ObservableCollection <object>(results); //EntityGridView viewCast = (View as EntityGridView); //List<Issue> issues = new List<Issue>(); //issues.AddRange(results.Cast<Issue>()); //viewCast.dataGridControl.ItemsSource = Items; //viewCast.dataGridControl.ItemsSource = issues;// new List<Tuple<string, string>> { new Tuple<string, string>("C1", "C2") }; //viewCast.devDataGrid.ItemsSource = Items; //foreach(var property in ) //viewCast.dataGridControl.Columns.Add(new Xceed.Wpf.DataGrid.Column(fieldName, displayMemberBinding)) foreach (var itemUncast in Items)//.Cast<IModelObject>()) { IModelObject item = itemUncast as IModelObject; item.Relationships = new List <List <object> >(); var type = item.GetType(); var relationshipProperties = item.GetNavigationProperties(); foreach (var relatedProperty in relationshipProperties) { if (relatedProperty.PropertyType.IsGenericType && relatedProperty.PropertyType.GetGenericTypeDefinition() == typeof(ICollection <>)) { var relatedItem = relatedProperty.GetValue(item); //List<object> something = new List<object>(relatedItem); var hash = relatedItem as IEnumerable; var relatedItemList = new List <object>();// hash.ToList<PollIssue>(); foreach (var existing in hash) { relatedItemList.Add(existing); } //relatedItemList.Add(new PollIssue() { Id = 1 }); //relatedItemList.Add(new PollIssue() { Id = 2 }); //relatedItemList.Add(new PollIssue() { Id = 3 }); item.Relationships.Add(relatedItemList); } } item.IsChanged = false; } SelectedItem = Items.FirstOrDefault() as IModelObject; IsLoading = false; }
private void AddNodeRecursively(IModelObject node, IList <ObjectRepositoryEntryChanges> changed, Stack <string> stack, Func <string, IModelObject, ObjectRepositoryEntryChanges> changeFactory) { var path = stack.ToDataPath(); changed.Add(changeFactory(path, node)); var dataAccessor = _modelDataProvider.Get(node.GetType()); foreach (var childProperty in dataAccessor.ChildProperties) { stack.Push(childProperty.Name); foreach (var child in node.Children) { stack.Push(child.Id.ToString()); AddNodeRecursively(child, changed, stack, changeFactory); stack.Pop(); } stack.Pop(); } }
private void CompareNode(IModelObject original, IModelObject @new, IList <ObjectRepositoryEntryChanges> changes, Stack <string> stack) { var accessor = _modelDataProvider.Get(original.GetType()); UpdateNodeIfNeeded(original, @new, stack, accessor, changes); foreach (var childProperty in accessor.ChildProperties) { if (!childProperty.ShouldVisitChildren(original) && !childProperty.ShouldVisitChildren(@new)) { // Do not visit children if they were not generated. // This means that they were not modified! continue; } stack.Push(childProperty.FolderName); CompareNodeChildren(original, @new, changes, stack, childProperty); stack.Pop(); } }
public void SerializeAttributes(IModelObject obj, SectionedStreamWriter strm) { var allAttributes = ReflectionHelper.GetAllAttributes(obj.GetType()); strm.EnterSection("EntityAttributes"); foreach (var att in allAttributes) { var value = att.GetData(obj); if (value == null) { continue; } writeAttribute(strm, att, value); } strm.ExitSection(); }
/// <summary> /// Queues an object and all the recursively references modelobjects to the queue /// </summary> /// <param name="obj"></param> public void QueueForSerialization(IModelObject obj) { if (serializationQueue.Contains(obj)) { return; } serializationQueue.Add(obj); var attributes = ReflectionHelper.GetAllAttributes(obj.GetType()); foreach (var att in attributes) { if (ReflectionHelper.IsGenericType(att.Type, typeof(List <>))) { IList list = (IList)att.GetData(obj); var elementType = ReflectionHelper.GetGenericListType(list.GetType()); if (!typeof(IModelObject).IsAssignableFrom(elementType)) { continue; } foreach (var el in list) { QueueForSerialization((IModelObject)el); } } if (!typeof(IModelObject).IsAssignableFrom(att.Type)) { continue; } var subelement = (IModelObject)att.GetData(obj); if (subelement == null) { continue; } //TODO: support persist attribute? QueueForSerialization(subelement); } }
/// <summary> /// Retourne le nom de l'objet. /// </summary> /// <param name="objet">L'objet.</param> /// <returns>Le nom de l'objet.</returns> private static string GetObjectType(IModelObject objet) { switch (objet.GetType().Name) { case "ModelRoot": return("Modèle"); case "ModelDomain": return("Domaine"); case "ModelNamespace": return("Namespace"); case "ModelClass": return("Classe"); case "ModelProperty": return("Propriété"); default: return(null); } }
public override int SaveChanges() { List <RuleViolation> errors = new List <RuleViolation>(); KcsarContext comparisonContext = new KcsarContext(); List <AuditLog> changes = new List <AuditLog>(); // Validate the state of each entity in the context // before SaveChanges can succeed. Random rand = new Random(); ObjectContext oc = ((IObjectContextAdapter)this).ObjectContext; var osm = oc.ObjectStateManager; oc.DetectChanges(); // Added and modified objects - we can describe the state of the object with // the information already present. foreach (ObjectStateEntry entry in osm.GetObjectStateEntries( EntityState.Added | EntityState.Modified)) { // Do Validation if (entry.Entity is IValidatedEntity) { IValidatedEntity validate = (IValidatedEntity)entry.Entity; if (!validate.Validate()) { errors.AddRange(validate.Errors); } else { Document d = entry.Entity as Document; if (d != null) { if (string.IsNullOrWhiteSpace(d.StorePath)) { string path = string.Empty; for (int i = 0; i < Document.StorageTreeDepth; i++) { path += ((i > 0) ? "\\" : "") + rand.Next(Document.StorageTreeSpan).ToString(); } if (!System.IO.Directory.Exists(Document.StorageRoot + path)) { System.IO.Directory.CreateDirectory(Document.StorageRoot + path); } path += "\\" + d.Id.ToString(); d.StorePath = path; } System.IO.File.WriteAllBytes(Document.StorageRoot + d.StorePath, d.Contents); } // New values are valid if (entry.Entity is IModelObject) { IModelObject obj = (IModelObject)entry.Entity; // Keep track of the change for reporting. obj.LastChanged = DateTime.Now; obj.ChangedBy = Thread.CurrentPrincipal.Identity.Name; IModelObject original = (entry.State == EntityState.Added) ? null : GetOriginalVersion(comparisonContext, entry); if (original == null) { changes.Add(new AuditLog { Action = entry.State.ToString(), Comment = obj.GetReportHtml(), Collection = entry.EntitySet.Name, Changed = DateTime.Now, ObjectId = obj.Id, User = Thread.CurrentPrincipal.Identity.Name }); } else { string report = string.Format("<b>{0}</b><br/>", obj); foreach (PropertyInfo pi in GetReportableProperties(obj.GetType())) { object left = pi.GetValue(original, null); object right = pi.GetValue(obj, null); if ((left == null && right == null) || (left != null && left.Equals(right))) { // report += string.Format("{0}: unchanged<br/>", pi.Name); } else { report += string.Format("{0}: {1} => {2}<br/>", pi.Name, left, right); } } changes.Add(new AuditLog { Action = entry.State.ToString(), Comment = report, Collection = entry.EntitySet.Name, Changed = DateTime.Now, ObjectId = obj.Id, User = Thread.CurrentPrincipal.Identity.Name }); } } } } } // Added and modified objects - we need to fetch more data before we can report what the change was in readable form. foreach (ObjectStateEntry entry in osm.GetObjectStateEntries(EntityState.Deleted)) { IModelObject modelObject = GetOriginalVersion(comparisonContext, entry); if (modelObject != null) { Document d = modelObject as Document; if (d != null && !string.IsNullOrWhiteSpace(d.StorePath)) { string path = Document.StorageRoot + d.StorePath; System.IO.File.Delete(path); for (int i = 0; i < Document.StorageTreeDepth; i++) { path = System.IO.Path.GetDirectoryName(path); if (System.IO.Directory.GetDirectories(path).Length + System.IO.Directory.GetFiles(path).Length == 0) { System.IO.Directory.Delete(path); } } } changes.Add(new AuditLog { Action = entry.State.ToString(), Comment = modelObject.GetReportHtml(), Collection = entry.EntitySet.Name, Changed = DateTime.Now, ObjectId = modelObject.Id, User = Thread.CurrentPrincipal.Identity.Name }); } } if (errors.Count > 0) { throw new RuleViolationsException(errors); } changes.ForEach(f => this.AuditLog.Add(f)); return(base.SaveChanges()); //if (SavedChanges != null) //{ // SavedChanges(this, new SavingChangesArgs { Changes = changes.Select(f => f.Comment).ToList() }); //} }
public void ShowPresenter(IModelObject obj) { currentPresenter = null; if (obj == null) { return; } // Find the first type in the list of types the presenter handles that the obj is an instance of. // This means that if an object implements two displayable interfaces, the first one we find will // be used. object o = obj; Type first = Presenters.Keys.FirstOrDefault(k => k.IsInstanceOfType(o)); //if (obj is CollectionPlaceholder) //{ // CollectionPlaceholder placeholder = (CollectionPlaceholder) obj; // if (placeholder.ItemType == typeof(ITable)) // first = typeof (ITableContainer); // else if (placeholder.ItemType == typeof(IKey)) // first = typeof(IKeyContainer); // else if (placeholder.ItemType == typeof(IIndex)) // first = typeof(IIndexContainer); // else if (placeholder.ItemType == typeof(IColumn)) // first = typeof(IColumnContainer); // obj = placeholder.Entity; //} if (first == null) { log.InfoFormat("We are not handling model objects of type {0} in PresenterController.ShowPresenter()", obj.GetType()); return; } currentPresenter = Presenters[first]; currentPresenter.DetachFromModel(); currentPresenter.AttachToModel(obj); currentPresenter.Show(); }