Exemple #1
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 #2
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
            }));
        }
Exemple #3
0
        private ActionResult ApplyEditAction(ObjectAndControlData controlData)
        {
            var nakedObject = controlData.GetNakedObject(NakedObjectsContext);
            var ok          = ValidateChanges(nakedObject, controlData) && ApplyChanges(nakedObject, controlData);

            if (ok)
            {
                string      targetActionId = controlData.DataDict["targetActionId"];
                IActionSpec targetAction   = NakedObjectsContext.GetActions(nakedObject).Single(a => a.Id == targetActionId);
                return(ExecuteAction(controlData, nakedObject, targetAction));
            }
            return(View("ViewModel", nakedObject.Object));
        }
Exemple #4
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 #5
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 #6
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 #7
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 #8
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));
        }