public void MakePersistent(INakedObjectAdapter nakedObjectAdapter) { if (nakedObjectAdapter.Spec.IsCollection) { Log.Info("Persist " + nakedObjectAdapter); nakedObjectAdapter.GetAsEnumerable(manager).ForEach(Persist); if (nakedObjectAdapter.ResolveState.IsGhost()) { nakedObjectAdapter.ResolveState.Handle(Events.StartResolvingEvent); nakedObjectAdapter.ResolveState.Handle(Events.EndResolvingEvent); } } else { if (nakedObjectAdapter.ResolveState.IsPersistent()) { throw new NotPersistableException("can't make object persistent as it is already persistent: " + nakedObjectAdapter); } if (nakedObjectAdapter.Spec.Persistable == PersistableType.Transient) { throw new NotPersistableException("can't make object persistent as it is not persistable: " + nakedObjectAdapter); } Persist(nakedObjectAdapter); } }
// 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, INakedObjectAdapter collection, INakedObjectManager manager ) { SetAttribute(element, "feature", NofMetamodelFeatureCollection); SetAttribute(element, "type", prefix + ":" + fullyQualifiedClassName); SetAttribute(element, "size", "" + collection.GetAsEnumerable(manager).Count()); }
private void MakeCollectionPersistent(INakedObjectAdapter collection) { if (collection.ResolveState.IsPersistent() || collection.Spec.Persistable == PersistableType.Transient) { return; } if (collection.ResolveState.IsTransient()) { collection.ResolveState.Handle(Events.StartResolvingEvent); collection.ResolveState.Handle(Events.EndResolvingEvent); } manager.MadePersistent(collection); collection.GetAsEnumerable(manager).ForEach(MakePersistent); }
private void SetResolveStateForDerivedCollections(INakedObjectAdapter 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 void MakePersistent(INakedObjectAdapter nakedObjectAdapter) { if (nakedObjectAdapter.Spec.IsCollection) { nakedObjectAdapter.GetAsEnumerable(manager).ForEach(Persist); if (nakedObjectAdapter.ResolveState.IsGhost()) { nakedObjectAdapter.ResolveState.Handle(Events.StartResolvingEvent); nakedObjectAdapter.ResolveState.Handle(Events.EndResolvingEvent); } } else { if (nakedObjectAdapter.ResolveState.IsPersistent()) { throw new NotPersistableException(Log.LogAndReturn($"Can't make object persistent as it is already persistent: {nakedObjectAdapter}")); } if (nakedObjectAdapter.Spec.Persistable == PersistableType.Transient) { throw new NotPersistableException(Log.LogAndReturn($"Can't make object persistent as it is not persistable: {nakedObjectAdapter}")); } Persist(nakedObjectAdapter); } }
// 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) { INakedObjectAdapter nakedObjectAdapter = place.NakedObjectAdapter; 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); // locate the field in the object's class var nos = (IObjectSpec)nakedObjectAdapter.Spec; IAssociationSpec field = nos.Properties.SingleOrDefault(p => p.Id.ToLower() == fieldName); if (field == null) { return(false); } // locate the corresponding XML element // (the corresponding XSD element will later be attached to xmlElement // as its userData) XElement[] xmlFieldElements = ElementsUnder(xmlElement, field.Id).ToArray(); int fieldCount = xmlFieldElements.Length; if (fieldCount != 1) { 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(nakedObjectAdapter, xmlFieldElement); if (field.ReturnSpec.IsParseable) { return(false); } var oneToOneAssociation = field as IOneToOneAssociationSpec; if (oneToOneAssociation != null) { INakedObjectAdapter referencedObjectAdapter = oneToOneAssociation.GetNakedObject(fieldPlace.NakedObjectAdapter); if (referencedObjectAdapter == null) { return(true); // not a failure if the reference was null } bool appendedXml = AppendXmlThenIncludeRemaining(fieldPlace, referencedObjectAdapter, fieldNames, annotation); return(appendedXml); } var oneToManyAssociation = field as IOneToManyAssociationSpec; if (oneToManyAssociation != null) { INakedObjectAdapter collection = oneToManyAssociation.GetNakedObject(fieldPlace.NakedObjectAdapter); INakedObjectAdapter[] collectionAsEnumerable = collection.GetAsEnumerable(nakedObjectManager).ToArray(); bool allFieldsNavigated = true; foreach (INakedObjectAdapter referencedObject in collectionAsEnumerable) { bool appendedXml = AppendXmlThenIncludeRemaining(fieldPlace, referencedObject, fieldNames, annotation); allFieldsNavigated = allFieldsNavigated && appendedXml; } return(allFieldsNavigated); } return(false); // fall through, shouldn't get here but just in case. }
public TestCollection(INakedObjectAdapter collection, ITestObjectFactory factory, INakedObjectManager manager) { this.collection = collection; wrappedCollection = collection.GetAsEnumerable(manager).Select(factory.CreateTestObject); }
// 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)); INakedObjectAdapter nakedObjectAdapter = place.NakedObjectAdapter; 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 var nos = (IObjectSpec)nakedObjectAdapter.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(nakedObjectAdapter, 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"); INakedObjectAdapter referencedObjectAdapter = oneToOneAssociation.GetNakedObject(fieldPlace.NakedObjectAdapter); if (referencedObjectAdapter == null) { return(true); // not a failure if the reference was null } bool appendedXml = AppendXmlThenIncludeRemaining(fieldPlace, referencedObjectAdapter, fieldNames, annotation); Log.Debug("includeField(Pl, Vec, Str): 1->1: invoked appendXmlThenIncludeRemaining for " + DoLog("referencedObj", referencedObjectAdapter) + AndLog("returned", "" + appendedXml)); return(appendedXml); } var oneToManyAssociation = field as IOneToManyAssociationSpec; if (oneToManyAssociation != null) { Log.Debug("includeField(Pl, Vec, Str): field is 1->M"); INakedObjectAdapter collection = oneToManyAssociation.GetNakedObject(fieldPlace.NakedObjectAdapter); INakedObjectAdapter[] collectionAsEnumerable = collection.GetAsEnumerable(nakedObjectManager).ToArray(); Log.Debug("includeField(Pl, Vec, Str): 1->M: " + DoLog("collection.size", "" + collectionAsEnumerable.Count())); bool allFieldsNavigated = true; foreach (INakedObjectAdapter 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. }
public TestCollection(INakedObjectAdapter collection, ITestObjectFactory factory, INakedObjectManager manager) { NakedObject = collection; wrappedCollection = collection.GetAsEnumerable(manager).Select(factory.CreateTestObject); }