private static MapRepresentation GetMap(IOidStrategy oidStrategy, IFrameworkFacade frameworkFacade, HttpRequest req, ContextFacade context, RestControlFlags flags)
        {
            MapRepresentation value;

            // All reasons why we cannot create a link representation
            if (context.Specification.IsCollection && context.ElementSpecification != null && !context.ElementSpecification.IsParseable)
            {
                var proposedObjectFacade = frameworkFacade.GetObject(context.ProposedValue);
                var coll = proposedObjectFacade.ToEnumerable().Select(no => CreateObjectRef(oidStrategy, req, no, flags)).ToArray();
                value = CreateMap(context, coll);
            }
            else if (context.Specification.IsParseable ||
                     context.ProposedValue == null ||
                     context.ProposedObjectFacade == null ||
                     context.ProposedObjectFacade.Specification.IsParseable)
            {
                value = CreateMap(context, context.ProposedValue);
            }
            else
            {
                value = CreateMap(context, RefValueRepresentation.Create(oidStrategy, new ObjectRelType(RelValues.Self, new UriMtHelper(oidStrategy, req, context.ProposedObjectFacade)), flags));
            }

            return(value);
        }
        private static RefValueRepresentation CreateObjectRef(IOidStrategy oidStrategy, HttpRequest req, IObjectFacade no, RestControlFlags flags)
        {
            var helper = new UriMtHelper(oidStrategy, req, no);
            var rt     = new ObjectRelType(RelValues.Element, helper);

            return(RefValueRepresentation.Create(oidStrategy, rt, flags));
        }
Пример #3
0
        private MapRepresentation CreateArguments(HttpRequest req, ActionResultContextFacade actionResult)
        {
            var optionalProperties = new List <OptionalProperty>();

            foreach (var visibleParamContext in actionResult.ActionContext.VisibleParameters)
            {
                IRepresentation value;

                if (visibleParamContext.Specification.IsParseable)
                {
                    var proposedObj = visibleParamContext.ProposedObjectFacade == null ? visibleParamContext.ProposedValue : visibleParamContext.ProposedObjectFacade?.Object;
                    var valueObj    = proposedObj != null?RestUtils.ObjectToPredefinedType(proposedObj) : null;

                    value = MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, valueObj));
                }
                else if (visibleParamContext.Specification.IsCollection)
                {
                    if (visibleParamContext.ElementSpecification.IsParseable)
                    {
                        var proposedEnumerable = (visibleParamContext.ProposedObjectFacade == null
                            ? visibleParamContext.ProposedValue
                            : visibleParamContext.ProposedObjectFacade?.Object) as IEnumerable;

                        var proposedCollection = proposedEnumerable == null ? new object[] { } : proposedEnumerable.Cast <object>();

                        var valueObjs = proposedCollection.Select(i => RestUtils.ObjectToPredefinedType(i)).ToArray();
                        value = MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, valueObjs));
                    }
                    else
                    {
                        var refNos = visibleParamContext.ProposedObjectFacade.ToEnumerable().Select(no => no).ToArray();
                        var refs   = refNos.Select(no => RefValueRepresentation.Create(OidStrategy, new ObjectRelType(RelValues.Self, new UriMtHelper(OidStrategy, req, no)), Flags)).ToArray();

                        value = MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, refs));
                    }
                }
                else
                {
                    RefValueRepresentation valueRef = null;
                    if (visibleParamContext.ProposedObjectFacade != null)
                    {
                        valueRef = RefValueRepresentation.Create(OidStrategy, new ObjectRelType(RelValues.Self, new UriMtHelper(OidStrategy, req, visibleParamContext.ProposedObjectFacade)), Flags);
                    }

                    value = MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, valueRef));
                }

                optionalProperties.Add(new OptionalProperty(visibleParamContext.Id, value));
            }

            return(MapRepresentation.Create(optionalProperties.ToArray()));
        }
        public static object GetPropertyValue(IOidStrategy oidStrategy, HttpRequest req, IAssociationFacade property, IObjectFacade target, RestControlFlags flags, bool valueOnly, bool useDateOverDateTime)
        {
            var valueNakedObject = property.GetValue(target);

            if (valueNakedObject == null)
            {
                return(null);
            }

            if (target.IsTransient && property.IsUsable(target).IsAllowed&& property.IsVisible(target) && property.IsSetToImplicitDefault(target))
            {
                return(null);
            }

            if (property.Specification.IsParseable || property.Specification.IsCollection)
            {
                return(RestUtils.ObjectToPredefinedType(valueNakedObject.Object, useDateOverDateTime));
            }

            if (valueOnly)
            {
                return(RefValueRepresentation.Create(oidStrategy, new ValueRelType(property, new UriMtHelper(oidStrategy, req, valueNakedObject)), flags));
            }

            var title     = RestUtils.SafeGetTitle(property, valueNakedObject);
            var helper    = new UriMtHelper(oidStrategy, req, property.IsInline ? target : valueNakedObject);
            var optionals = new List <OptionalProperty> {
                new OptionalProperty(JsonPropertyNames.Title, title)
            };

            if (property.IsEager(target))
            {
                optionals.Add(new OptionalProperty(JsonPropertyNames.Value, ObjectRepresentation.Create(oidStrategy, valueNakedObject, req, flags)));
            }

            return(LinkRepresentation.Create(oidStrategy, new ValueRelType(property, helper), flags, optionals.ToArray()));
        }