private ObjectContextSurface SetObject(INakedObject nakedObject, ArgumentsContext arguments) {
            if (((IObjectSpec) nakedObject.Spec).Properties.OfType<IOneToOneAssociationSpec>().Any(p => !arguments.Values.Keys.Contains(p.Id))) {
                throw new BadRequestNOSException("Malformed arguments");
            }

            Dictionary<string, PropertyContext> contexts = arguments.Values.ToDictionary(kvp => kvp.Key, kvp => CanSetProperty(nakedObject, kvp.Key, kvp.Value));
            var objectContext = new ObjectContext(contexts.First().Value.Target) {VisibleProperties = contexts.Values.ToArray()};

            // if we fail we need to display all - if OK only those that are visible 
            PropertyContext[] propertiesToDisplay = objectContext.VisibleProperties;

            if (contexts.Values.All(c => string.IsNullOrEmpty(c.Reason))) {
                if (ConsentHandler(CrossValidate(objectContext), objectContext, Cause.Other)) {
                    if (!arguments.ValidateOnly) {
                        Array.ForEach(objectContext.VisibleProperties, SetProperty);

                        if (nakedObject.Spec.Persistable == PersistableType.UserPersistable) {
                            framework.LifecycleManager.MakePersistent(nakedObject);
                        }
                        else {
                            framework.Persistor.ObjectChanged(nakedObject, framework.LifecycleManager, framework.MetamodelManager);
                        }
                        propertiesToDisplay = ((IObjectSpec)nakedObject.Spec).Properties.
                            Where(p => p.IsVisible(nakedObject)).
                            Select(p => new PropertyContext {Target = nakedObject, Property = p}).ToArray();
                    }
                }
            }

            ObjectContext oc = GetObjectContext(objectContext.Target);
            oc.Reason = objectContext.Reason;
            oc.VisibleProperties = propertiesToDisplay;
            return oc.ToObjectContextSurface(this, framework);
        }
        private ObjectContextSurface CreateObject(string typeName, ArgumentsContext arguments) {
            if (string.IsNullOrWhiteSpace(typeName)) {
                throw new BadRequestNOSException();
            }

            IObjectSpec spec = (IObjectSpec) GetDomainTypeInternal(typeName);
            INakedObject nakedObject = framework.LifecycleManager.CreateInstance(spec);

            return SetObject(nakedObject, arguments);
        }
        private ObjectContextSurface ChangeObject(INakedObject nakedObject, ArgumentsContext arguments) {
            ValidateConcurrency(nakedObject, arguments.Digest);

            Dictionary<string, PropertyContext> contexts;
            try {
                contexts = arguments.Values.ToDictionary(kvp => kvp.Key, kvp => CanChangeProperty(nakedObject, kvp.Key, kvp.Value));
            }
            catch (PropertyResourceNotFoundNOSException e) {
                // no matching property for argument - consider this a syntax error 
                throw new BadRequestNOSException(e.Message);
            }


            var objectContext = new ObjectContext(contexts.First().Value.Target) {VisibleProperties = contexts.Values.ToArray()};

            // if we fail we need to display passed in properties - if OK all visible
            PropertyContext[] propertiesToDisplay = objectContext.VisibleProperties;

            if (contexts.Values.All(c => string.IsNullOrEmpty(c.Reason))) {
                if (ConsentHandler(CrossValidate(objectContext), objectContext, Cause.Other)) {
                    if (!arguments.ValidateOnly) {
                        Array.ForEach(objectContext.VisibleProperties, SetProperty);
                    }

                    propertiesToDisplay = ((IObjectSpec)nakedObject.Spec).Properties.
                        Where(p => p.IsVisible(nakedObject)).
                        Select(p => new PropertyContext {Target = nakedObject, Property = p}).ToArray();
                }
            }

            ObjectContext oc = GetObjectContext(objectContext.Target);
            oc.Mutated = true;
            oc.Reason = objectContext.Reason;
            oc.VisibleProperties = propertiesToDisplay;
            return oc.ToObjectContextSurface(this, framework);
        }
 public INakedObject[] GetList(INakedObject nakedObject, ArgumentsContext arguments) {
     return IsAutoCompleteEnabled ? GetAutocompleteList(nakedObject, arguments) : GetConditionalList(nakedObject, arguments);
 }
        private ActionResultContextSurface ExecuteAction(ActionContext actionContext, ArgumentsContext arguments) {
            ValidateConcurrency(actionContext.Target, arguments.Digest);

            var actionResultContext = new ActionResultContext {Target = actionContext.Target, ActionContext = actionContext};
            if (ConsentHandler(actionContext.Action.IsUsable(actionContext.Target), actionResultContext, Cause.Disabled)) {
                if (ValidateParameters(actionContext, arguments.Values) && !arguments.ValidateOnly) {
                    INakedObject result = actionContext.Action.Execute(actionContext.Target, actionContext.VisibleParameters.Select(p => p.ProposedNakedObject).ToArray());
                    actionResultContext.Result = GetObjectContext(result);
                }
            }
            return actionResultContext.ToActionResultContextSurface(this, framework);
        }
