Exemple #1
0
        public Lite <S> TryModifyEntity(MappingContext <Lite <S> > ctx, Lite <S> lite)
        {
            //commented out because of Lite<FileEntity/FilePathEntity>
            if (AvoidEntityMapping || !EntityHasChanges(ctx))
            {
                return(lite); // If form does not contains changes to the entity
            }
            if (EntityMapping == null)
            {
                throw new InvalidOperationException("Changes to Entity {0} are not allowed because EntityMapping is null".FormatWith(lite?.ToString()));
            }

            var sc = new SubContext <S>(ctx.Prefix, null, ctx.PropertyRoute.Add("Entity"), ctx)
            {
                Value = lite.Retrieve()
            };

            sc.Value = EntityMapping(sc);

            ctx.AddChild(sc);

            if (sc.SupressChange)
            {
                return(lite);
            }

            return(sc.Value.ToLite(sc.Value.IsNew));
        }
Exemple #2
0
        public void SetProperty(MappingContext <T> parent)
        {
            SubContext <P> ctx = CreateSubContext(parent);

            try
            {
                var old = ctx.Value;
                ctx.Value = Mapping(ctx);

                if (!ctx.SupressChange)
                {
                    if (!object.Equals(old, ctx.Value))
                    {
                        Signum.Web.Mapping.AssertCanChange(ctx.PropertyRoute);
                    }

                    SetValue(parent.Value, ctx.Value);
                }
            }
            catch (Exception e)
            {
                string error = e is FormatException?ValidationMessage._0HasAnInvalidFormat.NiceToString() :
                                   e is UnauthorizedAccessException ? e.Message :
                                   ValidationMessage.NotPossibleToaAssign0.NiceToString();

                ctx.Error.Add(error.FormatWith(PropertyValidator.PropertyInfo.NiceName()));
            }

            if (!ctx.Empty())
            {
                parent.AddChild(ctx);
            }
        }
Exemple #3
0
        public override MList <S> GetValue(MappingContext <MList <S> > ctx)
        {
            using (HeavyProfiler.LogNoStackTrace("GetValue", () => "MListCorrelatedMapping<{0}>".FormatWith(typeof(S).TypeName())))
            {
                MList <S> list = ctx.Value;
                int       i    = 0;

                foreach (MappingContext <S> itemCtx in GenerateItemContexts(ctx).OrderBy(mc => mc.Prefix.Substring(mc.Prefix.LastIndexOf("_") + 1).ToInt().Value))
                {
                    Debug.Assert(!itemCtx.Empty());

                    itemCtx.Value = list[i];
                    itemCtx.Value = ElementMapping(itemCtx);

                    ctx.AddChild(itemCtx);

                    if (!itemCtx.SupressChange && !object.Equals(list[i], itemCtx.Value))
                    {
                        Signum.Web.Mapping.AssertCanChange(ctx.PropertyRoute);
                    }

                    if (!list[i].Equals(itemCtx.Value))
                    {
                        list[i] = itemCtx.Value;
                    }

                    i++;
                }

                return(list);
            }
        }
Exemple #4
0
        public R GetRuntimeValue <R>(MappingContext <T> ctx, PropertyRoute route)
            where R : class, T
        {
            if (AllowedMappings != null && !AllowedMappings.ContainsKey(typeof(R)))
            {
                return((R)(object)ctx.None(ValidationMessage.Type0NotAllowed.NiceToString().FormatWith(typeof(R))));
            }

            Mapping <R>    mapping = (Mapping <R>)(AllowedMappings?.TryGetC(typeof(R)) ?? Navigator.EntitySettings(typeof(R)).UntypedMappingLine);
            SubContext <R> sc      = new SubContext <R>(ctx.Prefix, null, route, ctx)
            {
                Value = ctx.Value as R
            };                                                                                             // If the type is different, the AutoEntityMapping has the current value but EntityMapping just null

            sc.Value          = mapping(sc);
            ctx.SupressChange = sc.SupressChange;
            ctx.AddChild(sc);
            return(sc.Value);
        }
