public void TestCloneAndUpdateUpdateFields()
        {
            var svc = new EntityInfoService();

            string initialName        = "Initial name" + Guid.NewGuid();
            string initialDescription = "Initial description" + Guid.NewGuid();

            string newName        = "New name" + Guid.NewGuid();
            string newDescription = "New description" + Guid.NewGuid();

            IEntity e = Entity.Create(new EntityRef("test:person"));

            e.SetField("core:name", initialName);
            e.SetField("core:description", initialDescription);
            e.Save();

            Assert.AreEqual(initialName, e.GetField("core:name"));
            Assert.AreEqual(initialDescription, e.GetField("core:description"));

            var data = new EntityData
            {
                Id     = e.Id,
                Fields = new List <FieldData>()
                {
                    new FieldData
                    {
                        FieldId = new EntityRef("name"),
                        Value   = new TypedValue(newName)
                    },
                    new FieldData
                    {
                        FieldId = new EntityRef("description"),
                        Value   = new TypedValue(newDescription)
                    }
                },
                DataState = DataState.Update
            };

            var cloneIdsMap = svc.CloneAndUpdateEntity(data);

            long cloneId;

            Assert.IsTrue(cloneIdsMap.TryGetValue(e.Id, out cloneId), "The initial entity is not found.");

            // Check the fields were cloned
            IEntity clonedEntity = Entity.Get(cloneId);

            Assert.AreEqual(newName, clonedEntity.GetField("core:name"));
            Assert.AreEqual(newDescription, clonedEntity.GetField("core:description"));

            // Check initial entity is not touched
            IEntity initialEntity = Entity.Get(e.Id);

            Assert.AreEqual(initialName, initialEntity.GetField("core:name"));
            Assert.AreEqual(initialDescription, initialEntity.GetField("core:description"));
        }
        public void TestCloneAndUpdateTemporaryEntity()
        {
            var svc = new EntityInfoService();

            IEntity e = Entity.Create(new EntityRef("test:person"));

            var ed = new EntityData
            {
                Id = e.Id
            };

            Assert.Throws <InvalidOperationException>(() => svc.CloneAndUpdateEntity(ed));
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Handles cloning data.
        /// </summary>
        /// <param name="entityData">The entity data.</param>
        /// <returns>The id of the cloned entity</returns>
        private IHttpActionResult HandleCloneAndUpdate(JsonEntityQueryResult entityData)
        {
            // resolve all entity ids above our 'known id hi watermark' that actually do exist
            entityData.ResolveIds();

            long id      = entityData.Ids.FirstOrDefault();
            long cloneId = -1;

            if (id >= JsonEntityQueryResult.BaseNewId)
            {
                return(BadRequest("Cannot clone a temporary entity."));
            }

            EventLog.Application.WriteTrace("Cloning entity " + id);
            EntityData newEntityData = entityData.GetEntityData(id);

            DatabaseContext.RunWithRetry(() =>
            {
                using (DatabaseContext context = DatabaseContext.GetContext(true))
                {
#pragma warning disable 618
                    var svc = new EntityInfoService();
#pragma warning restore 618

                    var clonedIds = svc.CloneAndUpdateEntity(newEntityData);

                    if (!clonedIds.TryGetValue(id, out cloneId))
                    {
                        cloneId = -1;
                    }

                    context.CommitTransaction();
                }
            });

            return(Ok(cloneId));
        }
        public void TestCloneAndUpdateCloneByEntityRelationships()
        {
            var svc = new EntityInfoService();

            EntityType type1 = new EntityType();

            type1.Save();

            EntityType type2 = new EntityType();

            type2.Save();

            Relationship rel = new Relationship
            {
                FromType         = type1,
                ToType           = type2,
                CloneAction_Enum = CloneActionEnum_Enumeration.CloneEntities,
                Cardinality_Enum = CardinalityEnum_Enumeration.ManyToMany
            };

            rel.Save();

            string  initialName   = Guid.NewGuid().ToString();
            IEntity entityRelated = Entity.Create(type2.Id);

            entityRelated.SetField("core:name", initialName);
            entityRelated.Save();

            IEntity entity1 = Entity.Create(type1.Id);

            entity1.SetRelationships(rel, new EntityRelationshipCollection <IEntity> {
                entityRelated
            }, Direction.Forward);
            entity1.Save();

            string newEntityName = Guid.NewGuid().ToString();

            var data = new EntityData
            {
                Id            = entity1.Id,
                Relationships = new List <RelationshipData>
                {
                    new RelationshipData
                    {
                        RelationshipTypeId = new EntityRef(rel),
                        Instances          = new List <RelationshipInstanceData>
                        {
                            new RelationshipInstanceData
                            {
                                DataState = DataState.Unchanged,
                                Entity    = new EntityData
                                {
                                    DataState = DataState.Update,
                                    Id        = new EntityRef(entityRelated.Id),
                                    Fields    = new List <FieldData>()
                                    {
                                        new FieldData
                                        {
                                            FieldId = new EntityRef("name"),
                                            Value   = new TypedValue(newEntityName)
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                DataState = DataState.Update
            };

            var cloneIdsMap = svc.CloneAndUpdateEntity(data);

            long cloneId1, cloneIdRelated;

            Assert.IsTrue(cloneIdsMap.TryGetValue(entity1.Id, out cloneId1), "The initial entity is not found.");
            Assert.IsTrue(cloneIdsMap.TryGetValue(entityRelated.Id, out cloneIdRelated), "The related entity is not found.");

            // Check the relationships were cloned
            IEntity clonedEntity        = Entity.Get(cloneId1);
            var     clonedRelationships = clonedEntity.GetRelationships(rel, Direction.Forward);

            Assert.AreEqual(1, clonedRelationships.Count);
            Assert.IsTrue(clonedRelationships.Any(r => r.Id == cloneIdRelated));
            Assert.IsTrue(clonedRelationships.Any(r => (string)r.GetField("core:name") == newEntityName));

            // Check initial entity is not touched
            IEntity initialEntity        = Entity.Get(entity1.Id);
            var     initialRelationships = initialEntity.GetRelationships(rel, Direction.Forward);

            Assert.AreEqual(1, initialRelationships.Count);
            Assert.IsTrue(initialRelationships.Any(r => r.Id == entityRelated.Id));
            Assert.IsTrue(initialRelationships.Any(r => (string)r.GetField("core:name") == initialName));
        }