/// <summary> /// Gets the name of the data store that the provided entity is stored in. /// </summary> /// <param name="entity">The entity to get the data store name for.</param> /// <param name="throwErrorIfNotFound">A flag indicating whether an error should be thrown when no data store attribute is found.</param> /// <returns>The data store that the provided entity is stored in.</returns> static public string GetDataStoreName(IEntity entity, bool throwErrorIfNotFound) { if (entity == null) { throw new ArgumentNullException("entity"); } string dataStoreName = String.Empty; Type type = entity.GetType(); using (LogGroup logGroup = LogGroup.Start("Retrieving data store name for entity of type '" + type.ToString() + "'.", LogLevel.Debug)) { if (EntitiesUtilities.IsReference(entity)) { LogWriter.Debug("Provided entity is an EntityReference"); EntityReference reference = (EntityReference)entity; dataStoreName = GetDataStoreName(new string[] { reference.Type1Name, reference.Type2Name }); } else { LogWriter.Debug("Provided entity is NOT an EntityReference."); dataStoreName = GetDataStoreName(entity.GetType()); } LogWriter.Debug("Data store name: " + dataStoreName); } return(dataStoreName); }
/// <summary> /// Counts all the entities of the specified type matching the specified values. /// </summary> /// <param name="type">The type of entity to retrieve.</param> /// <param name="parameters">The parameters to query with.</param> /// <returns>The total number of entities counted.</returns> public override int CountEntities <T>(string propertyName, object propertyValue) { int total = 0; Type type = typeof(T); //using (LogGroup logGroup = LogGroup.StartDebug("Counting the entities of the specified type with a property matching the provided name and value.")) //{ // LogWriter.Debug("Type: " + type.ToString()); // LogWriter.Debug("Property name: " + propertyName); // LogWriter.Debug("Property value: " + (propertyValue == null ? "[null]" : propertyValue.ToString())); Db4oDataStore store = ((Db4oDataStore)GetDataStore(type)); if (store != null) { if (store.ObjectContainer != null) { IQuery query = store.ObjectContainer.Query(); query.Constrain(typeof(T)); query.Descend(EntitiesUtilities.GetFieldName(typeof(T), propertyName)).Constrain(propertyValue); IObjectSet os = query.Execute(); total = os.Count; } } //LogWriter.Debug("Results: " + results.Count.ToString()); //} return(total); }
public void Test_IsEntity_True() { TestArticle article = new TestArticle(); Assert.IsTrue(EntitiesUtilities.IsEntity(article.GetType()), "Returned false when it should have returned true."); }
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); }
/// <summary> /// Retrieves the references that are no longer active on the provided entity and haven't yet been removed from the data store. /// </summary> /// <param name="entity">The entity containing the active references to compare with.</param> /// <param name="idsOfEntitiesToKeep">An array of IDs of the entities that are still active and are not obsolete.</param> /// <returns>A collection of the obsolete references that correspond with the provided entity.</returns> public virtual EntityReferenceCollection GetObsoleteReferences(IEntity entity, Guid[] idsOfEntitiesToKeep) { EntityReferenceCollection collection = new EntityReferenceCollection(entity); using (LogGroup logGroup = LogGroup.StartDebug("Retrieving the obsolete references for the entity provided.")) { Type entityType = entity.GetType(); foreach (PropertyInfo property in entityType.GetProperties()) { if (EntitiesUtilities.IsReference(entityType, property.Name, property.PropertyType)) { LogWriter.Debug("Checking reference property: " + property.Name); Type referencedEntityType = EntitiesUtilities.GetReferenceType(entity, property); // If the referenced entity type is not null then get the obsolete references if (referencedEntityType != null) { collection.AddRange( GetObsoleteReferences(entity, property.Name, referencedEntityType, idsOfEntitiesToKeep ) ); } // else // Otherwise skip it because a null referenced entity type means its a dynamically typed reference property which hasn't been set } } } return(collection); }
public bool SetCountProperties(IEntity entity, bool autoUpdate) { bool changed = false; foreach (PropertyInfo property in entity.GetType().GetProperties()) { if (EntitiesUtilities.IsReference(entity.GetType(), property)) { if (SetCountProperties(entity, property)) { changed = true; } } } // If the count property was changed then activate and update the referenced entity if (changed && autoUpdate) { LogWriter.Debug("Entity was updated. Updating to data store."); if (Provider.IsStored(entity)) { Provider.Activator.Activate(entity); Provider.Updater.Update(entity); } else { Provider.Saver.Save(entity); } } return(changed); }
private void ValidateDataSource(object dataSource) { if (dataSource == null) { throw new Exception("DataSource == null"); } bool isValid = false; if (EntitiesUtilities.IsEntity(dataSource.GetType())) { isValid = true; } else if (dataSource is Array) { isValid = true; foreach (object item in (Array)dataSource) { if (!EntitiesUtilities.IsEntity(item.GetType())) { isValid = false; } } } if (!isValid) { throw new Exception("Invalid data source: " + dataSource.GetType().ToString()); } }
public override IEntity Authorise(IEntity entity) { IEntity output = null; bool isAuthorised = false; using (LogGroup logGroup = LogGroup.StartDebug("Authorising the creation of references on the provided '" + entity.ShortTypeName + "' entity.")) { foreach (PropertyInfo property in entity.GetType().GetProperties()) { if (EntitiesUtilities.IsReference(entity.GetType(), property)) { IAuthoriseReferenceStrategy strategy = AuthoriseReferenceStrategy.New(entity, property); // If the strategy is null then skip it because it means the reference property isn't set if (strategy != null) { strategy.Authorise(); } } } isAuthorised = IsAuthorised(entity); LogWriter.Debug("Is authorised: " + isAuthorised); if (isAuthorised) { output = entity; } } return(output); }
public void Test_IsEntity_False() { object obj = String.Empty; Assert.IsFalse(EntitiesUtilities.IsEntity(obj.GetType()), "Returned true when it should have returned false."); }
static public Type GetEntityType(IEntity entity, PropertyInfo property) { Type type = null; using (LogGroup group = LogGroup.Start("Retrieving the type of entity being referenced by the provided property.", LogLevel.Debug)) { if (entity == null) { throw new ArgumentNullException("entity"); } if (property == null) { throw new ArgumentNullException("property"); } LogWriter.Debug("Entity type: " + entity.GetType().ToString()); LogWriter.Debug("Property name: " + property.Name); LogWriter.Debug("Property type: " + property.PropertyType.Name); type = EntitiesUtilities.GetReferenceType(entity, property); } if (type == null) { LogWriter.Debug("return type == null"); } else { LogWriter.Debug("return type == " + type.ToString()); } return(type); }
// TODO: Check if function should be removed static public void StripReferences(IEntity entity) { using (LogGroup logGroup2 = LogGroup.Start("Clearing all the object references so that they don't cascade automatically.", LogLevel.Debug)) { if (entity is EntityReference) { ((EntityReference)entity).Deactivate(); } else { // Clear all the references from the entity once they're ready to be saved separately foreach (PropertyInfo property in entity.GetType().GetProperties()) { LogWriter.Debug("Property name: " + property.Name); LogWriter.Debug("Property type: " + property.PropertyType.ToString()); // If the property is a reference // OR the actual provided entity is a reference AND the property holds an IEntity instance if (EntitiesUtilities.IsReference(entity.GetType(), property.Name, property.PropertyType)) { LogWriter.Debug("Cleared property. (Set to null)"); Reflector.SetPropertyValue(entity, property.Name, null); } } } } }
public override string CreateUrl(string action, string typeName, string propertyName, string dataKey) { if (action == null || action == String.Empty) { throw new ArgumentNullException("action"); } if (typeName == null || typeName == String.Empty) { throw new ArgumentNullException("typeName"); } if (propertyName == null || propertyName == String.Empty) { throw new ArgumentNullException("propertyName"); } if (dataKey == null || dataKey == String.Empty) { throw new ArgumentNullException("dataKey"); } return("http://localhost/TestApplication/" + EntitiesUtilities.FormatUniqueKey(action) + "-" + EntitiesUtilities.FormatUniqueKey(typeName) + "/" + EntitiesUtilities.FormatUniqueKey(propertyName) + "--" + EntitiesUtilities.FormatUniqueKey(dataKey)); }
public override void Activate(IEntity entity, int depth) { if (entity == null) { throw new ArgumentNullException("entity"); } using (LogGroup logGroup = LogGroup.StartDebug("Activating the references on type: " + entity.GetType().ToString())) { Type entityType = entity.GetType(); EntityReferenceCollection references = Provider.Referencer.GetReferences(entity); foreach (PropertyInfo property in entity.GetType().GetProperties()) { if (EntitiesUtilities.IsReference(entityType, property.Name, property.PropertyType)) { LogWriter.Debug("Found reference property: " + property.Name); LogWriter.Debug("Property type: " + property.PropertyType.ToString()); Activate(entity, property.Name, property.PropertyType, depth, references); } } } }
public void Test_IsMultipleReference_False() { EntityOne e1 = new EntityOne(); PropertyInfo property = e1.GetType().GetProperty("SingleReferenceProperty"); Assert.IsFalse(EntitiesUtilities.IsMultipleReference(e1.GetType(), property), "Returned true when it should have returned false."); }
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."); }
public void Test_IsReference_False() { TestArticle article = new TestArticle(); PropertyInfo property = article.GetType().GetProperty("ID"); Assert.IsFalse(EntitiesUtilities.IsReference(article.GetType(), property), "Returned true when it should have returned false."); }
public void Test_IsReference_True_InterfaceType() { EntityFive e5 = new EntityFive(); PropertyInfo property = e5.GetType().GetProperty("InterfaceReferencedEntities"); Assert.IsTrue(EntitiesUtilities.IsReference(e5.GetType(), property), "Returned false when it should have returned true."); }
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."); }
public void Test_IsMultipleReference_True() { TestArticle article = new TestArticle(); PropertyInfo property = article.GetType().GetProperty("Categories"); Assert.IsTrue(EntitiesUtilities.IsMultipleReference(article.GetType(), property), "Returned false when it should have returned true."); }
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."); }
public void Activate(IEntity entity, string propertyName, Type propertyType, int depth, EntityReferenceCollection references) { using (LogGroup logGroup = LogGroup.StartDebug("Activating the '" + propertyName + "' property on the '" + entity.ShortTypeName + "' type.")) { if (entity == null) { throw new ArgumentNullException("entity"); } if (propertyName == null || propertyName == String.Empty) { throw new ArgumentException("propertyName", "Cannot be null or String.Empty."); } PropertyInfo property = EntitiesUtilities.GetProperty(entity.GetType(), propertyName, propertyType); if (property == null) { throw new Exception("Cannot find property with name '" + propertyName + "' and type '" + (propertyType == null ? "[null]" : propertyType.ToString()) + "' on the type '" + entity.ShortTypeName + "'."); } Type referenceType = DataUtilities.GetEntityType(entity, property); // If the reference type is not null then activate the property // otherwise skip it because it means it's a dynamic reference property which hasn't been set if (referenceType != null) { LogWriter.Debug("Reference entity type: " + referenceType.ToString()); IEntity[] referencedEntities = Provider.Indexer.GetEntitiesWithReference(entity, property.Name, references); if (referencedEntities == null) { LogWriter.Debug("# of entities found: [null]"); } else { LogWriter.Debug("# of entities found:" + referencedEntities.Length); } // Multiple references. if (EntitiesUtilities.IsMultipleReference(entity.GetType(), property)) { LogWriter.Debug("Multiple reference property"); ActivateMultipleReferenceProperty(entity, property, referencedEntities); } // Single reference. else { LogWriter.Debug("Single reference property"); ActivateSingleReferenceProperty(entity, property, referencedEntities); } } } }
public virtual void ApplyAuthorisation(IEntity fromEntity, PropertyInfo property) { using (LogGroup logGroup = LogGroup.StartDebug("Applying authorisation on '" + property.Name + "' property of '" + fromEntity.GetType().FullName + "'.")) { object value = property.GetValue(fromEntity, null); object newValue = null; Type type = fromEntity.GetType(); Type referenceType = EntitiesUtilities.GetReferenceType(fromEntity, property); if (EntitiesUtilities.IsSingleReference(type, property)) { LogWriter.Debug("Is single reference."); if (IsAuthorised((IEntity)value)) { newValue = value; } } else { LogWriter.Debug("Multiple reference."); if (value != null) { IEntity[] toEntities = (IEntity[])value; ArrayList entities = new ArrayList(); LogWriter.Debug("# before: " + toEntities.Length.ToString()); foreach (IEntity entity in toEntities) { if (IsAuthorised(entity)) { entities.Add(entity); } } LogWriter.Debug("# after: " + entities.Count.ToString()); newValue = entities.ToArray(referenceType); } else { LogWriter.Debug("Property value is null. Skipping authorisation check."); } } if (value != newValue) { LogWriter.Debug("Setting new value to property."); property.SetValue(fromEntity, newValue, null); } } }
public void Test_GetMirrorPropertyNameReverse_Multiple_Implicit_Async() { MockEntity entity = new MockEntity(); MockPublicEntity publicEntity = new MockPublicEntity(); string mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyNameReverse(publicEntity, "", typeof(MockEntity)); Assert.AreEqual("PublicEntities", mirrorPropertyName, "The mirror property name wasn't determined correctly."); }
public virtual void Authorise(IEntity fromEntity, PropertyInfo property) { using (LogGroup logGroup = LogGroup.StartDebug("Checking whether the user is authorised to create a reference on the '" + property.Name + "' property of the '" + fromEntity.GetType().FullName + "' entity type.")) { Type type = EntitiesUtilities.GetReferenceType(fromEntity, property.Name); ApplyAuthorisation(fromEntity, property); } }
/// <summary> /// Checks whether the provided entity is stored in the corresponding data store. /// </summary> /// <param name="entity">The entity to look for in the corresponding data store.</param> /// <returns>A boolean value indicating whether the entity was found.</returns> public override bool IsStored(IEntity entity) { bool isStored = false; using (LogGroup logGroup = LogGroup.StartDebug("Checking whether the provided entity has already been stored.")) { if (entity == null) { throw new ArgumentNullException("entity"); } LogWriter.Debug("Entity type: " + entity.GetType()); if (EntitiesUtilities.IsReference(entity.GetType())) { LogWriter.Debug("Is reference == true"); EntityReference reference = (EntityReference)entity; Type type = EntityState.GetType(reference.Type1Name); Type type2 = EntityState.GetType(reference.Type2Name); LogWriter.Debug("Type 1: " + type.ToString()); LogWriter.Debug("Type 2: " + type2.ToString()); if (Stores[entity].IsStored(entity)) { LogWriter.Debug("Reference object found in store."); isStored = true; } else { LogWriter.Debug("Reference object NOT found. Checking for one with matching properties."); isStored = Referencer.MatchReference( type, reference.Entity1ID, reference.Property1Name, type2, reference.Entity2ID, reference.Property2Name); } } else { LogWriter.Debug("Is reference != true"); isStored = Stores[entity].IsStored(entity); } } LogWriter.Debug("Is stored: " + isStored.ToString()); return(isStored); }
public void Test_GetFieldName() { TestArticle article = new TestArticle(); article.ID = Guid.NewGuid(); string fieldName = EntitiesUtilities.GetFieldName(article.GetType(), "Title"); Assert.AreEqual("title", fieldName, "Incorrect field name returned."); }
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 override IEntity[] GetEntitiesWithReference(IEntity entity, string propertyName, EntityReferenceCollection references) { List <IEntity> entities = new List <IEntity>(); using (LogGroup logGroup = LogGroup.StartDebug("Querying the data store based on the provided parameters.")) { LogWriter.Debug("Property name: " + propertyName); LogWriter.Debug("References #: " + references.Count); if (references.Count > 0) { Type referencedEntityType = EntitiesUtilities.GetReferenceType(entity, propertyName); if (referencedEntityType != null) { LogWriter.Debug("Referenced entity type: " + referencedEntityType.ToString()); } else { LogWriter.Debug("Referenced entity type: [null]"); } foreach (EntityReference reference in references) { if (reference.Includes(entity.ID, propertyName)) { Guid otherID = reference.GetOtherID(entity.ID); string otherTypeName = reference.GetOtherType(entity.ShortTypeName); Type otherType = EntityState.GetType(otherTypeName); IEntity referencedEntity = Provider.Reader.GetEntity(otherType, "ID", otherID); if (referencedEntity != null) { entities.Add(referencedEntity); } } } if (entities != null) { LogWriter.Debug("entities != null"); } else { LogWriter.Debug("entities == null"); } LogWriter.Debug("Total objects: " + entities.Count); } } return(Release(entities.ToArray())); }
/// <summary> /// Creates the file path for the entity. /// </summary> /// <returns>The full file path for the entity</returns> public string CreateFilePath() { if (EntitiesUtilities.IsReference(entity.GetType())) { return(CreateReferencePath()); } else { return(CreateEntityPath()); } }
public bool ContainsCircularReference_StepDown <T>(T[] originalList, Stack stack) where T : IEntity { bool does = false; T foundEntity = (T)stack.Peek(); T entity = foundEntity; //Collection<T>.GetByID(originalList, foundEntity.ID); // TODO: Check if entity should be activated foreach (PropertyInfo property in entity.GetType().GetProperties()) { if (EntitiesUtilities.IsReference(entity.GetType(), property)) { if (EntitiesUtilities.GetReferencedEntities(entity, property).Length == 0) { DataAccess.Data.Activator.Activate(entity, property.Name); } foreach (IEntity referencedEntity in EntitiesUtilities.GetReferencedEntities(entity, entity.GetType().GetProperty(property.Name))) { // If the referenced entity is already in the stack then it's a circular reference if (stack != null && stack.Count > 0) { foreach (IEntity e in stack.ToArray()) { if (e.ID == referencedEntity.ID) { return(true); } } stack.Push(referencedEntity); if (ContainsCircularReference_StepDown <T>(originalList, stack)) { does = true; } stack.Pop(); if (does) { return(true); } } } } } return(does); }