Example #1
0
        private IDList GetChildIDsTemplate(SitecoreClassConfig template, ItemDefinition itemDefinition)
        {
            IDList fields = new IDList();

            List <string> processed = new List <string>();
            var           sections  = template.Properties
                                      .Where(x => x.Property.DeclaringType == template.Type)
                                      .Select(x => x.Attribute).OfType <SitecoreFieldAttribute>()
                                      .Select(x => x.SectionName);

            foreach (var section in sections)
            {
                if (processed.Contains(section) || section.IsNullOrEmpty())
                {
                    continue;
                }

                var record = SectionTable.FirstOrDefault(x => x.TemplateId == itemDefinition.ID && x.Name == section);

                if (record == null)
                {
                    record = new SectionInfo(section, new ID(Guid.NewGuid()), itemDefinition.ID);
                    SectionTable.Add(record);
                }
                processed.Add(section);
                fields.Add(record.SectionId);
            }
            return(fields);
        }
        private IDList GetChildIDsSection(SectionInfo section, CallContext context)
        {
            var cls = Classes.First(x => x.Value.TemplateId == section.TemplateId.Guid).Value;

            var fields = cls.Properties.Where(x => x.Attribute is SitecoreFieldAttribute);

            IDList fieldIds = new IDList();

            var providers     = Database.GetDataProviders();
            var otherProvider = providers.FirstOrDefault(x => !(x is GlassDataProvider));

            foreach (var field in fields)
            {
                if (field.Property.DeclaringType != cls.Type)
                {
                    continue;
                }


                var attr = field.Attribute as SitecoreFieldAttribute;
                if (attr != null && attr.CodeFirst && attr.SectionName == section.Name)
                {
                    Guid guidId = Guid.Empty;
                    if (Utility.GuidTryParse(attr.FieldId, out guidId))
                    {
                        var record = FieldTable.FirstOrDefault(x => x.FieldId.Guid == guidId);
                        //test if the fields exists in the database: if so, we're using codefirst now, so remove it.
                        var existing = otherProvider.GetItemDefinition(new ID(guidId), context);
                        if (existing != null)
                        {
                            using (new SecurityDisabler())
                                otherProvider.DeleteItem(existing, context);
                        }
                        if (record == null)
                        {
                            string fieldName = attr.FieldName.IsNullOrEmpty() ? field.Property.Name : attr.FieldName;


                            record = new FieldInfo(new ID(guidId), section.SectionId, fieldName, attr.FieldType, attr.FieldSource, attr.FieldTitle, attr.IsShared, attr.IsUnversioned, attr.FieldSortOrder, attr.ValidationRegularExpression, attr.ValidationErrorText, attr.IsRequired);
                            var fieldfieldInfoAttributes =
                                field.Property.GetCustomAttributes(typeof(SitecoreFieldFieldValueAttribute), true);
                            if (fieldfieldInfoAttributes != null && fieldfieldInfoAttributes.Any())
                            {
                                foreach (var ffv in fieldfieldInfoAttributes.Cast <SitecoreFieldFieldValueAttribute>())
                                {
                                    record.FieldFieldValues.Add(ffv.FieldId, ffv.FieldValue);
                                }
                            }
                        }

                        fieldIds.Add(record.FieldId);
                        FieldTable.Add(record);
                    }
                }
            }

            return(fieldIds);
        }
        private IDList GetChildIDsTemplate(SitecoreClassConfig template, ItemDefinition itemDefinition, CallContext context)
        {
            IDList fields = new IDList();

            List <string> processed = new List <string>();
            var           sections  = template.Properties
                                      .Where(x => x.Property.DeclaringType == template.Type)
                                      .Select(x => x.Attribute).OfType <SitecoreFieldAttribute>()
                                      .Select(x => new { x.SectionName, x.SectionSortOrder });

            var providers     = Database.GetDataProviders();
            var otherProvider = providers.FirstOrDefault(x => !(x is GlassDataProvider));
            //If sitecore contains a section with the same name in the database, use that one instead of creating a new one
            var existing = otherProvider.GetChildIDs(itemDefinition, context).OfType <ID>().Select(id => otherProvider.GetItemDefinition(id, context)).ToList();

            foreach (var section in sections)
            {
                if (processed.Contains(section.SectionName) || section.SectionName.IsNullOrEmpty())
                {
                    continue;
                }

                var record = SectionTable.FirstOrDefault(x => x.TemplateId == itemDefinition.ID && x.Name == section.SectionName);

                if (record == null)
                {
                    var exists = existing.FirstOrDefault(def => def.Name.Equals(section));
                    if (exists != null)
                    {
                        record = new SectionInfo(section.SectionName, exists.ID, itemDefinition.ID, section.SectionSortOrder)
                        {
                            Existing = true
                        };
                    }
                    else
                    {
                        record = new SectionInfo(section.SectionName, new ID(Guid.NewGuid()), itemDefinition.ID, section.SectionSortOrder);
                    }
                    SectionTable.Add(record);
                }
                processed.Add(section.SectionName);
                if (!record.Existing)
                {
                    fields.Add(record.SectionId);
                }
            }
            return(fields);
        }
