private void CacheLookup(OrmLookup lookup)
        {
            _cachedLookups[lookup.LookupId] = lookup;

            string lookupStrWithAlias = null;

            try
            {
                lookupStrWithAlias = GenerateLookupDef(lookup, false);
            }
            catch (MigrationException ex)
            {
                LogWarning(ex.Message);
            }

            if (lookupStrWithAlias != null)
            {
                if (_cachedLookups.ContainsKey(lookupStrWithAlias))
                {
                    LogWarning("Duplicate lookup '{0}' found", lookupStrWithAlias);
                }
                else
                {
                    _cachedLookups.Add(lookupStrWithAlias, lookup);
                }
            }

            string lookupStrWithPhysical = null;

            try
            {
                lookupStrWithPhysical = GenerateLookupDef(lookup, true);
            }
            catch (MigrationException ex)
            {
                LogWarning(ex.Message);
            }

            if (lookupStrWithPhysical != null && !StringUtils.CaseInsensitiveEquals(lookupStrWithAlias, lookupStrWithPhysical))
            {
                if (_cachedLookups.ContainsKey(lookupStrWithPhysical))
                {
                    LogWarning("Duplicate lookup '{0}' found", lookupStrWithPhysical);
                }
                else
                {
                    _cachedLookups.Add(lookupStrWithPhysical, lookup);
                }
            }
        }
Exemplo n.º 2
0
        protected override void OnExtractSchemaHints()
        {
            DataPath textBindingPath;

            if (Control.Bindings != null)
            {
                Control.Bindings.TryGetValue(LookupIdBindingCode, out _lookupIdBindingPath);
                Control.Bindings.TryGetValue(TextBindingCode, out textBindingPath);
            }
            else
            {
                textBindingPath = null;
            }

            string lookupDef;

            if (Component.TryGetPropertyValue("Lookup", out lookupDef))
            {
                _lookup = EntityLoader.LoadLookup(lookupDef);

                if (_lookup != null)
                {
                    try
                    {
                        _idDataPath = DataPath.Parse(_lookup.IdField);
                    }
                    catch (FormatException)
                    {
                        LogError("Unable to parse '{0}' lookup id field", _lookup.IdField);
                    }

                    try
                    {
                        _nameDataPath = DataPath.Parse(_lookup.NameField);
                    }
                    catch (FormatException)
                    {
                        LogError("Unable to parse '{0}' lookup name field", _lookup.NameField);
                    }

                    DataPathTranslator.RegisterTable(_lookup.MainTable);

                    if (_idDataPath != null)
                    {
                        _columnPrefixPath = _idDataPath.Reverse();
                        DataPathTranslator.RegisterField(_idDataPath);
                        DataPathTranslator.RegisterField(_columnPrefixPath);
                    }

                    if (_nameDataPath != null)
                    {
                        DataPathTranslator.RegisterField(_nameDataPath);
                    }

                    _columns = new List<ColumnDefinition>();
                    _dataPaths = new List<DataPath>();

                    if (_lookup.Layout != null)
                    {
                        foreach (string columnString in _lookup.Layout.Split(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries))
                        {
                            ColumnDefinition column = ColumnDefinition.Parse(columnString);

                            if (column.Visible)
                            {
                                DataPath dataPath = null;

                                try
                                {
                                    dataPath = DataPath.Parse(column.Binding);
                                }
                                catch (FormatException)
                                {
                                    LogError("Unable to parse '{0}' column binding string", column.Binding);
                                }

                                if (dataPath != null)
                                {
                                    DataPathTranslator.RegisterField(dataPath);
                                    _columns.Add(column);
                                    _dataPaths.Add(dataPath);
                                }
                            }
                        }
                    }
                }
                else
                {
                    LogWarning("Unable to find '{0}' lookup", lookupDef);
                }
            }

            if (_lookupIdBindingPath != null && _idDataPath != null)
            {
                if (textBindingPath != null &&
                    _nameDataPath != null &&
                    textBindingPath.Joins.Count == 0)
                {
                    Debug.Assert(_lookupIdBindingPath.Joins.Count == 0);
                    Debug.Assert(_idDataPath.Joins.Count == 0);
                    Debug.Assert(_nameDataPath.Joins.Count == 0);
                    DataPathTranslator.RegisterJoin(_lookupIdBindingPath, _idDataPath);
                    Context.SecondaryJoins[
                        new DataPathJoin(_lookupIdBindingPath.TargetTable, _lookupIdBindingPath.TargetField, _idDataPath.TargetTable, _idDataPath.TargetField)] =
                        new DataPathJoin(textBindingPath.TargetTable, textBindingPath.TargetField, _nameDataPath.TargetTable, _nameDataPath.TargetField);
                    _isObject = true;
                }

                if (!_isObject)
                {
                    OrmEntity entity = EntityLoader.LoadEntity(_lookupIdBindingPath.TargetTable);

                    if (entity != null)
                    {
                        string targetField = _lookupIdBindingPath.TargetField;

                        if (targetField.StartsWith("@"))
                        {
                            targetField = targetField.Substring(1);
                        }

                        OrmEntityProperty property = entity.Properties.GetFieldPropertyByFieldName(targetField);

                        if (property != null)
                        {
                            _isObject = !property.Include;
                        }
                    }
                }

                if (_isObject)
                {
                    DataPathTranslator.RegisterJoin(_idDataPath, _lookupIdBindingPath);
                }
            }
        }
        private static string GenerateLookupDef(OrmLookup lookup, bool usePhysicalName)
        {
            List<string> idParts = new List<string>();
            DataPath searchDataPath;

            try
            {
                searchDataPath = DataPath.Parse(lookup.SearchField);
            }
            catch (FormatException)
            {
                throw new MigrationException(string.Format("Unable to parse '{0}' lookup search field", lookup.SearchField));
            }

            if (searchDataPath != null)
            {
                string tableName = searchDataPath.RootTable;
                string rootTable = tableName;

                foreach (DataPathJoin join in searchDataPath.Joins)
                {
                    tableName = join.ToTable;
                    idParts.Add(usePhysicalName
                                    ? tableName
                                    : LookupTableDisplayName(tableName));
                }

                if (usePhysicalName)
                {
                    idParts.Add(searchDataPath.TargetField);
                }
                else
                {
                    idParts.Add(LookupFieldDisplayName(tableName, searchDataPath.TargetField));
                }

                string def = string.Format("{0}:{1}", rootTable, string.Join(".", idParts.ToArray()));

                if (!string.IsNullOrEmpty(lookup.LookupName))
                {
                    def += ":" + lookup.LookupName;
                }

                return def;
            }
            else
            {
                return null;
            }
        }