private ViewResult InvokeAction(string objectId, LambdaExpression expression, FormCollection parameters, string viewNameForFailure, string viewNameForSuccess)
        {
            INakedObject nakedObject = NakedObjectsContext.GetNakedObjectFromId(objectId);
            MethodInfo   methodInfo  = GetAction(expression);

            return(InvokeAction(nakedObject, methodInfo.Name, parameters, viewNameForFailure, viewNameForSuccess));
        }
Exemple #2
0
        private ActionResult Find(ObjectAndControlData controlData)
        {
            string spec            = controlData.DataDict["spec"];
            string contextObjectId = controlData.DataDict["contextObjectId"];
            string propertyName    = controlData.DataDict["propertyName"];
            string contextActionId = controlData.DataDict["contextActionId"];

            var objectSet = Session.CachedObjectsOfType(NakedObjectsContext, NakedObjectsContext.MetamodelManager.GetSpecification(spec)).ToList();

            if (!objectSet.Any())
            {
                Log.InfoFormat("No Cached objects of type {0} found", spec);
                NakedObjectsContext.MessageBroker.AddWarning("No objects of appropriate type viewed recently");
            }
            var contextNakedObject = FilterCollection(NakedObjectsContext.GetNakedObjectFromId(contextObjectId), controlData);
            var contextAction      = string.IsNullOrEmpty(contextActionId) ? null : NakedObjectsContext.GetActionFromId(contextActionId);

            if (objectSet.Count == 1)
            {
                var selectedItem = new Dictionary <string, string> {
                    { propertyName, NakedObjectsContext.GetObjectId(objectSet.Single()) }
                };
                return(SelectSingleItem(contextNakedObject, contextAction, controlData, selectedItem));
            }

            return(View(Request.IsAjaxRequest() ? "PropertyEdit" : "FormWithSelections", new FindViewModel {
                ActionResult = objectSet, ContextObject = contextNakedObject.Object, ContextAction = contextAction, PropertyName = propertyName
            }));
        }
Exemple #3
0
        public virtual JsonResult ValidateProperty(string id, string value, string propertyName)
        {
            INakedObject nakedObject = NakedObjectsContext.GetNakedObjectFromId(id);

            if (nakedObject.ResolveState.IsTransient())
            {
                // if transient then we cannot validate now - need to wait until save
                return(Jsonp(true));
            }

            IAssociationSpec property = ((IObjectSpec)nakedObject.Spec).Properties.SingleOrDefault(p => p.Id == propertyName);
            string           fieldId  = GetFieldInputId(nakedObject, property);

            bool isValid = false;

            if (value == null)
            {
                value = Request.Params[fieldId];
            }

            if (property != null && property is IOneToOneAssociationSpec)
            {
                ValidateAssociation(nakedObject, property as IOneToOneAssociationSpec, value);
                isValid = ModelState.IsValid;
            }

            if (isValid)
            {
                return(Jsonp(true));
            }

            ModelError error = ModelState[fieldId].Errors.FirstOrDefault();

            return(Jsonp(error == null ? "" : error.ErrorMessage));
        }
Exemple #4
0
        private ActionResult ActionOnNotPersistentObject(ObjectAndControlData controlData)
        {
            string targetActionId = controlData.DataDict["targetActionId"];
            string targetObjectId = controlData.DataDict["targetObjectId"];

            INakedObject targetNakedObject = NakedObjectsContext.GetNakedObjectFromId(targetObjectId);

            if (targetNakedObject.Spec.IsCollection)
            {
                INakedObject         filteredNakedObject = FilterCollection(targetNakedObject, controlData);
                var                  metamodel           = NakedObjectsContext.MetamodelManager.Metamodel;
                IObjectSpecImmutable elementSpecImmut    =
                    filteredNakedObject.Spec.GetFacet <ITypeOfFacet>().GetValueSpec(filteredNakedObject, metamodel);

                var elementSpec = NakedObjectsContext.MetamodelManager.GetSpecification(elementSpecImmut) as IObjectSpec;
                Trace.Assert(elementSpec != null);
                var targetAction = elementSpec.GetCollectionContributedActions().Single(a => a.Id == targetActionId);

                if (!filteredNakedObject.GetAsEnumerable(NakedObjectsContext.NakedObjectManager).Any())
                {
                    NakedObjectsContext.MessageBroker.AddWarning("No objects selected");
                    return(AppropriateView(controlData, targetNakedObject, targetAction));
                }
                // force any result to not be queryable
                filteredNakedObject.SetNotQueryable(true);
                return(ExecuteAction(controlData, filteredNakedObject, targetAction));
            }
            else
            {
                var targetAction = NakedObjectsContext.GetActions(targetNakedObject).Single(a => a.Id == targetActionId);
                return(ExecuteAction(controlData, targetNakedObject, targetAction));
            }
        }
