public void Process(CreateReferenceCommand c) { XmlNode parentEntity = _Document.GetElementById(GetKey(c.ParentType, c.ParentId)); if(parentEntity == null) throw new Exception("Not found parent Entity while creating Reference"); XmlElement reference; parentEntity.AppendChild(reference = _Document.CreateElement("Reference")); XmlAttribute name = reference.Attributes.Append(_Document.CreateAttribute("Role")); name.Value = c.Role; XmlAttribute child = reference.Attributes.Append(_Document.CreateAttribute("RefId")); child.Value = GetKey(c.ChildType, c.ChildId); }
internal Command CreateCommand(Entity e) { switch (e.Type) { case SyncUtils.CREATE_ENTITY: CreateEntityCommand ce = new CreateEntityCommand( e.GetString(SyncUtils.PARENTID), e.GetString(SyncUtils.TYPE) ); return ce; case SyncUtils.DELETE_ENTITY: DeleteEntityCommand de = new DeleteEntityCommand( e.GetString(SyncUtils.PARENTID), e.GetString(SyncUtils.TYPE) ); return de; case SyncUtils.CREATE_ATTRIBUTE: CreateAttributeCommand ca = new CreateAttributeCommand( e.GetString(SyncUtils.PARENTID), e.GetString(SyncUtils.PARENTTYPE), e.GetString(SyncUtils.NAME), MetaData.TypeResolver.GetType(e.GetString(SyncUtils.TYPE)), Factory.Serializer.Unserialize(e.GetString(SyncUtils.VALUE)) ); return ca; case SyncUtils.DELETE_ATTRIBUTE: DeleteAttributeCommand da = new DeleteAttributeCommand( e.GetString(SyncUtils.PARENTID), e.GetString(SyncUtils.PARENTTYPE), e.GetString(SyncUtils.NAME), MetaData.TypeResolver.GetType(e.GetString(SyncUtils.TYPE)), null ); return da; case SyncUtils.UPDATE_ATTRIBUTE: UpdateAttributeCommand ua = new UpdateAttributeCommand( e.GetString(SyncUtils.PARENTID), e.GetString(SyncUtils.PARENTTYPE), e.GetString(SyncUtils.NAME), MetaData.TypeResolver.GetType(e.GetString(SyncUtils.TYPE)), Factory.Serializer.Unserialize(e.GetString(SyncUtils.VALUE)) ); return ua; case SyncUtils.CREATE_REFERENCE: CreateReferenceCommand cr = new CreateReferenceCommand( e.GetString(SyncUtils.ROLE), e.GetString(SyncUtils.PARENTID), e.GetString(SyncUtils.PARENTTYPE), e.GetString(SyncUtils.CHILDID), e.GetString(SyncUtils.CHILDTYPE) ); return cr; case SyncUtils.DELETE_REFERENCE: DeleteReferenceCommand dr = new DeleteReferenceCommand( e.GetString(SyncUtils.ROLE), e.GetString(SyncUtils.PARENTID), e.GetString(SyncUtils.PARENTTYPE), e.GetString(SyncUtils.CHILDID), e.GetString(SyncUtils.CHILDTYPE) ); return dr; default: throw new UniversalStorageException("Unexpected command type"); } }
public override CreateReferenceCommand Visit(CreateReferenceCommand c) { _RWL.AcquireWriterLock(); try { Entity childEntity = GetEntity(c.ChildType, c.ChildId); Entity parentEntity = GetEntity(c.ParentType, c.ParentId); State tmpState = parentEntity.State; if (c.Reference.Cardinality.IsToMany) { MultipleEntry me = (MultipleEntry)parentEntity[c.Role ?? c.Reference.Name]; if (me == null) parentEntity.Add(me = new MultipleEntry(c.Role ?? c.Reference.Name)); me.Add(Entry.Create(c.Role ?? c.Reference.Name, tmpState, childEntity)); } else parentEntity.Add(c.Role ?? c.Reference.Name, childEntity); parentEntity.State = tmpState; } finally { _RWL.ReleaseWriterLock(); } return c; }
public override CreateReferenceCommand Visit(CreateReferenceCommand c) { Entity e = new Entity(SyncUtils.CREATE_REFERENCE); PopulateDefaults(e, c); e.SetValue(SyncUtils.PARENTID, c.ParentId); e.SetValue(SyncUtils.PARENTTYPE, c.ParentType); e.SetValue(SyncUtils.ROLE, c.Role); e.SetValue(SyncUtils.CHILDID, c.ChildId); e.SetValue(SyncUtils.CHILDTYPE, c.ChildType); transaction.Serialize(e); return c; }
public void Process(CreateReferenceCommand c) { // Exception management ReferenceMapping referenceMapping = _Mapping.Entities[c.ParentType, true].References[c.Role, c.ParentType, c.ChildType]; Evaluant.Uss.Models.Entity current = _Engine.Model.GetEntity(c.ParentType); if (referenceMapping == null) { while (referenceMapping == null && current != null) { Evaluant.Uss.Models.Entity currentChild = _Engine.Model.GetEntity(c.ChildType); while (referenceMapping == null && currentChild != null) { referenceMapping = _Mapping.Entities[current.Type, true].References[c.Role, current.Type, currentChild.Type]; currentChild = _Engine.Model.GetParent(currentChild); } if (referenceMapping == null) current = _Engine.Model.GetParent(current); } } if (referenceMapping == null) throw new MappingNotFoundException(String.Format("Rule mapping [{0}] not found for [{1}]", c.Role, c.ParentType)); string currentType = string.Empty; if (current != null) currentType = current.Type; else currentType = c.ParentType; CacheReferenceEntry referenceEntry = _Engine.GetCacheEntityEntry(currentType).GetCreateReferenceEntries(referenceMapping.Name, referenceMapping.EntityParent.Type, referenceMapping.EntityChild); foreach (CacheQueryEntry entry in referenceEntry.QueryEntries) { foreach (string table in referenceEntry.DisableTable) DisableForeignKeys(table); string cmdText = _Dialect.RenderQueries(entry.Query, _Driver)[0]; if (!string.IsNullOrEmpty(cmdText)) { IDbCommand command = _Driver.CreateCommand(cmdText, _Connection, _Transaction); foreach (Parameter param in entry.Parameters) { if (param.TagMapping is RuleMapping) { if (((RuleMapping)param.TagMapping).IsParentId) { EntityMapping parentMapping = ((RuleMapping)param.TagMapping).ParentReference.EntityParent; string parentid = ConvertId(parentMapping, param.Name, c.ParentId).ToString(); if (!command.Parameters.Contains(_Driver.FormatParameter(param.Name))) command.Parameters.Add(_Driver.CreateParameter(param.Name, _Dialect.GetDbTypeToPrimaryKey(parentMapping.Ids[GetIndexOfPrimaryKey(parentMapping, param.Name)].Generator), parentid)); } else { EntityMapping childMapping = _Mapping.Entities[((RuleMapping)param.TagMapping).ParentReference.EntityChild, true]; string childid = ConvertId(childMapping, param.Name, c.ChildId).ToString(); if (!command.Parameters.Contains(_Driver.FormatParameter(param.Name))) command.Parameters.Add(_Driver.CreateParameter(param.Name, _Dialect.GetDbTypeToPrimaryKey(childMapping.Ids[GetIndexOfPrimaryKey(childMapping, param.Name)].Generator), childid)); } } if (param.TagMapping is ReferenceMapping) command.Parameters.Add(_Driver.CreateParameter(param.Name, DbType.String, c.Role)); if (param.TagMapping is EntityMapping) { EntityMapping mapping = param.TagMapping as EntityMapping; IList children = _Engine.Model.GetTree(mapping.Type); StringCollection childrenTypes = new StringCollection(); foreach (Evaluant.Uss.Models.Entity child in children) childrenTypes.Add(child.Type); if (childrenTypes.Contains(c.ParentType) && childrenTypes.Contains(c.ChildType)) { if (param.UseParentValue) { string parentid = ConvertId(mapping, param.Name, c.ParentId).ToString(); if (!command.Parameters.Contains(_Driver.FormatParameter(param.Name))) command.Parameters.Add(_Driver.CreateParameter(param.Name, _Dialect.GetDbTypeToPrimaryKey(mapping.Ids[GetIndexOfPrimaryKey(mapping, param.Name)].Generator), parentid)); } else { string childid = ConvertId(mapping, param.Name, c.ChildId).ToString(); if (!command.Parameters.Contains(_Driver.FormatParameter(param.Name))) command.Parameters.Add(_Driver.CreateParameter(param.Name, _Dialect.GetDbTypeToPrimaryKey(mapping.Ids[GetIndexOfPrimaryKey(mapping, param.Name)].Generator), childid)); } } else { if (childrenTypes.Contains(c.ParentType)) { string parentid = ConvertId(mapping, param.Name, c.ParentId).ToString(); if (!command.Parameters.Contains(_Driver.FormatParameter(param.Name))) command.Parameters.Add(_Driver.CreateParameter(param.Name, _Dialect.GetDbTypeToPrimaryKey(mapping.Ids[GetIndexOfPrimaryKey(mapping, param.Name)].Generator), parentid)); } if (childrenTypes.Contains(c.ChildType)) { string childid = ConvertId(mapping, param.Name, c.ChildId).ToString(); if (!command.Parameters.Contains(_Driver.FormatParameter(param.Name))) command.Parameters.Add(_Driver.CreateParameter(param.Name, _Dialect.GetDbTypeToPrimaryKey(mapping.Ids[GetIndexOfPrimaryKey(mapping, param.Name)].Generator), childid)); } } } } command.Transaction = _Transaction; if (_Engine.TraceSqlSwitch.Enabled) { TraceHelpler.Trace(command, _Dialect); } command.ExecuteNonQuery(); } } }
public abstract CreateReferenceCommand Visit(CreateReferenceCommand item);
/// <summary> /// Generated a set of commands to be processed to take into account any modification applied to an entity graph /// </summary> /// <param name="entity">The Entity to compute</param> /// <param name="processing">A collection of all currently processed entities</param> /// <param name="commands">A collection of all currently created commands</param> public void ComputeChanges(Entity entity, ICollection<Entity> processing, ICollection<Command> commands) { // Nothing to do if the entity is currently processed if (processing.Contains(entity)) return; processing.Add(entity); // To know if we can optimize the command with a compound bool canCreateCompound = true; // Will contain the commands created for the attributes of the current entity List<AttributeCommand> attributeCommands = new List<AttributeCommand>(entity.Count); // First process all attributes not to increase the stack size because of deep hierarchies in the object graph and the recursive algorithm if (entity.State == State.Modified || entity.State == State.New) foreach (Entry e in entity) { if (!e.IsEntity) { // The current entry is an Attribute switch (e.State) { case State.Deleted: attributeCommands.Add(new DeleteAttributeCommand(e)); break; case State.Modified: attributeCommands.Add(new UpdateAttributeCommand(e)); break; case State.New: attributeCommands.Add(new CreateAttributeCommand(e)); break; default: break; } } } // Selects the optimized command /// TODO: Add a property to PE so that the user could specify whether he wants all attributes to be sent with a compound command switch (entity.State) { case State.New: if (canCreateCompound) commands.Add(new CompoundCreateCommand(entity, attributeCommands)); else { commands.Add(new CreateEntityCommand(entity)); foreach (Command attributeCommand in attributeCommands) commands.Add(attributeCommand); } break; case State.Deleted: commands.Add(new DeleteEntityCommand(entity.Id, entity.Type)); break; case State.Modified: if (attributeCommands.Count > 1) { CompoundUpdateCommand cuc = new CompoundUpdateCommand(entity, attributeCommands); commands.Add(cuc); } else foreach (Command attributeCommand in attributeCommands) commands.Add(attributeCommand); break; default: foreach (Command attributeCommand in attributeCommands) commands.Add(attributeCommand); break; } foreach (Entry e in entity) { if (e.IsEntity) { Model.Reference referenceModel = Model.GetReference(entity.Type, e.Name); if (e.IsMultiple) { foreach (Entity child in ((IEnumerable<Entity>)e)) { ComputeChanges(child, processing, commands); switch (e.State) { case State.New: CreateReferenceCommand crc1 = new CreateReferenceCommand(referenceModel, entity, child); commands.Add(crc1); break; case State.Deleted: DeleteReferenceCommand drc = new DeleteReferenceCommand(referenceModel, entity, child); commands.Add(drc); break; } } } else { // The current entry is an Entity Entity child = (Entity)e.Value; ComputeChanges(child, processing, commands); switch (e.State) { case State.New: CreateReferenceCommand crc1 = new CreateReferenceCommand(referenceModel, entity, child); commands.Add(crc1); break; case State.Deleted: DeleteReferenceCommand drc = new DeleteReferenceCommand(referenceModel, entity, child); commands.Add(drc); break; } } } } }
public override CreateReferenceCommand Visit(CreateReferenceCommand c) { _RWL.AcquireWriterLock(Timeout.Infinite); try { Entity parentEntity = _Entities[CacheEngine.GetCacheKey(c.ParentType, c.ParentId)]; if (parentEntity == null) return c; Entity childEntity = new Entity(c.ChildType); childEntity.Id = c.ChildId; State tmpState = parentEntity.State; parentEntity.Add(c.Role, childEntity, typeof(Entity), State.UpToDate); parentEntity.State = tmpState; } finally { _RWL.ReleaseWriterLock(); } return c; }