Exemple #5
0
        public override MList <S> GetValue(MappingContext <MList <S> > ctx)
        {
            using (HeavyProfiler.LogNoStackTrace("GetValue", () => "MListDictionaryMapping<{0}>".FormatWith(typeof(S).TypeName())))
            {
                if (ctx.Empty())
                {
                    return(ctx.None());
                }

                MList <S> list = ctx.Value;

                var dic = (FilterElements == null ? list : list.Where(FilterElements)).ToDictionary(GetKey);

                PropertyRoute route = ctx.PropertyRoute.Add("Item").Continue(MemberList);

                string[] namesToAppend = MemberList.Select(MemberAccessGatherer.GetName).NotNull().ToArray();

                foreach (MappingContext <S> itemCtx in GenerateItemContexts(ctx))
                {
                    var tce = new TypeContextExpression(new PropertyInfo[0], typeof(S), itemCtx.PropertyRoute, itemCtx.Value);

                    SubContext <K> subContext = new SubContext <K>(TypeContextUtilities.Compose(itemCtx.Prefix, namesToAppend), null, route, itemCtx);

                    subContext.Value = KeyMapping(subContext);

                    if (!dic.ContainsKey(subContext.Value) && OnlyIfPossible)
                    {
                        continue;
                    }

                    itemCtx.Value = dic.GetOrThrow(subContext.Value);

                    itemCtx.Value = ElementMapping(itemCtx);

                    ctx.AddChild(itemCtx);
                }

                return(list);
            }
        }
Exemple #6
0
        public override MList <S> GetValue(MappingContext <MList <S> > ctx)
        {
            using (HeavyProfiler.LogNoStackTrace("GetValue", () => "MListMapping<{0}>".FormatWith(typeof(S).TypeName())))
            {
                if (ctx.Empty())
                {
                    return(ctx.None());
                }

                IMListPrivate <S> mlistPriv = ctx.Value;

                var dic = mlistPriv == null ? new Dictionary <PrimaryKey, MList <S> .RowIdElement>() :
                          mlistPriv.InnerList.Where(a => a.RowId.HasValue).ToDictionary(a => a.RowId.Value, a => a);

                var newList = new List <MList <S> .RowIdElement>();
                foreach (MappingContext <S> itemCtx in GenerateItemContexts(ctx))
                {
                    Debug.Assert(!itemCtx.Empty());

                    string rowIdString = (itemCtx.Inputs.ContainsKey(EntityListBaseKeys.RowId)? itemCtx.Inputs[EntityListBaseKeys.RowId]:null);

                    if (rowIdString.HasText())
                    {
                        var rowId = new PrimaryKey((IComparable)ReflectionTools.Parse(rowIdString, GetRowIdType(ctx)));

                        var oldValue = dic.GetOrThrow(rowId, "No RowID {0} found");

                        itemCtx.Value = oldValue.Element;
                        itemCtx.Value = ElementMapping(itemCtx);

                        ctx.AddChild(itemCtx);

                        if (itemCtx.Value != null)
                        {
                            var val = itemCtx.SupressChange ? oldValue.Element : itemCtx.Value;

                            if (oldValue.Element.Equals(val))
                            {
                                newList.Add(new MList <S> .RowIdElement(val, rowId, oldValue.OldIndex));
                            }
                            else
                            {
                                newList.Add(new MList <S> .RowIdElement(val));
                            }
                        }
                    }
                    else
                    {
                        itemCtx.Value = ElementMapping(itemCtx);
                        ctx.AddChild(itemCtx);
                        if (itemCtx.Value != null && !itemCtx.SupressChange)
                        {
                            newList.Add(new MList <S> .RowIdElement(itemCtx.Value));
                        }
                    }
                }

                if (!AreEqual(newList, mlistPriv == null ? null : mlistPriv.InnerList))
                {
                    Signum.Web.Mapping.AssertCanChange(ctx.PropertyRoute);

                    if (ctx.Value == null)
                    {
                        mlistPriv = ctx.Value = new MList <S>();
                    }

                    var added   = newList.Select(a => a.Element).Except(mlistPriv.InnerList.Select(a => a.Element)).ToList();
                    var removed = mlistPriv.InnerList.Select(a => a.Element).Except(newList.Select(a => a.Element)).ToList();

                    mlistPriv.InnerList.Clear();
                    mlistPriv.InnerList.AddRange(newList);
                    mlistPriv.InnerListModified(added, removed);
                }

                return(ctx.Value);
            }
        }