示例#1
0
 private void FilterContentTypeProperties(IContentTypeBase contentType, IEnumerable <string> exclude)
 {
     //remove all properties based on the exclusion list
     foreach (var remove in exclude)
     {
         if (contentType.PropertyTypeExists(remove))
         {
             contentType.RemovePropertyType(remove);
         }
     }
 }
        private void RemoveProperties(IContentTypeBase item, XElement properties)
        {
            logger.Debug(serializerType, "RemoveProperties");

            List <string> removals = new List <string>();

            var nodes = properties.Elements("GenericProperty")
                        .Where(x => x.Element("Key") != null)
                        .Select(x =>
                                new
            {
                Key   = x.Element("Key").ValueOrDefault(Guid.Empty),
                Alias = x.Element("Alias").ValueOrDefault(string.Empty)
            })
                        .ToDictionary(k => k.Key, a => a.Alias);


            foreach (var property in item.PropertyTypes)
            {
                if (nodes.ContainsKey(property.Key))
                {
                    continue;
                }
                if (nodes.Any(x => x.Value == property.Alias))
                {
                    continue;
                }
                removals.Add(property.Alias);
            }

            if (removals.Any())
            {
                foreach (var alias in removals)
                {
                    // if you remove something with lots of
                    // content this can timeout (still? - need to check on v8)
                    logger.Debug(serializerType, "Removing {0}", alias);
                    item.RemovePropertyType(alias);
                }
            }
        }
