private MapRepresentation CreateArguments(HttpRequestMessage req, ActionResultContextFacade actionResult) {
            var optionalProperties = new List<OptionalProperty>();

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

                if (visibleParamContext.Specification.IsParseable) {
                    object proposedObj = visibleParamContext.ProposedObjectFacade == null ? visibleParamContext.ProposedValue : visibleParamContext.ProposedObjectFacade.GetDomainObject();
                    object valueObj = RestUtils.ObjectToPredefinedType(proposedObj);
                    value = MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, valueObj));
                }
                else if (visibleParamContext.Specification.IsCollection) {
                    if (visibleParamContext.ElementSpecification.IsParseable) {
                        var proposedCollection = ((IEnumerable) (visibleParamContext.ProposedObjectFacade == null ? visibleParamContext.ProposedValue : visibleParamContext.ProposedObjectFacade.GetDomainObject())).Cast<object>();
                        var valueObjs = proposedCollection.Select(RestUtils.ObjectToPredefinedType).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 {
                    var 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 RestSnapshot(IOidStrategy oidStrategy, ActionResultContextFacade actionResultContext, HttpRequestMessage req, RestControlFlags flags)
     : this(oidStrategy, actionResultContext, req, true) {
     populator = () => {
         Representation = ActionResultRepresentation.Create(oidStrategy, req, actionResultContext, flags);
         SetHeaders();
     };
 }
 protected ActionResultRepresentation(IOidStrategy oidStrategy, HttpRequestMessage req, ActionResultContextFacade actionResult, RestControlFlags flags)
     : base(oidStrategy, flags) {
     SelfRelType = new ActionResultRelType(RelValues.Self, new UriMtHelper(OidStrategy, req, actionResult.ActionContext));
     SetResultType(actionResult);
     SetLinks(req, actionResult);
     SetExtensions();
     SetHeader();
 }
        private void SetExtensions(ActionResultContextFacade actionResult) {

            var exts = new Dictionary<string, object>();

            AddIfPresent(exts, actionResult.Warnings, JsonPropertyNames.CustomWarnings);
            AddIfPresent(exts, actionResult.Messages, JsonPropertyNames.CustomMessages);

            Extensions = exts.Count > 0 ? RestUtils.CreateMap(exts) : new MapRepresentation();
        }
 private void SetResultType(ActionResultContextFacade actionResult) {
     if (actionResult.Specification.IsParseable) {
         ResultType = ResultTypes.Scalar;
     }
     else if (actionResult.Specification.IsCollection) {
         ResultType = ResultTypes.List;
     }
     else {
         ResultType = actionResult.Specification.IsVoid ? ResultTypes.Void : ResultTypes.Object;
     }
 }
     private void SetHeader(ActionResultContextFacade actionResult) {
         Caching = CacheType.Transactional;
 
         if (actionResult.Specification.IsObject && actionResult.Result != null) {
             if (actionResult.Result.Target.IsTransient) {
                 SetEtag(actionResult.TransientSecurityHash);
             }
             else {
                 SetEtag(actionResult.Result.Target);
             }
         }
     }
 public ActionResultContextFacade ToActionResultContextFacade(IFrameworkFacade facade) {
     var ac = new ActionResultContextFacade {
         Result = Result == null ? null : Result.ToObjectContextFacade(facade),
         ActionContext = ActionContext.ToActionContextFacade(facade),
         HasResult = HasResult
     };
     if (Reason == null) {
         Reason = ActionContext.Reason;
         ErrorCause = ActionContext.ErrorCause;
     }
     return ToContextFacade(ac, facade);
 }
        public ActionResultContextFacade ToActionResultContextFacade(IFrameworkFacade facade, INakedObjectsFramework framework) {
            var ac = new ActionResultContextFacade {
                Result = Result == null ? null : Result.ToObjectContextFacade(facade, framework),
                ActionContext = ActionContext.ToActionContextFacade(facade, framework),
                HasResult = HasResult,
                TransientSecurityHash = TransientSecurityHash
            };

            if (Reason == null) {
                Reason = ActionContext.Reason;
                ErrorCause = ActionContext.ErrorCause;
            }

            return ToContextFacade(ac, facade, framework);
        }
        public static ActionResultRepresentation Create(IOidStrategy oidStrategy, HttpRequestMessage req, ActionResultContextFacade actionResult, RestControlFlags flags) {
            if (actionResult.HasResult) {
                IRepresentation result;

                if (actionResult.Result == null) {
                    result = null;
                }
                else if (actionResult.Specification.IsParseable) {
                    result = ScalarRepresentation.Create(oidStrategy, actionResult.Result, req, flags);
                }
                else if (actionResult.Specification.IsObject) {
                    result = ObjectRepresentation.Create(oidStrategy, actionResult.Result, req, flags);
                }
                else {
                    result = PagedListRepresentation.Create(oidStrategy, actionResult, req, flags);
                }

                return CreateWithOptionals<ActionResultRepresentation>(new object[] {oidStrategy, req, actionResult, flags}, new[] {new OptionalProperty(JsonPropertyNames.Result, result)});
            }
            return new ActionResultRepresentation(oidStrategy, req, actionResult, flags);
        }
 private void SetLinks(HttpRequestMessage req, ActionResultContextFacade actionResult) {
     Links = actionResult.ActionContext.Action.IsQueryOnly ? new[] {LinkRepresentation.Create(OidStrategy, SelfRelType, Flags, new OptionalProperty(JsonPropertyNames.Arguments, CreateArguments(req, actionResult)))} : new LinkRepresentation[] {};
 }
 public static ListRepresentation Create(IOidStrategy oidStrategy, ActionResultContextFacade actionResultContext, HttpRequestMessage req, RestControlFlags flags) {
     return new ListRepresentation(oidStrategy, actionResultContext.Result, req, flags, actionResultContext.ActionContext);
 }
 private bool HasError(ActionResultContextFacade ar) {
     return !string.IsNullOrEmpty(ar.ActionContext.Reason) || ar.ActionContext.VisibleParameters.Any(p => !string.IsNullOrEmpty(p.Reason));
 }
        private IObjectFacade GetResult(ActionResultContextFacade context) {

            if (context.HasResult && context.Result != null) {
                var result = context.Result;

                if (result.RedirectedUrl != null) {
                    throw new RedirectException(result.RedirectedUrl);
                }

                return result.Target;
            }
            return null;
        }