Esempio n. 1
1
        public static MapRepresentation Create(HttpRequestMessage req, IList<ContextSurface> contexts, Format format, RestControlFlags flags, MediaTypeHeaderValue mt) {
            OptionalProperty[] memberValues = contexts.Select(c => new OptionalProperty(c.Id, GetMap(req, c, flags))).ToArray();
            INakedObjectSurface target = contexts.First().Target;
            MapRepresentation mapRepresentation;

            if (format == Format.Full) {
                var tempProperties = new List<OptionalProperty>();

                if (flags.SimpleDomainModel) {
                    var dt = new OptionalProperty(JsonPropertyNames.DomainType, target.Specification.DomainTypeName());
                    tempProperties.Add(dt);
                }

                if (flags.FormalDomainModel) {
                    var links = new OptionalProperty(JsonPropertyNames.Links, new[] {
                        Create(new OptionalProperty(JsonPropertyNames.Rel, RelValues.DescribedBy),
                               new OptionalProperty(JsonPropertyNames.Href, new UriMtHelper(req, target.Specification).GetDomainTypeUri()))
                    });
                    tempProperties.Add(links);
                }

                var members = new OptionalProperty(JsonPropertyNames.Members, Create(memberValues));
                tempProperties.Add(members);
                mapRepresentation = Create(tempProperties.ToArray());
            }
            else {
                mapRepresentation = Create(memberValues);
            }

            mapRepresentation.SetContentType(mt);

            return mapRepresentation;
        }
