private static void AssertOpportunityProductHasUoM(Entity entity, DelayedException exception) { if (entity.GetAttributeValue <EntityReference>(OpportunityProduct.Fields.UoMId) == null) { exception.Exception = CrmExceptions.GetFaultException(ErrorCodes.MissingUomId); } }
private static void AssertIncidentHasCustomer(Entity entity, DelayedException exception) { if (entity.GetAttributeValue <EntityReference>(Incident.Fields.CustomerId) == null) { exception.Exception = CrmExceptions.GetFaultException(ErrorCodes.unManagedidsincidentparentaccountandparentcontactnotpresent); } }
public static void Update <T>(LocalCrmDatabaseOrganizationService service, T entity) where T : Entity { var delay = new DelayedException(); Update(service, entity, delay); if (delay.Exception != null) { throw delay.Exception; } }
public static void Delete <T>(LocalCrmDatabaseOrganizationService service, Guid id) where T : Entity { var delay = new DelayedException(); Delete <T>(service, id, delay); if (delay.Exception != null) { throw delay.Exception; } }
public static EntityCollection ReadEntitiesByAttribute <T>(LocalCrmDatabaseOrganizationService service, QueryByAttribute query) where T : Entity { var delay = new DelayedException(); var result = ReadEntitiesByAttribute <T>(service, query, delay); if (delay.Exception != null) { throw delay.Exception; } return(result); }
public static T Read <T>(LocalCrmDatabaseOrganizationService service, Guid id, ColumnSet cs) where T : Entity { var delay = new DelayedException(); var result = Read <T>(service, id, cs, delay); if (delay.Exception != null) { throw delay.Exception; } return(result); }
public static Guid Create <T>(LocalCrmDatabaseOrganizationService service, T entity) where T : Entity { var delay = new DelayedException(); var id = Create(service, entity, delay); if (delay.Exception != null) { throw delay.Exception; } return(id); }
private static bool SimulateCrmCreateActionPrevention <T>(T entity, DelayedException exception) where T : Entity { switch (entity.LogicalName) { case Incident.EntityLogicalName: AssertIncidentHasCustomer(entity, exception); break; case OpportunityProduct.EntityLogicalName: AssertOpportunityProductHasUoM(entity, exception); break; } return(exception.Exception != null); }
private static bool AssertValidQueryByAttribute(QueryByAttribute query, DelayedException delay) { if (!query.Attributes.Any()) { delay.Exception = CrmExceptions.GetFaultException(ErrorCodes.QueryBuilderByAttributeNonEmpty); return(true); } if (query.Attributes.Count != query.Values.Count) { delay.Exception = CrmExceptions.GetFaultException(ErrorCodes.QueryBuilderByAttributeMismatch); return(true); } return(false); }
private static Guid Create <T>(LocalCrmDatabaseOrganizationService service, T entity, DelayedException exception) where T : Entity { // Clone entity so no changes will affect actual entity entity = entity.Serialize().DeserializeEntity <T>(); AssertTypeContainsColumns <T>(entity.Attributes.Keys); AssertEntityReferencesExists(service, entity); SimulateCrmAttributeManipulations(entity); if (SimulateCrmCreateActionPrevention(entity, exception)) { return(Guid.Empty); } var table = SchemaGetOrCreate <T>(service.Info); service.PopulateAutoPopulatedAttributes(entity, true); // Clear non Attribute Related Values entity.FormattedValues.Clear(); entity.KeyAttributes.Clear(); //var relatedEntities = entity.RelatedEntities.ToList(); entity.RelatedEntities.Clear(); if (entity.Id == Guid.Empty) { entity.Id = Guid.NewGuid(); } table.Insert(entity); CreateActivityPointer(service, entity); return(entity.Id); }
private static bool SimulateCrmCreateActionPrevention <T>(LocalCrmDatabaseOrganizationService service, T entity, DelayedException exception) where T : Entity { switch (entity.LogicalName) { case Incident.EntityLogicalName: AssertIncidentHasCustomer(entity, exception); break; case OpportunityProduct.EntityLogicalName: AssertOpportunityProductHasUoM(entity, exception); break; case Connection.EntityLogicalName: AssertConnectionRolesArePopulated(entity, false, exception); AssertConnectionRolesAreAssociated(service, entity, false, exception); break; } return(exception.Exception != null); }
private static T Read <T>(LocalCrmDatabaseOrganizationService service, Guid id, ColumnSet cs, DelayedException exception) where T : Entity { var query = SchemaGetOrCreate <T>(service.Info).Where("Id == @0", id); var entity = query.FirstOrDefault(); if (entity == null) { entity = Activator.CreateInstance <T>(); entity.Id = id; exception.Exception = CrmExceptions.GetEntityDoesNotExistException(entity); return(null); } return(ProcessEntityForReturn(service, cs, entity, false)); }
private static EntityCollection ReadEntitiesByAttribute <T>(LocalCrmDatabaseOrganizationService service, QueryByAttribute query, DelayedException delay) where T : Entity { if (AssertValidQueryByAttribute(query, delay)) { return(null); } var qe = new QueryExpression(query.EntityName) { ColumnSet = query.ColumnSet, PageInfo = query.PageInfo, TopCount = query.TopCount, }; qe.Orders.AddRange(query.Orders); for (var i = 0; i < query.Attributes.Count; i++) { qe.WhereEqual(query.Attributes[i], query.Values[i]); } return(ReadEntities <T>(service, qe)); }
private static Guid AssociateN2N <TOne, TTwo>(LocalCrmDatabaseOrganizationService service, EntityReference one, EntityReference two, Relationship relationship, DelayedException exception) where TOne : Entity where TTwo : Entity { var associationEntity = Activator.CreateInstance <N2NAssociation <TOne, TTwo> >(); N2NAssociation <TOne, TTwo> .EntityLogicalName = relationship.SchemaName; associationEntity.LogicalName = N2NAssociation <TOne, TTwo> .EntityLogicalName; associationEntity.One = one; associationEntity.Two = two; _n2NAssociations.TryAdd(relationship.SchemaName, AssociationInfo.Create(typeof(N2NAssociation <TOne, TTwo>), associationEntity)); return(Create(service, associationEntity, exception)); }
private static void Update <T>(LocalCrmDatabaseOrganizationService service, T entity, DelayedException exception) where T : Entity { AssertTypeContainsColumns <T>(entity.Attributes.Keys); AssertEntityReferencesExists(service, entity); SimulateCrmAttributeManipulations(entity); if (SimulateCrmUpdateActionPrevention(service, entity, exception)) { return; } var schema = SchemaGetOrCreate <T>(service.Info); // Get the Entity From the database var databaseValue = schema.FirstOrDefault(e => e.Id == entity.Id); if (databaseValue == null) { exception.Exception = CrmExceptions.GetEntityDoesNotExistException(entity); return; } // Clone Entity attributes so updating a non-primative attribute type does not cause changes to the database value entity = entity.Serialize().DeserializeEntity <T>(); // Update all of the attributes from the entity passed in, to the database entity foreach (var attribute in entity.Attributes) { databaseValue[attribute.Key] = attribute.Value; } // Set all Auto populated values PopulateAutoPopulatedAttributes(service, databaseValue, false); schema.Update(databaseValue); UpdateActivityPointer(service, databaseValue); CreateActivityParties(service, entity); SetCachePrimaryName(service, schema.FirstOrDefault(e => e.Id == entity.Id)); }
private static void AssertConnectionRolesAreAssociated(LocalCrmDatabaseOrganizationService service, Entity entity, bool isUpdate, DelayedException exception) { var role1 = entity.GetAttributeValue <EntityReference>(Connection.Fields.Record1RoleId); var role2 = entity.GetAttributeValue <EntityReference>(Connection.Fields.Record2RoleId); if (isUpdate) { if (role1 == null && role2 == null) { // Role never got set, exit return; } if (role1 == null || role2 == null) { // One is null, attempt to populate it var dbVersion = service.Retrieve(entity.LogicalName, entity.Id, new ColumnSet(true)); var dbRole1 = dbVersion.GetAttributeValue <EntityReference>(Connection.Fields.Record1RoleId); var dbRole2 = dbVersion.GetAttributeValue <EntityReference>(Connection.Fields.Record2RoleId); if (role1 == null) { role1 = role2.NullSafeEquals(dbRole1) ? dbRole2 : dbRole1; } else { role2 = role1.NullSafeEquals(dbRole2) ? dbRole1 : dbRole2; } } } if (role1 == null || role2 == null) { return; } var qe = new QueryExpression { ColumnSet = new ColumnSet(true), EntityName = ConnectionRoleAssociation.EntityLogicalName }; qe.First().WhereEqual( ConnectionRoleAssociation.Fields.ConnectionRoleId, role1.Id, ConnectionRoleAssociation.Fields.AssociatedConnectionRoleId, role2.Id, LogicalOperator.Or, ConnectionRoleAssociation.Fields.ConnectionRoleId, role2.Id, ConnectionRoleAssociation.Fields.AssociatedConnectionRoleId, role1.Id); if (!service.RetrieveMultiple(qe).Entities.Any()) { exception.Exception = CrmExceptions.GetFaultException(ErrorCodes.UnrelatedConnectionRoles); } }
private static void DisassociateN2N <TOne, TTwo>(LocalCrmDatabaseOrganizationService service, EntityReference one, EntityReference two, Relationship relationship, DelayedException exception) where TOne : Entity where TTwo : Entity { if (string.IsNullOrEmpty(N2NAssociation <TOne, TTwo> .EntityLogicalName)) { N2NAssociation <TOne, TTwo> .EntityLogicalName = relationship.SchemaName; } var table = SchemaGetOrCreate <N2NAssociation <TOne, TTwo> >(service.Info); var entity = table.First(e => (e.One.Equals(two) && e.Two.Equals(one) || e.One.Equals(one) && e.Two.Equals(two))); table.Delete(entity); }
private static void Update <T>(LocalCrmDatabaseOrganizationService service, T entity, DelayedException exception) where T : Entity { AssertTypeContainsColumns <T>(entity.Attributes.Keys); AssertEntityReferencesExists(service, entity); SimulateCrmAttributeManipulations(entity); if (SimulateCrmUpdateActionPrevention(service, entity, exception)) { return; } // Get the Entity From the database var databaseValue = SchemaGetOrCreate <T>(service.Info).FirstOrDefault(e => e.Id == entity.Id); if (databaseValue == null) { exception.Exception = CrmExceptions.GetEntityDoesNotExistException(entity); return; } // Update all of the attributes from the entity passed in, to the database entity foreach (var attribute in entity.Attributes) { databaseValue[attribute.Key] = attribute.Value; } // Set all Autopopulated values service.PopulateAutoPopulatedAttributes(databaseValue, false); SchemaGetOrCreate <T>(service.Info).Update(databaseValue); UpdateActivityPointer(service, databaseValue); }
private static void AssertConnectionRolesArePopulated(Entity entity, bool isUpdate, DelayedException exception) { var record1Null = (entity.GetAttributeValue <EntityReference>(Connection.Fields.Record1RoleId) ?? entity.GetAttributeValue <EntityReference>(Connection.Fields.Record1Id)) == null; var record2Null = (entity.GetAttributeValue <EntityReference>(Connection.Fields.Record2RoleId) ?? entity.GetAttributeValue <EntityReference>(Connection.Fields.Record2Id)) == null; var aConnectionIsMissing = record1Null || record2Null; if (isUpdate) { var containsRecord1 = entity.Contains(Connection.Fields.Record1Id) || entity.Contains(Connection.Fields.Record1RoleId); var containsRecord2 = entity.Contains(Connection.Fields.Record2Id) || entity.Contains(Connection.Fields.Record2RoleId); aConnectionIsMissing = containsRecord1 && record1Null || containsRecord2 && record2Null; } if (aConnectionIsMissing) { exception.Exception = CrmExceptions.GetFaultException(ErrorCodes.BothConnectionSidesAreNeeded); } }
private static T Read <T>(LocalCrmDatabaseOrganizationService service, Guid id, ColumnSet cs, DelayedException exception) where T : Entity { var query = SchemaGetOrCreate <T>(service.Info). Where("Id == @0", id); var entity = query.FirstOrDefault(); if (entity == null) { entity = Activator.CreateInstance <T>(); entity.Id = id; exception.Exception = CrmExceptions.GetEntityDoesNotExistException(entity); return(null); } if (!cs.AllColumns) { foreach (var key in entity.Attributes.Keys.Where(k => !cs.Columns.Contains(k)).ToList()) { entity.Attributes.Remove(key); } } service.RemoveFieldsCrmDoesNotReturn(entity); PopulateFormattedValues <T>(entity); return(entity); }
/// <summary> /// Simulates CRM update action preventions. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="service">The service.</param> /// <param name="entity">The entity.</param> /// <param name="exception">The exception.</param> /// <returns></returns> private static bool SimulateCrmUpdateActionPrevention <T>(LocalCrmDatabaseOrganizationService service, T entity, DelayedException exception) where T : Entity { switch (entity.LogicalName) { case Incident.EntityLogicalName: #if Xrm2015 break; #else if (service.CurrentRequestName != new CloseIncidentRequest().RequestName&& entity.GetAttributeValue <OptionSetValue>(Incident.Fields.StateCode).GetValueOrDefault() == (int)IncidentState.Resolved) { // Not executing as a part of a CloseIncidentRequest. Disallow updating the State Code to Resolved. exception.Exception = CrmExceptions.GetFaultException(ErrorCodes.UseCloseIncidentRequest); } break; #endif case Connection.EntityLogicalName: AssertConnectionRolesArePopulated(entity, true, exception); AssertConnectionRolesAreAssociated(service, entity, true, exception); break; } return(exception.Exception != null); }
private static Guid Create <T>(LocalCrmDatabaseOrganizationService service, T entity, DelayedException exception) where T : Entity { // Clone entity so no changes will affect actual entity entity = entity.Serialize().DeserializeEntity <T>(); AssertTypeContainsColumns <T>(entity.Attributes.Keys); AssertEntityReferencesExists(service, entity); SimulateCrmAttributeManipulations(entity); if (SimulateCrmCreateActionPrevention(service, entity, exception)) { return(Guid.Empty); } var table = SchemaGetOrCreate <T>(service.Info); PopulateAutoPopulatedAttributes(service, entity, true); // Clear non Attribute Related Values entity.FormattedValues.Clear(); #if !PRE_KEYATTRIBUTE entity.KeyAttributes.Clear(); #endif //var relatedEntities = entity.RelatedEntities.ToList(); entity.RelatedEntities.Clear(); if (entity.Id == Guid.Empty) { entity.Id = Guid.NewGuid(); } try { table.Insert(entity); } catch (MultipleUniqueKeyFoundException) { throw new Exception("Cannot insert duplicate key"); } CreateActivityPointer(service, entity); CreateActivityParties(service, entity); SetCachePrimaryName(service, entity); return(entity.Id); }
private static void Delete <T>(LocalCrmDatabaseOrganizationService service, Guid id, DelayedException exception) where T : Entity { var entity = Activator.CreateInstance <T>(); entity.Id = id; if (!SchemaGetOrCreate <T>(service.Info).Any(e => e.Id == id)) { exception.Exception = CrmExceptions.GetEntityDoesNotExistException(entity); return; } SchemaGetOrCreate <T>(service.Info).Delete(entity); DeleteActivityPointer <T>(service, id); }
/// <summary> /// Simulates CRM update action preventions. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="service">The service.</param> /// <param name="entity">The entity.</param> /// <param name="exception">The exception.</param> /// <returns></returns> private static bool SimulateCrmUpdateActionPrevention <T>(LocalCrmDatabaseOrganizationService service, T entity, DelayedException exception) where T : Entity { switch (entity.LogicalName) { case Incident.EntityLogicalName: if (service.CurrentRequestName != new CloseIncidentRequest().RequestName&& entity.GetAttributeValue <OptionSetValue>(Incident.Fields.StateCode).GetValueOrDefault() == (int)IncidentState.Resolved) { // Not executing as a part of a CloseIncidentRequest. Disallow updating the State Code to Resolved. exception.Exception = GetFaultException(ErrorCodes.UseCloseIncidentRequest); return(true); } break; } return(false); }