Esempio n. 1
0
        /// <summary>
        /// Creates a new instance of the specified type of entity.
        /// </summary>
        /// <param name="shortTypeName">The short name of the type of entity to create an instance of.</param>
        public override IEntity Create()
        {
            // TODO: Check if needed. Should be unnecessary

            ISubEntity entity = null;

            using (LogGroup logGroup = LogGroup.Start("Creating a new entity of type '" + TypeName + "'.", NLog.LogLevel.Debug))
            {
                if (!EntityState.IsType(TypeName))
                {
                    throw new InvalidOperationException("The TypeName property needs to be set to the type of an entity. Invalid type: " + TypeName);
                }

                Type type = EntityState.GetType(TypeName);

                LogWriter.Debug("Authorisation required: " + RequireAuthorisation.ToString());

                if (RequireAuthorisation)
                {
                    AuthoriseCreateStrategy.New(TypeName).EnsureAuthorised(TypeName);
                }

                entity    = (ISubEntity)base.Create();
                entity.ID = Guid.NewGuid();
            }
            return(entity);
        }
Esempio n. 2
0
        public override void NavigateAfterUpdate()
        {
            using (LogGroup logGroup = LogGroup.Start("Navigating after a update operation.", NLog.LogLevel.Debug))
            {
                if (DataSource == null)
                {
                    throw new InvalidOperationException("The DataSource property isn't set.");
                }

                ISubEntity entity = (ISubEntity)DataSource;

                if (entity.Parent == null)
                {
                    LogWriter.Debug("No parent found. Activating entity.Parent.");

                    ActivateStrategy.New <ISubEntity>().Activate(entity, entity.ParentPropertyName);
                }

                if (entity.Parent == null)
                {
                    throw new Exception("No parent assigned to entity.");
                }

                Navigator.Current.NavigateAfterOperation("View", entity.Parent);
            }
        }
Esempio n. 3
0
        public virtual int SetNumber(ISubEntity entity)
        {
            using (LogGroup logGroup = LogGroup.Start("Setting the number of the new sub entity.", NLog.LogLevel.Debug))
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                if (entity.Parent == null)
                {
                    throw new Exception("No parent is assigned to this item.");
                }

                IEntity parent = entity.Parent;

                ActivateStrategy.New(parent).Activate(parent, entity.ItemsPropertyName);

                ISubEntity[] items = Collection <ISubEntity> .ConvertAll(EntitiesUtilities.GetPropertyValue(parent, entity.ItemsPropertyName));

                LogWriter.Debug("Existing items: " + items.Length.ToString());

                entity.Number = items.Length + 1;

                LogWriter.Debug("Entity number: " + entity.Number.ToString());
            }
            return(entity.Number);
        }
        public override void Delete(IEntity entity)
        {
            if (entity is ISubEntity)
            {
                ISubEntity subEntity = (ISubEntity)entity;

                if (subEntity.Parent == null)
                {
                    ActivateStrategy.New(subEntity, RequireAuthorisation).Activate(subEntity, subEntity.ParentPropertyName);
                }

                IEntity parent = subEntity.Parent;

                if (parent == null)
                {
                    throw new Exception("No parent assigned to sub entity.");
                }

                base.Delete(subEntity);

                RefreshNumbers(subEntity.Parent, subEntity.ItemsPropertyName);
            }
            else
            {
                throw new ArgumentException("entity must be ISubEntity");
            }
        }
Esempio n. 5
0
        public virtual bool ExecuteSave(ISubEntity entity)
        {
            bool success = false;

            using (LogGroup logGroup = LogGroup.Start("Saving the new entity.", NLog.LogLevel.Debug))
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                if (entity.Parent == null)
                {
                    ActivateStrategy.New <ISubEntity>().Activate(entity, entity.ParentPropertyName);
                }

                if (entity.Parent == null)
                {
                    throw new Exception("No parent found for entity with ID: " + entity.ID.ToString());
                }

                DataSource = entity;

                success = base.ExecuteSave(entity);
            }

            return(success);
        }
        public override void NavigateAfterDelete(IEntity e)
        {
            ISubEntity entity = (ISubEntity)e;

            if (entity.Parent == null)
            {
                throw new Exception("No parent has been assigned to sub entity.");
            }

            Navigator.Current.NavigateAfterOperation("View", entity.Parent);
        }
Esempio n. 7
0
        public virtual ISubEntity Create(IEntity parent)
        {
            ISubEntity entity = null;

            using (LogGroup logGroup = LogGroup.Start("Creating a new sub entity with the provided parent.", NLog.LogLevel.Debug))
            {
                entity.Parent = parent;

                SetNumber(entity);
            }
            return(entity);
        }
        public override void ExecuteDelete(IEntity e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }

            ISubEntity entity = (ISubEntity)e;

            if (entity.Parent == null)
            {
                ActivateStrategy.New <ISubEntity>().Activate(entity, entity.ParentPropertyName);
            }

            base.ExecuteDelete(entity);
        }