Exemple #5
0
        public virtual FileContentResult GetFile(string Id, string PropertyId)
        {
            INakedObject     target = NakedObjectsContext.GetNakedObjectFromId(Id);
            IAssociationSpec assoc  = ((IObjectSpec)target.Spec).Properties.Single(a => a.Id == PropertyId);
            var domainObject        = assoc.GetNakedObject(target).GetDomainObject();

            return(AsFile(domainObject));
        }
        public virtual ActionResult ClearHistoryOthers(string id, ObjectAndControlData controlData)
        {
            var nakedObject = NakedObjectsContext.GetNakedObjectFromId(id);

            Session.RemoveOthersFromCache(NakedObjectsContext, nakedObject.Object, ObjectCache.ObjectFlag.BreadCrumb);
            SetNewCollectionFormats(controlData);
            SetControllerName(nakedObject.Object);
            return(AppropriateView(controlData, nakedObject));
        }
Exemple #7
0
        private ActionResult InvokeActionAsFind(ObjectAndControlData controlData)
        {
            string targetActionId  = controlData.DataDict["targetActionId"];
            string targetObjectId  = controlData.DataDict["targetObjectId"];
            string contextObjectId = controlData.DataDict["contextObjectId"];
            string propertyName    = controlData.DataDict["propertyName"];
            string contextActionId = controlData.DataDict["contextActionId"];

            INakedObject targetNakedObject  = NakedObjectsContext.GetNakedObjectFromId(targetObjectId);
            INakedObject contextNakedObject = FilterCollection(NakedObjectsContext.GetNakedObjectFromId(contextObjectId), controlData);
            IActionSpec  targetAction       = NakedObjectsContext.GetActions(targetNakedObject).Single(a => a.Id == targetActionId);
            IActionSpec  contextAction      = string.IsNullOrEmpty(contextActionId) ? null : NakedObjectsContext.GetActionFromId(contextActionId);

            SetContextObjectAsParameterValue(targetAction, contextNakedObject);

            if (ValidateParameters(targetNakedObject, targetAction, controlData))
            {
                IEnumerable <INakedObject> parms = GetParameterValues(targetAction, controlData);
                INakedObject result = targetAction.Execute(targetNakedObject, parms.ToArray());

                if (result != null)
                {
                    IEnumerable resultAsEnumerable = !result.Spec.IsCollection ? new List <object> {
                        result.Object
                    } : (IEnumerable)result.Object;

                    if (resultAsEnumerable.Cast <object>().Count() == 1)
                    {
                        var selectedItem = new Dictionary <string, string> {
                            { propertyName, NakedObjectsContext.GetObjectId(resultAsEnumerable.Cast <object>().Single()) }
                        };
                        return(SelectSingleItem(contextNakedObject, contextAction, controlData, selectedItem));
                    }
                    string view = Request.IsAjaxRequest() ? "PropertyEdit" : "FormWithSelections";
                    return(View(view, new FindViewModel {
                        ActionResult = resultAsEnumerable,
                        TargetObject = targetNakedObject.Object,
                        ContextObject = contextNakedObject.Object,
                        TargetAction = targetAction,
                        ContextAction = contextAction,
                        PropertyName = propertyName
                    }));
                }
            }
            return(View(Request.IsAjaxRequest() ? "PropertyEdit" : "FormWithFinderDialog", new FindViewModel {
                TargetObject = targetNakedObject.Object,
                ContextObject = contextNakedObject.Object,
                TargetAction = targetAction,
                ContextAction = contextAction,
                PropertyName = propertyName
            }));
        }
        public virtual ActionResult Cancel(string nextId, ObjectAndControlData controlData)
        {
            var nextNakedObject = string.IsNullOrEmpty(nextId) ? null : NakedObjectsContext.GetNakedObjectFromId(nextId);

            if (nextNakedObject == null)
            {
                return(RedirectToAction(IdHelper.IndexAction, IdHelper.HomeName));
            }

            SetNewCollectionFormats(controlData);
            SetControllerName(nextNakedObject.Object);
            return(AppropriateView(controlData, nextNakedObject));
        }
