public void OnReturn(IListRequestHandler handler)
        {
            if (ReferenceEquals(null, Target) ||
                !handler.AllowSelectField(Target) ||
                !handler.ShouldSelectField(Target) ||
                handler.Response.Entities.IsEmptyOrNull())
            {
                return;
            }

            var idField = (Field)((handler.Row as IIdRow).IdField);

            var listHandler = listHandlerFactory();

            var listRequest = new ListRequest
            {
                ColumnSelection = this.attr.ColumnSelection,
                IncludeColumns  = this.includeColumns
            };

            var enumerator = handler.Response.Entities.Cast <Row>();

            while (true)
            {
                var part = enumerator.Take(1000);
                if (!part.Any())
                {
                    break;
                }

                enumerator = enumerator.Skip(1000);

                listRequest.Criteria = foreignKeyCriteria.In(
                    part.Select(x => idField.AsObject(x))) & filterCriteria;

                IListResponse response = listHandler.Process(
                    handler.Connection, listRequest);

                var lookup = response.Entities.Cast <Row>()
                             .ToLookup(x => foreignKeyField.AsObject(x).ToString());

                foreach (var row in part)
                {
                    var list     = rowListFactory();
                    var matching = lookup[idField.AsObject(row).ToString()];
                    foreach (var x in matching)
                    {
                        list.Add(x);
                    }

                    Target.AsObject(row, list);
                }
            }
        }
        public void OnReturn(IListRequestHandler handler)
        {
            if (Target is null ||
                !handler.AllowSelectField(Target) ||
                !handler.ShouldSelectField(Target) ||
                handler.Response.Entities.IsEmptyOrNull())
            {
                return;
            }

            var listHandler = handlerFactory.CreateHandler <IListRequestProcessor>(rowType);
            var listRequest = listHandler.CreateRequest();

            listRequest.ColumnSelection = attr.ColumnSelection;
            listRequest.IncludeColumns  = includeColumns;

            var enumerator = handler.Response.Entities.Cast <IRow>();

            while (true)
            {
                var part = enumerator.Take(1000);
                if (!part.Any())
                {
                    break;
                }

                enumerator = enumerator.Skip(1000);

                listRequest.Criteria = foreignKeyCriteria.In(
                    part.Select(x => masterKeyField.AsObject(x))) & filterCriteria;

                IListResponse response = listHandler.Process(
                    handler.Connection, listRequest);

                var lookup = response.Entities.Cast <IRow>()
                             .ToLookup(x => foreignKeyField.AsObject(x).ToString());

                foreach (var row in part)
                {
                    var list     = rowListFactory();
                    var matching = lookup[masterKeyField.AsObject(row).ToString()];
                    foreach (var x in matching)
                    {
                        list.Add(x);
                    }

                    Target.AsObject(row, list);
                }
            }
        }