Пример #6
0
 public MockComparisonContext(SelectorContext selectorContext, ArgumentsContext argumentsContext) : base(null, 0)
 {
     _selectorContext  = selectorContext;
     _argumentsContext = argumentsContext;
 }
 private ListContext GetParameterCompletions(INakedObject nakedObject, string actionName, string parmName, ArgumentsContext arguments) {
     IActionParameterSpec parm = GetParameterInternal(actionName, parmName, nakedObject);
     return GetCompletions(new PropParmAdapter(parm, this, framework), nakedObject, arguments);
 }
 public ActionResultContextSurface ExecuteServiceAction(LinkObjectId serviceName, string actionName, ArgumentsContext arguments) {
     return MapErrors(() => {
         ActionContext actionContext = GetInvokeActionOnService(serviceName, actionName);
         return ExecuteAction(actionContext, arguments);
     });
 }
        private ListContext GetCompletions(PropParmAdapter propParm, INakedObject nakedObject, ArgumentsContext arguments) {
            INakedObject[] list = propParm.GetList(nakedObject, arguments);

            return new ListContext {
                ElementType = propParm.Specification,
                List = list,
                IsListOfServices = false
            };
        }
 public ListContextSurface GetServiceParameterCompletions(LinkObjectId objectId, string actionName, string parmName, ArgumentsContext arguments) {
     return MapErrors(() => GetParameterCompletions(GetServiceAsNakedObject(objectId), actionName, parmName, arguments).ToListContextSurface(this, framework));
 }
 public ActionResultContextSurface ExecuteObjectAction(LinkObjectId objectId, string actionName, ArgumentsContext arguments) {
     return MapErrors(() => {
         ActionContext actionContext = GetInvokeActionOnObject(objectId, actionName);
         return ExecuteAction(actionContext, arguments);
     });
 }
 public ListContextSurface GetPropertyCompletions(LinkObjectId objectId, string propertyName, ArgumentsContext arguments) {
     return MapErrors(() => GetPropertyCompletions(GetObjectAsNakedObject(objectId), propertyName, arguments).ToListContextSurface(this, framework));
 }
 public ObjectContextSurface PutObject(LinkObjectId oid, ArgumentsContext arguments) {
     return MapErrors(() => ChangeObject(GetObjectAsNakedObject(oid), arguments));
 }
 public ObjectContextSurface Persist(string typeName, ArgumentsContext arguments) {
     return MapErrors(() => CreateObject(typeName, arguments));
 }
            private INakedObject[] GetConditionalList(INakedObject nakedObject, ArgumentsContext arguments) {
                Tuple<string, IObjectSpec>[] expectedParms = GetChoicesParameters();
                IDictionary<string, object> actualParms = arguments.Values;

                string[] expectedParmNames = expectedParms.Select(t => t.Item1).ToArray();
                string[] actualParmNames = actualParms.Keys.ToArray();

                if (expectedParmNames.Count() < actualParmNames.Count()) {
                    throw new BadRequestNOSException("Wrong number of conditional arguments");
                }

                if (!actualParmNames.All(expectedParmNames.Contains)) {
                    throw new BadRequestNOSException("Unrecognised conditional argument(s)");
                }

                Func<Tuple<string, IObjectSpec>, object> getValue = ep => {
                    if (actualParms.ContainsKey(ep.Item1)) {
                        return actualParms[ep.Item1];
                    }
                    return ep.Item2.IsParseable ? "" : null;
                };


                var matchedParms = expectedParms.ToDictionary(ep => ep.Item1, ep => new {
                    expectedType = ep.Item2,
                    value = getValue(ep),
                    actualType = getValue(ep) == null ? null : framework.MetamodelManager.GetSpecification(getValue(ep).GetType())
                });

                var errors = new List<ContextSurface>();

                var mappedArguments = new Dictionary<string, INakedObject>();

                foreach (var ep in expectedParms) {
                    string key = ep.Item1;
                    var mp = matchedParms[key];
                    object value = mp.value;
                    IObjectSpec expectedType = mp.expectedType;
                    ITypeSpec actualType = mp.actualType;

                    if (expectedType.IsParseable && actualType.IsParseable) {
                        string rawValue = value.ToString();

                        try {
                            mappedArguments[key] = expectedType.GetFacet<IParseableFacet>().ParseTextEntry(rawValue, framework.NakedObjectManager);

                            errors.Add(new ChoiceContextSurface(key, GetSpecificationWrapper(expectedType)) {
                                ProposedValue = rawValue
                            });
                        }
                        catch (Exception e) {
                            errors.Add(new ChoiceContextSurface(key, GetSpecificationWrapper(expectedType)) {
                                Reason = e.Message,
                                ProposedValue = rawValue
                            });
                        }
                    }
                    else if (actualType != null && !actualType.IsOfType(expectedType)) {
                        errors.Add(new ChoiceContextSurface(key, GetSpecificationWrapper(expectedType)) {
                            Reason = string.Format("Argument is of wrong type is {0} expect {1}", actualType.FullName, expectedType.FullName),
                            ProposedValue = actualParms[ep.Item1]
                        });
                    }
                    else {
                        mappedArguments[key] = framework.NakedObjectManager.CreateAdapter(value, null, null);

                        errors.Add(new ChoiceContextSurface(key, GetSpecificationWrapper(expectedType)) {
                            ProposedValue = getValue(ep)
                        });
                    }
                }

                if (errors.Any(e => !string.IsNullOrEmpty(e.Reason))) {
                    throw new BadRequestNOSException("Wrong type of conditional argument(s)", errors);
                }

                return GetChoices(nakedObject, mappedArguments);
            }
 private ListContext GetPropertyCompletions(INakedObject nakedObject, string propertyName, ArgumentsContext arguments) {
     var property = GetPropertyInternal(nakedObject, propertyName) as IOneToOneAssociationSpec;
     return GetCompletions(new PropParmAdapter(property, this, framework), nakedObject, arguments);
 }
 private INakedObject[] GetAutocompleteList(INakedObject nakedObject, ArgumentsContext arguments) {
     if (arguments.SearchTerm == null) {
         throw new BadRequestNOSException("Missing or malformed search term");
     }
     return GetCompletions(nakedObject, arguments.SearchTerm);
 }
