private void EmitClass(ClassGenerationContext classContext)
        {
            if (classContext.target.ConfigurationScope.HasValue)
            {
                var synonym = Call.Синоним(classContext.comObject);
                classContext.target.Synonym = GenerateHelpers.EscapeString(synonym);
                if (classContext.target.ConfigurationScope.Value != ConfigurationScope.егистрыСведений)
                {
                    var presentation = Call.StringProp(classContext.comObject, "ObjectPresentation");
                    classContext.target.ObjectPresentation = GenerateHelpers.EscapeString(presentation);
                }
            }
            var name       = classContext.configurationName;
            var attributes = MetadataHelpers.GetAttributes(classContext.comObject, classContext.descriptor);

            foreach (var attr in attributes)
            {
                var propertyName           = Call.Имя(attr);
                var propertyTypeDescriptor = GetTypeDescriptor(attr,
                                                               classContext.configurationItemFullName + "." + propertyName,
                                                               classContext.generationContext);
                if (!propertyTypeDescriptor.HasValue)
                {
                    continue;
                }
                var propertyModel = new PropertyModel
                {
                    Type         = propertyTypeDescriptor.Value.dotnetType,
                    PropertyName = propertyName,
                    MaxLength    = propertyTypeDescriptor.Value.maxLength
                };
                classContext.target.Properties.Add(propertyModel);
            }
            if (name.HasValue && name.Value.HasReference)
            {
                classContext.EmitProperty(new PropertyModel
                {
                    Type         = "Guid?",
                    PropertyName = EntityHelpers.idPropertyName
                });
            }
            if (classContext.descriptor.HasStandardTableSections)
            {
                EmitTableSections(classContext, "СтандартныеТабличныеЧасти", MetadataHelpers.standardTableSectionDescriptor, false);
            }
            if (classContext.descriptor.HasTableSections)
            {
                EmitTableSections(classContext, "ТабличныеЧасти", MetadataHelpers.tableSectionDescriptor, true);
            }
        }
        private UnionLayout CreateRegistratorLayout()
        {
            //У РегистрыБухгалтерии.Хозрасчетный.Субконто нельзя получить метаданные
            //globalContext.FindMetaByName - кидает исключение
            //Поэтому мы берём метаданные по Регистратору из РегистрыБухгалтерии.Хозрасчетный
            //Т.к. они скорее всего имеют одинаковые метаданные. В силу того, что Региcтратор
            //является частью составного ключа, по которым Субконто матчатся с проводками
            var configurationName   = new ConfigurationName(ConfigurationScope.егистрыБухгалтерии, "Хозрасчетный");
            var configurationItem   = globalContext.FindMetaByName(configurationName);
            var comObject           = configurationItem.ComObject;
            var descriptor          = MetadataHelpers.GetDescriptorOrNull(configurationName.Scope);
            var propertyDescriptors = comObject == null
                ? new Dictionary <string, string[]>()
                : MetadataHelpers.GetAttributes(comObject, descriptor)
                                      .ToDictionary(Call.Имя, GetConfigurationTypes);
            var propertyTypes = propertyDescriptors.GetOrDefault("Регистратор");

            return(new UnionLayout(null, "_RecorderTRef", "_RecorderRRef", propertyTypes));
        }
        private TableMapping CreateDefaultTableMapping(ValueTableRow tableRow,
                                                       Dictionary <string, TableMapping> tableMappingByQueryName)
        {
            var queryTableName = tableRow.GetString("ИмяТаблицы");

            if (string.IsNullOrEmpty(queryTableName))
            {
                return(null);
            }
            var dbTableName = tableRow.GetString("ИмяТаблицыХранения");

            if (string.IsNullOrEmpty(dbTableName))
            {
                return(null);
            }
            var purpose = tableRow.GetString("Назначение");
            ConfigurationItemDescriptor descriptor;
            object    comObject;
            TableType tableType;

            if (purpose == "Основная" || purpose == "Константа")
            {
                var configurationName = ConfigurationName.ParseOrNull(queryTableName);
                if (!configurationName.HasValue)
                {
                    return(null);
                }
                tableType  = TableType.Main;
                descriptor = MetadataHelpers.GetDescriptorOrNull(configurationName.Value.Scope);
                if (descriptor == null)
                {
                    comObject = null;
                }
                else
                {
                    var configurationItem = globalContext.FindMetaByName(configurationName.Value);
                    comObject = configurationItem.ComObject;
                }
            }
            else if (purpose == "ТабличнаяЧасть")
            {
                descriptor = MetadataHelpers.tableSectionDescriptor;
                var fullname = TableSectionQueryNameToFullName(queryTableName);
                comObject = ComHelpers.Invoke(globalContext.Metadata, "НайтиПоПолномуИмени", fullname);
                if (comObject == null)
                {
                    return(null);
                }
                tableType = TableType.TableSection;
            }
            else
            {
                return(null);
            }
            var propertyDescriptors = comObject == null
                ? new Dictionary <string, string[]>()
                : MetadataHelpers.GetAttributes(comObject, descriptor)
                                      .ToDictionary(Call.Имя, GetConfigurationTypes);
            var propertyMappings = new ValueTable(tableRow["Поля"])
                                   .Select(x => new
            {
                queryName = x.GetString("ИмяПоля"),
                dbName    = x.GetString("ИмяПоляХранения")
            })
                                   .Where(x => !string.IsNullOrEmpty(x.queryName))
                                   .Where(x => !string.IsNullOrEmpty(x.dbName))
                                   .GroupBy(x => x.queryName,
                                            (x, y) => new
            {
                queryName = x,
                columns   = y.Select(z => z.dbName).ToArray()
            }, StringComparer.OrdinalIgnoreCase)
                                   .Select(x =>
            {
                var propertyName  = x.queryName.ExcludeSuffix("Кт").ExcludeSuffix("Дт");
                var propertyTypes = propertyName == "Счет"
                        ? new[] { "ПланСчетов.Хозрасчетный" }
                        : propertyDescriptors.GetOrDefault(propertyName);
                if (propertyTypes == null || propertyTypes.Length == 1)
                {
                    if (x.columns.Length != 1)
                    {
                        return(null);
                    }
                    var nestedTableName = propertyTypes == null ? null : propertyTypes[0];
                    var singleLayout    = new SingleLayout(x.columns[0], nestedTableName);
                    return(new PropertyMapping(x.queryName, singleLayout, null));
                }
                var unionLayout = x.queryName == "Регистратор"
                        ? new UnionLayout(
                    null,
                    GetColumnBySuffixOrNull("_RecorderTRef", x.columns),
                    GetColumnBySuffixOrNull("_RecorderRRef", x.columns),
                    propertyTypes)
                        : new UnionLayout(
                    GetColumnBySuffixOrNull("_type", x.columns),
                    GetColumnBySuffixOrNull("_rtref", x.columns),
                    GetColumnBySuffixOrNull("_rrref", x.columns),
                    propertyTypes);
                return(new PropertyMapping(x.queryName, null, unionLayout));
            })
                                   .NotNull()
                                   .ToList();

            if (tableType == TableType.TableSection)
            {
                if (!HasProperty(propertyMappings, PropertyNames.id))
                {
                    var refLayout = new SingleLayout(GetTableSectionIdColumnNameByTableName(dbTableName), null);
                    propertyMappings.Add(new PropertyMapping(PropertyNames.id, refLayout, null));
                }
                if (!HasProperty(propertyMappings, PropertyNames.area))
                {
                    var          mainQueryName = TableMapping.GetMainQueryNameByTableSectionQueryName(queryTableName);
                    TableMapping mainTableMapping;
                    if (!tableMappingByQueryName.TryGetValue(mainQueryName, out mainTableMapping))
                    {
                        return(null);
                    }
                    PropertyMapping mainAreaProperty;
                    if (!mainTableMapping.TryGetProperty(PropertyNames.area, out mainAreaProperty))
                    {
                        return(null);
                    }
                    propertyMappings.Add(mainAreaProperty);
                }
            }
            return(new TableMapping(queryTableName, dbTableName, tableType, propertyMappings.ToArray()));
        }