/// <summary>
        /// Gets the fields of a specific item version.
        /// </summary>
        /// <param name="itemDefinition">The item.</param>
        /// <param name="versionUri">The version URI.</param>
        /// <param name="context">The context.</param>
        /// <returns>Sitecore.Data.FieldList.</returns>
        public override FieldList GetItemFields(ItemDefinition itemDefinition, VersionUri versionUri, CallContext context)
        {
            if (!_setupComplete)
            {
                return(base.GetItemFields(itemDefinition, versionUri, context));
            }

            var fields = new FieldList();

            var sectionInfo = SectionTable.FirstOrDefault(x => x.SectionId == itemDefinition.ID);

            if (sectionInfo != null)
            {
                GetStandardFields(fields,
                                  sectionInfo.SectionSortOrder >= 0
                        ? sectionInfo.SectionSortOrder
                        : (SectionTable.IndexOf(sectionInfo) + 100));

                return(fields);
            }

            var fieldInfo = FieldTable.FirstOrDefault(x => x.FieldId == itemDefinition.ID);

            if (fieldInfo != null)
            {
                GetStandardFields(fields,
                                  fieldInfo.FieldSortOrder >= 0 ? fieldInfo.FieldSortOrder : (FieldTable.IndexOf(fieldInfo) + 100));
                GetFieldFields(fieldInfo, fields);
                return(fields);
            }

            return(base.GetItemFields(itemDefinition, versionUri, context));
        }
        /// <summary>
        /// Gets the child I ds section.
        /// </summary>
        /// <param name="section">The section.</param>
        /// <param name="context">The context.</param>
        /// <param name="sqlProvider">The SQL provider.</param>
        /// <returns>
        /// IDList.
        /// </returns>
        private IDList GetChildIDsSection(SectionInfo section, CallContext context, DataProvider sqlProvider)
        {
            var cls      = TypeConfigurations.First(x => x.Value.TemplateId == section.TemplateId).Value;
            var fields   = cls.Properties.OfType <SitecoreFieldConfiguration>();
            var fieldIds = new IDList();

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

                if (field.CodeFirst && field.SectionName == section.Name && !ID.IsNullOrEmpty(field.FieldId))
                {
                    var record = FieldTable.FirstOrDefault(x => x.FieldId == field.FieldId);
                    //test if the fields exists in the database: if so, we're using codefirst now, so remove it.
                    var existing = sqlProvider.GetItemDefinition(field.FieldId, context);
                    if (existing != null)
                    {
                        using (new SecurityDisabler())
                            sqlProvider.DeleteItem(existing, context);
                    }

                    if (record == null)
                    {
                        string fieldName = field.FieldName.IsNullOrEmpty() ? field.PropertyInfo.Name : field.FieldName;

                        record = new FieldInfo(field.FieldId, section.SectionId, fieldName, field.FieldType, field.CustomFieldType,
                                               field.FieldSource, field.FieldTitle, field.IsShared, field.IsUnversioned,
                                               field.FieldSortOrder, field.ValidationRegularExpression,
                                               field.ValidationErrorText, field.IsRequired);

                        if (field.FieldValueConfigs != null && field.FieldValueConfigs.Any())
                        {
                            foreach (var ffv in field.FieldValueConfigs)
                            {
                                record.FieldFieldValues.Add(ffv.FieldId, ffv.FieldValue);
                            }
                        }
                    }

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

            return(fieldIds);
        }
        /// <summary>
        /// Gets the parent ID of an item.
        /// </summary>
        /// <param name="itemDefinition">The item definition.</param>
        /// <param name="context">The context.</param>
        /// <returns>Sitecore.Data.ID.</returns>
        public override ID GetParentID(ItemDefinition itemDefinition, CallContext context)
        {
            if (!_setupComplete)
            {
                return(base.GetParentID(itemDefinition, context));
            }

            var section = SectionTable.FirstOrDefault(x => x.SectionId == itemDefinition.ID);

            if (section != null)
            {
                return(section.TemplateId);
            }

            var field = FieldTable.FirstOrDefault(x => x.FieldId == itemDefinition.ID);

            if (field != null)
            {
                return(field.SectionId);
            }

            return(base.GetParentID(itemDefinition, context));
        }
        /// <summary>
        /// Gets the definition of an item.
        /// </summary>
        /// <param name="itemId">The item ID.</param>
        /// <param name="context">The context.</param>
        /// <returns>Sitecore.Data.ItemDefinition.</returns>
        public override ItemDefinition GetItemDefinition(ID itemId, CallContext context)
        {
            if (!_setupComplete)
            {
                return(base.GetItemDefinition(itemId, context));
            }

            var section = SectionTable.FirstOrDefault(x => x.SectionId == itemId);

            if (section != null)
            {
                return(new ItemDefinition(itemId, section.Name, SectionTemplateId, ID.Null));
            }

            var field = FieldTable.FirstOrDefault(x => x.FieldId == itemId);

            if (field != null)
            {
                return(new ItemDefinition(itemId, field.Name, FieldTemplateId, ID.Null));
            }

            return(base.GetItemDefinition(itemId, context));
        }
        /// <summary>
        /// Gets the child I ds section.
        /// </summary>
        /// <param name="section">The section.</param>
        /// <param name="context">The context.</param>
        /// <param name="sqlProvider">The SQL provider.</param>
        /// <returns>
        /// IDList.
        /// </returns>
        private IDList GetChildIDsSection(SectionInfo section, CallContext context, DataProvider sqlProvider)
        {
            var config = TypeConfigurations.First(x => x.Value.TemplateId == section.TemplateId);
            var cls    = config.Value;

            var fields = cls.Properties.OfType <SitecoreFieldConfiguration>();

            IDList fieldIds = new IDList();

            var interfaces = cls.Type.GetInterfaces();

            foreach (var field in fields)
            {
                //fix: added check on interfaces, if field resides on interface then skip here
                var propertyFromInterface = interfaces.FirstOrDefault(inter => inter.GetProperty(field.PropertyInfo.Name) != null &&
                                                                      inter.GetProperty(field.PropertyInfo.Name).GetCustomAttributes(typeof(SitecoreFieldAttribute), false).Any());
                if (field.PropertyInfo.DeclaringType != cls.Type || propertyFromInterface != null)
                {
                    continue;
                }

                if (field.CodeFirst && field.SectionName == section.Name && !ID.IsNullOrEmpty(field.FieldId))
                {
                    var record = FieldTable.FirstOrDefault(x => x.FieldId == field.FieldId);
                    //test if the fields exists in the database: if so, we're using codefirst now, so remove it.
                    var existing = sqlProvider.GetItemDefinition(field.FieldId, context);
                    if (existing != null)
                    {
                        using (new SecurityDisabler())
                        {
                            if (DisableItemHandlerWhenDeletingFields)
                            {
                                using (new DisableItemHandler())
                                    sqlProvider.DeleteItem(existing, context);
                            }
                            else
                            {
                                sqlProvider.DeleteItem(existing, context);
                            }
                        }
                    }

                    if (record == null)
                    {
                        string fieldName = field.FieldName.IsNullOrEmpty() ? field.PropertyInfo.Name : field.FieldName;

                        record = new FieldInfo(field.FieldId, section.SectionId, fieldName, field.FieldType, field.CustomFieldType,
                                               field.FieldSource, field.FieldTitle, field.IsShared, field.IsUnversioned,
                                               field.FieldSortOrder, field.ValidationRegularExpression,
                                               field.ValidationErrorText, field.IsRequired);

                        if (field.FieldValueConfigs != null && field.FieldValueConfigs.Any())
                        {
                            foreach (var ffv in field.FieldValueConfigs)
                            {
                                record.FieldFieldValues.Add(ffv.FieldId, ffv.FieldValue);
                            }
                        }
                    }

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

            return(fieldIds);
        }