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)); } }
public virtual void MakePersistent(INakedObject nakedObject) { if (nakedObject.Spec.IsCollection) { Log.Info("Persist " + nakedObject); nakedObject.GetAsEnumerable(manager).ForEach(Persist); if (nakedObject.ResolveState.IsGhost()) { nakedObject.ResolveState.Handle(Events.StartResolvingEvent); nakedObject.ResolveState.Handle(Events.EndResolvingEvent); } } else { if (nakedObject.ResolveState.IsPersistent()) { throw new NotPersistableException("can't make object persistent as it is already persistent: " + nakedObject); } if (nakedObject.Spec.Persistable == PersistableType.Transient) { throw new NotPersistableException("can't make object persistent as it is not persistable: " + nakedObject); } Persist(nakedObject); } }
// Adds <code>nof:feature="collection"</code> attribute, the // <code>nof:type="e;..."</code> and the // <code>nof:size="e;..."</code> for the supplied element. public static void SetNofCollection(XElement element, string prefix, string fullyQualifiedClassName, INakedObject collection, INakedObjectManager manager ) { SetAttribute(element, "feature", NofMetamodelFeatureCollection); SetAttribute(element, "type", prefix + ":" + fullyQualifiedClassName); SetAttribute(element, "size", "" + collection.GetAsEnumerable(manager).Count()); }
private void MakeCollectionPersistent(INakedObject collection, IPersistedObjectAdder persistor) { if (collection.ResolveState.IsPersistent() || collection.Specification.Persistable == Persistable.TRANSIENT) { return; } Log.Info("persist " + collection); if (collection.ResolveState.IsTransient()) { collection.ResolveState.Handle(Events.StartResolvingEvent); collection.ResolveState.Handle(Events.EndResolvingEvent); } persistor.MadePersistent(collection); collection.GetAsEnumerable().ForEach(no => MakePersistent(no, persistor)); }
private void SetResolveStateForDerivedCollections(INakedObject adapterFor) { bool isDerived = !IsPersisted; if (isDerived && !adapterFor.ResolveState.IsResolved()) { if (adapterFor.GetAsEnumerable(Manager).Any()) { adapterFor.ResolveState.Handle(Events.StartResolvingEvent); adapterFor.ResolveState.Handle(Events.EndResolvingEvent); } } }
public virtual void AddInternalCollection(INakedObject collection, string fieldId, bool ensurePersistent) { InitCollection(fieldId); foreach (INakedObject element in collection.GetAsEnumerable()) { IOid elementOid = element.Oid; if (elementOid == null) { throw new Exception("Element is not persistent " + element); } AddElement(fieldId, elementOid); } }
private void MakeCollectionPersistent(INakedObject collection) { if (collection.ResolveState.IsPersistent() || collection.Spec.Persistable == PersistableType.Transient) { return; } Log.Info("persist " + collection); if (collection.ResolveState.IsTransient()) { collection.ResolveState.Handle(Events.StartResolvingEvent); collection.ResolveState.Handle(Events.EndResolvingEvent); } manager.MadePersistent(collection); collection.GetAsEnumerable(manager).ForEach(MakePersistent); }
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 virtual void MakePersistent(INakedObject nakedObject, IPersistedObjectAdder persistor) { if (nakedObject.Specification.IsCollection) { Log.Info("Persist " + nakedObject); nakedObject.GetAsEnumerable().ForEach(no => Persist(no, persistor)); if (nakedObject.ResolveState.IsGhost()) { nakedObject.ResolveState.Handle(Events.StartResolvingEvent); nakedObject.ResolveState.Handle(Events.EndResolvingEvent); } } else { if (nakedObject.ResolveState.IsPersistent()) { throw new NotPersistableException("can't make object persistent as it is already persistent: " + nakedObject); } if (nakedObject.Specification.Persistable == Persistable.TRANSIENT) { throw new NotPersistableException("can't make object persistent as it is not persistable: " + nakedObject); } Persist(nakedObject, persistor); } }
public virtual void AddInternalCollection(INakedObject collection, string fieldId, bool ensurePersistent) { InitCollection(fieldId); foreach (INakedObject element in collection.GetAsEnumerable()) { IOid elementOid = element.Oid; if (elementOid == null) { throw new Exception("Element is not persistent " + element); } AddElement(fieldId, elementOid); } }
public TestCollection(INakedObject collection, ITestObjectFactory factory) { this.collection = collection; wrappedCollection = collection.GetAsEnumerable().Select(factory.CreateTestObject); }
public TestCollection(INakedObject collection, ITestObjectFactory factory, INakedObjectManager manager) { this.collection = collection; wrappedCollection = collection.GetAsEnumerable(manager).Select(factory.CreateTestObject); }
internal INakedObject FilterCollection(INakedObject nakedObject, ObjectAndControlData controlData) { var form = controlData.Form; if (form != null && nakedObject != null && nakedObject.Specification.IsCollection && nakedObject.Oid is CollectionMemento) { nakedObject = Page(nakedObject, nakedObject.GetAsQueryable().Count(), controlData, false); var map = nakedObject.GetAsEnumerable().ToDictionary(FrameworkHelper.GetObjectId, y => y.Object); var selected = map.Where(kvp => form.Keys.Cast<string>().Contains(kvp.Key) && form[kvp.Key].Contains("true")).Select(kvp => kvp.Value).ToArray(); return CloneAndPopulateCollection(nakedObject, selected, false); } return nakedObject; }
internal void SetPagingValues(ObjectAndControlData controlData, INakedObject nakedObject) { if (nakedObject.Specification.IsCollection) { int sink1, sink2; CurrentlyPaging(controlData, nakedObject.GetAsEnumerable().Count(), out sink1, out sink2); } }
private bool IsObjectCompleteAndSaved(INakedObject fieldTarget) { if (fieldTarget.Specification.IsCollection) { if (fieldTarget.GetAsEnumerable().Any(no => !IsReferenceValidToPersist(no))) { ModelState.AddModelError("", MvcUi.CollectionIncomplete); return false; } } else { if (!IsReferenceValidToPersist(fieldTarget)) { ModelState.AddModelError("", MvcUi.ObjectIncomplete); return false; } } return true; }
private void SetResolveStateForDerivedCollections(INakedObject adapterFor) { bool isDerived = !IsPersisted; if (isDerived && !adapterFor.ResolveState.IsResolved()) { if (adapterFor.GetAsEnumerable().Any()) { adapterFor.ResolveState.Handle(Events.StartResolvingEvent); adapterFor.ResolveState.Handle(Events.EndResolvingEvent); } } }
// Adds <code>nof:feature="collection"</code> attribute, the // <code>nof:type="e;..."</code> and the // <code>nof:size="e;..."</code> for the supplied element. public static void SetNofCollection(XElement element, string prefix, string fullyQualifiedClassName, INakedObject collection) { SetAttribute(element, "feature", NofMetamodelFeatureCollection); SetAttribute(element, "type", prefix + ":" + fullyQualifiedClassName); SetAttribute(element, "size", "" + collection.GetAsEnumerable().Count()); }
// return true if able to navigate the complete vector of field names // successfully; false if a field could not be located or it turned // out to be a value. private bool IncludeField(Place place, IList <string> fieldNames, string annotation) { Log.DebugFormat("includeField(: {0})", DoLog("place", place) + AndLog("fieldNames", fieldNames) + AndLog("annotation", annotation)); INakedObject nakedObject = place.NakedObject; XElement xmlElement = place.XmlElement; // we use a copy of the path so that we can safely traverse collections // without side-effects fieldNames = fieldNames.ToList(); // see if we have any fields to process if (!fieldNames.Any()) { return(true); } // take the first field name from the list, and remove string fieldName = fieldNames.First(); fieldNames.Remove(fieldName); Log.Debug("includeField(Pl, Vec, Str):" + DoLog("processing field", fieldName) + AndLog("left", "" + fieldNames.Count())); // locate the field in the object's class IObjectSpec nos = (IObjectSpec)nakedObject.Spec; IAssociationSpec field = nos.Properties.SingleOrDefault(p => p.Id.ToLower() == fieldName); if (field == null) { Log.Info("includeField(Pl, Vec, Str): could not locate field, skipping"); return(false); } // locate the corresponding XML element // (the corresponding XSD element will later be attached to xmlElement // as its userData) Log.Debug("includeField(Pl, Vec, Str): locating corresponding XML element"); XElement[] xmlFieldElements = ElementsUnder(xmlElement, field.Id).ToArray(); int fieldCount = xmlFieldElements.Count(); if (fieldCount != 1) { Log.Info("includeField(Pl, Vec, Str): could not locate " + DoLog("field", field.Id) + AndLog("xmlFieldElements.size", "" + fieldCount)); return(false); } XElement xmlFieldElement = xmlFieldElements.First(); if (!fieldNames.Any() && annotation != null) { // nothing left in the path, so we will apply the annotation now NofMetaModel.SetAnnotationAttribute(xmlFieldElement, annotation); } var fieldPlace = new Place(nakedObject, xmlFieldElement); if (field.ReturnSpec.IsParseable) { Log.Debug("includeField(Pl, Vec, Str): field is value; done"); return(false); } var oneToOneAssociation = field as IOneToOneAssociationSpec; if (oneToOneAssociation != null) { Log.Debug("includeField(Pl, Vec, Str): field is 1->1"); INakedObject referencedObject = oneToOneAssociation.GetNakedObject(fieldPlace.NakedObject); if (referencedObject == null) { return(true); // not a failure if the reference was null } bool appendedXml = AppendXmlThenIncludeRemaining(fieldPlace, referencedObject, fieldNames, annotation); Log.Debug("includeField(Pl, Vec, Str): 1->1: invoked appendXmlThenIncludeRemaining for " + DoLog("referencedObj", referencedObject) + AndLog("returned", "" + appendedXml)); return(appendedXml); } var oneToManyAssociation = field as IOneToManyAssociationSpec; if (oneToManyAssociation != null) { Log.Debug("includeField(Pl, Vec, Str): field is 1->M"); INakedObject collection = oneToManyAssociation.GetNakedObject(fieldPlace.NakedObject); INakedObject[] collectionAsEnumerable = collection.GetAsEnumerable(nakedObjectManager).ToArray(); Log.Debug("includeField(Pl, Vec, Str): 1->M: " + DoLog("collection.size", "" + collectionAsEnumerable.Count())); bool allFieldsNavigated = true; foreach (INakedObject referencedObject in collectionAsEnumerable) { bool appendedXml = AppendXmlThenIncludeRemaining(fieldPlace, referencedObject, fieldNames, annotation); Log.Debug("includeField(Pl, Vec, Str): 1->M: + invoked appendXmlThenIncludeRemaining for " + DoLog("referencedObj", referencedObject) + AndLog("returned", "" + appendedXml)); allFieldsNavigated = allFieldsNavigated && appendedXml; } Log.Debug("includeField(Pl, Vec, Str): " + DoLog("returning", "" + allFieldsNavigated)); return(allFieldsNavigated); } return(false); // fall through, shouldn't get here but just in case. }
private static string CollectionTable(this HtmlHelper html, INakedObject collectionNakedObject, Func<INakedObject, string> linkFunc, Func<INakedObjectAssociation, bool> filter, Func<INakedObjectAssociation, int> order, bool isStandalone, bool withSelection, bool withTitle, bool defaultChecked = false) { var table = new TagBuilder("table"); table.AddCssClass(CollectionItemTypeName(collectionNakedObject)); table.InnerHtml += Environment.NewLine; string innerHtml = ""; INakedObject[] collection = collectionNakedObject.GetAsEnumerable().ToArray(); INakedObjectSpecification collectionSpec = collectionNakedObject.GetTypeOfFacetFromSpec().ValueSpec; INakedObjectAssociation[] collectionAssocs = CollectionAssociations(collection, collectionSpec, filter, order); int index = 0; foreach (INakedObject item in collection) { var row = new TagBuilder("tr"); if (withSelection) { var cbTag = new TagBuilder("td"); int i = index++; string id = "checkbox" + i; string label = GetLabelTag(true, (i + 1).ToString(CultureInfo.InvariantCulture), () => id); cbTag.InnerHtml += (label + html.CheckBox(FrameworkHelper.GetObjectId(item), defaultChecked, new {id, @class = IdHelper.CheckboxClass})); row.InnerHtml += cbTag.ToString(); } if (withTitle) { var itemTag = new TagBuilder("td"); itemTag.InnerHtml += linkFunc(item); row.InnerHtml += itemTag.ToString(); } string[] collectionValues = collectionAssocs.Select(a => html.GetViewField(new PropertyContext(item, a, false), a.Description, true, true)).ToArray(); foreach (string s in collectionValues) { row.InnerHtml += new TagBuilder("td") {InnerHtml = s}; } innerHtml += (row + Environment.NewLine); } var headers = collectionAssocs.Select(a => a.Name).ToArray(); html.AddHeader(headers, table, isStandalone, withSelection, withTitle, defaultChecked); table.InnerHtml += innerHtml; return table + html.AddFooter(collectionNakedObject); }