Ejemplo n.º 3
0
        public void OnReturn(IListRequestHandler handler)
        {
            if (handler.Response.Entities.Count == 0)
            {
                return;
            }

            // Lets Get the main row and translate it
            var culture      = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
            var userLanguage = new LanguageRow();

            using (var connectionLanguage = SqlConnections.NewFor <LanguageRow>())
            {
                userLanguage = connectionLanguage.TryFirst <LanguageRow>(new Criteria(LanguageRow.Fields.LanguageId).Like($"%{culture}%"));
                if (userLanguage == null)
                {
                    return;
                }
            }

            var listHandler = DefaultHandlerFactory.ListHandlerFor(localRowType);
            var listRequest = DefaultHandlerFactory.ListRequestFor(localRowType);

            listRequest.ColumnSelection = ColumnSelection.List;

            List <IOMPLocalizationRow> localList = handler.Response.Entities.Cast <IOMPLocalizationRow>().ToList();

            listRequest.Criteria = (foreignKeyCriteria.In(localList.Select(s => s.IdField[s as Row])) && languageIdCriteria == userLanguage.Id.Value);

            var translationsResponse = listHandler.Process(handler.Connection, listRequest);

            if (translationsResponse.Entities.Count > 0)
            {
                var responseLang = translationsResponse.Entities.Cast <IOMPLocalizationLangRow>();
                // Validate They have IName row
                foreach (IOMPLocalizationRow responseEntity in handler.Response.Entities)
                {
                    var entityLang = responseLang
                                     .FirstOrDefault(s =>
                                                     (Int32)foreignKeyField.AsObject(s as Row) ==
                                                     responseEntity.IdField[responseEntity as Row]);

                    if (entityLang != null && !string.IsNullOrEmpty(entityLang?.NameField[entityLang as Row]))
                    {
                        responseEntity.NameField[responseEntity as Row] = entityLang?.NameField[entityLang as Row];
                    }
                }
            }

            // How about the joined tables, lets translate them too
            var localizationRowFields = handler.Row.GetFields();

            foreach (var localizationRowField in localizationRowFields.Where(f =>
                                                                             !String.IsNullOrEmpty(f.ForeignTable)))
            {
                var foreignKeyAttr   = localizationRowField.GetAttribute <ForeignKeyAttribute>();
                var textualFieldAttr = localizationRowField.GetAttribute <TextualFieldAttribute>();
                if (foreignKeyAttr.RowType == null || textualFieldAttr == null || string.IsNullOrEmpty(textualFieldAttr.Value))
                {
                    continue;
                }

                var foreignRowFactory = FastReflection.DelegateForConstructor <Row>(foreignKeyAttr.RowType);
                var foreignRow        = foreignRowFactory();
                var foreignAttr       = foreignRow.GetType().GetCustomAttribute <LocalizationRowAttribute>();

                if (foreignAttr == null)
                {
                    continue;
                }

                var foreignLangType = foreignAttr.LocalizationRow;

                var foreignLangRowFactory = FastReflection.DelegateForConstructor <Row>(foreignLangType);
                var foreignLangRow        = foreignLangRowFactory();

                var externalForeignKeyFieldName =
                    foreignAttr.MappedIdField ?? ((Field)((IIdRow)foreignRow).IdField).PropertyName;
                var externalForeignKeyField = foreignLangRow.FindFieldByPropertyName(externalForeignKeyFieldName) ??
                                              foreignLangRow.FindField(externalForeignKeyFieldName);

                var listForeignHandler = DefaultHandlerFactory.ListHandlerFor(foreignLangType);
                var listForeignRequest = DefaultHandlerFactory.ListRequestFor(foreignLangType);
                listRequest.ColumnSelection = ColumnSelection.List;

                var externalForeignCriteria =
                    new Criteria(externalForeignKeyField.PropertyName ?? externalForeignKeyField.Name);

                var idList = new List <Int32>();
                foreach (IOMPLocalizationRow responseEntity in handler.Response.Entities)
                {
                    var idToAdd = (Int32?)localizationRowField.AsObject(responseEntity as Row);
                    if (idToAdd.HasValue)
                    {
                        idList.Add(idToAdd.Value);
                    }
                }
                if (!idList.Any())
                {
                    continue;
                }
                listForeignRequest.Criteria =
                    externalForeignCriteria.In(idList.Distinct()) &&
                    languageIdCriteria == userLanguage.Id.Value;

                var translationsForeignResponse =
                    listForeignHandler.Process(handler.Connection, listForeignRequest);

                if (translationsForeignResponse.TotalCount > 0)
                {
                    foreach (IOMPLocalizationRow responseEntity in handler.Response.Entities)
                    {
                        var localId = (Int32?)localizationRowField.AsObject(responseEntity as Row);

                        var entityLang = translationsForeignResponse.Entities.Cast <IOMPLocalizationLangRow>()
                                         .FirstOrDefault(s =>
                                                         (Int32?)externalForeignKeyField.AsObject(s as Row) == localId);

                        if (entityLang != null)
                        {
                            var foreignEntityName = entityLang?.NameField[entityLang as Row];

                            var textualFieldExternal = (responseEntity as Row).FindField(textualFieldAttr.Value);
                            if (!string.IsNullOrEmpty(foreignEntityName))
                            {
                                textualFieldExternal.AsObject(responseEntity as Row, foreignEntityName);
                            }
                        }
                    }
                }
            }
        }