/// <summary> /// Ensures that parameters in an action dialog contain default values specified in the Model. /// Should be called in a Controller method that sets up a view that will contain an action dialog. /// </summary> /// <example> /// SetUpDefaultParameters(customer, "CreateNewAddress") /// </example> protected void SetUpDefaultParameters(object domainObject, string actionName) { INakedObject nakedObject = NakedObjectsContext.GetNakedObject(domainObject); IActionSpec findOrder = nakedObject.Spec.GetObjectActions().Single(x => x.Id == actionName); SetDefaults(nakedObject, findOrder); }
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)); }
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 })); }
/// <summary> /// Invoke the action on the domain object with the parameters in the form. Return result and update valid bool to indicate if /// parameters valid. /// </summary> protected T InvokeAction <T>(object domainObject, string actionName, FormCollection parameters, out bool valid) { INakedObject nakedObject = NakedObjectsContext.GetNakedObject(domainObject); IActionSpec action = nakedObject.GetActionLeafNode(actionName); return(InvokeAction <T>(nakedObject, action, parameters, out valid)); }
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)); }
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)); } }
/// <summary> /// Creates and populates the values in a transient object from a form /// </summary> /// <param name="form">Form to populate from</param> protected T RecreateTransient <T>(FormCollection form) where T : new() { var obj = Container.NewTransientInstance <T>(); INakedObject naked = NakedObjectsContext.GetNakedObject(obj); RefreshTransient(naked, form); return(obj); }
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)); }
private object GetCompletionData(INakedObject nakedObject, ITypeSpec spec) { string label = nakedObject.TitleString(); string value = nakedObject.TitleString(); string link = spec.IsParseable ? label : NakedObjectsContext.GetObjectId(nakedObject); string src = GetIconSrc(nakedObject); string alt = GetIconAlt(nakedObject); return(new { label, value, link, src, alt }); }
/// <summary> /// Apply changes from form and attempt to save. Go to indicated View based on result of save /// </summary> protected ActionResult SaveObject(FormCollection form, string id, string viewNameForFailure, string viewNameForSuccess, object modelForSuccessViewIfDifferent = null) { object obj = NakedObjectsContext.GetObjectFromId(id); //Assuming id is for a transient, this will re-create a transient of same type if (SaveObject(form, ref obj)) { return(View(viewNameForSuccess, modelForSuccessViewIfDifferent ?? obj)); } return(View(viewNameForFailure, obj)); }
public virtual ActionResult ClearHistory(bool clearAll) { object lastObject = Session.LastObject(NakedObjectsContext, ObjectCache.ObjectFlag.BreadCrumb); Session.ClearCachedObjects(ObjectCache.ObjectFlag.BreadCrumb); if (lastObject == null || clearAll) { return(RedirectToAction(IdHelper.IndexAction, IdHelper.HomeName)); } SetControllerName(lastObject); return(View(NakedObjectsContext.GetNakedObject(lastObject))); }
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)); }
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)); }
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)); }
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)); }
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 })); }
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())); }
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)); }
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)); }
/// <summary> /// Apply changes from form and attempt to save /// </summary> /// <typeparam name="T"></typeparam> /// <param name="form"></param> /// <param name="obj"></param> /// <returns>true if changes in form are valid and object saved</returns> protected bool SaveObject <T>(FormCollection form, ref T obj) { INakedObject naked = NakedObjectsContext.GetNakedObject(obj); bool result = false; if (ValidateChanges(naked, new ObjectAndControlData() { Form = form })) { if (ApplyChanges(naked, new ObjectAndControlData() { Form = form })) { result = true; } } obj = naked.GetDomainObject <T>(); return(result); }
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 })); }
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. Return result and update valid bool to indicate if /// parameters valid. /// </summary> protected TResult InvokeAction <TTarget, TParm1, TParm2, TParm3, TParm4, TResult>(TTarget domainObject, Expression <Func <TTarget, Func <TParm1, TParm2, TParm3, TParm4, TResult> > > expression, FormCollection parameters, out bool valid) { return(InvokeAction <TResult>(NakedObjectsContext.GetNakedObject(domainObject), expression, parameters, out valid)); }
private T InvokeAction <T>(INakedObject nakedObject, IActionSpec action, FormCollection parameters, out bool valid) { if (ActionExecutingAsContributed(action, nakedObject)) { if (action.ParameterCount == 1) { // contributed action being invoked with a single parm that is the current target // no dialog - go straight through INakedObject result = action.Execute(nakedObject, new[] { nakedObject }); valid = true; return(result.GetDomainObject <T>()); } if (action.ParameterCount > 1) { // contributed action being invoked with multiple parms - populate first that match the target IActionParameterSpec parmToPopulate = action.Parameters.FirstOrDefault(p => nakedObject.Spec.IsOfType(p.Spec)); if (parmToPopulate != null) { ViewData[IdHelper.GetParameterInputId(action, parmToPopulate)] = NakedObjectsContext.GetObjectId(nakedObject.Object); } } } if (ValidateParameters(nakedObject, action, new ObjectAndControlData { Form = parameters })) { IEnumerable <INakedObject> parms = GetParameterValues(action, new ObjectAndControlData { Form = parameters }); INakedObject result = action.Execute(nakedObject, parms.ToArray()); valid = true; return(result.GetDomainObject <T>()); } valid = false; return(default(T)); }
/// <summary> /// Obtains the Id for the specified object /// </summary> protected string GetIdFromObject(object domainObject) { return(NakedObjectsContext.GetObjectId(domainObject)); }
private ActionResult ExecuteAction(ObjectAndControlData controlData, INakedObject nakedObject, IActionSpec action) { if (ActionExecutingAsContributed(action, nakedObject) && action.ParameterCount == 1) { // contributed action being invoked with a single parm that is the current target // no dialog - go straight through var newForm = new FormCollection { { IdHelper.GetParameterInputId(action, action.Parameters.First()), NakedObjectsContext.GetObjectId(nakedObject) } }; // horrid kludge var oldForm = controlData.Form; controlData.Form = newForm; if (ValidateParameters(nakedObject, action, controlData)) { return(AppropriateView(controlData, Execute(action, nakedObject, new[] { nakedObject }), action)); } controlData.Form = oldForm; AddAttemptedValues(controlData); } if (!action.Parameters.Any()) { return(AppropriateView(controlData, Execute(action, nakedObject, new INakedObject[] { }), action)); } SetDefaults(nakedObject, action); // do after any parameters set by contributed action so this takes priority SetSelectedParameters(action); SetPagingValues(controlData, nakedObject); var property = DisplaySingleProperty(controlData, controlData.DataDict); return(View(property == null ? "ActionDialog" : "PropertyEdit", new FindViewModel { ContextObject = nakedObject.Object, ContextAction = action, PropertyName = property })); }
/// <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>()); }
/// <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(object domainObject, string actionName, FormCollection parameters, String viewNameForFailure, string viewNameForSuccess = null) { INakedObject nakedObject = NakedObjectsContext.GetNakedObject(domainObject); return(InvokeAction(nakedObject, actionName, parameters, viewNameForFailure, viewNameForSuccess)); }