Exemple #1
0
        public void Test_GetMirrorPropertyName_Single_Explicit_Sync()
        {
            TestSample sample = new TestSample();

            string mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyName(sample, sample.GetType().GetProperty("Articles"));

            Assert.AreEqual("Samples", mirrorPropertyName, "The mirror property name wasn't determined correctly.");
        }
Exemple #2
0
        public void Test_GetMirrorPropertyName_Single_Implicit_Sync()
        {
            EntityOne e1 = new EntityOne();

            string mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyName(e1, e1.GetType().GetProperty("SingleReferenceProperty"));

            Assert.AreEqual(String.Empty, mirrorPropertyName, "The mirror property name wasn't determined correctly.");
        }
Exemple #3
0
        public void Test_GetMirrorPropertyName_Multiple_Implicit_Sync()
        {
            TestUser user = new TestUser();

            string mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyName(user, user.GetType().GetProperty("Roles"));

            Assert.AreEqual("Users", mirrorPropertyName, "The mirror property name wasn't determined correctly.");
        }
Exemple #4
0
        public void Test_GetMirrorPropertyName_Multiple_Implicit_Async_SameEntity()
        {
            TestGoal goal = new TestGoal();

            goal.ID = Guid.NewGuid();

            string mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyName(goal, goal.GetType().GetProperty("Prerequisites"));

            Assert.AreEqual(String.Empty, mirrorPropertyName, "The mirror property name wasn't determined correctly.");
        }
        public bool Match(IEntity entity)
        {
            bool doesMatch = true;

            //using (LogGroup logGroup2 = LogGroup.Start("Querying entity.", NLog.LogLevel.Debug))
            //{

            //LogWriter.Debug("Checking type " + e.GetType().ToString());
            //LogWriter.Debug("Entity ID: " + e.ID);
            try
            {
                string mirrorPropertyName = String.Empty;
                if (PropertyName != String.Empty)
                {
                    mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyName(entity, PropertyName);
                }
                else
                {
                    mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyNameReverse(entity, PropertyName, ReferencedEntityType);
                }

                bool foundReference = false;
                foreach (Guid id in ReferencedEntityIDs)
                {
                    if (DataAccess.Data.Referencer.MatchReference(entity.GetType(), entity.ID, PropertyName, ReferencedEntityType, id, mirrorPropertyName))
                    {
                        foundReference = true;
                    }
                }

                // If references are provided then it matches if found
                if (ReferencedEntityIDs.Length > 0)
                {
                    doesMatch = foundReference;
                }
                // Otherwise the calling code is trying to get entities where NO reference exists, therefore it matches when no reference is found
                else
                {
                    doesMatch = !foundReference;
                }
            }
            catch (Exception ex)
            {
                LogWriter.Error(ex);
                throw ex;
            }
            //LogWriter.Debug("Matches: " + matches);
            //}
            return(doesMatch);
        }
        public virtual void Test_MatchReference()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the MatchReference function to ensure matches properly.", NLog.LogLevel.Debug))
            {
                TestArticle article = new TestArticle();
                article.ID    = Guid.NewGuid();
                article.Title = "Test Article";

                TestCategory category = new TestCategory();
                category.ID   = Guid.NewGuid();
                category.Name = "Test Category";

                TestArticle article2 = new TestArticle();
                article2.ID    = Guid.NewGuid();
                article2.Title = "Test Article 2";

                TestCategory category2 = new TestCategory();
                category2.ID   = Guid.NewGuid();
                category2.Name = "Test Category 2";

                article.Categories = new TestCategory[] { category };

                EntityReference originalReference = DataAccess.Data.Referencer.GetActiveReferences(article)[0];

                LogWriter.Debug("Original reference - Entity 1 ID: " + originalReference.Entity1ID.ToString());
                LogWriter.Debug("Original reference - Entity 2 ID: " + originalReference.Entity2ID.ToString());
                LogWriter.Debug("Original reference - Property 1 name: " + originalReference.Property1Name);
                LogWriter.Debug("Original reference - Property 2 name: " + originalReference.Property2Name);
                LogWriter.Debug("Original reference - Type 1 name: " + originalReference.Type1Name);
                LogWriter.Debug("Original reference - Type 2 name: " + originalReference.Type2Name);

                foreach (EntityReference r in DataAccess.Data.Referencer.GetActiveReferences(article))
                {
                    DataAccess.Data.Saver.Save(r);
                }

                string mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyName(article,
                                                                                    EntitiesUtilities.GetProperty(article.GetType(), "Categories", typeof(TestCategory[])));

                bool match = DataAccess.Data.Referencer.MatchReference(article.GetType(), article.ID, "Categories", category.GetType(), category.ID, mirrorPropertyName);

                Assert.IsTrue(match, "Didn't match when it should have.");

                DataAccess.Data.Deleter.Delete(article);
                DataAccess.Data.Deleter.Delete(category);
            }
        }
