protected override List <TRow> GetItems() { var items = base.GetItems(); if (items.Any()) { var attr = items.First().GetType().GetCustomAttribute <LocalizationRowAttribute>(); if (attr == null) { return(items); } var localRowType = attr.LocalizationRow; var localRowFactory = FastReflection.DelegateForConstructor <Row>(localRowType); var localRow = localRowFactory(); var foreignKeyFieldName = attr.MappedIdField ?? ((Field)((IIdRow)items.First()).IdField).PropertyName; var foreignKeyField = localRow.FindFieldByPropertyName(foreignKeyFieldName) ?? localRow.FindField(foreignKeyFieldName); var foreignKeyCriteria = new Criteria(foreignKeyField.PropertyName ?? foreignKeyField.Name); var languageIdKeyField = ((Field)((ILocalizationRow)localRow).CultureIdField).PropertyName; var languageIdField = localRow.FindFieldByPropertyName(languageIdKeyField) ?? localRow.FindField(languageIdKeyField); var languageIdCriteria = new Criteria(languageIdField.PropertyName ?? languageIdField.Name); 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(items); } } var listHandler = DefaultHandlerFactory.ListHandlerFor(localRowType); var listRequest = DefaultHandlerFactory.ListRequestFor(localRowType); listRequest.ColumnSelection = ColumnSelection.List; listRequest.Criteria = (foreignKeyCriteria.In(items.Select(i => i.IdField[i])) && languageIdCriteria == userLanguage.Id.Value); var translationsResponse = listHandler.Process(SqlConnections.NewFor <TRow>(), listRequest); if (translationsResponse.Entities.Count == 0) { return(items); } foreach (var item in items) { var translated = translationsResponse.Entities.Cast <IOMPLocalizationLangRow>().FirstOrDefault( t => (Int32)foreignKeyField.AsObject(t as Row) == item.IdField[item]); if (translated != null && !string.IsNullOrEmpty(translated.NameField[translated as Row])) { item.NameField[item] = translated.NameField[translated as Row]; } } } return(items); }
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); } } } } } }