private void WriteAssociation(BeanWrapper beanWrapper, Hl7Source source, IList <XmlNode> nodes, Relationship relationship, string traversalName) { this.log.Debug("Writing association: traversalName=" + traversalName + ", relationshipType=" + relationship.Type); // 1. collapsed relationship if (relationship.Cardinality.Single && beanWrapper.IsAssociationMappedToSameBean(relationship)) { this.log.Debug("COLLAPSE RECURSE : " + traversalName + " as collapsed relationship to " + beanWrapper.GetWrappedType()); BeanWrapper childBeanWrapper = beanWrapper.CreateSubWrapper(relationship); WriteSpecialAssociation(childBeanWrapper, source, nodes, relationship); } else { //1b. trivial collapsed relationship with cardinality change (e.g. "RecordId" collapsed into "Location criteria" if (relationship.Cardinality.Multiple && beanWrapper.IsAssociationMappedToSameBean(relationship) && IsTypeWithSingleNonFixedRelationship (relationship, source)) { BeanWrapper childBeanWrapper = beanWrapper.CreateSubWrapper(relationship); foreach (XmlNode node in nodes) { WriteAssociation(childBeanWrapper, source, (XmlElement)node, relationship); } } else { // 2. initialized read-only association if (relationship.Cardinality.Single && beanWrapper.IsPreInitializedDelegate(relationship)) { this.log.Debug("READ-ONLY ASSOCIATION: " + traversalName + " as collapsed relationship to " + beanWrapper.GetWrappedType( )); BeanWrapper childBeanWrapper = new BeanWrapper(beanWrapper.GetInitializedReadOnlyAssociation(relationship)); WriteSpecialAssociation(childBeanWrapper, source, nodes, relationship); } else { // 3a. non-collapsed, multiple-cardinality choice or single-cardinality choice with node name same as choice name if (IsCdaChoice(nodes, relationship, source)) { IList <object> convertedBeans = HandleCdaChoice(nodes, traversalName, relationship, source); if (relationship.Cardinality.Multiple) { this.log.Debug("Special choice handling: WRITING MULTIPLE-CARDINALITY CHOICE: " + beanWrapper.GetWrappedType() + " property with annotation=" + traversalName + " - values=" + convertedBeans); beanWrapper.Write(relationship, convertedBeans); } else { if (relationship.Cardinality.Single && convertedBeans.IsEmpty()) { throw new MarshallingException("Special choice handling: Why is this empty? : " + relationship.Name + " on " + source.Type ); } else { this.log.Debug("Special choice handling: WRITING SINGLE: " + beanWrapper.GetWrappedType() + " property with annotation=" + traversalName + " - value=" + convertedBeans[0]); // may need to ignore values beyond the first; an error will have been logged beanWrapper.Write(relationship, convertedBeans[0]); } } } else { // 3. non-collapsed (including choice, specializationChild, and template type, handling for which is encapsulated in // Source.createChildSource()) if (relationship.TemplateRelationship || relationship.Choice || MessageBeanRegistry.GetInstance().IsMessagePartDefined(source .GetVersion(), relationship.Type)) { IList <object> convertedBeans = new List <object>(); foreach (XmlNode node in nodes) { XmlElement childNode = (XmlElement)node; Hl7PartSource childSource = source.CreatePartSource(relationship, childNode); this.log.Debug("RECURSE for node=" + source.GetCurrentElement().Name + " - relationship=" + relationship.Name + ", tarversalName=" + traversalName + ", of type: " + childSource.Type); object tealChild = MapPartSourceToTeal(childSource, relationship); convertedBeans.Add(tealChild); } if (relationship.Cardinality.Multiple) { this.log.Debug("WRITING MULTIPLE: " + beanWrapper.GetWrappedType() + " property with annotation=" + traversalName + " - values=" + convertedBeans); beanWrapper.Write(relationship, convertedBeans); } else { if (relationship.Cardinality.Single && convertedBeans.IsEmpty()) { throw new MarshallingException("Why is this empty? : " + relationship.Name + " on " + source.Type); } else { this.log.Debug("WRITING SINGLE: " + beanWrapper.GetWrappedType() + " property with annotation=" + traversalName + " - value=" + convertedBeans[0]); // may need to ignore values beyond the first; an error will have been logged beanWrapper.Write(relationship, convertedBeans[0]); } } } else { if (!ConformanceLevelUtil.IsOptional(relationship) && !IsFullyFixedType(relationship, source)) { this.log.Info("IGNORING: HL7 type " + relationship.Type + " with traversalName=" + traversalName + "(" + Describer.Describe (source.GetMessagePartName(), relationship) + ") cannot be mapped to any teal bean"); } } } } } } }