Example #1
0
        public bool HasSomeIndex(PropertyRoute route)
        {
            var field = TryField(route);

            if (field == null)
            {
                return(false);
            }

            if (field.UniqueIndex != null)
            {
                return(true);
            }

            var cols = field.Columns();

            if (cols.Any(c => c.ReferenceTable != null))
            {
                return(true);
            }

            var mlistPr = route.GetMListItemsRoute();

            ITable table = mlistPr == null ?
                           (ITable)Table(route.RootType) :
                           (ITable)((FieldMList)Field(mlistPr.Parent)).TableMList;

            return(table.MultiColumnIndexes != null && table.MultiColumnIndexes.Any(index => index.Columns.Any(cols.Contains)));
        }
        public static void Start(SchemaBuilder sb, Func <CultureInfo> defaultCulture)
        {
            if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
            {
                sb.Include <TranslatedInstanceEntity>()
                .WithUniqueIndex(ti => new { ti.Culture, ti.PropertyRoute, ti.Instance, ti.RowId })
                .WithQuery(() => e => new
                {
                    Entity = e,
                    e.Id,
                    e.Culture,
                    e.Instance,
                    e.PropertyRoute,
                    e.TranslatedText,
                    e.OriginalText,
                });
                TranslatedInstanceLogic.getDefaultCulture = defaultCulture ?? throw new ArgumentNullException(nameof(defaultCulture));

                LocalizationCache = sb.GlobalLazy(() =>
                                                  Database.Query <TranslatedInstanceEntity>()
                                                  .ToList()
                                                  .AgGroupToDictionary(a => a.Culture.ToCultureInfo(),
                                                                       gr2 => gr2.GroupBy(a => a.PropertyRoute)
                                                                       .SelectMany(gr =>
                {
                    PropertyRoute pr = gr.Key.ToPropertyRoute();

                    PropertyRoute?mListRoute = pr.GetMListItemsRoute();
                    if (mListRoute == null)
                    {
                        return(gr.Select(ti => KeyValuePair.Create(new LocalizedInstanceKey(pr, ti.Instance, null), ti)));
                    }

                    Type type = ((FieldMList)Schema.Current.Field(mListRoute.Parent !)).TableMList.PrimaryKey.Type;

                    return(gr.Select(ti => KeyValuePair.Create(new LocalizedInstanceKey(pr, ti.Instance, new PrimaryKey((IComparable)ReflectionTools.Parse(ti.RowId !, type) !)), ti)));
                }).ToDictionary())
                                                  , new InvalidateWith(typeof(TranslatedInstanceEntity)));

                sb.Schema.SchemaCompleted += () =>
                {
                    var s = Schema.Current;

                    var prs = (from t in s.Tables.Keys
                               from pr in PropertyRoute.GenerateRoutes(t)
                               where pr.PropertyRouteType == PropertyRouteType.FieldOrProperty && pr.FieldInfo != null && pr.FieldInfo.FieldType == typeof(string) &&
                               s.Settings.FieldAttribute <TranslateFieldAttribute>(pr) != null &&
                               s.Settings.FieldAttribute <IgnoreAttribute>(pr) == null
                               select KeyValuePair.Create(pr, s.Settings.FieldAttribute <TranslateFieldAttribute>(pr) !.TranslatableRouteType)).ToList();

                    foreach (var kvp in prs)
                    {
                        AddRoute(kvp.Key, kvp.Value);
                    }
                };
            }
        }
        static Dictionary <LocalizedInstanceKey, string> FromRouteMList <T, M>(PropertyRoute pr) where T : Entity
        {
            var mlItemPr      = pr.GetMListItemsRoute() !;
            var mListProperty = mlItemPr.Parent !.GetLambdaExpression <T, MList <M> >(safeNullAccess: false);
            var selector      = pr.GetLambdaExpression <M, string>(safeNullAccess: false, skipBefore: mlItemPr);

            return((from mle in Database.MListQuery(mListProperty)
                    select KeyValuePair.Create(new LocalizedInstanceKey(pr, mle.Parent.ToLite(), mle.RowId), selector.Evaluate(mle.Element))).ToDictionary());
        }
        static Action <T> GetUpdater <T>(PropertyRoute route)
            where T : Entity
        {
            string propertyPath = route.PropertyString();
            string rootType     = TypeLogic.GetCleanName(route.RootType);

            var mlistRoute = route.GetMListItemsRoute();

            if (mlistRoute == null)
            {
                var exp  = route.GetLambdaExpression <T, FilePathEmbedded?>(true);
                var func = exp.Compile();

                return((e) =>
                {
                    var fpe = func(e);
                    if (fpe != null)
                    {
                        fpe.EntityId = e.Id;
                        fpe.MListRowId = null;
                        fpe.PropertyRoute = route.PropertyString();
                        fpe.RootType = rootType;
                    }
                });
            }
            else
            {
                var mlistExpr = mlistRoute.Parent !.GetLambdaExpression <T, IMListPrivate>(true);
                var mlistFunc = mlistExpr.Compile();

                var fileExpr = route.GetLambdaExpression <ModifiableEntity, FilePathEmbedded>(true, mlistRoute);
                var fileFunc = fileExpr.Compile();

                return((e) =>
                {
                    var mlist = mlistFunc(e);
                    if (mlist != null)
                    {
                        var list = (IList)mlist;
                        for (int i = 0; i < list.Count; i++)
                        {
                            var mod = (ModifiableEntity)list[i] !;

                            var fpe = fileFunc(mod);
                            if (fpe != null)
                            {
                                fpe.EntityId = e.Id;
                                fpe.MListRowId = mlist.GetRowId(i);
                                fpe.PropertyRoute = route.PropertyString();
                                fpe.RootType = rootType;
                            }
                        }
                    }
                });
            }
        }
        public static bool AnyNoTranslated(PropertyRoute pr, CultureInfo ci)
        {
            var mlist = pr.GetMListItemsRoute();

            if (mlist == null)
            {
                return(giAnyNoTranslated.GetInvoker(pr.RootType)(pr, ci));
            }

            return(giAnyNoTranslatedMList.GetInvoker(pr.RootType, mlist.Type)(pr, ci));
        }