Exemple #7
0
        public static IAuthoriseReferenceStrategy New(IEntity entity, PropertyInfo property)
        {
            IAuthoriseReferenceStrategy strategy = null;

            using (LogGroup logGroup = LogGroup.StartDebug("Instantiating a new AuthoriseReferenceStrategy for '" + property.Name + "' property on '" + entity.ShortTypeName + "' type."))
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                if (property == null)
                {
                    throw new ArgumentNullException("property");
                }

                Type referenceType = EntitiesUtilities.GetReferenceType(entity, property);

                // If the reference type is not null then continue
                if (referenceType != null)
                {
                    LogWriter.Debug("Referenced type: " + referenceType.FullName);

                    string mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyName(entity, property);

                    LogWriter.Debug("Mirror property name: " + mirrorPropertyName);

                    strategy = New(entity.ShortTypeName, property.Name, referenceType.Name, mirrorPropertyName);

                    if (strategy != null)
                    {
                        strategy.SourceEntity   = entity;
                        strategy.SourceProperty = property.Name;
                    }
                }
                // Otherwise skip it because it means the property is not set
                else
                {
                    LogWriter.Debug("Reference type is null. Skipping.");
                }
            }
            return(strategy);
        }
Exemple #8
0
        public override bool IsMatch(IEntity entity)
        {
            bool typeMatches      = false;
            bool referenceMatches = false;

            //using (LogGroup logGroup = LogGroup.Start("Checking whether provided entity matches this filter.", LogLevel.Debug))
            //{
            try
            {
                if (referenceType == null)
                {
                    throw new InvalidOperationException("ReferenceType property has not been set.");
                }

                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                if (this.ReferencedEntityID == Guid.Empty)
                {
                    throw new InvalidOperationException("ReferencedEntityID is not set.");
                }

                if (Types == null || Types.Length == 0)
                {
                    throw new Exception("No types have been added. Use the AddType function.");
                }

                //LogWriter.Debug("Property name: " + propertyName);
                //LogWriter.Debug("Referenced entity ID: " + referencedEntityID.ToString());

                //LogWriter.Debug("Referenced type: " + referenceType.ToString());

                Type entityType = entity.GetType();

                //LogWriter.Debug("Checking entity type: " + entityType.ToString());
                //LogWriter.Debug("Checking entity with ID: " + entity.ID);

                foreach (Type type in Types)
                {
                    if (type.Equals(entityType) ||
                        entityType.IsSubclassOf(type) ||
                        type.ToString() == entityType.ToString())
                    {
                        typeMatches = true;
                    }
                }

                string mirrorPropertyName = String.Empty;
                mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyName(entity, PropertyName);

                referenceMatches = DataAccess.Data.Referencer.MatchReference(entity.GetType(), entity.ID, propertyName, referenceType, referencedEntityID, mirrorPropertyName);

                //LogWriter.Debug("Type matches: " + typeMatches.ToString());
                //LogWriter.Debug("Reference matches: " + referenceMatches.ToString());
            }
            catch (Exception ex)
            {
                LogWriter.Error(ex);
            }

            //}
            return(typeMatches && referenceMatches);
        }
        /// <summary>
        /// Retrieves the active references from the provided property. This only includes those references currently active and not those in the data store.
        /// </summary>
        /// <param name="entity">The entity containing the property that the references are assigned to.</param>
        /// <param name="propertyName">The name of the property that the references are assigned to.</param>
        /// <param name="returnType">The type of the property that the references are assigned to.</param>
        /// /// <param name="autoBind">A value indicating whether to bind the reference objects to the data store ready for updating.</param>
        /// <returns>A collection of the entity references.</returns>
        public virtual EntityReferenceCollection GetActiveReferences(IEntity entity, string propertyName, Type returnType, bool autoBind)
        {
            EntityReferenceCollection collection = new EntityReferenceCollection(entity);

            using (LogGroup logGroup = LogGroup.Start("Retrieving the reference entities from the specified property on the provided entity.", LogLevel.Debug))
            {
                Type entityType = entity.GetType();

                PropertyInfo property = EntitiesUtilities.GetProperty(entityType, propertyName, returnType);

                if (property == null)
                {
                    LogWriter.Debug("Property: [null]");
                }
                else
                {
                    LogWriter.Debug("Property name: " + property.Name);
                }

                if (property != null)
                {
                    if (EntitiesUtilities.IsReference(entityType, propertyName, returnType))
                    {
                        Type referencedEntityType = EntitiesUtilities.GetReferenceType(entity, propertyName, returnType);

                        string mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyName(entity, property);

                        if (EntitiesUtilities.IsMultipleReference(entity.GetType(), property))
                        {
                            foreach (EntityReference r in GetActiveReferencesFromMultipleReferenceProperty(entity, property, mirrorPropertyName))
                            {
                                if (r != null)
                                {
                                    collection.Add(r);
                                }
                            }
                        }
                        else if (EntitiesUtilities.IsSingleReference(entityType, property))
                        {
                            EntityReference r = GetActiveReferenceFromSingleReferenceProperty(entity, property, mirrorPropertyName);
                            if (r != null)
                            {
                                collection.Add(r);
                            }
                        }
                        else
                        {
                            throw new NotSupportedException("The property type '" + property.PropertyType.ToString() + "' is not supported.");
                        }
                    }
                    else
                    {
                        throw new ArgumentException("The specified property is not a reference.");
                    }

                    LogWriter.Debug("References found: " + collection.Count.ToString());
                }
                else
                {
                    throw new Exception("Cannot find property '" + propertyName + "' on type '" + entity.GetType().ToString() + "'.");
                }

                // Assign the provided entity to each reference so it doesn't need to be loaded again
                foreach (EntityReference reference in collection)
                {
                    if (reference.Entity1ID == entity.ID)
                    {
                        reference.SourceEntity = entity;
                    }
                    else if (reference.Entity2ID == entity.ID)
                    {
                        reference.ReferenceEntity = entity;
                    }
                }

                if (autoBind)
                {
                    BindReferences(collection);
                }
            }

            return(collection);
        }
        public bool SetCountProperty(IEntity entity, string referencePropertyName, Guid referencedEntityID)
        {
            bool wasChanged = false;

            using (LogGroup logGroup = LogGroup.StartDebug("Setting the specified reference count property."))
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                LogWriter.Debug("Entity type: " + entity.GetType().FullName);
                LogWriter.Debug("Reference property name: " + referencePropertyName);
                LogWriter.Debug("Referenced entity ID: " + referencedEntityID);

                ReferenceAttribute attribute = EntitiesUtilities.GetReferenceAttribute(entity, referencePropertyName);

                Type referencedType = EntitiesUtilities.GetReferenceType(entity, referencePropertyName);

                // If the referenced entity type is not null then set the corresponding count property
                // otherwise skip it be cause a null referenced entity type means its a dynamically type reference property which hasn't been set
                if (referencedType != null)
                {
                    LogWriter.Debug("Referenced type: " + referencedType.FullName);

                    if (attribute.CountPropertyName != String.Empty)
                    {
                        LogWriter.Debug("Count property name: " + attribute.CountPropertyName);

                        string mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyName(entity, referencePropertyName);

                        PropertyInfo property = entity.GetType().GetProperty(attribute.CountPropertyName);

                        // Get the original reference count
                        int originalCount = (int)property.GetValue(entity, null);

                        LogWriter.Debug("Original count: " + originalCount);

                        if (property == null)
                        {
                            throw new Exception("'" + attribute.CountPropertyName + "' count property not found on type '" + entity.ShortTypeName + "'.");
                        }

                        // Get the latest reference count
                        int count = Provider.Counter.CountEntitiesWithReference(
                            entity.GetType(),
                            entity.ID,
                            referencePropertyName,
                            referencedType,
                            mirrorPropertyName
                            );

                        LogWriter.Debug("Count: " + count.ToString());

                        // If the new count is not the same as the old one then update the property
                        // otherwise skip
                        if (count != originalCount)
                        {
                            wasChanged = true;

                            property.SetValue(entity, count, null);
                        }
                    }
                    else
                    {
                        LogWriter.Debug("No count property specified on the reference attribute.");
                    }
                }
            }
            return(wasChanged);
        }
