Example #1
0
        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);
        }
Example #2
0
        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);
        }
Example #3
0
        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);
        }
Example #4
0
        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));
        }
Example #5
0
        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));
        }