Example #6
0
        static Expression <Func <object, bool> > GetPredicate(List <PropertySetter> predicate, PropertyRoute mainRoute, JsonSerializerOptions options)
        {
            var param = Expression.Parameter(typeof(object), "p");

            var body = predicate.Select(p =>
            {
                var pr = mainRoute.Add(p.Property);

                var lambda = pr.GetLambdaExpression <object, object>(true, mainRoute.GetMListItemsRoute());

                var left        = Expression.Invoke(lambda, param);
                object?objClean = ConvertObject(p.Value, pr, options);

                return((Expression)QueryUtils.GetCompareExpression(p.FilterOperation !.Value, left, Expression.Constant(objClean), inMemory: true));
            }).Aggregate((a, b) => Expression.AndAlso(a, b));

            return(Expression.Lambda <Func <object, bool> >(body, param));
        }
        static bool AnyNoTranslatedMList <T, M>(PropertyRoute pr, CultureInfo ci) where T : Entity
        {
            var mlistItemPr   = pr.GetMListItemsRoute() !;
            var mListProperty = mlistItemPr.Parent !.GetLambdaExpression <T, MList <M> >(safeNullAccess: false);

            var exp = pr.GetLambdaExpression <M, string>(safeNullAccess: false, skipBefore: mlistItemPr);

            return((from mle in Database.MListQuery(mListProperty)
                    let str = exp.Evaluate(mle.Element)
                              where str != null &&
                              !Database.Query <TranslatedInstanceEntity>().Any(ti =>
                                                                               ti.Instance.Is(mle.Parent) &&
                                                                               ti.PropertyRoute.IsPropertyRoute(pr) &&
                                                                               ti.RowId == mle.RowId.ToString() &&
                                                                               ti.Culture == ci.ToCultureInfoEntity() &&
                                                                               ti.OriginalText == str)
                              select mle).Any());
        }
        public static TranslatedInstanceEntity?GetTranslatedInstance(Lite <Entity> lite, PropertyRoute route, PrimaryKey?rowId)
        {
            var hastMList = route.GetMListItemsRoute() != null;

            if (hastMList && !rowId.HasValue)
            {
                throw new InvalidOperationException("Route {0} has MList so rowId should have a value".FormatWith(route));
            }

            if (!hastMList && rowId.HasValue)
            {
                throw new InvalidOperationException("Route {0} has not MList so rowId should be null".FormatWith(route));
            }

            if (route.RootType != lite.EntityType)
            {
                throw new InvalidOperationException("Route {0} belongs to type {1}, not {2}".FormatWith(route, route.RootType.TypeName(), lite.EntityType.TypeName()));
            }

            var key = new LocalizedInstanceKey(route, lite, rowId);

            var result = LocalizationCache.Value.TryGetC(CultureInfo.CurrentUICulture)?.TryGetC(key);

            if (result != null)
            {
                return(result);
            }

            if (CultureInfo.CurrentUICulture.IsNeutralCulture)
            {
                return(null);
            }

            result = LocalizationCache.Value.TryGetC(CultureInfo.CurrentUICulture.Parent)?.TryGetC(key);

            if (result != null)
            {
                return(result);
            }

            return(null);
        }