Exemple #9
0
        public virtual JsonResult GetPropertyCompletions(string id, string propertyId, string autoCompleteParm)
        {
            INakedObject   nakedObject = NakedObjectsContext.GetNakedObjectFromId(id);
            IList <object> completions = new List <object>();
            var            assoc       = ((IObjectSpec)nakedObject.Spec).Properties.OfType <IOneToOneAssociationSpec>().Single(p => p.Id == propertyId);

            if (assoc.IsAutoCompleteEnabled)
            {
                INakedObject[] nakedObjectCompletions = assoc.GetCompletions(nakedObject, autoCompleteParm);
                completions = nakedObjectCompletions.Select(no => GetCompletionData(no, assoc.ReturnSpec)).ToList();
            }

            return(Jsonp(completions));
        }
Exemple #10
0
        public virtual JsonResult GetActionCompletions(string id, string actionName, int parameterIndex, string autoCompleteParm)
        {
            INakedObject   nakedObject = NakedObjectsContext.GetNakedObjectFromId(id);
            IActionSpec    action      = NakedObjectsContext.GetActions(nakedObject).SingleOrDefault(a => a.Id == actionName);
            IList <object> completions = new List <object>();

            IActionParameterSpec p = action.Parameters[parameterIndex];

            if (p.IsAutoCompleteEnabled)
            {
                INakedObject[] nakedObjectCompletions = p.GetCompletions(nakedObject, autoCompleteParm);
                completions = nakedObjectCompletions.Select(no => GetCompletionData(no, p.Spec)).ToList();
            }

            return(Jsonp(completions));
        }
Exemple #11
0
        private ActionResult ActionAsFind(ObjectAndControlData controlData)
        {
            string targetActionId  = controlData.DataDict["targetActionId"];
            string targetObjectId  = controlData.DataDict["targetObjectId"];
            string contextObjectId = controlData.DataDict["contextObjectId"];
            string propertyName    = controlData.DataDict["propertyName"];
            string contextActionId = controlData.DataDict["contextActionId"];

            INakedObject targetNakedObject  = NakedObjectsContext.GetNakedObjectFromId(targetObjectId);
            INakedObject contextNakedObject = FilterCollection(NakedObjectsContext.GetNakedObjectFromId(contextObjectId), controlData);
            IActionSpec  targetAction       = NakedObjectsContext.GetActions(targetNakedObject).Single(a => a.Id == targetActionId);
            IActionSpec  contextAction      = string.IsNullOrEmpty(contextActionId) ? null : NakedObjectsContext.GetActionFromId(contextActionId);

            SetContextObjectAsParameterValue(targetAction, contextNakedObject);
            if (targetAction.ParameterCount == 0)
            {
                INakedObject result             = Execute(targetAction, targetNakedObject, new INakedObject[] {});
                IEnumerable  resultAsEnumerable = GetResultAsEnumerable(result, contextAction, propertyName);

                if (resultAsEnumerable.Cast <object>().Count() == 1 && result.ResolveState.IsPersistent())
                {
                    var selectedItem = new Dictionary <string, string> {
                        { propertyName, NakedObjectsContext.GetObjectId(resultAsEnumerable.Cast <object>().Single()) }
                    };
                    return(SelectSingleItem(contextNakedObject, contextAction, controlData, selectedItem));
                }

                string view = Request.IsAjaxRequest() ? "PropertyEdit" : "FormWithSelections";
                return(View(view, new FindViewModel {
                    ActionResult = resultAsEnumerable,
                    TargetObject = targetNakedObject.Object,
                    ContextObject = contextNakedObject.Object,
                    TargetAction = targetAction,
                    ContextAction = contextAction,
                    PropertyName = propertyName
                }));
            }

            SetDefaults(targetNakedObject, targetAction);
            return(View(Request.IsAjaxRequest() ? "PropertyEdit" : "FormWithFinderDialog", new FindViewModel {
                TargetObject = targetNakedObject.Object,
                ContextObject = contextNakedObject.Object,
                TargetAction = targetAction,
                ContextAction = contextAction,
                PropertyName = propertyName
            }));
        }
