Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
0
 public abstract T GetValue(MappingContext <T> mapping);
Ejemplo n.º 3
0
 private static bool AnyNonSpecialImput(MappingContext <Lite <S> > ctx)
 {
     return(ctx.Inputs.Keys.Except(Mapping.specialProperties).Any());
 }