private PropertyContext GetProperty(NakedObject nakedObject, string propertyName, bool onlyVisible = true)
        {
            if (string.IsNullOrEmpty(propertyName.Trim()))
            {
                throw new BadRequestNOSException();
            }
            string nof2Id = propertyName.ToLower();

            IEnumerable <NakedObjectField> fields = nakedObject.getSpecification().getFields();

            if (onlyVisible)
            {
                fields = fields.Where(p => !p.isHidden() && p.isVisible(nakedObject).isAllowed());
            }

            NakedObjectField property = fields.SingleOrDefault(p => p.getId() == nof2Id);

            if (property == null)
            {
                throw new PropertyResourceNotFoundNOSException(propertyName);
            }

            if (!property.isCollection())
            {
                property.get(nakedObject); // get value so any errors happen inside error mapping code
            }

            return(new PropertyContext {
                Target = nakedObject, Property = property
            });
        }
        private ObjectContextFacade SetObject(NakedObject nakedObject, ArgumentsContextFacade arguments)
        {
            if (nakedObject.getSpecification().getFields().Where(f => !f.isCollection()).Any(p => !arguments.Values.Keys.Select(s => s.ToLower()).Contains(p.getId())))
            {
                throw new BadRequestNOSException("Malformed arguments");
            }

            PropertyContext[] pc = arguments.Values.Select(kvp => CanSetProperty(nakedObject, kvp.Key, arguments.ValidateOnly, kvp.Value)).ToArray();
            var objectContext    = new ObjectContext(pc.First().Target)
            {
                VisibleProperties = pc
            };

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

            if (pc.All(c => string.IsNullOrEmpty(c.Reason)))
            {
                if (nakedObject.getResolveState().isTransient())
                {
                    if (nakedObject.getSpecification().persistable() == Persistable.USER_PERSISTABLE)
                    {
                        [email protected]().makePersistent(nakedObject);
                    }
                    else
                    {
                        [email protected]().objectChanged(nakedObject);
                    }
                }

                propertiesToDisplay = nakedObject.getSpecification().getFields().
                                      Where(p => !p.isHidden() && p.isVisible(nakedObject).isAllowed()).
                                      Select(p => new PropertyContext {
                    Target = nakedObject, Property = p
                }).ToArray();
            }

            ObjectContext oc = GetObjectContext(objectContext.Target);

            oc.Mutated           = false;
            oc.Reason            = objectContext.Reason;
            oc.VisibleProperties = propertiesToDisplay;
            return(oc.ToObjectContextFacade(this));
        }
        private static ActionContext GetAction(string actionName, NakedObject nakedObject)
        {
            if (string.IsNullOrWhiteSpace(actionName.Trim()))
            {
                throw new BadRequestNOSException();
            }

            ActionWrapper action = nakedObject.getSpecification().GetActionLeafNodes().
                                   Where(p => p.isVisible(nakedObject).isAllowed()).SingleOrDefault(p => p.getId() == actionName);

            if (action == null)
            {
                throw new ActionResourceNotFoundNOSException(actionName);
            }

            return(new ActionContext {
                Target = nakedObject, Action = action, VisibleParameters = GetContextParms(action, nakedObject)
            });
        }
        private ObjectContextFacade ChangeObject(NakedObject nakedObject, ArgumentsContextFacade arguments)
        {
            ValidateConcurrency(nakedObject, arguments.Digest);

            PropertyContext[] pc;
            try {
                pc = arguments.Values.Select(kvp => CanChangeProperty(nakedObject, kvp.Key, arguments.ValidateOnly, kvp.Value)).ToArray();
            }
            catch (PropertyResourceNotFoundNOSException e) {
                // no matching property for argument - consider this a syntax error
                throw new BadRequestNOSException(e.Message);
            }

            var objectContext = new ObjectContext(pc.First().Target)
            {
                VisibleProperties = pc
            };

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

            if (pc.All(c => string.IsNullOrEmpty(c.Reason)))
            {
                propertiesToDisplay = nakedObject.getSpecification().getFields().
                                      Where(p => !p.isHidden() && p.isVisible(nakedObject).isAllowed()).
                                      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.ToObjectContextFacade(this));
        }
 public static ActionWrapper[] GetActionLeafNodes(this NakedObject nakedObject)
 {
     return(nakedObject.getSpecification().GetActionLeafNodes());
 }