/// <summary> /// Remove arguments that have no reverse relationships other than a reference back to the expression map. /// </summary> /// <param name="wf"></param> private static void CleanArgumentsThroughExpressionMap(Workflow wf) { var delete = new List <ActivityArgument>(); foreach (var wfExpression in wf.ExpressionMap) { var arg = wfExpression.ArgumentToPopulate; if (arg != null) { var rels = EntityTypeHelper.GetAllRelationships(arg, Direction.Reverse).ToList(); Entity.Get(arg, rels.ToArray <IEntityRef>()); // i.e. core:populatedByExpression var any = (from rel in rels where rel.Alias != "core:argumentToPopulate" select arg.GetRelationships(rel, Direction.Reverse)).Any(members => members.Count > 0); if (!any) { delete.Add(arg); } } } if (delete.Count > 0) { EventLog.Application.WriteWarning("Workflow has {0} dangling arguments referenced in the expression map, deleting.", delete.Count); Entity.Delete(delete.Select(d => d.Id)); } }
public async Task TakeCareOf(EntityPatchBag <T> eub, T existing) { var updatablePrimitiveProperties = EntityTypeHelper.GetUpdatablePrimitveProperties <T>( EntityTypeHelper.GetPropertiesNames(eub.PropertiesToUpdate)); foreach (var upp in updatablePrimitiveProperties) { upp.SetValue(existing, upp.GetValue(eub.Model)); } var updateableEntityProperties = EntityTypeHelper.GetUpdateableEntityProperties <T>( EntityTypeHelper.GetPropertiesNames(eub.PropertiesToUpdate)); foreach (var uep in updateableEntityProperties) { var e = (IEntity)uep.GetValue(eub.Model); if (e == null || string.IsNullOrEmpty(e.Id)) { uep.SetValue(existing, null); } else { var propRepo = _dataLayer.Repository(uep.PropertyType); var e2 = await propRepo.GetOneEntity(e.Id); uep.SetValue(existing, e2); } } }
public async Task BindRelatedEntities(T e) { var updateableEntityProperties = EntityTypeHelper.GetUpdateableEntityProperties <T>(); if (e == null) { return; } foreach (var uep in updateableEntityProperties) { var value = (IEntity)uep.GetValue(e); if (value == null || string.IsNullOrEmpty(value.Id)) { continue; } var existingEntity = await _dataLayer.Repository(uep.PropertyType).GetOneEntity(value.Id); if (existingEntity == null) { continue; } uep.SetValue(e, existingEntity); } }
/// <summary> /// Get the relationships that may have policies that are effected by the deletion of the entity. /// </summary> IEnumerable <Tuple <Relationship, Direction> > GetRelsWithPotentialPolicies(IResourceTriggerFilterPolicyCache policyCache, IEntity entity) { var forwardRelIds = EntityTypeHelper.GetAllRelationships(entity, Direction.Forward).Select(r => r.Id); var reverseRelIds = EntityTypeHelper.GetAllRelationships(entity, Direction.Reverse).Select(r => r.Id); // Prefill cache - note that we are requesting both the toType and fromType for each rel so we only need to do a single trip to the DB var allRelIds = forwardRelIds.Union(reverseRelIds).Select(r => new EntityRef(r)); BulkPreloader.Preload(new EntityRequest(allRelIds, "toType.id, fromType.id")); return(GetRelsWithPotentialPolicies(policyCache, forwardRelIds, Direction.Forward).Union(GetRelsWithPotentialPolicies(policyCache, reverseRelIds, Direction.Reverse))); }
/// <summary> /// Gets the default type of the picker report for. /// </summary> /// <param name="types">The types.</param> /// <returns></returns> internal static ResourcePicker GetDefaultPickerReportForType(IEnumerable <EntityType> types) { // Null checks. Again. if (types == null || !types.Any(t => t != null)) { return(null); } // Enumerator over type and inherited types IEnumerable <EntityType> allTypes = EntityTypeHelper.GetAllTypes(types, true); return((from e in allTypes where e.DefaultPickerReport != null select e.DefaultPickerReport).FirstOrDefault()); }
public static EntityPatchBag <T> GetEpbFor(T current, T existing) { var epb = new EntityPatchBag <T> { Id = existing.Id, Model = current, PropertiesToUpdate = new Dictionary <string, bool>() }; var updateablePrimitiveProperties = EntityTypeHelper.GetUpdatablePrimitveProperties <T>(); foreach (var upp in updateablePrimitiveProperties) { var crtValue = upp.GetValue(current); if (crtValue == null && upp.GetValue(existing) != null || crtValue != null && !crtValue.Equals(upp.GetValue(existing))) { epb.PropertiesToUpdate.Add(upp.Name, true); } } var updateableEntityProperties = EntityTypeHelper.GetUpdateableEntityProperties <T>(); foreach (var uep in updateableEntityProperties) { var crtValue = (IEntity)uep.GetValue(current); var existingValue = (IEntity)uep.GetValue(existing); if (string.IsNullOrEmpty(crtValue?.Id)) { crtValue = null; } if (crtValue == null && existingValue == null) { continue; } if (crtValue == null || existingValue == null || !string.IsNullOrEmpty(crtValue.Id) && crtValue.Id != existingValue.Id) { epb.PropertiesToUpdate.Add(uep.Name, true); } } return(epb); }
/// <summary> /// Get all the mapping columns from selected entity Type. /// </summary> public static void AddAllFields(ImportConfig importConfig) // List<ColumnInfo> mappingColumnCollection, EntityType type, DbDataTable sampleDataTable ) { EntityType type = importConfig.ImportConfigMapping.MappedType; var allFields = EntityTypeHelper.GetAllFields(type); foreach (Field field in allFields) { if (field.Name == "Alias") { continue; } ApiFieldMapping fieldMapping = new ApiFieldMapping( ); fieldMapping.Name = field.Name; fieldMapping.MappedField = field; } }
public static void ClearNullRelatedEntities(T e) { if (e == null) { return; } var updateableEntityProperties = EntityTypeHelper.GetUpdateableEntityProperties <T>(); foreach (var uep in updateableEntityProperties) { var value = (IEntity)uep.GetValue(e); if (value != null && string.IsNullOrEmpty(value.Id)) { uep.SetValue(e, null); } } }
private static void CloneRelationships(Resource resource, IEntity clone, Direction direction, IDictionary <long, IEntity> cloneTracker) { var relsToIgnore = new List <Relationship> { Resource.IsOfType_Field.As <Relationship>(), Resource.ResourceHasResourceKeyDataHashes_Field.As <Relationship>() }; var resourceRels = EntityTypeHelper.GetAllRelationships(resource, Direction.Forward); var cloneRels = EntityTypeHelper.GetAllRelationships(clone, Direction.Forward); var toCloneRels = resourceRels.Intersect(cloneRels, EntityIdComparer.Singleton).Except(relsToIgnore, EntityIdComparer.Singleton).ToList(); Entity.Get(resource, toCloneRels.ToArray <IEntityRef>()); foreach (var rel in toCloneRels) { var relationship = rel; switch (relationship.CloneAction_Enum) { case CloneActionEnum_Enumeration.CloneEntities: var entities = resource.GetRelationships(relationship, direction); // clone each entity, unless it has already been cloned (in which case substitute that one) var clones = entities.Select(entity => cloneTracker.ContainsKey(entity.Id) ? cloneTracker[entity.Id] : CloneNoDuplicates(entity.As <Resource>(), cloneTracker)).ToList(); clone.SetRelationships(relationship, new EntityRelationshipCollection <IEntity>(clones), direction); break; case CloneActionEnum_Enumeration.CloneReferences: var refs = resource.GetRelationships(relationship, direction).ToList(); clone.SetRelationships(relationship, new EntityRelationshipCollection <IEntity>(refs), direction); break; } } }
/// <summary> /// Remove argument instances that have no reverse relationships. /// </summary> /// <param name="wf"></param> private static void CleanArgumentInstanceFromActivity(Workflow wf) { var delete = new List <WfArgumentInstance>(); foreach (var argumentInstanceFromActivity in wf.ArgumentInstanceFromActivity) { var rels = EntityTypeHelper.GetAllRelationships(argumentInstanceFromActivity, Direction.Reverse).ToList(); Entity.Get(argumentInstanceFromActivity, rels.ToArray <IEntityRef>()); if (rels.Any(rel => argumentInstanceFromActivity.GetRelationships(rel, Direction.Reverse).Any(r => r != null))) { continue; } delete.Add(argumentInstanceFromActivity); } if (delete.Count > 0) { EventLog.Application.WriteWarning("Workflow has {0} dangling argument instances, deleting.", delete.Count); Entity.Delete(delete.Select(d => d.Id)); } }
/// <summary> /// Get all the mapping columns from selected entity Type. /// </summary> private static void AddAllFields(ImportConfig importConfig, SampleTable sample) // List<ColumnInfo> mappingColumnCollection, EntityType type, DbDataTable sampleDataTable ) { EntityType type = importConfig.ImportConfigMapping.MappedType; var allFields = EntityTypeHelper.GetAllFields(type); foreach (Field field in allFields) { if (field.Name == "Alias") { continue; } SampleColumn column = sample.Columns.FirstOrDefault(col => col.Name == field.Name); if (column == null) { continue; } ApiFieldMapping fieldMapping = new ApiFieldMapping( ); fieldMapping.Name = column.ColumnName; fieldMapping.MappedField = field; importConfig.ImportConfigMapping.ResourceMemberMappings.Add(fieldMapping.As <ApiMemberMapping>( )); } }
/// <summary> /// Gets the default type of the display report for. /// </summary> /// <param name="types">The types.</param> /// <returns></returns> internal static Report GetDefaultDisplayReportForType(IEnumerable <EntityType> types) { // Null checks, you would think wouldn't be necessary if (types == null || !types.Any(t => t != null)) { return(null); } // If we genuinely want to relate to resource, or editable resource, then use their default type. // But for any other type, if no default type is specified then default to the template report. (Rather than the default report for 'resource'). string alias = types.First().Alias; bool ignoreTemplateReport = alias == "core:resource" || alias == "core:userResource"; // Enumerator over type and inherited types IEnumerable <EntityType> allTypes = EntityTypeHelper.GetAllTypes(types, true); return((from e in allTypes where e.DefaultDisplayReport != null && (e.Alias == null || ignoreTemplateReport || (e.Alias.Replace("core:", "") != "userResource" && e.Alias.Replace("core:", "") != "resource")) select e.DefaultDisplayReport).FirstOrDefault()); }
protected virtual async Task ExecuteTruncateAsync(IEntityType entityType, CancellationToken cancellationToken) { _ = await _dbContext.Database.ExecuteSqlCommandAsync( new RawSqlString($"TRUNCATE TABLE {EntityTypeHelper.GetFullTableName(entityType)}"), cancellationToken) .ConfigureAwait(false); }
public static string GetFullTableName <TEntity>(this IModel model) where TEntity : class { return(EntityTypeHelper.GetFullTableName(model?.GetEntityType <TEntity>())); }