Beispiel #1
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);
            }
        }
Beispiel #2
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));
        }
Beispiel #3
0
        public SubContext <P> CreateSubContext(MappingContext <T> parent)
        {
            string        newPrefix = TypeContextUtilities.Compose(parent.Prefix, PropertyValidator.PropertyInfo.Name);
            PropertyRoute route     = parent.PropertyRoute.Add(this.PropertyValidator.PropertyInfo);

            SubContext <P> ctx = new SubContext <P>(newPrefix, PropertyValidator, route, parent);

            if (parent.Value != null)
            {
                ctx.Value = GetValue(parent.Value);
            }
            return(ctx);
        }
Beispiel #4
0
        public IEnumerable <MappingContext <S> > GenerateItemContexts(MappingContext <MList <S> > ctx)
        {
            PropertyRoute route = ctx.PropertyRoute.Add("Item");

            var indexPrefixes = ctx.Inputs.IndexPrefixes();

            foreach (var index in indexPrefixes.OrderBy(ip => (ctx.GlobalInputs.TryGetC(TypeContextUtilities.Compose(ctx.Prefix, ip, EntityListBaseKeys.Index)) ?? ip).ToInt()))
            {
                SubContext <S> itemCtx = new SubContext <S>(TypeContextUtilities.Compose(ctx.Prefix, index), null, route, ctx);

                yield return(itemCtx);
            }
        }
Beispiel #5
0
        public bool Parse <V>(out V value)
        {
            var mapping = Mapping.ForValue <V>();

            if (mapping == null)
            {
                throw new InvalidOperationException("No mapping for value {0}".FormatWith(typeof(V).TypeName()));
            }

            var sc = new SubContext <V>(this.Prefix, null, null, this);

            value = mapping(sc);

            return(!sc.SupressChange);
        }
Beispiel #6
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);
        }
Beispiel #7
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);
            }
        }