Exemple #11
0
        public bool Match(IEntity entity)
        {
            bool doesMatch = true;

            //using (LogGroup logGroup = LogGroup.StartDebug("Querying entity."))
            //{
            // Catch and log errors otherwise they won't get caught as this is executed within db4o
            try
            {
                //		LogWriter.Debug("Checking type " + entity.GetType().ToString());
                //		LogWriter.Debug("Entity ID: " + entity.ID);
                //		LogWriter.Debug("Property name: " + PropertyName);

                string mirrorPropertyName = MirrorPropertyName;

                // If no mirror property name was specified by the calling code then detect it
                if (mirrorPropertyName == null || mirrorPropertyName == String.Empty)
                {
                    if (PropertyName != String.Empty)
                    {
                        mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyName(entity, PropertyName);
                    }
                    else
                    {
                        mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyNameReverse(entity, PropertyName, ReferencedEntityType);
                    }
                }

                //		LogWriter.Debug("Mirror property name: " + mirrorPropertyName);

                bool referenceFound = false;

                if (ReferencedEntityID != Guid.Empty)
                {
                    referenceFound = DataAccess.Data.Referencer.MatchReference(entity.GetType(), entity.ID, PropertyName, ReferencedEntityType, ReferencedEntityID, mirrorPropertyName);
                }
                else
                {
                    referenceFound = DataAccess.Data.Referencer.MatchReference(entity.GetType(), entity.ID, PropertyName, ReferencedEntityType, mirrorPropertyName);
                }

                //		LogWriter.Debug("Reference found: " + referenceFound);

                // If a reference entity ID is specified then it matches if a reference is found
                if (ReferencedEntityID != Guid.Empty)
                {
                    //			LogWriter.Debug("Referenced entity ID is specified. Will match if a reference is found.");
                    doesMatch = referenceFound;
                }
                // Otherwise it matches if NO references are found, because the calling code wants to match an entity with no found references matching the one specified
                else
                {
                    //			LogWriter.Debug("Referenced entity ID is empty. Will match if no reference is found.");
                    doesMatch = !referenceFound;
                }
            }
            catch (Exception ex)
            {
                //		LogWriter.Error(ex);
                throw ex;
            }
            //	LogWriter.Debug("Matches: " + doesMatch);
            //}
            return(doesMatch);
        }