Beispiel #1
0
 public static PropertyMapping Parse(string s)
 {
     try
     {
         var columnDesc = s.Split(new[] { " " }, StringSplitOptions.None);
         if (columnDesc.Length < 2)
         {
             throw new InvalidOperationException(string.Format("can't parse line [{0}]", s));
         }
         var            queryName = columnDesc[0];
         PropertyLauout propertyLauout;
         if (!Enum.TryParse(columnDesc[1], out propertyLauout))
         {
             const string messageFormat = "can't parse [{0}] from [{1}]";
             throw new InvalidOperationException(string.Format(messageFormat,
                                                               typeof(PropertyLauout).FormatName(), s));
         }
         if (propertyLauout == PropertyLauout.Single)
         {
             var nestedTableName = columnDesc.Length >= 4 ? columnDesc[3] : null;
             var singleInfo      = new SingleLayout(columnDesc[2], nestedTableName);
             return(new PropertyMapping(queryName, singleInfo, null));
         }
         var unionInfo = new UnionLayout(columnDesc[2],
                                         columnDesc[3], columnDesc[4],
                                         columnDesc.Skip(5).ToArray());
         return(new PropertyMapping(queryName, null, unionInfo));
     }
     catch (Exception e)
     {
         const string messageFormat = "could not parse property mapping from [{0}]";
         throw new InvalidOperationException(string.Format(messageFormat, s), e);
     }
 }
        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()));
        }
        private TableMapping CreateAccountingRegisterTableMapping(ValueTableRow tableRow)
        {
            var queryTableName = tableRow.GetString("ИмяТаблицы");

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

            if (string.IsNullOrEmpty(dbTableName))
            {
                return(null);
            }
            var fieldsTable      = new ValueTable(tableRow["Поля"]);
            var propertyMappings = new List <PropertyMapping>();
            var subkontoCount    = 0;

            foreach (var fieldRow in fieldsTable)
            {
                var queryName = fieldRow.GetString("ИмяПоля");
                var dbName    = fieldRow.GetString("ИмяПоляХранения");
                if (string.IsNullOrWhiteSpace(dbName))
                {
                    throw new InvalidOperationException("assertion failure");
                }
                if (dbName.EndsWith("_RTRef") || dbName.EndsWith("_RRRef"))
                {
                    continue;
                }
                UnionLayout unionLauout = null;
                if (string.IsNullOrWhiteSpace(queryName))
                {
                    if (dbName == "_AccountRRef")
                    {
                        queryName = "Счет";
                    }
                    else if (dbName == "_Period")
                    {
                        queryName = "Период";
                    }
                    else if (dbName == "_Splitter")
                    {
                        queryName = "Разделитель";
                    }
                    else if (dbName == "_Value1_TYPE")
                    {
                        queryName   = "Субконто1";
                        unionLauout = CreateSubkontoLayout(1);
                        subkontoCount++;
                    }
                    else if (dbName == "_Value2_TYPE")
                    {
                        queryName   = "Субконто2";
                        unionLauout = CreateSubkontoLayout(2);
                        subkontoCount++;
                    }
                    else if (dbName == "_Value3_TYPE")
                    {
                        queryName   = "Субконто3";
                        unionLauout = CreateSubkontoLayout(3);
                        subkontoCount++;
                    }
                    else
                    {
                        const string messageFormat = "unexpected empty query name for field, db name [{0}]";
                        throw new InvalidOperationException(string.Format(messageFormat, dbName));
                    }
                }
                SingleLayout singleLayout = null;
                if (unionLauout == null)
                {
                    string nestedTableName;
                    if (queryName == "Счет")
                    {
                        nestedTableName = "ПланСчетов.Хозрасчетный";
                    }
                    else if (queryName == "Организация")
                    {
                        nestedTableName = "Справочник.Организации";
                    }
                    else if (queryName == "Валюта")
                    {
                        nestedTableName = "Справочник.Валюты";
                    }
                    else if (queryName == "Подразделение")
                    {
                        nestedTableName = "Справочник.ПодразделенияОрганизаций";
                    }
                    else
                    {
                        nestedTableName = null;
                    }
                    singleLayout = new SingleLayout(dbName, nestedTableName);
                }
                if (dbName.ContainsIgnoringCase("Turnover"))
                {
                    queryName += "Оборот";
                    if (dbName.Contains("Dt"))
                    {
                        queryName += "Дт";
                    }
                    else if (dbName.Contains("Ct"))
                    {
                        queryName += "Кт";
                    }
                }
                propertyMappings.Add(new PropertyMapping(queryName, singleLayout, unionLauout));
            }
            if (queryTableName == AccountingRegisterTables.subkontoName)
            {
                queryTableName += subkontoCount;
            }
            return(new TableMapping(queryTableName, dbTableName, TableType.Main, propertyMappings.ToArray()));
        }
Beispiel #4
0
 public PropertyMapping(string propertyName, SingleLayout singleLayout, UnionLayout unionLayout)
 {
     PropertyName = propertyName;
     SingleLayout = singleLayout;
     UnionLayout  = unionLayout;
 }