Пример #1
0
		public void Process(DeleteReferenceCommand c)
		{
			XmlElement reference = FindReference(GetKey(c.ParentType, c.ParentId), GetKey(c.ChildType, c.ChildId));

			if(reference == null)
				throw new Exception("Unknow Reference");

			reference.ParentNode.RemoveChild(reference);
		}
Пример #2
0
        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");
            }
        }
Пример #3
0
        public override DeleteReferenceCommand Visit(DeleteReferenceCommand c)
        {
            _RWL.AcquireWriterLock();

            try
            {
                Entity entity = GetEntity(c.ParentType, c.ParentId);

                for (int i = entity.EntityEntries.Count - 1; i >= 0; i--)
                {
                    Entry ee = (Entry)entity.EntityEntries[i];
                    if (ee.IsEntity && ((Entity)ee.Value).Id == c.ChildId)
                        entity.EntityEntries.Remove(ee);
                }
            }
            finally
            {
                _RWL.ReleaseWriterLock();
            }
            return c;
        }
Пример #4
0
        public override DeleteReferenceCommand Visit(DeleteReferenceCommand c)
        {
            Entity e = new Entity(SyncUtils.DELETE_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;
        }
Пример #5
0
        public void Process(DeleteReferenceCommand c)
        {
            ReferenceMapping referenceMapping = null;
            if (!string.IsNullOrEmpty(c.ChildType))
                referenceMapping = _Mapping.Entities[c.ParentType, true].References[c.Role, c.ChildType];
            else
                referenceMapping = _Mapping.Entities[c.ParentType, true].References[c.Role];

            if (referenceMapping == null)
            {
                Evaluant.Uss.Models.Entity current = _Engine.Model.GetEntity(c.ParentType);
                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 Exception(String.Format("The reference '{0}' of the entity '{1}' is not defined in your mapping file", c.Role, c.ParentType));

            EntityMapping parentMapping = referenceMapping.EntityParent;
            EntityMapping childMapping = _Mapping.Entities[referenceMapping.EntityChild, true];

            RuleMappingCollection rules = referenceMapping.Rules;

            bool isParentRefered = true;

            // Delete all records to index tables
            for (int index = 0; index < rules.Count; index++)
            {
                ISQLExpression query = null;

                //if first rule : ParentField is a foreign key ==> update parent table
                if ((index == 0) && (rules[index].ParentField != parentMapping.IdFields))
                {
                    // UPDATE parenttable SET FK_column = NULL WHERE PK_Column = PK_Value
                    query = new UpdateCommand(rules[index], rules[index].ParentTable);

                    for (int i = 0; i < rules[index].ParentFields.Length; i++)
                    {
                        if (parentMapping.Ids[rules[index].ParentFields[i]] == null)
                        {
                            Constant constant = new Constant(DBNull.Value, _Dialect.GetDbTypeToPrimaryKey(childMapping.Ids[i].Generator));
                            if (rules[index].ParentDefaultValues.Length > i && !string.IsNullOrEmpty(rules[index].ParentDefaultValues[i]))
                                constant = new Constant(rules[index].ParentDefaultValues[i], _Dialect.GetDbTypeToPrimaryKey(childMapping.Ids[i].Generator));

                            ((UpdateCommand)query).ColumnValueCollection.Add(rules[index].ParentFields[i], constant);
                        }
                    }

                    for (int i = 0; i < parentMapping.Ids.Count; i++)
                    {
                        string PK_Column = parentMapping.IdFields.Split(SqlMapperProvider.IDSEP)[i];
                        string PK_Value = ConvertId(parentMapping, PK_Column, c.ParentId);
                        ((UpdateCommand)query).WhereClause.SearchCondition.Add(new BinaryLogicExpression(
                             new Column(parentMapping.Ids[i], PK_Column),
                             BinaryLogicOperator.Equals,
                             new Constant(PK_Value, _Dialect.GetDbTypeToPrimaryKey(parentMapping.Ids[i].Generator))));
                    }



                    ExecuteCommand(query);
                }

                //if last rule : ChildField is a foreign key ==> update child table
                if ((index == rules.Count - 1) && (rules[index].ChildField != childMapping.IdFields))
                {
                    // UPDATE childtable SET FK_column = NULL WHERE PK_Column = PK_Value
                    query = new UpdateCommand(rules[index], rules[index].ChildTable);


                    for (int i = 0; i < rules[index].ChildFields.Length; i++)
                    {
                        if (childMapping.Ids[rules[index].ChildFields[i]] == null)
                        {
                            Constant constant = new Constant(DBNull.Value, _Dialect.GetDbTypeToPrimaryKey(parentMapping.Ids[i].Generator));
                            if (rules[index].ChildDefaultValues.Length > i && !string.IsNullOrEmpty(rules[index].ChildDefaultValues[i]))
                                constant = new Constant(rules[index].ChildDefaultValues[i], _Dialect.GetDbTypeToPrimaryKey(parentMapping.Ids[i].Generator));

                            ((UpdateCommand)query).ColumnValueCollection.Add(rules[index].ChildFields[i], constant);
                        }
                    }

                    for (int i = 0; i < childMapping.Ids.Count; i++)
                        ((UpdateCommand)query).WhereClause.SearchCondition.Add(new BinaryLogicExpression(
                           new Column(childMapping.Ids[i], childMapping.IdFields.Split(SqlMapperProvider.IDSEP)[i]),
                           BinaryLogicOperator.Equals,
                           new Constant(ConvertId(childMapping, childMapping.IdFields.Split(SqlMapperProvider.IDSEP)[i], c.ChildId), _Dialect.GetDbTypeToPrimaryKey(childMapping.Ids[i].Generator))));

                    //PrimaryKeyMapping PK_mapping = index == 0 ? parentMapping.Ids[0] : childMapping.Ids[0];
                    //string PK_column = childMapping.IdFields;
                    //string PK_value = c.ChildId;



                    ExecuteCommand(query);
                }

                // Execute a delete command to index table
                if (index != rules.Count - 1)
                {
                    // Delete records to index table with one foreign key : rule(n).ChildField == rule(n+1).ParentField
                    if (rules[index].ChildField == rules[index + 1].ParentField)
                    {
                        // the refered table is parent table ==> WHERE rule(n).ChildField == ParentId
                        if (isParentRefered)
                        {
                            query = new DeleteCommand(rules[index], rules[index].ChildTable);
                            ((DeleteCommand)query).Condition.SearchCondition.Add(new BinaryLogicExpression(
                                new Column(rules[index], rules[index].ChildField),
                                BinaryLogicOperator.Equals,
                                new Constant(c.ParentId, _Dialect.GetDbTypeToPrimaryKey(parentMapping.Ids[0].Generator))));

                        }

                        // the refered table is child table ==>  WHERE rule(n).ChildField == ChildId
                        else
                        {
                            query = new DeleteCommand(rules[index], rules[index].ChildTable);
                            ((DeleteCommand)query).Condition.SearchCondition.Add(new BinaryLogicExpression(
                                new Column(rules[index], rules[index].ChildField),
                                BinaryLogicOperator.Equals,
                                new Constant(c.ChildId, _Dialect.GetDbTypeToPrimaryKey(childMapping.Ids[0].Generator))));
                        }

                    }

                    // Delete records to index table with two foreign key : rule(n).ChildField != rule(n+1).ParentField
                    else
                    {
                        query = new DeleteCommand(rules[index], rules[index].ChildTable);
                        ((DeleteCommand)query).Condition.SearchCondition.Add(new BinaryLogicExpression(
                            new Column(rules[index], rules[index].ChildField),
                            BinaryLogicOperator.Equals,
                            new Constant(c.ParentId, _Dialect.GetDbTypeToPrimaryKey(parentMapping.Ids[0].Generator))));
                        ((DeleteCommand)query).Condition.SearchCondition.Add(new BinaryLogicExpression(
                            new Column(rules[index + 1], rules[index + 1].ParentField),
                            BinaryLogicOperator.Equals,
                            new Constant(c.ChildId, _Dialect.GetDbTypeToPrimaryKey(childMapping.Ids[0].Generator))));
                        isParentRefered = false;
                    }

                    ExecuteCommand(query);
                }
            }
        }
Пример #6
0
 public abstract DeleteReferenceCommand Visit(DeleteReferenceCommand item);
Пример #7
0
        /// <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;
                        }
                    }
                }
            }
        }
Пример #8
0
        public override DeleteReferenceCommand Visit(DeleteReferenceCommand c)
        {
            _RWL.AcquireWriterLock(Timeout.Infinite);

            try
            {
                Entity entity = _Entities[CacheEngine.GetCacheKey(c.ParentType, c.ParentId)];
                if (entity == null)
                    return c;

                for (int i = entity.EntityEntries.Count - 1; i >= 0; i--)
                {
                    Entry ee = (Entry)entity.EntityEntries[i];
                    if (ee.IsEntity && ((Entity)ee.Value).Id == c.ChildId)
                        entity.EntityEntries.Remove(ee);
                }
            }
            finally
            {
                _RWL.ReleaseWriterLock();
            }
            return c;
        }