Пример #18
0
        public ArgumentsContext arguments()
        {
            ArgumentsContext _localctx = new ArgumentsContext(Context, State);

            EnterRule(_localctx, 16, RULE_arguments);
            int _la;

            try {
                State = 70;
                ErrorHandler.Sync(this);
                switch (TokenStream.LA(1))
                {
                case GROUP_START:
                    EnterOuterAlt(_localctx, 1);
                    {
                        {
                            State = 58; Match(GROUP_START);
                            State = 59; value();
                            State = 64;
                            ErrorHandler.Sync(this);
                            _la = TokenStream.LA(1);
                            while (_la == T__1)
                            {
                                {
                                    {
                                        State = 60; Match(T__1);
                                        State = 61; value();
                                    }
                                }
                                State = 66;
                                ErrorHandler.Sync(this);
                                _la = TokenStream.LA(1);
                            }
                            State = 67; Match(GROUP_END);
                        }
                    }
                    break;

                case T__8:
                case T__9:
                case T__10:
                case T__11:
                case T__12:
                case T__13:
                case T__14:
                case T__15:
                case T__16:
                case T__17:
                case T__18:
                case T__19:
                case T__20:
                case T__21:
                case T__22:
                case SINGLE_QUOTE:
                case DOUBLE_QUOTE:
                case WHITESPACE:
                case ANY:
                    EnterOuterAlt(_localctx, 2);
                    {
                        State = 69; value();
                    }
                    break;

                default:
                    throw new NoViableAltException(this);
                }
            }
            catch (RecognitionException re) {
                _localctx.exception = re;
                ErrorHandler.ReportError(this, re);
                ErrorHandler.Recover(this, re);
            }
            finally {
                ExitRule();
            }
            return(_localctx);
        }