Example #4
0
        private IDList GetChildIDsSection(ItemDefinition itemDefinition, SectionInfo section)
        {
            var cls = Classes.First(x => x.Value.TemplateId == section.TemplateId.Guid).Value;

            var fields = cls.Properties.Where(x => x.Attribute is SitecoreFieldAttribute);

            IDList fieldIds = new IDList();


            foreach (var field in fields)
            {
                if (field.Property.DeclaringType != cls.Type)
                {
                    continue;
                }

                var attr = field.Attribute as SitecoreFieldAttribute;
                if (attr != null && attr.CodeFirst && attr.SectionName == itemDefinition.Name)
                {
                    Guid guidId;
                    if (Guid.TryParse(attr.FieldId, out guidId))
                    {
                        var record = FieldTable.FirstOrDefault(x => x.FieldId.Guid == guidId);

                        if (record == null)
                        {
                            string fieldName = attr.FieldName.IsNullOrEmpty() ? field.Property.Name : attr.FieldName;
                            record = new FieldInfo(new ID(guidId), itemDefinition.ID, fieldName, attr.FieldType, attr.FieldSource, attr.FieldTitle, attr.IsShared, attr.IsUnversioned);
                        }

                        fieldIds.Add(record.FieldId);
                        FieldTable.Add(record);
                    }
                }
            }

            return(fieldIds);
        }
        private IDList GetChildIDsSection(SectionInfo section, CallContext context)
        {
            var cls = Classes.First(x => x.Value.TemplateId == section.TemplateId.Guid).Value;

            var fields = cls.Properties.Where(x=>x.Attribute is SitecoreFieldAttribute);

            IDList fieldIds = new IDList();

            var providers = Database.GetDataProviders();
            var otherProvider = providers.FirstOrDefault(x => !(x is GlassDataProvider));

            foreach (var field in fields)
            {
                if (field.Property.DeclaringType != cls.Type)
                    continue;


                var attr = field.Attribute as SitecoreFieldAttribute;
                if (attr != null && attr.CodeFirst && attr.SectionName == section.Name)
                {

                    Guid guidId = Guid.Empty;
                    if (Utility.GuidTryParse(attr.FieldId, out guidId))
                    {
                        var record = FieldTable.FirstOrDefault(x => x.FieldId.Guid == guidId);
                        //test if the fields exists in the database: if so, we're using codefirst now, so remove it.
                        var existing = otherProvider.GetItemDefinition(new ID(guidId), context);
                        if (existing != null)
                        {
                            ID nullId = null; //hmm, there is a bug in sitecore ID's equality operator
                            //ONLY delete fields belonging directly to this template!!! if the parent is not in the database OR if it is a codefirst class/section, we can safely delete it to prevent duplicates
                            var existingParent = otherProvider.GetParentID(existing, context);
                            if (existingParent == nullId || existingParent == ID.Null ||
                                this.SectionTable.Any(s => s.SectionId == existing.ID) ||
                                this.Classes.Any(c => c.Value.TemplateId == existing.ID.ToGuid()))
                            {
                                using (new SecurityDisabler())
                                    otherProvider.DeleteItem(existing, context);
                            }
                        }
                        if (record == null)
                        {
                            string fieldName = attr.FieldName.IsNullOrEmpty() ? field.Property.Name : attr.FieldName;
                           

                            record = new FieldInfo(new ID(guidId), section.SectionId, fieldName, attr.FieldType, attr.FieldSource, attr.FieldTitle, attr.IsShared, attr.IsUnversioned, attr.FieldSortOrder, attr.ValidationRegularExpression, attr.ValidationErrorText, attr.IsRequired);
                            var fieldfieldInfoAttributes = field.Property.GetCustomAttributes(typeof (SitecoreFieldFieldValueAttribute), true);
                            if (fieldfieldInfoAttributes != null && fieldfieldInfoAttributes.Any())
                            {
                                foreach (var ffv in fieldfieldInfoAttributes.Cast<SitecoreFieldFieldValueAttribute>())
                                {
                                    record.FieldFieldValues.Add(ffv.FieldId, ffv.FieldValue);
                                }
                            }
                          
                        }

                        fieldIds.Add(record.FieldId);
                        FieldTable.Add(record);
                    }
                }


            }

            return fieldIds;
        }
        private IDList GetChildIDsTemplate(SitecoreClassConfig template, ItemDefinition itemDefinition, CallContext context)
        {
            IDList fields = new IDList();

            List<string> processed = new List<string>();
            var sections = template.Properties
                .Where(x=>x.Property.DeclaringType == template.Type)
                .Select(x=>x.Attribute).OfType<SitecoreFieldAttribute>()
                .Select(x => new { x.SectionName, x.SectionSortOrder });

            var providers = Database.GetDataProviders();
            var otherProvider = providers.FirstOrDefault(x => !(x is GlassDataProvider));
            //If sitecore contains a section with the same name in the database, use that one instead of creating a new one
            var otherChildIds = otherProvider.GetChildIDs(itemDefinition, context);
            var existing = (otherChildIds ?? new IDList()).OfType<ID>().Select(id => otherProvider.GetItemDefinition(id, context)).ToList();

            foreach (var section in sections)
            {
                if (processed.Contains(section.SectionName) || section.SectionName.IsNullOrEmpty())
                    continue;

                var record = SectionTable.FirstOrDefault(x => x.TemplateId == itemDefinition.ID && x.Name == section.SectionName);

                if (record == null)
                {
                    var exists = existing.FirstOrDefault(def => def.Name.Equals(section));
                    if (exists != null)
                    {
                        record = new SectionInfo(section.SectionName, exists.ID, itemDefinition.ID, section.SectionSortOrder) { Existing = true };
                    }
                    else
                    {
                        record = new SectionInfo(section.SectionName, new ID(Guid.NewGuid()), itemDefinition.ID, section.SectionSortOrder);
                    }
                    SectionTable.Add(record);
                }
                processed.Add(section.SectionName);
                if (!record.Existing)
                    fields.Add(record.SectionId);
            }
            return fields;
        }