public virtual void Test_Update_MaintainReferences()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the update function to see if it maintains references.", NLog.LogLevel.Debug))
            {
                TestUser user = new TestUser();
                user.ID = Guid.NewGuid();
                user.FirstName = "Test";
                user.LastName = "User";

                TestRole role = new TestRole();
                role.ID = Guid.NewGuid();
                role.Name = "Test role";

                // This should remain commented out to check for exclusion
                user.Roles = new TestRole[]{role};

                DataAccess.Data.Saver.Save(role);
                DataAccess.Data.Saver.Save(user);

                EntityReference reference = new EntityReference();
                reference.ID = Guid.NewGuid();
                reference.Type1Name = "TestUser";
                reference.Type2Name = "TestRole";
                reference.Entity1ID = user.ID;
                reference.Entity2ID = role.ID;
                reference.Property1Name = "Roles";
                reference.Property2Name = "Users";

                TestUser user2 = DataAccess.Data.Reader.GetEntity<TestUser>("ID", user.ID);

                DataAccess.Data.Activator.Activate(user2);

                string newFirstName =  "Something else";
                user2.FirstName  = newFirstName;

                DataAccess.Data.Updater.Update(user2);

                TestUser user3 = DataAccess.Data.Reader.GetEntity<TestUser>("ID", user.ID);

                DataAccess.Data.Activator.Activate(user3);

                Assert.IsNotNull(user3.Roles);

                Assert.AreEqual(newFirstName, user3.FirstName, "First name mismatch.");

                if (user3.Roles != null)
                    Assert.AreEqual(1, user3.Roles.Length, "Incorrect number of roles.");

            }
        }
        public override IEntity Clone()
        {
            EntityReference reference = new EntityReference();

            CopyTo(reference);

            return reference;
        }
        public void Test_GetDataStoreName_Reference()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the GetDataStoreName function with a provided entity reference.", NLog.LogLevel.Debug))
            {
                EntityReference reference = new EntityReference();
                reference.Type1Name = "TestArticle";
                reference.Type2Name = "TestCategory";

                string name = DataUtilities.GetDataStoreName(reference);

                string expected = "TestArticle-TestCategory";

                Assert.AreEqual(expected, name, "The wrong data store name was returned.");
            }
        }
        public override void ActivateReference(EntityReference reference)
        {
            using (LogGroup logGroup = LogGroup.StartDebug("Activating reference."))
            {
                if (reference.Type1Name == string.Empty)
                    throw new ArgumentNullException("reference.Type1Name");
                if (reference.Type2Name == string.Empty)
                    throw new ArgumentNullException("reference.Type2Name");

                LogWriter.Debug("Type 1: " + reference.Type1Name);
                LogWriter.Debug("Type 2: " + reference.Type2Name);
                LogWriter.Debug("ID 1: " + reference.Entity1ID);
                LogWriter.Debug("ID 2: " + reference.Entity2ID);

                Type type1 = EntityState.GetType(reference.Type1Name);
                Type type2 = EntityState.GetType(reference.Type2Name);

                LogWriter.Debug("Full type 1: " + type1.ToString());
                LogWriter.Debug("Full type 2: " + type2.ToString());

                if (reference.Entity1ID == Guid.Empty || reference.Entity2ID == Guid.Empty)
                {
                    LogWriter.Debug("Skipped activation because both IDs weren't found.");
                }
                else
                {
                    // If the source entity is not yet set
                    if (reference.SourceEntity == null)
                    {
                        IEntity entity1 = DataAccess.Data.Reader.GetEntity(
                            type1,
                            "ID",
                            reference.Entity1ID);

                        // TODO: Check if exceptions should be thrown when the entity isn't found
                        // Currently references are simply skipped if entity isn't found
                        //if (entity1 == null)
                        //	throw new Exception("Can't find '" + type1.Name + "' entity with ID '" + reference.Entity1ID.ToString() + "'. Make sure the entity has been saved BEFORE trying to reference it.");

                        if (entity1 != null)
                            reference.SourceEntity = entity1;
                    }

                    // If the reference entity is not yet set
                    if (reference.ReferenceEntity == null)
                    {
                        IEntity entity2 = DataAccess.Data.Reader.GetEntity(
                            type2,
                            "ID",
                            reference.Entity2ID);

                        // TODO: Check if exceptions should be thrown when the entity isn't found
                        // Currently references are simply skipped if entity isn't found
                        //if (entity2 == null)
                        //	throw new Exception("Can't find '" + type2.Name + "' entity with ID '" + reference.Entity2ID.ToString() + "'. Make sure the entity has been saved BEFORE trying to reference it.");

                        if (entity2 != null)
                            reference.ReferenceEntity = entity2;
                    }
                }
            }
        }
        public bool IsValidReference(EntityReference reference)
        {
            if (reference == null)
                return false;

            if (reference.Entity1ID == Guid.Empty)
                return false;

            if (reference.Entity2ID == Guid.Empty)
                return false;

            if ((reference.Property1Name == null || reference.Property1Name == String.Empty)
                && (reference.Property2Name == null || reference.Property2Name == String.Empty))
                return false;

            if (reference.Type1Name == null || reference.Type1Name == String.Empty)
                return false;

            if (reference.Type2Name == null || reference.Type2Name == String.Empty)
                return false;

            return true;
        }
 private void CreateDummyReferences(int count)
 {
     EntityReference reference = new EntityReference();
     reference.Entity1ID = Guid.NewGuid();
     reference.Entity2ID = Guid.NewGuid();
     reference.Property1Name = "TestProperty1-" + Guid.NewGuid().ToString().Substring(0, 5);
     reference.Property2Name = "TestProperty2-" + Guid.NewGuid().ToString().Substring(0, 5);
     reference.Type1Name = "TestUser";
     reference.Type2Name = "TestRole";
 }
        public void Test_IsValid_ReferenceParameter_True()
        {
            EntityReference reference = new EntityReference();
            reference.Entity1ID = Guid.NewGuid();
            reference.Entity2ID = Guid.NewGuid();
            reference.Type2Name = "TestArticle";
            reference.Type1Name = "TestCategory";
            reference.Property1Name = "Title";
            reference.Property2Name = "Name";

            bool isValid = DataAccess.Data.Importer.IsValid(reference);

            Assert.IsTrue(isValid, "Should have returned true.");
        }
        public void Test_IsValid_ReferenceParameter_False()
        {
            EntityReference reference = new EntityReference();

            bool isValid = DataAccess.Data.Importer.IsValid(reference);

            Assert.IsFalse(isValid, "Should have returned false.");
        }
 public abstract void ActivateReference(EntityReference reference);
        public virtual void Test_Save_EntityReference()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing saving of an EntityReference.", NLog.LogLevel.Debug))
            {
                EntityReference reference = new EntityReference();
                reference.ID = Guid.NewGuid();
                reference.Type1Name = "TestArticle";
                reference.Type2Name = "TestArticlePage";
                reference.Entity1ID = Guid.NewGuid();
                reference.Entity2ID = Guid.NewGuid();
                reference.Property1Name = "Pages";
                reference.Property2Name = "Article";

                DataAccess.Data.Saver.Save(reference);

                IDataStore store = DataAccess.Data.Stores["Testing_Articles-Testing_Articles"];

                Assert.IsNotNull(store, "The data store wasn't created/initialized.");

            }
        }