/// <summary> /// Creates a code expression following the rules /// </summary> private string CreateCodeExpression(IConceptDescriptor code, bool isOwnSegment) { // Validate that all qualifiers come from the same code system string codeSystemOid = code.CodeSystem; foreach (IConceptQualifier cr in code.Qualifier) { if (cr.Name.CodeSystem != codeSystemOid || cr.Value.CodeSystem != codeSystemOid) { throw new InvalidOperationException("The specified code cannot be represented as a code expression as all qualifiers must come from the same code system as the parent"); } } // Expression builder StringBuilder expr = new StringBuilder(CreateCodeExpression(code as ICodedValue)); // Create qualifiers if (code.Qualifier != null) { if (code.Qualifier.Count == 1) { expr.AppendFormat(":{0}={1}", CreateCodeExpression((code.Qualifier[0] as IConceptQualifier).Name), CreateCodeExpression((code.Qualifier[0] as IConceptQualifier).Value, false)); } else if (code.Qualifier.Count > 1) { if (isOwnSegment) { expr.Append(":{"); } else { expr.Append("{"); } foreach (IConceptQualifier itm in code.Qualifier) { string formatString = "{0}={1},"; if (itm.Value.Qualifier != null && itm.Value.Qualifier.Count > 0) { formatString = "{0}=({1}),"; } expr.AppendFormat(formatString, CreateCodeExpression(itm.Name), CreateCodeExpression(itm.Value, false)); } expr.Remove(expr.Length - 1, 1); expr.Append("}"); } } return(expr.ToString()); }
/// <summary> /// Graph object <paramref name="o"/> onto <paramref name="s"/> /// </summary> /// <param name="s">The stream to graph to</param> /// <param name="o">The object to graph</param> public override void Graph(System.Xml.XmlWriter s, object o, DatatypeFormatterGraphResult result) { // Get an instance ref IConceptDescriptor instance_ics = (IConceptDescriptor)o; // Do a base format base.Graph(s, o, result); // Format the coded simple if (instance_ics.Qualifier != null) // Original Text { foreach (IGraphable ig in instance_ics.Qualifier) { s.WriteStartElement("qualifier", "urn:hl7-org:v3"); var hostResult = Host.Graph(s, ig); result.AddResultDetail(hostResult.Details); result.Code = hostResult.Code; s.WriteEndElement(); } } }
/// <summary> /// Create code expression for the specified concept descriptor /// </summary> public string CreateCodeExpression(IConceptDescriptor code) { return(CreateCodeExpression(code, true)); }
/// <summary> /// Graph <paramref name="o"/> onto <paramref name="s"/> /// </summary> public void Graph(System.Xml.XmlWriter s, object o, DatatypeR2FormatterGraphResult result) { // Base formatter ANYFormatter baseFormatter = new ANYFormatter(); baseFormatter.Graph(s, o, result); // Next, start to format the properties ICodedSimple cs = o as ICodedSimple; ICodedValue cv = o as ICodedValue; ICodedEquivalents ce = o as ICodedEquivalents; IConceptDescriptor cd = o as IConceptDescriptor; ST displayName = cv.DisplayName; IAny any = o as IAny; // Unless "other" is specified don't serialize if (any.NullFlavor != null && !((NullFlavor)any.NullFlavor).IsChildConcept(NullFlavor.Other)) { return; } // First we need to serialize code, now Code in R2 is a little wierd // It is an IHTSDO standard as described: // http://www.ihtsdo.org/fileadmin/user_upload/Docs_01/Technical_Docs/abstract_models_and_representational_forms.pdf // Serialize attributes first if (cs.CodeValue != null) { try { if (cd != null && cd.Qualifier != null && cd.Qualifier.Count > 0) { s.WriteAttributeString("code", CreateCodeExpression(cd)); displayName = null; } else { s.WriteAttributeString("code", Util.ToWireFormat(cs.CodeValue)); } } catch (Exception e) { result.AddResultDetail(new VocabularyIssueResultDetail(ResultDetailType.Error, e.Message, s.ToString(), e)); } } if (cv.CodeSystem != null) { s.WriteAttributeString("codeSystem", Util.ToWireFormat(cv.CodeSystem)); } if (cv.CodeSystemName != null) { s.WriteAttributeString("codeSystemName", Util.ToWireFormat(cv.CodeSystemName)); } if (cv.CodeSystemVersion != null) { s.WriteAttributeString("codeSystemVersion", Util.ToWireFormat(cv.CodeSystemVersion)); } if (cv.ValueSet != null) { s.WriteAttributeString("valueSet", Util.ToWireFormat(cv.ValueSet)); } if (cv.ValueSetVersion != null) { s.WriteAttributeString("valueSetVersion", Util.ToWireFormat(cv.ValueSetVersion)); } if (cv.CodingRationale != null) { s.WriteAttributeString("codingRationale", Util.ToWireFormat(cv.CodingRationale)); } // Elements // Display name if (displayName != null) { s.WriteStartElement("displayName", "urn:hl7-org:v3"); var hostResult = this.Host.Graph(s, displayName as IGraphable); result.Code = hostResult.Code; result.AddResultDetail(hostResult.Details); s.WriteEndElement(); } // Original text if (cv.OriginalText != null) { s.WriteStartElement("originalText", "urn:hl7-org:v3"); var hostResult = this.Host.Graph(s, cv.OriginalText); result.Code = hostResult.Code; result.AddResultDetail(hostResult.Details); s.WriteEndElement(); } // Translation if (ce != null && ce.Translation != null) { foreach (var translation in ce.Translation) { s.WriteStartElement("translation", "urn:hl7-org:v3"); var hostResult = this.Host.Graph(s, translation); result.Code = hostResult.Code; result.AddResultDetail(hostResult.Details); s.WriteEndElement(); } } // Done }