private void DeleteDocument(TableMapping mapping, IContentService service, IContent document)
        {
            // Look for a document that already exists
            var keyProp = mapping.FieldMappings.SingleOrDefault(n => n.Key);

            int key;
            bool found = int.TryParse(document.Properties[keyProp.Name].Value.ToString(), out key);
            if (found)
            {
                using (var context = ContextFunc())
                {
                    dynamic dbset = GetPropertyValueByName(context, mapping.EntityPropertyName);
                    dynamic entity = dbset.Find(key);

                    if (mapping.FieldMappings.Any(n => n.IsEnabledColumn))
                    {
                        SetValue(entity, false, mapping.FieldMappings.Single(n => n.IsEnabledColumn).Name);
                    }
                    else
                        dbset.Remove(entity);

                    context.SaveChanges();
                }
            }
        }
        private void SyncDocument(TableMapping mapping, IContentService service, IContent document, dynamic entity)
        {
            var completed = new List<string>();

            // Try apply all values from the document to the entity (Explicit, from Map)
            foreach (var prop in mapping.FieldMappings)
            {
                completed.Add(prop.Alias);
                if (!prop.Key)
                {
                    object value = GetDocumentValue(document, prop.Alias);
                    if (value != null)
                    {
                        SetValue(entity, value, prop.Name);
                        continue;
                    }

                    // Default Value
                    if (prop.DefaultValue != null)
                    {
                        if (prop.FieldType == DataType.String)
                        {
                            if (string.IsNullOrWhiteSpace(GetPropertyValueByName(entity, prop.Name)))
                                SetValue(entity, prop.DefaultValue, prop.Name);
                        }
                        else if (prop.FieldType == DataType.Integer)
                        {
                            if (0 == GetPropertyValueByName(entity, prop.Name) || GetPropertyValueByName(entity, prop.Name) == null)
                                SetValue(entity, prop.DefaultValue, prop.Name);
                        }
                        else if (prop.FieldType == DataType.DateTime)
                        {
                            if (DateTime.MinValue == GetPropertyValueByName(entity, prop.Name) || GetPropertyValueByName(entity, prop.Name) == null)
                                SetValue(entity, prop.DefaultValue, prop.Name);
                        }
                        else if (prop.FieldType == DataType.Boolean)
                        {
                            SetValue(entity, prop.DefaultValue, prop.Name);
                        }
                        else if (prop.FieldType == DataType.DateTimeStamp)
                        {
                            SetValue(entity, DateTime.Now, prop.Name);
                        }
                    }

                    // Inherited?
                    if (prop.Inherit)
                    {
                        if (prop.Alias == "UserID" && Membership.GetUser() != null)
                            SetValue(entity, Membership.GetUser().ProviderUserKey, prop.Name);
                        else
                        {
                            value = SearchAncestorForProperty(document, prop.Alias);
                            if (value != null)
                                SetValue(entity, value, prop.Name);
                        }
                    }
                }
            }

            // Try apply all values from the document to the entity (Implicit / Auto Mapped)
            if (mapping.AutoMapFields)
                foreach (var prop in document.Properties)
                {
                    try
                    {
                        if (!completed.Contains(prop.Alias) && ObjectHasProperty(entity, prop.Alias))
                        {
                            var objType = GetPropertyTypeByName(entity, prop.Alias);
                            if (objType != null && (objType.IsValueType || objType == typeof(string)))
                                SetValue(entity, prop.Value, prop.Alias);
                        }
                    }
                    catch (NullReferenceException) { /* no way to check for a document property without an exception */ }
                }
        }
        private void SaveDocument(TableMapping mapping, IContentService service, IContent document)
        {
            // Look for a document that already exists
            var keyProp = mapping.FieldMappings.SingleOrDefault(n => n.Key);

            int key;
            bool updating = int.TryParse(GetDocumentValue(document, keyProp.Alias), out key);
            using (var context = ContextFunc())
            {
                dynamic entity;
                dynamic dbset = GetPropertyValueByName(context, mapping.EntityPropertyName);

                if (!updating)
                {

                    entity = CreateObjectFromTypeName(mapping.Assembly, mapping.EntityTypeFullName);
                    dbset.Add(entity);
                }
                else
                    entity = dbset.Find(key);

                // Apply the mappings!
                SyncDocument(mapping, service, document, entity);
                context.SaveChanges();

                // Set the key
                if (!updating)
                {
                    SetDocumentValue(document, keyProp.Alias, GetPropertyValueByName(entity, keyProp.Name));
                }
            }
        }
        protected void ProcessFieldMapping(TableMapping tableMap, XElement field)
        {
            var fieldMap = new FieldMapping { Table = tableMap };

            MapValue(field, "name", n => fieldMap.Name = n);
            MapValue(field, "alias", n => fieldMap.Alias = n);
            MapValue(field, "key", n => fieldMap.Key = bool.Parse(n));
            MapValue(field, "isEnabledColumn", n => fieldMap.IsEnabledColumn = bool.Parse(n));
            MapValue(field, "inherit", n => fieldMap.Inherit = bool.Parse(n));
            MapValue(field, "dataType", n => fieldMap.FieldType = (DataType)Enum.Parse(typeof(DataType), n, true));

            MapValue(field, "defaultValue", n =>
            {
                fieldMap.DefaultValue = MapStringToValue(fieldMap.FieldType, n);
            });

            // Normalization
            if (string.IsNullOrWhiteSpace(fieldMap.Alias))
                fieldMap.Alias = fieldMap.Name;

            tableMap.FieldMappings.Add(fieldMap);
        }
        protected void ProcessTableMapping(XElement table, string defaultNamespace)
        {
            var tableMap = new TableMapping();
            if (table.Attribute("name") == null)
                throw new ArgumentException("Table attribute 'name' is required!");

            tableMap.Name = table.Attribute("name").Value;
            MapValue(table, "documentType", n => tableMap.DocumentType = n);
            MapValue(table, "entityTypeName", n => tableMap.EntityTypeFullName = n);
            MapValue(table, "autoMap", n => tableMap.AutoMapFields = bool.Parse(n));
            MapValue(table, "allowDelete", n => tableMap.AllowDelete = bool.Parse(n));
            MapValue(table, "entityPropertyName", n => tableMap.EntityPropertyName = n);
            MapValue(table, "namespace", n => tableMap.Namespace = n);
            MapValue(table, "assemlby", n => tableMap.Assembly = n);

            if (string.IsNullOrWhiteSpace(tableMap.Namespace))
                tableMap.Namespace = defaultNamespace;

            if (string.IsNullOrWhiteSpace(tableMap.Assembly))
                tableMap.Assembly = Assembly;

            // Normalization
            if (string.IsNullOrWhiteSpace(tableMap.EntityPropertyName))
                tableMap.EntityPropertyName = tableMap.Name;

            tableMap.EntityTypeFullName = tableMap.Namespace + "." + (string.IsNullOrWhiteSpace(tableMap.EntityTypeFullName) ?
                tableMap.Name : tableMap.EntityTypeFullName);

            // Children
            foreach (var field in table.Descendants())
            {
                ProcessFieldMapping(tableMap, field);
            }
            Mappings.Add(tableMap);
        }