Esempio n. 2
0
        private LinkRepresentation CreatePromptLink(HttpRequestMessage req, INakedObjectSurface nakedObject, INakedObjectActionParameterSurface parameter) {
            var opts = new List<OptionalProperty>();

            var parameterContext = new ParameterContextSurface {
                Action = parameter.Action,
                Target = nakedObject,
                Parameter = parameter
            };

            if (parameter.IsAutoCompleteEnabled) {
                var arguments = new OptionalProperty(JsonPropertyNames.Arguments, MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.XRoSearchTerm, MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, null, typeof (object))))));
                var extensions = new OptionalProperty(JsonPropertyNames.Extensions, MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.MinLength, parameter.AutoCompleteMinLength())));

                opts.Add(arguments);
                opts.Add(extensions);
            }
            else {
                Tuple<string, INakedObjectSpecificationSurface>[] parms = parameter.GetChoicesParameters();
                OptionalProperty[] args = parms.Select(tuple => RestUtils.CreateArgumentProperty(req, tuple, Flags)).ToArray();
                var arguments = new OptionalProperty(JsonPropertyNames.Arguments, MapRepresentation.Create(args));
                opts.Add(arguments);
            }

            return LinkRepresentation.Create(new PromptRelType(new UriMtHelper(req, parameterContext)), Flags, opts.ToArray());
        }
        private LinkRepresentation CreatePromptLink(HttpRequestMessage req, INakedObjectSurface nakedObject, INakedObjectActionParameterSurface parameter)
        {
            var opts = new List <OptionalProperty>();

            var parameterContext = new ParameterContextSurface {
                Action    = parameter.Action,
                Target    = nakedObject,
                Parameter = parameter
            };

            if (parameter.IsAutoCompleteEnabled)
            {
                var arguments  = new OptionalProperty(JsonPropertyNames.Arguments, MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.XRoSearchTerm, MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, null, typeof(object))))));
                var extensions = new OptionalProperty(JsonPropertyNames.Extensions, MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.MinLength, parameter.AutoCompleteMinLength())));

                opts.Add(arguments);
                opts.Add(extensions);
            }
            else
            {
                Tuple <string, INakedObjectSpecificationSurface>[] parms = parameter.GetChoicesParameters();
                OptionalProperty[] args = parms.Select(tuple => RestUtils.CreateArgumentProperty(req, tuple, Flags)).ToArray();
                var arguments           = new OptionalProperty(JsonPropertyNames.Arguments, MapRepresentation.Create(args));
                opts.Add(arguments);
            }

            return(LinkRepresentation.Create(new PromptRelType(new UriMtHelper(req, parameterContext)), Flags, opts.ToArray()));
        }
        private void SetMembers(ObjectContextSurface objectContext, HttpRequestMessage req, List <LinkRepresentation> tempLinks)
        {
            PropertyContextSurface[] visiblePropertiesAndCollections = objectContext.VisibleProperties;

            if (!Flags.BlobsClobs)
            {
                // filter any blobs and clobs
                visiblePropertiesAndCollections = visiblePropertiesAndCollections.Where(vp => !RestUtils.IsBlobOrClob(vp.Specification)).ToArray();
            }

            PropertyContextSurface[] visibleProperties = visiblePropertiesAndCollections.Where(p => !p.Property.IsCollection()).ToArray();

            if (!objectContext.Target.IsTransient() && visibleProperties.Any(p => p.Property.IsUsable(objectContext.Target).IsAllowed))
            {
                string[]           ids   = visibleProperties.Where(p => p.Property.IsUsable(objectContext.Target).IsAllowed&& !p.Property.IsInline()).Select(p => p.Id).ToArray();
                OptionalProperty[] props = ids.Select(s => new OptionalProperty(s, MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, null, typeof(object))))).ToArray();

                LinkRepresentation modifyLink = LinkRepresentation.Create(new ObjectRelType(RelValues.Update, new UriMtHelper(req, objectContext.Target))
                {
                    Method = RelMethod.Put
                }, Flags,
                                                                          new OptionalProperty(JsonPropertyNames.Arguments, MapRepresentation.Create(props)));

                tempLinks.Add(modifyLink);
            }

            if (objectContext.Target.IsTransient())
            {
                KeyValuePair <string, object>[] ids = objectContext.Target.Specification.Properties.Where(p => !p.IsCollection() && !p.IsInline()).ToDictionary(p => p.Id, p => GetPropertyValue(req, p, objectContext.Target, Flags, true)).ToArray();
                OptionalProperty[] props            = ids.Select(kvp => new OptionalProperty(kvp.Key, MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, kvp.Value)))).ToArray();

                var argMembers = new OptionalProperty(JsonPropertyNames.Members, MapRepresentation.Create(props));
                var args       = new List <OptionalProperty> {
                    argMembers
                };

                LinkRepresentation persistLink = LinkRepresentation.Create(new ObjectsRelType(RelValues.Persist, new UriMtHelper(req, objectContext.Target.Specification))
                {
                    Method = RelMethod.Post
                }, Flags,
                                                                           new OptionalProperty(JsonPropertyNames.Arguments, MapRepresentation.Create(args.ToArray())));

                tempLinks.Add(persistLink);
            }

            InlineMemberAbstractRepresentation[] properties = visiblePropertiesAndCollections.Select(p => InlineMemberAbstractRepresentation.Create(req, p, Flags)).ToArray();

            InlineActionRepresentation[] actions = objectContext.Target.IsTransient() ? new InlineActionRepresentation[] {}
                : objectContext.VisibleActions.Select(a => InlineActionRepresentation.Create(req, a, Flags)).ToArray();


            IEnumerable <InlineMemberAbstractRepresentation> allMembers = properties.Union(actions);

            Members = RestUtils.CreateMap(allMembers.ToDictionary(m => m.Id, m => (object)m));
        }
        public static MapRepresentation Create(IOidStrategy oidStrategy, HttpRequestMessage req, ContextFacade contextFacade, IList <ContextFacade> contexts, Format format, RestControlFlags flags, MediaTypeHeaderValue mt)
        {
            var               memberValues = contexts.Select(c => new OptionalProperty(c.Id, GetMap(oidStrategy, req, c, flags))).ToList();
            IObjectFacade     target       = contexts.First().Target;
            MapRepresentation mapRepresentation;

            if (format == Format.Full)
            {
                var tempProperties = new List <OptionalProperty>();

                if (contextFacade != null && !string.IsNullOrEmpty(contextFacade.Reason))
                {
                    tempProperties.Add(new OptionalProperty(JsonPropertyNames.XRoInvalidReason, contextFacade.Reason));
                }

                if (flags.SimpleDomainModel)
                {
                    var dt = new OptionalProperty(JsonPropertyNames.DomainType, target.Specification.DomainTypeName(oidStrategy));
                    tempProperties.Add(dt);
                }

                if (flags.FormalDomainModel)
                {
                    var links = new OptionalProperty(JsonPropertyNames.Links, new[] {
                        Create(new OptionalProperty(JsonPropertyNames.Rel, RelValues.DescribedBy),
                               new OptionalProperty(JsonPropertyNames.Href, new UriMtHelper(oidStrategy, req, target.Specification).GetDomainTypeUri()))
                    });
                    tempProperties.Add(links);
                }

                if (contextFacade != null && !string.IsNullOrEmpty(contextFacade.Reason))
                {
                    tempProperties.Add(new OptionalProperty(JsonPropertyNames.XRoInvalidReason, contextFacade.Reason));
                }


                var members = new OptionalProperty(JsonPropertyNames.Members, Create(memberValues.ToArray()));
                tempProperties.Add(members);
                mapRepresentation = Create(tempProperties.ToArray());
            }
            else
            {
                mapRepresentation = Create(memberValues.ToArray());
            }

            mapRepresentation.SetContentType(mt);

            return(mapRepresentation);
        }
        private LinkRepresentation CreatePromptLink() {
            var opts = new List<OptionalProperty>();

            if (propertyContext.Property.IsAutoCompleteEnabled) {
                var arguments = new OptionalProperty(JsonPropertyNames.Arguments, MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.XRoSearchTerm, MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, null, typeof (object))))));
                var extensions = new OptionalProperty(JsonPropertyNames.Extensions, MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.MinLength, propertyContext.Property.AutoCompleteMinLength)));

                opts.Add(arguments);
                opts.Add(extensions);
            }
            else {
                Tuple<string, ITypeFacade>[] parms = propertyContext.Property.GetChoicesParameters();
                OptionalProperty[] args = parms.Select(pnt => RestUtils.CreateArgumentProperty(OidStrategy ,req, pnt, Flags)).ToArray();
                var arguments = new OptionalProperty(JsonPropertyNames.Arguments, MapRepresentation.Create(args));
                opts.Add(arguments);
            }

            return LinkRepresentation.Create(OidStrategy,new PromptRelType(new UriMtHelper(OidStrategy ,req, propertyContext)), Flags, opts.ToArray());
        }
        public static MapRepresentation Create(IOidStrategy oidStrategy, HttpRequestMessage req, ContextFacade contextFacade, IList<ContextFacade> contexts, Format format, RestControlFlags flags, MediaTypeHeaderValue mt) {
            var memberValues = contexts.Select(c => new OptionalProperty(c.Id, GetMap(oidStrategy, req, c, flags))).ToList();
            IObjectFacade target = contexts.First().Target;
            MapRepresentation mapRepresentation;

            if (format == Format.Full) {
                var tempProperties = new List<OptionalProperty>();

                if (contextFacade != null &&  !string.IsNullOrEmpty(contextFacade.Reason)) {
                    tempProperties.Add(new OptionalProperty(JsonPropertyNames.XRoInvalidReason, contextFacade.Reason));
                }

                if (flags.SimpleDomainModel) {
                    var dt = new OptionalProperty(JsonPropertyNames.DomainType, target.Specification.DomainTypeName(oidStrategy));
                    tempProperties.Add(dt);
                }

                if (flags.FormalDomainModel) {
                    var links = new OptionalProperty(JsonPropertyNames.Links, new[] {
                        Create(new OptionalProperty(JsonPropertyNames.Rel, RelValues.DescribedBy),
                            new OptionalProperty(JsonPropertyNames.Href, new UriMtHelper(oidStrategy, req, target.Specification).GetDomainTypeUri()))
                    });
                    tempProperties.Add(links);
                }

                if (contextFacade != null && !string.IsNullOrEmpty(contextFacade.Reason)) {
                    tempProperties.Add(new OptionalProperty(JsonPropertyNames.XRoInvalidReason, contextFacade.Reason));
                }


                var members = new OptionalProperty(JsonPropertyNames.Members, Create(memberValues.ToArray()));
                tempProperties.Add(members);
                mapRepresentation = Create(tempProperties.ToArray());
            }
            else {
                mapRepresentation = Create(memberValues.ToArray());
            }

            mapRepresentation.SetContentType(mt);

            return mapRepresentation;
        }
        public static MapRepresentation Create(HttpRequestMessage req, IList <ContextSurface> contexts, Format format, RestControlFlags flags, MediaTypeHeaderValue mt)
        {
            OptionalProperty[]  memberValues = contexts.Select(c => new OptionalProperty(c.Id, GetMap(req, c, flags))).ToArray();
            INakedObjectSurface target       = contexts.First().Target;
            MapRepresentation   mapRepresentation;

            if (format == Format.Full)
            {
                var tempProperties = new List <OptionalProperty>();

                if (flags.SimpleDomainModel)
                {
                    var dt = new OptionalProperty(JsonPropertyNames.DomainType, target.Specification.DomainTypeName());
                    tempProperties.Add(dt);
                }

                if (flags.FormalDomainModel)
                {
                    var links = new OptionalProperty(JsonPropertyNames.Links, new[] {
                        Create(new OptionalProperty(JsonPropertyNames.Rel, RelValues.DescribedBy),
                               new OptionalProperty(JsonPropertyNames.Href, new UriMtHelper(req, target.Specification).GetDomainTypeUri()))
                    });
                    tempProperties.Add(links);
                }

                var members = new OptionalProperty(JsonPropertyNames.Members, Create(memberValues));
                tempProperties.Add(members);
                mapRepresentation = Create(tempProperties.ToArray());
            }
            else
            {
                mapRepresentation = Create(memberValues);
            }

            mapRepresentation.SetContentType(mt);

            return(mapRepresentation);
        }
        private void SetMembers(ObjectContextFacade objectContext, HttpRequestMessage req, List<LinkRepresentation> tempLinks) {
            PropertyContextFacade[] visiblePropertiesAndCollections = objectContext.VisibleProperties;

            if (!Flags.BlobsClobs) {
                // filter any blobs and clobs 
                visiblePropertiesAndCollections = visiblePropertiesAndCollections.Where(vp => !RestUtils.IsBlobOrClob(vp.Specification)).ToArray();
            }

            PropertyContextFacade[] visibleProperties = visiblePropertiesAndCollections.Where(p => !p.Property.IsCollection).ToArray();

            if (!objectContext.Target.IsTransient && visibleProperties.Any(p => p.Property.IsUsable(objectContext.Target).IsAllowed)) {
                string[] ids = visibleProperties.Where(p => p.Property.IsUsable(objectContext.Target).IsAllowed && !p.Property.IsInline).Select(p => p.Id).ToArray();
                OptionalProperty[] props = ids.Select(s => new OptionalProperty(s, MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, null, typeof (object))))).ToArray();

                LinkRepresentation modifyLink = LinkRepresentation.Create(OidStrategy, new ObjectRelType(RelValues.Update, new UriMtHelper(OidStrategy ,req, objectContext.Target)) {Method = RelMethod.Put}, Flags,
                    new OptionalProperty(JsonPropertyNames.Arguments, MapRepresentation.Create(props)));

                tempLinks.Add(modifyLink);
            }

            if (objectContext.Target.IsTransient) {
                KeyValuePair<string, object>[] ids = objectContext.Target.Specification.Properties.Where(p => !p.IsCollection && !p.IsInline).ToDictionary(p => p.Id, p => GetPropertyValue(OidStrategy ,req, p, objectContext.Target, Flags, true)).ToArray();
                OptionalProperty[] props = ids.Select(kvp => new OptionalProperty(kvp.Key, MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, kvp.Value)))).ToArray();

                var argMembers = new OptionalProperty(JsonPropertyNames.Members, MapRepresentation.Create(props));
                var args = new List<OptionalProperty> {argMembers};

                LinkRepresentation persistLink = LinkRepresentation.Create(OidStrategy, new ObjectsRelType(RelValues.Persist, new UriMtHelper(OidStrategy, req, objectContext.Target.Specification)) { Method = RelMethod.Post }, Flags,
                    new OptionalProperty(JsonPropertyNames.Arguments, MapRepresentation.Create(args.ToArray())));

                tempLinks.Add(persistLink);
            }

            InlineMemberAbstractRepresentation[] properties = visiblePropertiesAndCollections.Select(p => InlineMemberAbstractRepresentation.Create(OidStrategy ,req, p, Flags)).ToArray();

            InlineActionRepresentation[] actions = objectContext.Target.IsTransient ? new InlineActionRepresentation[] {}
                : objectContext.VisibleActions.Select(a => InlineActionRepresentation.Create(OidStrategy ,req, a, Flags)).ToArray();


            IEnumerable<InlineMemberAbstractRepresentation> allMembers = properties.Union(actions);

            Members = RestUtils.CreateMap(allMembers.ToDictionary(m => m.Id, m => (object) m));
        }