public IActionSpec[] GetLocallyContributedActions(ITypeSpec typeSpec, string id) { if (!locallyContributedActions.ContainsKey(id)) { locallyContributedActions[id] = ObjectActions.Where(oa => oa.IsLocallyContributedTo(typeSpec, id)).ToArray(); } return locallyContributedActions[id]; }
public TypeFacade(ITypeSpec spec, IFrameworkFacade frameworkFacade, INakedObjectsFramework framework) { FacadeUtils.AssertNotNull(spec, "Spec is null"); FacadeUtils.AssertNotNull(frameworkFacade, "FrameworkFacade is null"); FacadeUtils.AssertNotNull(framework, "framework is null"); FrameworkFacade = frameworkFacade; WrappedValue = spec; this.framework = framework; }
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 }); }
public static bool IsImmutableOncePersisted(this ITypeSpec spec) { var immutableFacet = spec.GetFacet <IImmutableFacet>(); if (immutableFacet == null) { return(false); } return(immutableFacet.Value == WhenTo.OncePersisted); }
public static bool IsAlwaysImmutable(this ITypeSpec spec) { var immutableFacet = spec.GetFacet <IImmutableFacet>(); if (immutableFacet == null) { return(false); } return(immutableFacet.Value == WhenTo.Always); }
public virtual string GetObjectActionOrder() { ITypeSpec spec = NakedObject.Spec; IActionSpec[] actionsSpec = spec.GetObjectActions(); var order = new StringBuilder(); order.Append(AppendActions(actionsSpec)); return(order.ToString()); }
public ITestObject AssertIsImmutable() { ITypeSpec spec = NakedObject.Spec; var facet = spec.GetFacet <IImmutableFacet>(); bool immutable = facet.Value == WhenTo.Always || facet.Value == WhenTo.OncePersisted && NakedObject.ResolveState.IsPersistent(); Assert.IsTrue(immutable, "Not immutable"); return((ITestObject)this); }
public NakedObjectSpecificationWrapper(ITypeSpec spec, INakedObjectsSurface surface, INakedObjectsFramework framework) { SurfaceUtils.AssertNotNull(spec, "Spec is null"); SurfaceUtils.AssertNotNull(surface, "Surface is null"); SurfaceUtils.AssertNotNull(framework, "framework is null"); Surface = surface; this.spec = spec; this.framework = framework; }
private INakedObjectAdapter NewAdapterForViewModel(object domainObject, ITypeSpec spec) { if (spec.IsViewModel) { INakedObjectAdapter adapter = CreateAdapterForViewModel(domainObject, (IObjectSpec)spec); adapter.ResolveState.Handle(Events.InitializePersistentEvent); return(adapter); } return(null); }
public INakedObjectAdapter RecreateInstance(IOid oid, ITypeSpec spec) { INakedObjectAdapter adapter = nakedObjectManager.GetAdapterFor(oid); if (adapter != null) { if (!adapter.Spec.Equals(spec)) { throw new AdapterException(Log.LogAndReturn($"Mapped adapter is for a different type of object: {spec.FullName}; {adapter}")); } return adapter; } object obj = CreateObject(spec); return nakedObjectManager.AdapterForExistingObject(obj, oid); }
public static IActionSpec GetOverloadedAction(string actionName, ITypeSpec spec) { IActionSpec action = null; IActionSpec[] actions = spec.GetActionLeafNodes(); Tuple<IActionSpec, string>[] overloadedActions = GetOverloadedActionsAndUIds(actions); if (overloadedActions.Any()) { Tuple<IActionSpec, string> matchingAction = overloadedActions.SingleOrDefault(oa => oa.Item1.Id + oa.Item2 == actionName); if (matchingAction != null) { action = matchingAction.Item1; } } return action; }
public override IConsent IsUsable(INakedObjectAdapter target) { bool isPersistent = target.ResolveState.IsPersistent(); IConsent disabledConsent = IsUsableDeclaratively(isPersistent); if (disabledConsent != null) { return(disabledConsent); } var viewModelFacet = target.Spec.GetFacet <IViewModelFacet>(); if (viewModelFacet != null) { // all fields on a non-editable view model are disabled if (!viewModelFacet.IsEditView(target)) { return(new Veto(Resources.NakedObjects.FieldDisabled)); } } var immutableFacet = GetFacet <IImmutableFacet>(); if (immutableFacet != null) { WhenTo when = immutableFacet.Value; if (when == WhenTo.UntilPersisted && !isPersistent) { return(new Veto(Resources.NakedObjects.FieldDisabledUntil)); } if (when == WhenTo.OncePersisted && isPersistent) { return(new Veto(Resources.NakedObjects.FieldDisabledOnce)); } ITypeSpec tgtSpec = target.Spec; if (tgtSpec.IsAlwaysImmutable() || (tgtSpec.IsImmutableOncePersisted() && isPersistent)) { return(new Veto(Resources.NakedObjects.FieldDisabled)); } } var f = GetFacet <IDisableForContextFacet>(); string reason = f == null ? null : f.DisabledReason(target); if (reason == null) { var fs = GetFacet <IDisableForSessionFacet>(); reason = fs == null ? null : fs.DisabledReason(Session, target, LifecycleManager, MetamodelManager); } return(GetConsent(reason)); }
public INakedObjectAdapter LoadObject(IOid oid, ITypeSpec spec) { if (oid == null) { throw new NakedObjectSystemException("needs an OID"); } if (spec == null) { throw new NakedObjectSystemException("needs a specification"); } return(nakedObjectManager.GetKnownAdapter(oid) ?? objectPersistor.LoadObject(oid, (IObjectSpec)spec)); }
private object CreateObject(ITypeSpec spec) { Log.DebugFormat("CreateObject: " + spec); Type type = TypeUtils.GetType(spec.FullName); if (spec.IsViewModel) { object viewModel = Activator.CreateInstance(type); InitDomainObject(viewModel); return(viewModel); } return(objectPersistor.CreateObject(spec)); }
public static Tuple <IActionSpec, string> GetActionandUidFromSpec(ITypeSpec spec, string actionName, string typeName) { IActionSpec[] actions = spec.GetActionLeafNodes(); IActionSpec action = actions.SingleOrDefault(p => p.Id == actionName) ?? GetOverloadedAction(actionName, spec); if (action == null) { throw new TypeActionResourceNotFoundNOSException(actionName, typeName); } string uid = GetOverloadedUId(action, spec); return(new Tuple <IActionSpec, string>(action, uid)); }
private static ElementDescriptor MenuActionAsElementDescriptor(this HtmlHelper html, IMenuActionImmutable menuAction, INakedObject nakedObject, bool isEdit) { IActionSpecImmutable actionIm = menuAction.Action; IActionSpec actionSpec = html.Framework().MetamodelManager.GetActionSpec(actionIm); if (nakedObject == null) { IObjectSpecImmutable objectIm = actionIm.OwnerSpec; //This is the spec for the service if (!objectIm.Service) { throw new Exception("Action is not on a known object or service"); } //TODO: Add method to IServicesManager to get a service by its IObjectSpec (or IObjectSpecImmutable) ITypeSpec objectSpec = html.Framework().MetamodelManager.GetSpecification(objectIm); nakedObject = html.Framework().ServicesManager.GetServices().Single(s => s.Spec == objectSpec); } var actionContext = new ActionContext(false, nakedObject, actionSpec); RouteValueDictionary attributes; string tagType; string value; if (!actionContext.Action.IsVisible(actionContext.Target)) { return(null); } IConsent consent = actionContext.Action.IsUsable(actionContext.Target); if (consent.IsVetoed) { tagType = html.GetVetoedAction(actionContext, consent, out value, out attributes); } else if (isEdit) { tagType = html.GetActionAsButton(actionContext, out value, out attributes); } else { tagType = html.GetActionAsForm(actionContext, html.Framework().GetObjectTypeName(actionContext.Target.Object), new { id = html.Framework().GetObjectId(actionContext.Target) }, out value, out attributes); } return(new ElementDescriptor { TagType = tagType, Value = value, Attributes = attributes }); }
private Tuple<IActionSpec, ITypeSpec, IActionParameterSpec, string> GetActionParameterTypeInternal(string typeName, string actionName, string parmName) { if (string.IsNullOrWhiteSpace(typeName) || string.IsNullOrWhiteSpace(actionName) || string.IsNullOrWhiteSpace(parmName)) { throw new BadRequestNOSException(); } ITypeSpec spec = GetDomainTypeInternal(typeName); Tuple<IActionSpec, string> actionAndUid = SurfaceUtils.GetActionandUidFromSpec(spec, actionName, typeName); IActionParameterSpec parm = actionAndUid.Item1.Parameters.SingleOrDefault(p => p.Id == parmName); if (parm == null) { throw new TypeActionParameterResourceNotFoundNOSException(parmName, actionName, typeName); } return new Tuple<IActionSpec, ITypeSpec, IActionParameterSpec, string>(actionAndUid.Item1, spec, parm, actionAndUid.Item2); }
public INakedObjectAdapter RecreateInstance(IOid oid, ITypeSpec spec) { INakedObjectAdapter adapter = nakedObjectManager.GetAdapterFor(oid); if (adapter != null) { if (!adapter.Spec.Equals(spec)) { throw new AdapterException(Log.LogAndReturn($"Mapped adapter is for a different type of object: {spec.FullName}; {adapter}")); } return(adapter); } object obj = CreateObject(spec); return(nakedObjectManager.AdapterForExistingObject(obj, oid)); }
private Tuple<ActionContext, ITypeSpec> GetActionTypeInternal(string typeName, string actionName) { if (string.IsNullOrWhiteSpace(typeName) || string.IsNullOrWhiteSpace(actionName)) { throw new BadRequestNOSException(); } ITypeSpec spec = GetDomainTypeInternal(typeName); var actionAndUid = SurfaceUtils.GetActionandUidFromSpec(spec, actionName, typeName); var actionContext = new ActionContext { Action = actionAndUid.Item1, VisibleParameters = FilterParmsForContributedActions(actionAndUid.Item1, spec, actionAndUid.Item2), OverloadedUniqueId = actionAndUid.Item2 }; return new Tuple<ActionContext, ITypeSpec>(actionContext, spec); }
private IOid RestoreGenericOid(string[] encodedData) { string typeName = TypeNameUtils.DecodeTypeName(HttpUtility.UrlDecode(encodedData.First())); ITypeSpec spec = metamodel.GetSpecification(typeName); if (spec.IsCollection) { return(new CollectionMemento(this, nakedObjectManager, metamodel, encodedData)); } if (spec.ContainsFacet <IViewModelFacet>()) { return(new ViewModelOid(metamodel, encodedData)); } return(spec.ContainsFacet <IComplexTypeFacet>() ? new AggregateOid(metamodel, encodedData) : null); }
public static IActionSpec GetOverloadedAction(string actionName, ITypeSpec spec) { IActionSpec action = null; IActionSpec[] actions = spec.GetActionLeafNodes(); Tuple <IActionSpec, string>[] overloadedActions = GetOverloadedActionsAndUIds(actions); if (overloadedActions.Any()) { Tuple <IActionSpec, string> matchingAction = overloadedActions.SingleOrDefault(oa => oa.Item1.Id + oa.Item2 == actionName); if (matchingAction != null) { action = matchingAction.Item1; } } return(action); }
private static string GetCollectionTitle(INakedObject nakedObject, HtmlHelper html) { int pageSize, maxPage, currentPage, total; int count = nakedObject.GetAsEnumerable(html.Framework().NakedObjectManager).Count(); if (!html.GetPagingValues(out pageSize, out maxPage, out currentPage, out total)) { total = count; } string queryInd = nakedObject.Spec.IsQueryable ? MvcUi.QueryResult + ": " : ""; int viewSize = count; ITypeSpec typeSpec = html.Framework().MetamodelManager.GetSpecification(nakedObject.GetTypeOfFacetFromSpec().GetValueSpec(nakedObject, html.Framework().MetamodelManager.Metamodel)); string type = total == 1 ? typeSpec.SingularName : typeSpec.PluralName; return(queryInd + string.Format(MvcUi.ViewingNofXType, viewSize, total, type)); }
public INakedObjectAdapter RecreateInstance(IOid oid, ITypeSpec spec) { Log.DebugFormat("RecreateInstance oid: {0} hint: {1}", oid, spec); INakedObjectAdapter adapter = nakedObjectManager.GetAdapterFor(oid); if (adapter != null) { if (!adapter.Spec.Equals(spec)) { throw new AdapterException(string.Format("Mapped adapter is for a different type of object: {0}; {1}", spec.FullName, adapter)); } return(adapter); } Log.DebugFormat("Recreating instance for {0}", spec); object obj = CreateObject(spec); return(nakedObjectManager.AdapterForExistingObject(obj, oid)); }
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())); }
// TODO either move this into framework or (better?) add a VetoCause enum to Veto and use private static IConsent IsCurrentlyMutable(INakedObject target) { bool isPersistent = target.ResolveState.IsPersistent(); var immutableFacet = target.Spec.GetFacet<IImmutableFacet>(); if (immutableFacet != null) { WhenTo when = immutableFacet.Value; if (when == WhenTo.UntilPersisted && !isPersistent) { return new Veto(Resources.NakedObjects.FieldDisabledUntil); } if (when == WhenTo.OncePersisted && isPersistent) { return new Veto(Resources.NakedObjects.FieldDisabledOnce); } ITypeSpec tgtSpec = target.Spec; if (tgtSpec.IsAlwaysImmutable() || (tgtSpec.IsImmutableOncePersisted() && isPersistent)) { return new Veto(Resources.NakedObjects.FieldDisabled); } } return new Allow(); }
/// <summary> /// Makes a naked object persistent. The specified object should be stored away via this object store's /// persistence mechanism, and have an new and unique OID assigned to it. The object, should also be added /// to the cache as the object is implicitly 'in use'. /// </summary> /// <para> /// If the object has any associations then each of these, where they aren't already persistent, should /// also be made persistent by recursively calling this method. /// </para> /// <para> /// If the object to be persisted is a collection, then each element of that collection, that is not /// already persistent, should be made persistent by recursively calling this method. /// </para> public void MakePersistent(INakedObjectAdapter nakedObjectAdapter) { if (IsPersistent(nakedObjectAdapter)) { throw new NotPersistableException(Log.LogAndReturn($"Object already persistent: {nakedObjectAdapter}")); } if (nakedObjectAdapter.Spec.Persistable == PersistableType.Transient) { throw new NotPersistableException(Log.LogAndReturn($"Object must be kept transient: {nakedObjectAdapter}")); } ITypeSpec spec = nakedObjectAdapter.Spec; if (spec is IServiceSpec) { throw new NotPersistableException(Log.LogAndReturn($"Cannot persist services: {nakedObjectAdapter}")); } persistAlgorithm.MakePersistent(nakedObjectAdapter); }
/// <summary> /// Makes a naked object persistent. The specified object should be stored away via this object store's /// persistence mechanism, and have an new and unique OID assigned to it. The object, should also be added /// to the cache as the object is implicitly 'in use'. /// </summary> /// <para> /// If the object has any associations then each of these, where they aren't already persistent, should /// also be made persistent by recursively calling this method. /// </para> /// <para> /// If the object to be persisted is a collection, then each element of that collection, that is not /// already persistent, should be made persistent by recursively calling this method. /// </para> public void MakePersistent(INakedObjectAdapter nakedObjectAdapter) { Log.DebugFormat("MakePersistent nakedObjectAdapter: {0}", nakedObjectAdapter); if (IsPersistent(nakedObjectAdapter)) { throw new NotPersistableException("Object already persistent: " + nakedObjectAdapter); } if (nakedObjectAdapter.Spec.Persistable == PersistableType.Transient) { throw new NotPersistableException("Object must be kept transient: " + nakedObjectAdapter); } ITypeSpec spec = nakedObjectAdapter.Spec; if (spec is IServiceSpec) { throw new NotPersistableException("Cannot persist services: " + nakedObjectAdapter); } persistAlgorithm.MakePersistent(nakedObjectAdapter); }
public INakedObjectAdapter CreateAdapter(object domainObject, IOid oid, IVersion version) { if (domainObject == null) { return(null); } if (oid == null) { ITypeSpec objectSpec = metamodel.GetSpecification(domainObject.GetType()); if (objectSpec.ContainsFacet(typeof(IComplexTypeFacet))) { return(GetAdapterFor(domainObject)); } if (objectSpec.HasNoIdentity) { return(AdapterForNoIdentityObject(domainObject)); } return(AdapterForExistingObject(domainObject, objectSpec)); } return(AdapterForExistingObject(domainObject, oid)); }
public IOidFacade RestoreSid(OidTranslationSlashSeparatedTypeAndIds id) { ITypeSpec spec = GetServiceTypeSpecByServiceName(id); if (spec is IServiceSpec) { var service = framework.ServicesManager.GetServicesWithVisibleActions(framework.LifecycleManager).SingleOrDefault(no => no.Spec.IsOfType(spec)); if (service == null) { throw new ServiceResourceNotFoundNOSException(spec.FullName); } return(new OidFacade(service.Oid)); } if (!spec.IsStatic) { // we were looking for a static class masquerading as a service throw new ServiceResourceNotFoundNOSException(spec.FullName); } return(null); }
private string InputOrSelect(ITypeSpec spec) { return (spec.IsParseable ? InputName : SelectName); }
private bool FindServiceOnSpecOrSpecSuperclass(ITypeSpec spec) { return(spec != null && (spec.Equals(OnSpec) || FindServiceOnSpecOrSpecSuperclass(spec.Superclass))); }
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 INakedObjectSpecificationSurface GetSpecificationWrapper(ITypeSpec spec) { return new NakedObjectSpecificationWrapper(spec, this, framework); }
private INakedObjectAdapter AdapterForExistingObject(object domainObject, ITypeSpec spec) { return identityMap.GetAdapterFor(domainObject) ?? NewAdapterForViewModel(domainObject, spec) ?? NewAdapterForTransient(domainObject); }
private INakedObjectAdapter NewAdapterForViewModel(object domainObject, ITypeSpec spec) { if (spec.IsViewModel) { INakedObjectAdapter adapter = CreateAdapterForViewModel(domainObject, (IObjectSpec) spec); adapter.ResolveState.Handle(Events.InitializePersistentEvent); return adapter; } return null; }
private ParameterContext[] FilterParmsForContributedActions(IActionSpec action, ITypeSpec targetSpec, string uid) { IActionParameterSpec[] parms; if (action.IsContributedMethod && !action.OnSpec.Equals(targetSpec)) { var tempParms = new List<IActionParameterSpec>(); bool skipped = false; foreach (IActionParameterSpec parameter in action.Parameters) { // skip the first parm that matches the target. if (targetSpec.IsOfType(parameter.Spec) && !skipped) { skipped = true; } else { tempParms.Add(parameter); } } parms = tempParms.ToArray(); } else { parms = action.Parameters; } return parms.Select(p => new ParameterContext { Action = action, Parameter = p, OverloadedUniqueId = uid }).ToArray(); }
public INakedObjectAdapter RecreateInstance(IOid oid, ITypeSpec spec) { Log.DebugFormat("RecreateInstance oid: {0} hint: {1}", oid, spec); INakedObjectAdapter adapter = nakedObjectManager.GetAdapterFor(oid); if (adapter != null) { if (!adapter.Spec.Equals(spec)) { throw new AdapterException(string.Format("Mapped adapter is for a different type of object: {0}; {1}", spec.FullName, adapter)); } return adapter; } Log.DebugFormat("Recreating instance for {0}", spec); object obj = CreateObject(spec); return nakedObjectManager.AdapterForExistingObject(obj, oid); }
public bool IsLocallyContributedTo(ITypeSpec typeSpec, string id) { var spec = MetamodelManager.Metamodel.GetSpecification(typeSpec.FullName) as IObjectSpecImmutable; return spec != null && actionSpecImmutable.IsContributedToLocalCollectionOf(spec, id); }
private bool FindServiceOnSpecOrSpecSuperclass(ITypeSpec spec) { return spec != null && (spec.Equals(OnSpec) || FindServiceOnSpecOrSpecSuperclass(spec.Superclass)); }
private object CreateObject(ITypeSpec spec) { Type type = TypeUtils.GetType(spec.FullName); return spec.IsViewModel || spec is IServiceSpec || spec.ContainsFacet<INotPersistedFacet>() ? CreateNotPersistedObject(type, spec is IServiceSpec) : objectPersistor.CreateObject(spec); }
public static Tuple<IActionSpec, string>[] GetActionsandUidFromSpec(ITypeSpec spec) { IActionSpec[] actions = spec.GetActionLeafNodes(); return actions.Select(action => new Tuple<IActionSpec, string>(action, GetOverloadedUId(action, spec))).ToArray(); }
public static string GetOverloadedUId(IActionSpec action, ITypeSpec spec) { IActionSpec[] actions = spec.GetActionLeafNodes(); Tuple<IActionSpec, string>[] overloadedActions = GetOverloadedActionsAndUIds(actions); return overloadedActions.Where(oa => oa.Item1 == action).Select(oa => oa.Item2).SingleOrDefault(); }
public INakedObjectAdapter LoadObject(IOid oid, ITypeSpec spec) { Assert.AssertNotNull("needs an OID", oid); Assert.AssertNotNull("needs a specification", spec); return nakedObjectManager.GetKnownAdapter(oid) ?? objectPersistor.LoadObject(oid, (IObjectSpec) spec); }