示例#3
0
        internal void DeserializeProperties(IContentTypeBase item, XElement node)
        {
            List <string> propertiesToRemove = new List <string>();
            Dictionary <string, string>        propertiesToMove = new Dictionary <string, string>();
            Dictionary <PropertyGroup, string> tabsToBlank      = new Dictionary <PropertyGroup, string>();

            var genericPropertyNode = node.Element("GenericProperties");

            if (genericPropertyNode != null)
            {
                // add or update properties
                foreach (var propertyNode in genericPropertyNode.Elements("GenericProperty"))
                {
                    bool newProperty = false;

                    var property = default(PropertyType);
                    var propKey  = propertyNode.Element("Key").ValueOrDefault(Guid.Empty);
                    if (propKey != Guid.Empty)
                    {
                        property = item.PropertyTypes.SingleOrDefault(x => x.Key == propKey);
                    }

                    var alias = propertyNode.Element("Alias").ValueOrDefault(string.Empty);

                    if (property == null)
                    {
                        // look up via alias?
                        property = item.PropertyTypes.SingleOrDefault(x => x.Alias == alias);
                    }

                    // we need to get element stuff now before we can create or update

                    var defGuid            = propertyNode.Element("Definition").ValueOrDefault(Guid.Empty);
                    var dataTypeDefinition = _dataTypeService.GetDataTypeDefinitionById(defGuid);

                    if (dataTypeDefinition == null)
                    {
                        var propEditorAlias = propertyNode.Element("Type").ValueOrDefault(string.Empty);
                        if (!string.IsNullOrEmpty(propEditorAlias))
                        {
                            dataTypeDefinition = _dataTypeService
                                                 .GetDataTypeDefinitionByPropertyEditorAlias(propEditorAlias)
                                                 .FirstOrDefault();
                        }
                    }

                    if (dataTypeDefinition == null)
                    {
                        LogHelper.Warn <Events>("Failed to get Definition for property type");
                        continue;
                    }

                    if (property == null)
                    {
                        // create the property
                        LogHelper.Debug <Events>("Creating new Property: {0} {1}", () => item.Alias, () => alias);
                        property    = new PropertyType(dataTypeDefinition, alias);
                        newProperty = true;
                    }

                    if (property != null)
                    {
                        LogHelper.Debug <Events>("Updating Property :{0} {1}", () => item.Alias, () => alias);

                        var key = propertyNode.Element("Key").ValueOrDefault(Guid.Empty);
                        if (key != Guid.Empty)
                        {
                            LogHelper.Debug <Events>("Setting Key :{0}", () => key);
                            property.Key = key;
                        }

                        LogHelper.Debug <Events>("Item Key    :{0}", () => property.Key);

                        // update settings.
                        property.Name = propertyNode.Element("Name").ValueOrDefault("unnamed" + DateTime.Now.ToString("yyyyMMdd_HHmmss"));

                        if (property.Alias != alias)
                        {
                            property.Alias = alias;
                        }

                        if (propertyNode.Element("Description") != null)
                        {
                            property.Description = propertyNode.Element("Description").Value;
                        }

                        if (propertyNode.Element("Mandatory") != null)
                        {
                            property.Mandatory = propertyNode.Element("Mandatory").Value.ToLowerInvariant().Equals("true");
                        }

                        if (propertyNode.Element("Validation") != null)
                        {
                            property.ValidationRegExp = propertyNode.Element("Validation").Value;
                        }

                        if (propertyNode.Element("SortOrder") != null)
                        {
                            property.SortOrder = int.Parse(propertyNode.Element("SortOrder").Value);
                        }

                        if (propertyNode.Element("Type") != null)
                        {
                            LogHelper.Debug <Events>("Setting Property Type : {0}", () => propertyNode.Element("Type").Value);
                            property.PropertyEditorAlias = propertyNode.Element("Type").Value;
                        }

                        if (property.DataTypeDefinitionId != dataTypeDefinition.Id)
                        {
                            property.DataTypeDefinitionId = dataTypeDefinition.Id;
                        }

                        var tabName = propertyNode.Element("Tab").ValueOrDefault(string.Empty);

                        if (_itemType == "MemberType")
                        {
                            ((IMemberType)item).SetMemberCanEditProperty(alias,
                                                                         propertyNode.Element("CanEdit").ValueOrDefault(false));

                            ((IMemberType)item).SetMemberCanViewProperty(alias,
                                                                         propertyNode.Element("CanView").ValueOrDefault(false));
                        }

                        if (!newProperty)
                        {
                            if (!string.IsNullOrEmpty(tabName))
                            {
                                var propGroup = item.PropertyGroups.FirstOrDefault(x => x.Name == tabName);
                                if (propGroup != null)
                                {
                                    if (!propGroup.PropertyTypes.Any(x => x.Alias == property.Alias))
                                    {
                                        // this tab currently doesn't contain this property, to we have to
                                        // move it (later)
                                        propertiesToMove.Add(property.Alias, tabName);
                                    }
                                }
                            }
                            else
                            {
                                // this property isn't in a tab (now!)
                                if (!newProperty)
                                {
                                    var existingTab = item.PropertyGroups.FirstOrDefault(x => x.PropertyTypes.Contains(property));
                                    if (existingTab != null)
                                    {
                                        // this item is now not in a tab (when it was)
                                        // so we have to remove it from tabs (later)
                                        tabsToBlank.Add(existingTab, property.Alias);
                                    }
                                }
                            }
                        }
                        else
                        {
                            // new propert needs to be added to content type..
                            if (string.IsNullOrEmpty(tabName))
                            {
                                item.AddPropertyType(property);
                            }
                            else
                            {
                                item.AddPropertyType(property, tabName);
                            }

                            // setting the key before here doesn't seem to work for new types.
                            if (key != Guid.Empty)
                            {
                                property.Key = key;
                            }
                        }
                    }
                } // end foreach property
            }     // end generic properties


            // look at what properties we need to remove.
            var propertyNodes = node.Elements("GenericProperties").Elements("GenericProperty");

            foreach (var property in item.PropertyTypes)
            {
                XElement propertyNode = propertyNodes
                                        .FirstOrDefault(x => x.Element("key") != null && x.Element("Key").Value == property.Key.ToString());

                if (propertyNode == null)
                {
                    LogHelper.Debug <uSync.Core.Events>("Looking up property type by alias {0}", () => property.Alias);
                    propertyNode = propertyNodes
                                   .SingleOrDefault(x => x.Element("Alias").Value == property.Alias);
                }

                if (propertyNode == null)
                {
                    propertiesToRemove.Add(property.Alias);
                }
            }


            // now we have gone through all the properties, we can do the moves and removes from the groups
            if (propertiesToMove.Any())
            {
                foreach (var move in propertiesToMove)
                {
                    LogHelper.Debug <Events>("Moving Property: {0} {1}", () => move.Key, () => move.Value);
                    item.MovePropertyType(move.Key, move.Value);
                }
            }

            if (propertiesToRemove.Any())
            {
                // removing properties can cause timeouts on installs with lots of content...
                foreach (var delete in propertiesToRemove)
                {
                    LogHelper.Debug <Events>("Removing Property: {0}", () => delete);
                    item.RemovePropertyType(delete);
                }
            }

            if (tabsToBlank.Any())
            {
                foreach (var blank in tabsToBlank)
                {
                    // there might be a bug here, we need to do some cheking of if this is
                    // possible with the public api

                    // blank.Key.PropertyTypes.Remove(blank.Value);
                }
            }
        }
        private void Prune(IContentTypeBase contentType, Type documentClrType, PropertyInfo[] tabProperties, IEnumerable<PropertyInfo> propertiesOfRoot)
        {
            bool modified = false;

            var propertiesToKeep = propertiesOfRoot.Where(x =>
            {
                var attr = x.DeclaringType.GetCodeFirstAttribute<CodeFirstCommonBaseAttribute>(false);
                return x.DeclaringType == documentClrType || attr != null;
            }).Select(x => x.GetCodeFirstAttribute<ContentPropertyAttribute>().Alias)
            .Union(tabProperties.Where(x =>
            {
                var attr = x.DeclaringType.GetCodeFirstAttribute<CodeFirstCommonBaseAttribute>(false);
                return x.DeclaringType == documentClrType || attr != null;
            }).SelectMany(x =>
            {
                return x.PropertyType.GetProperties().Where(y =>
                {
                    return (y.DeclaringType == x.PropertyType || y.DeclaringType.GetCodeFirstAttribute<CodeFirstCommonBaseAttribute>(false) != null) && y.GetCodeFirstAttribute<ContentPropertyAttribute>() != null;
                }).Select(y =>
                    {
                      var attr = y.GetCodeFirstAttribute<ContentPropertyAttribute>();
                      var tabAttr = x.GetCodeFirstAttribute<ContentTabAttribute>();
                      return attr.AddTabAliasToPropertyAlias ? attr.Alias + "_" + tabAttr.Name.Replace(" ", "_") : attr.Alias;
                    });
            })).ToList();

            propertiesToKeep.AddRange(documentClrType.GetCustomAttributes<DoNotRemovePropertyAttribute>(true).Select(x => x.PropertyAlias));

            //loop through all the properties on the ContentType to see if they should be removed.
            var existingUmbracoProperties = contentType.PropertyTypes.ToArray();
            int length = contentType.PropertyTypes.Count();
            for (int i = 0; i < length; i++)
            {
                if (!propertiesToKeep.Any(x => x.Equals(existingUmbracoProperties[i].Alias, StringComparison.InvariantCultureIgnoreCase)))
                {
                    if (contentType.PropertyTypeExists(existingUmbracoProperties[i].Alias))
                    {
                        modified = true;
                        //remove the property
                        contentType.RemovePropertyType(existingUmbracoProperties[i].Alias);
                        var alias = existingUmbracoProperties[i].Alias;
                        var children = GetChildren(contentType);

                        //TODO is this needed? I reckon it shouldn't be
                        RemovePropertyFromChildren(alias, children);
                    }
                }
            }

            if (modified)
            {
                contentType.ResetDirtyProperties(false);
                Save(contentType);
            }
        }
        /// <summary>
        /// Loop through all properties and remove existing ones if necessary
        /// </summary>
        /// <param name="contentType"></param>
        /// <param name="type"></param>
        /// <param name="dataTypeService"></param>
        private static void VerifyProperties(IContentTypeBase contentType, Type type, IDataTypeService dataTypeService)
        {
            var properties = type.GetProperties().Where(x => x.GetCustomAttribute<UmbracoTabAttribute>() != null).ToArray();
            List<string> propertiesThatShouldExist = new List<string>();

            foreach (var propertyTab in properties)
            {
                var tabAttribute = propertyTab.GetCustomAttribute<UmbracoTabAttribute>();
                if (!contentType.PropertyGroups.Any(x => x.Name == tabAttribute.Name))
                {
                    contentType.AddPropertyGroup(tabAttribute.Name);
                }

                propertiesThatShouldExist.AddRange(VerifyAllPropertiesOnTab(propertyTab, contentType, tabAttribute.Name, dataTypeService));
            }

            var propertiesOfRoot = type.GetProperties().Where(x => x.GetCustomAttribute<UmbracoPropertyAttribute>() != null);
            foreach (var item in propertiesOfRoot)
            {
                //TODO: check for correct name
                propertiesThatShouldExist.Add(VerifyExistingProperty(contentType, null, dataTypeService, item, true));
            }

            //loop through all the properties on the ContentType to see if they should be removed;
            var existingUmbracoProperties = contentType.PropertyTypes.ToArray();
            int length = contentType.PropertyTypes.Count();
            for (int i = 0; i < length; i++)
            {
                if (!propertiesThatShouldExist.Contains(existingUmbracoProperties[i].Alias))
                {
                    //remove the property
                    contentType.RemovePropertyType(existingUmbracoProperties[i].Alias);
                }
            }
        }