private void WriteAssociation(BeanWrapper childBeanWrapper, Hl7Source source, XmlElement element, Relationship relationship ) { Hl7PartSource childSource = source.CreatePartSource(relationship, element); MapToTeal(childSource, childBeanWrapper, relationship); }
internal BeanWrapper(object bean) { this.map = BeanProperty.GetProperties(bean); this.sorter = RelationshipSorter.Create(string.Empty, bean); this.contextName = null; this.parentWrapper = null; }
private BeanWrapper(Ca.Infoway.Messagebuilder.Marshalling.BeanWrapper parentWrapper, string contextName, RelationshipSorter sorter) { this.parentWrapper = parentWrapper; this.map = parentWrapper.map; this.sorter = sorter; this.contextName = Concatenate(parentWrapper.contextName, contextName); }
public virtual void ShouldWriteMultipleCardinalityAttribute() { BeanBPrime beanB = new BeanBPrime(); BeanWrapper wrapper = new BeanWrapper(beanB); wrapper.Write(new Relationship("text", "ST", Cardinality.Create("0-10")), "This is my text"); Assert.AreEqual("This is my text", beanB.Text[0], "text"); }
public virtual void ShouldWriteSimpleAttribute() { BeanB beanB = new BeanB(); BeanWrapper wrapper = new BeanWrapper(beanB); wrapper.Write(new Relationship("text", "ST", Cardinality.Create("0-1")), "This is my text"); Assert.AreEqual("This is my text", beanB.Text, "text"); }
private void MapToTeal(Hl7Source source, BeanWrapper wrapper, Relationship relationship) { bool hasNullFlavor = MapNodeAttributesToTeal(source, wrapper, relationship); IList <XmlElement> elements = NodeUtil.ToElementList(source.GetCurrentElement()); if (hasNullFlavor && elements.IsEmpty()) { // don't bother processing/validating any further // however, if there are elements and the association has a nullFlavor, something weird is going on so keep processing return; } // 1) "elements" contains the xml-order of the current part's relationships - note that this can have duplicates at this point // 2) "source" contains the message part being processed - this is not exposed // 3) "source" contains the result bean where errors can be stored - this *is* exposed // 4) need to watch choice/template cases (including choices with supertypes) // relationship.getType() is // - choice type (if choice) // - null (if template) // - or the actual type // source.getMessagePartName() is // - the choice option (if above was a choice) // - the actual template type (if above was null) // - or the actual type (which it is in all cases, really) IList <string> xmlElementNamesInProvidedOrder = new List <string>(); List <Relationship> sortedRelationshipsMatchingUpToXmlElementNames = new List <Relationship>(); IDictionary <string, string> resolvedRelationshipNames = new Dictionary <string, string>(); int length = elements.Count; for (int j = 0; j < length; j++) { XmlElement element = elements[j]; string nodeName = NodeUtil.GetLocalOrTagName(element); IList <XmlNode> nodes = new List <XmlNode>(); nodes.Add(element); while (j + 1 < length && IsSameElementName(element, elements[j + 1])) { nodes.Add(elements[++j]); } if (NamespaceUtil.IsHl7Node(element)) { Relationship xmlRelationship = source.GetRelationship(nodeName); if (xmlRelationship != null) { ValidateNamespace(element, xmlRelationship, source); // since we have a match we know that the xml name is correct; all we need the xmlRelationship for is sorting purposes // however, for choice and template relationships, there will be an apparent mismatch between relationship names xmlElementNamesInProvidedOrder.Add(nodeName); sortedRelationshipsMatchingUpToXmlElementNames.Add(xmlRelationship); resolvedRelationshipNames[GenerateRelationshipKey(xmlRelationship)] = nodeName; } Process(wrapper, source, nodes, nodeName); } } ValidateElementOrder(source, xmlElementNamesInProvidedOrder, sortedRelationshipsMatchingUpToXmlElementNames, resolvedRelationshipNames ); // only do this if relationship not null and relationship not a null flavor??? ValidateMissingMandatoryNonStructuralRelationships(source, resolvedRelationshipNames); }
internal virtual object MapPartSourceToTeal(Hl7PartSource source, Relationship relationship) { object bean = Instantiator.GetInstance().InstantiateMessagePartBean(source.GetVersion(), source.Type, source.GetInteraction ()); BeanWrapper wrapper = new BeanWrapper(bean); MapToTeal(source, wrapper, relationship); return(bean); }
public virtual void ShouldWriteSimpleAssociation() { BeanB beanB = new BeanB(); BeanC beanC = new BeanC(); BeanWrapper wrapper = new BeanWrapper(beanC); wrapper.Write(new Relationship("textHolder", "ABCD_IN123456CA.BeanB", Cardinality.Create("0-1")), beanB); Assert.IsNotNull(beanC.BeanB, "bean b"); }
public virtual void ShouldWriteNullFlavorOnCollapsedAssociation() { BeanCPrime beanC = new BeanCPrime(); BeanWrapper wrapper = new BeanWrapper(beanC); wrapper.WriteNullFlavor(null, new Relationship("component2", "ABCD_IN123456CA.BeanB", Cardinality.Create("0-1")), Ca.Infoway.Messagebuilder.Domainvalue.Nullflavor.NullFlavor .NO_INFORMATION); Assert.IsNull(beanC.BeanB, "bean"); }
public virtual void ShouldCopyOriginalTextFromCVImplObjectToBean() { BeanD beanD = new BeanD(); BeanWrapper wrapper = new BeanWrapper(beanD); CVImpl cvImpl = new CVImpl(CodeResolverRegistry.Lookup <IntoleranceValue>("CODE")); cvImpl.OriginalText = ORIGINAL_TEXT; wrapper.Write(new Relationship("value", "CV", Cardinality.Create("0-1")), cvImpl); Assert.AreEqual(ORIGINAL_TEXT, ((CV)beanD.GetField("someCode")).OriginalText, "originalText"); }
private void WriteSpecialAssociation(BeanWrapper childBeanWrapper, Hl7Source source, IList <XmlNode> nodes, Relationship relationship ) { if (nodes.Count == 1) { XmlElement childNode = (XmlElement)nodes[0]; WriteAssociation(childBeanWrapper, source, childNode, relationship); } else { throw new MarshallingException("Expected a single cardinality element to have only one node: "); } }
public virtual XmlToModelResult MapToTeal(Hl7MessageSource hl7MessageSource) { if (hl7MessageSource.GetInteraction() != null) { Type messageBeanType = MessageBeanRegistry.GetInstance().GetInteractionBeanType(hl7MessageSource.GetMessageTypeKey()); object messageBean = BeanUtil.Instantiate <object>(messageBeanType); BeanWrapper wrapper = new BeanWrapper(messageBean); MapToTeal(hl7MessageSource, wrapper, null); hl7MessageSource.GetResult().SetMessageObject(messageBean); CreateBeanPathOnErrorMessages(hl7MessageSource.GetResult(), messageBean); } return(hl7MessageSource.GetResult()); }
/// <exception cref="Ca.Infoway.Messagebuilder.Marshalling.HL7.XmlToModelTransformationException"></exception> private void WriteAttribute(BeanWrapper bean, Hl7Source source, IList <XmlNode> nodes, Relationship relationship, string traversalName ) { if (relationship.Structural) { source.GetResult().AddHl7Error(new Hl7Error(Hl7ErrorCode.INTERNAL_ERROR, "Data found for relationship as an element but should have been an attribute. " + (nodes.IsEmpty() ? ("(" + relationship.Name + ")") : XmlDescriber.DescribePath(nodes[0])), CollUtils.IsEmpty(nodes) ? null : (XmlElement)nodes[0])); } string type = DetermineType(nodes, relationship, source, source.GetResult()); ElementParser parser = (source.IsR2() ? ParserR2Registry.GetInstance().Get(type) : ParserRegistry.GetInstance().Get(type) ); if (parser != null) { try { ConstrainedDatatype constraints = source.GetService().GetConstraints(source.GetVersion(), relationship.ConstrainedType); ParseContextImpl context = new ParseContextImpl(relationship, constraints, source.GetVersion(), source.GetDateTimeZone(), source.GetDateTimeTimeZone(), CodeTypeRegistry.GetInstance(), source.IsCda()); BareANY @object = parser.Parse(context, nodes, source.GetResult()); ChangeDatatypeIfNecessary(type, relationship, @object); if (relationship.HasFixedValue()) { ValidateNonstructuralFixedValue(relationship, @object, source, nodes); } else { // fixed means nothing to write to bean bean.Write(relationship, @object); } } catch (InvalidCastException e) { source.GetResult().AddHl7Error(new Hl7Error(Hl7ErrorCode.INTERNAL_ERROR, "Can't parse relationship name=" + relationship. Name + ", traversalName=" + traversalName + " [" + e.Message + "]", CollUtils.IsEmpty(nodes) ? null : (XmlElement)nodes[ 0])); } } else { source.GetResult().AddHl7Error(new Hl7Error(Hl7ErrorCode.INTERNAL_ERROR, "No parser for type \"" + type + "\". " + (nodes .IsEmpty() ? ("(" + relationship.Name + ")") : XmlDescriber.DescribePath(nodes[0])), CollUtils.IsEmpty(nodes) ? null : ( XmlElement)nodes[0])); } }
/// <exception cref="Ca.Infoway.Messagebuilder.Marshalling.HL7.XmlToModelTransformationException"></exception> private void WriteIndicator(BeanWrapper bean, Hl7Source source, IList <XmlNode> nodes, Relationship relationship, string traversalName ) { try { // if "false", as in the indicator element is absent, we will never actually get here :) // can't really parse a boolean here, but we need to check for null flavor NullFlavorHelper nullFlavorHelper = new NullFlavorHelper(relationship.Conformance, nodes.IsEmpty() ? null : nodes[0], new XmlToModelResult(), true); NullFlavor nullFlavor = nullFlavorHelper.ParseNullNode(); object value = (nullFlavor == null ? new BLImpl(!nodes.IsEmpty()) : new BLImpl(nullFlavor)); bean.Write(relationship, value); } catch (InvalidCastException e) { this.log.Info("Can't parse relationship name=" + relationship.Name + ", traversalName=" + traversalName + " [" + e.Message + "]"); } }
private bool MapNodeAttributesToTeal(Hl7Source source, BeanWrapper wrapper, Relationship relationship) { XmlElement currentElement = source.GetCurrentElement(); NullFlavorHelper nullFlavorHelper = new NullFlavorHelper(relationship != null ? relationship.Conformance : Ca.Infoway.Messagebuilder.Xml.ConformanceLevel .OPTIONAL, currentElement, source.GetResult(), true); bool hasValidNullFlavorAttribute = nullFlavorHelper.HasValidNullFlavorAttribute(); if (hasValidNullFlavorAttribute) { wrapper.WriteNullFlavor(source, relationship, nullFlavorHelper.ParseNullNode()); } else { XmlAttributeCollection map = currentElement.Attributes; foreach (XmlNode attributeNode in new XmlNamedNodeMapIterable(map)) { Relationship attributeRelationship = source.GetRelationship(NodeUtil.GetLocalOrTagName(attributeNode)); if (!NamespaceUtil.IsHl7Node(attributeNode)) { } else { // quietly ignore it if (attributeRelationship == null) { this.log.Info("Can't find NodeAttribute relationship named: " + attributeNode.Name); } else { ValidateNamespace(attributeNode, attributeRelationship, source); if (attributeRelationship.HasFixedValue()) { ValidateFixedValue(source, currentElement, (XmlAttribute)attributeNode, attributeRelationship); } wrapper.WriteNodeAttribute(attributeRelationship, attributeNode.Value, source.GetVersion(), source.IsR2()); } } ValidateMandatoryAttributesExist(source, currentElement); } } return(hasValidNullFlavorAttribute); }
/// <exception cref="Ca.Infoway.Messagebuilder.Marshalling.HL7.XmlToModelTransformationException"></exception> private void WriteRealmCode(BeanWrapper bean, Hl7Source source, IList <XmlNode> nodes, string traversalName) { XPathHelper xPathHelper = new XPathHelper(); foreach (XmlNode node in nodes) { try { string codeValue = xPathHelper.GetAttributeValue(node, "@code", null); if (codeValue != null) { bean.WriteRealmCode(RealmCodeHelper.LookupRealm(codeValue)); } } catch (XPathException e) { throw new XmlToModelTransformationException("Exception encountered while parsing realmCode", e); } } }
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"); } } } } } } }
private void Process(BeanWrapper bean, Hl7Source source, IList <XmlNode> nodes, string traversalName) { if ("realmCode".Equals(traversalName)) { WriteRealmCode(bean, source, nodes, traversalName); } Relationship relationship = source.GetRelationship(traversalName); try { if (relationship == null) { string message = "Can't find a relationship named : " + traversalName + " on messagePart named " + source.GetMessagePartName (); if (MappingNotYetSupported(traversalName)) { this.log.Info(message); } else { if (nodes == null || nodes.IsEmpty()) { throw new MarshallingException(message); } else { source.GetResult().AddHl7Error(new Hl7Error(Hl7ErrorCode.SYNTAX_ERROR, message, (XmlElement)nodes[0])); } } } else { if (relationship.Attribute) { // attribute cardinality checked at datatype level WriteAttribute(bean, source, nodes, relationship, traversalName); } else { // need to check association cardinality here ValidateAssociationCardinality(source, nodes, traversalName, relationship); if (IsIndicator(source, relationship)) { WriteIndicator(bean, source, nodes, relationship, traversalName); } else { WriteAssociation(bean, source, nodes, relationship, traversalName); } } } } catch (MarshallingException e) { // RM18422 - log an error rather than throwing an exception up the chain // this is a "known" exception that has been handled to some extent XmlElement element = nodes == null || nodes.IsEmpty() ? null : (XmlElement)nodes[0]; source.GetResult().AddHl7Error(new Hl7Error(Hl7ErrorCode.DATA_TYPE_ERROR, e.Message, element)); } catch (Exception e) { // RM18422 - unexpected (and thus unhandled) exception; still, likely best to log it rather than kill entire process XmlElement element = nodes == null || nodes.IsEmpty() ? null : (XmlElement)nodes[0]; source.GetResult().AddHl7Error(new Hl7Error(Hl7ErrorCode.DATA_TYPE_ERROR, "Unexpected error: " + e.Message, element)); } }