Пример #6
0
        private void SyncDocument(TableMapping mapping, IContentService service, IContent document, dynamic entity)
        {
            var completed = new List <string>();

            // Try apply all values from the document to the entity (Explicit, from Map)
            foreach (var prop in mapping.FieldMappings)
            {
                completed.Add(prop.Alias);
                if (prop.Key)
                {
                    continue;
                }

                object value = GetDocumentValue(document, prop.Alias);
                if (value != null)
                {
                    SetValue(entity, value, prop.Name);
                    continue;
                }

                // Default Value
                if (prop.DefaultValue != null)
                {
                    if (prop.FieldType == DataType.String)
                    {
                        if (string.IsNullOrWhiteSpace(GetPropertyValueByName(entity, prop.Name)))
                        {
                            SetValue(entity, prop.DefaultValue, prop.Name);
                        }
                    }
                    else if (prop.FieldType == DataType.Integer)
                    {
                        if (0 == GetPropertyValueByName(entity, prop.Name) || GetPropertyValueByName(entity, prop.Name) == null)
                        {
                            SetValue(entity, prop.DefaultValue, prop.Name);
                        }
                    }
                    else if (prop.FieldType == DataType.DateTime)
                    {
                        if (DateTime.MinValue == GetPropertyValueByName(entity, prop.Name) || GetPropertyValueByName(entity, prop.Name) == null)
                        {
                            SetValue(entity, prop.DefaultValue, prop.Name);
                        }
                    }
                    else if (prop.FieldType == DataType.Boolean)
                    {
                        SetValue(entity, prop.DefaultValue, prop.Name);
                    }
                    else if (prop.FieldType == DataType.DateTimeStamp)
                    {
                        SetValue(entity, DateTime.Now, prop.Name);
                    }
                }

                // Inherited?
                if (prop.Inherit)
                {
                    if (prop.Alias == "UserID" && Membership.GetUser() != null)
                    {
                        SetValue(entity, Membership.GetUser().ProviderUserKey, prop.Name);
                    }
                    else
                    {
                        value = SearchAncestorForProperty(document, prop.Alias);
                        if (value != null)
                        {
                            SetValue(entity, value, prop.Name);
                        }
                    }
                }
            }

            // Try apply all values from the document to the entity (Implicit / Auto Mapped)
            if (mapping.AutoMapFields)
            {
                foreach (var prop in document.Properties)
                {
                    try
                    {
                        if (!completed.Contains(prop.Alias) && ObjectHasProperty(entity, prop.Alias))
                        {
                            var objType = GetPropertyTypeByName(entity, prop.Alias);
                            if (objType != null && (objType.IsValueType || objType == typeof(string)))
                            {
                                SetValue(entity, prop.Value, prop.Alias);
                            }
                        }
                    }
                    catch (NullReferenceException) { /* no way to check for a document property without an exception */ }
                }
            }
        }