Exemple #12
0
        private INakedObject GetValue(string[] values, ISpecification featureSpec, ITypeSpec spec)
        {
            if (!values.Any())
            {
                return(null);
            }

            if (spec.IsParseable)
            {
                return(spec.GetFacet <IParseableFacet>().ParseTextEntry(values.First(), NakedObjectsContext.NakedObjectManager));
            }
            if (spec.IsCollection)
            {
                return(NakedObjectsContext.GetTypedCollection(featureSpec, values));
            }

            return(NakedObjectsContext.GetNakedObjectFromId(values.First()));
        }
Exemple #13
0
        public virtual JsonResult GetPropertyChoices(string id)
        {
            INakedObject nakedObject = NakedObjectsContext.GetNakedObjectFromId(id);
            IDictionary <string, string[][]>   choices     = new Dictionary <string, string[][]>();
            IDictionary <string, INakedObject> otherValues = GetOtherValues(nakedObject);

            foreach (IOneToOneAssociationSpec assoc in ((IObjectSpec)nakedObject.Spec).Properties.OfType <IOneToOneAssociationSpec>())
            {
                if (assoc.IsChoicesEnabled)
                {
                    INakedObject[] nakedObjectChoices = assoc.GetChoices(nakedObject, otherValues);
                    string[]       content            = nakedObjectChoices.Select(c => c.TitleString()).ToArray();
                    string[]       value = assoc.ReturnSpec.IsParseable ? content : nakedObjectChoices.Select(NakedObjectsContext.GetObjectId).ToArray();

                    choices[GetFieldInputId(nakedObject, assoc)] = new[] { value, content };
                }
            }
            return(Jsonp(choices));
        }
Exemple #14
0
        public virtual JsonResult GetActionChoices(string id, string actionName)
        {
            INakedObject nakedObject = NakedObjectsContext.GetNakedObjectFromId(id);
            IActionSpec  action      = NakedObjectsContext.GetActions(nakedObject).SingleOrDefault(a => a.Id == actionName);
            IDictionary <string, string[][]>   choices     = new Dictionary <string, string[][]>();
            IDictionary <string, INakedObject> otherValues = GetOtherValues(action);

            foreach (IActionParameterSpec p in action.Parameters)
            {
                if (p.IsChoicesEnabled || p.IsMultipleChoicesEnabled)
                {
                    INakedObject[] nakedObjectChoices = p.GetChoices(nakedObject, otherValues);
                    string[]       content            = nakedObjectChoices.Select(c => c.TitleString()).ToArray();
                    string[]       value = NakedObjectsContext.IsParseableOrCollectionOfParseable(p) ? content : nakedObjectChoices.Select(NakedObjectsContext.GetObjectId).ToArray();

                    choices[IdHelper.GetParameterInputId(action, p)] = new[] { value, content };
                }
            }
            return(Jsonp(choices));
        }