Esempio n. 9
0
        public void Test_Create_Parent()
        {
            MockProjection projection = new MockProjection();

            projection.RequireAuthorisation = false;
            projection.Command = new CreateComandInfo(typeof(TestArticlePage).Name);

            TestArticle article = CreateStrategy.New <TestArticle>(false).Create <TestArticle>();

            article.ID    = Guid.NewGuid();
            article.Title = "Test title";

            SaveStrategy.New <TestArticle>(false).Save(article);

            CreateController controller = CreateController.New(projection);

            Assert.IsTrue(controller is CreateSubEntityController, "Invalid controller type: " + controller.GetType().ToString());

            CreateSubEntityController createSubEntityController = (CreateSubEntityController)controller;

            ISubEntity subEntity = (ISubEntity)createSubEntityController.Create(article.ID, String.Empty);

            Assert.IsNotNull(subEntity, "No sub entity returned.");

            Assert.IsTrue((subEntity is TestArticlePage), "Wrong type returned: " + subEntity.GetType().ToString());

            Assert.IsNotNull(subEntity.Parent, "No parent assigned to sub entity.");

            Assert.AreEqual(article.ID.ToString(), subEntity.Parent.ID.ToString(), "Parent ID doesn't match expected.");

            Assert.AreEqual(1, subEntity.Number, "Sub entity has wrong number.");

            SaveStrategy.New(subEntity, false).Save(subEntity);


            ISubEntity subEntity2 = (ISubEntity)createSubEntityController.Create(Guid.Empty, article.UniqueKey);

            Assert.IsNotNull(subEntity2, "No sub entity returned.");

            Assert.IsTrue((subEntity2 is TestArticlePage), "Wrong type returned: " + subEntity.GetType().ToString());

            Assert.IsNotNull(subEntity2.Parent, "No parent assigned to sub entity.");

            Assert.AreEqual(article.ID.ToString(), subEntity2.Parent.ID.ToString(), "Parent ID doesn't match expected.");

            Assert.AreEqual(2, subEntity2.Number, "Sub entity has wrong number.");
        }
Esempio n. 10
0
        public virtual ISubEntity Create(Guid parentID, string parentUniqueKey)
        {
            ISubEntity entity = null;

            using (LogGroup logGroup = LogGroup.Start("Creating a new sub entity with the specified parent.", NLog.LogLevel.Debug))
            {
                entity = (ISubEntity)Create();

                LogWriter.Debug("Parent ID: " + parentID.ToString());
                LogWriter.Debug("Parent unique key: " + parentUniqueKey);
                LogWriter.Debug("Parent type: " + entity.ParentTypeName);

                entity.Parent = GetParent(entity.ParentTypeName, parentID, parentUniqueKey);

                SetNumber(entity);
            }
            return(entity);
        }
        public virtual void Refresh(ISubEntity[] items, bool updateModified)
        {
            Collection<ISubEntity> updated = new Collection<ISubEntity>();

            for (int i = 0; i < items.Length; i++)
            {
                int originalNumber = items[i].Number;

                items[i].Number = i+1; // +1 to change index (0 based) to number (1 based)

                if (originalNumber != items[i].Number)
                {
                    updated.Add(items[i]);
                }
            }

            foreach (ISubEntity item in updated)
            {
                ActivateStrategy.New(item, RequireAuthorisation).Activate(item);

                UpdateStrategy.New(item, RequireAuthorisation).Update(item);
            }
        }
        public virtual bool ExecuteSave(ISubEntity entity)
        {
            bool success = false;

            using (LogGroup logGroup = LogGroup.Start("Saving the new entity.", NLog.LogLevel.Debug))
            {
                if (entity == null)
                    throw new ArgumentNullException("entity");

                if (entity.Parent == null)
                    ActivateStrategy.New<ISubEntity>().Activate(entity, entity.ParentPropertyName);

                if (entity.Parent == null)
                    throw new Exception("No parent found for entity with ID: " + entity.ID.ToString());

                DataSource = entity;

                success = base.ExecuteSave(entity);

            }

            return success;
        }
Esempio n. 13
0
 /// <summary>
 /// A collection of related sub-entities. If a sub-entit y contains an href value, it should be treated as an embedded link.
 /// Clients may choose to optimistically load embedded links. If no href value exists, the sub-entity is an embedded entity representation that contains all the characteristics of a typical entity.
 /// One difference is that a sub-entity MUST contain a rel attribute to describe its relationship to the parent entity.
 ///
 /// In JSON Siren, this is represented as an array.Optional.
 /// </summary>
 /// <param name="item"></param>
 public void AddEntity(ISubEntity item)
 {
     this.entities.Add(item);
 }
Esempio n. 14
0
 public IEntity AddSubEntity(ISubEntity subEntity)
 {
     this.Entities.Add(subEntity);
     return this;
 }
Esempio n. 15
0
 /// <summary>
 /// A collection of related sub-entities. If a sub-entit y contains an href value, it should be treated as an embedded link. 
 /// Clients may choose to optimistically load embedded links. If no href value exists, the sub-entity is an embedded entity representation that contains all the characteristics of a typical entity. 
 /// One difference is that a sub-entity MUST contain a rel attribute to describe its relationship to the parent entity. 
 /// 
 /// In JSON Siren, this is represented as an array.Optional.
 /// </summary>
 /// <param name="item"></param>
 public void AddEntity(ISubEntity item)
 {
     this.entities.Add(item);
 }