Exemple #15
0
        private ActionResult InvokeActionAsSave(ObjectAndControlData controlData)
        {
            var    form            = controlData.Form;
            string targetActionId  = controlData.DataDict["targetActionId"];
            string targetObjectId  = controlData.DataDict["targetObjectId"];
            string contextObjectId = controlData.DataDict["contextObjectId"];
            string propertyName    = controlData.DataDict["propertyName"];
            string contextActionId = controlData.DataDict["contextActionId"];
            string subEditObjectId = controlData.DataDict["subEditObjectId"];

            INakedObject targetNakedObject  = NakedObjectsContext.GetNakedObjectFromId(targetObjectId);
            INakedObject contextNakedObject = FilterCollection(NakedObjectsContext.GetNakedObjectFromId(contextObjectId), controlData);
            IActionSpec  targetAction       = NakedObjectsContext.GetActions(targetNakedObject).Single(a => a.Id == targetActionId);
            IActionSpec  contextAction      = string.IsNullOrEmpty(contextActionId) ? null : NakedObjectsContext.GetActionFromId(contextActionId);
            INakedObject subEditObject      = NakedObjectsContext.GetNakedObjectFromId(subEditObjectId);

            if (ValidateChanges(subEditObject, controlData))
            {
                ApplyChanges(subEditObject, controlData);
            }

            // tempting to try to associate the new object at once - however it is still transient until the end of the
            // transaction and so association may not work (possible persistent to transient). By doing this we split into two transactions
            // and so all OK.

            IEnumerable resultAsEnumerable = new List <object> {
                subEditObject.Object
            };

            return(View(Request.IsAjaxRequest() ? "PropertyEdit" : "FormWithSelections", new FindViewModel {
                ActionResult = resultAsEnumerable,
                TargetObject = targetNakedObject.Object,
                ContextObject = contextNakedObject.Object,
                TargetAction = targetAction,
                ContextAction = contextAction,
                PropertyName = propertyName
            }));
        }
Exemple #16
0
        public virtual JsonResult ValidateParameter(string id, string value, string actionName, string parameterName)
        {
            INakedObject nakedObject = NakedObjectsContext.GetNakedObjectFromId(id);
            IActionSpec  action      = NakedObjectsContext.GetActions(nakedObject).SingleOrDefault(a => a.Id == actionName);
            bool         isValid     = false;
            string       parmId      = "";

            if (action != null)
            {
                IActionParameterSpec parameter = action.Parameters.Where(p => p.Id.Equals(parameterName, StringComparison.InvariantCultureIgnoreCase)).Single();
                parmId = IdHelper.GetParameterInputId(action, parameter);

                if (value == null)
                {
                    value = Request.Params[parmId];
                }
                try {
                    INakedObject valueNakedObject = GetParameterValue(parameter, value);
                    ValidateParameter(action, parameter, nakedObject, valueNakedObject);
                }
                catch (InvalidEntryException) {
                    ModelState.AddModelError(parmId, MvcUi.InvalidEntry);
                }

                isValid = ModelState.IsValid;
            }

            if (isValid)
            {
                return(Jsonp(true));
            }

            ModelError error = ModelState[parmId].Errors.FirstOrDefault();

            return(Jsonp(error == null ? "" : error.ErrorMessage));
        }
        /// <summary>
        ///     Invoke the action on the domain object with the parameters in the form. Got to appropriate view based on result
        /// </summary>
        protected ViewResult InvokeAction(string objectId, string actionName, FormCollection parameters, String viewNameForFailure, string viewNameForSuccess = null)
        {
            INakedObject nakedObject = NakedObjectsContext.GetNakedObjectFromId(objectId);

            return(InvokeAction(nakedObject, actionName, parameters, viewNameForFailure, viewNameForSuccess));
        }
 /// <summary>
 ///     Returns the domain object that has the specified objectId (typically extracted from the URL)
 /// </summary>
 protected T GetObjectFromId <T>(string objectId)
 {
     return(NakedObjectsContext.GetNakedObjectFromId(objectId).GetDomainObject <T>());
 }