public static DocumentReference DocumentToDocumentReference(Composition composition, Bundle bundle, Binary bin, Uri binLocation)
        {
            var reference = new DocumentReference();
            reference.MasterIdentifier = new Identifier(Identifier.SYSTMEM_URI, bundle.Id.ToString());
            reference.Identifier = composition.Identifier != null ? new List<Identifier>() { composition.Identifier } : null;
            reference.Subject = composition.Subject;
            reference.Type = composition.Type;
            reference.Class = composition.Class;
            reference.Author = new List<ResourceReference>( composition.Author );
            reference.Custodian = composition.Custodian;
            reference.Authenticator = composition.Attester != null ? composition.Attester.Where(att => att.Mode.Any(am => am == Composition.CompositionAttestationMode.Professional ||
                                                                    am == Composition.CompositionAttestationMode.Legal) && att.Party != null).Select(att => att.Party).Last() : null;
            reference.CreatedElement = composition.Date != null ? new FhirDateTime(composition.Date) : null;
            reference.IndexedElement = Instant.Now();
            reference.Status = DocumentReference.DocumentReferenceStatus.Current;

            reference.DocStatus = composition.Status != null ?
                new CodeableConcept { Coding = new List<Coding>() { new Coding("http://hl7.org/fhir/composition-status", composition.Status.ToString()) } } : null;
            // todo: Use proper Enumerator conversion for composition.Status

            reference.Description = composition.Title;
            reference.Confidentiality = composition.Confidentiality != null ? new List<CodeableConcept>() { new CodeableConcept() { Coding = new List<Coding>() { composition.Confidentiality } } } : null;
            reference.PrimaryLanguage = composition.Language;
            reference.MimeType = bin.ContentType;
            reference.Format = new List<Uri>( bundle.Tags.FilterOnFhirSchemes().Where(t => t.Scheme != Tag.FHIRTAGSCHEME_SECURITY).Select(tg => new Uri(tg.Term)) );
            reference.Size = bin.Content.Length;
            reference.Hash = calculateSHA1(bin.Content);
            reference.Location = binLocation;

            if (composition.Event != null)
            {
                reference.Context = new DocumentReference.DocumentReferenceContextComponent();
                reference.Context.Event = composition.Event.Code != null ? new List<CodeableConcept>(composition.Event.Code) : null;
                reference.Context.Period = composition.Event.Period != null ? composition.Event.Period : null;
            }

            return reference;
        }
Пример #2
0
        public static void SerializeDocumentReference(Hl7.Fhir.Model.DocumentReference value, IFhirWriter writer, bool summary)
        {
            writer.WriteStartRootObject("DocumentReference");
            writer.WriteStartComplexContent();

            // Serialize element _id
            if (value.LocalIdElement != null)
            {
                writer.WritePrimitiveContents("_id", value.LocalIdElement, XmlSerializationHint.Attribute);
            }

            // Serialize element extension
            if (value.Extension != null && !summary && value.Extension.Count > 0)
            {
                writer.WriteStartArrayElement("extension");
                foreach (var item in value.Extension)
                {
                    writer.WriteStartArrayMember("extension");
                    ExtensionSerializer.SerializeExtension(item, writer, summary);
                    writer.WriteEndArrayMember();
                }
                writer.WriteEndArrayElement();
            }

            // Serialize element language
            if (value.LanguageElement != null && !summary)
            {
                writer.WriteStartElement("language");
                CodeSerializer.SerializeCode(value.LanguageElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element text
            if (value.Text != null && !summary)
            {
                writer.WriteStartElement("text");
                NarrativeSerializer.SerializeNarrative(value.Text, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element contained
            if (value.Contained != null && !summary && value.Contained.Count > 0)
            {
                writer.WriteStartArrayElement("contained");
                foreach (var item in value.Contained)
                {
                    writer.WriteStartArrayMember("contained");
                    FhirSerializer.SerializeResource(item, writer, summary);
                    writer.WriteEndArrayMember();
                }
                writer.WriteEndArrayElement();
            }

            // Serialize element masterIdentifier
            if (value.MasterIdentifier != null && !summary)
            {
                writer.WriteStartElement("masterIdentifier");
                IdentifierSerializer.SerializeIdentifier(value.MasterIdentifier, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element identifier
            if (value.Identifier != null && !summary && value.Identifier.Count > 0)
            {
                writer.WriteStartArrayElement("identifier");
                foreach (var item in value.Identifier)
                {
                    writer.WriteStartArrayMember("identifier");
                    IdentifierSerializer.SerializeIdentifier(item, writer, summary);
                    writer.WriteEndArrayMember();
                }
                writer.WriteEndArrayElement();
            }

            // Serialize element subject
            if (value.Subject != null && !summary)
            {
                writer.WriteStartElement("subject");
                ResourceReferenceSerializer.SerializeResourceReference(value.Subject, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element type
            if (value.Type != null && !summary)
            {
                writer.WriteStartElement("type");
                CodeableConceptSerializer.SerializeCodeableConcept(value.Type, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element subtype
            if (value.Subtype != null && !summary)
            {
                writer.WriteStartElement("subtype");
                CodeableConceptSerializer.SerializeCodeableConcept(value.Subtype, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element author
            if (value.Author != null && !summary && value.Author.Count > 0)
            {
                writer.WriteStartArrayElement("author");
                foreach (var item in value.Author)
                {
                    writer.WriteStartArrayMember("author");
                    ResourceReferenceSerializer.SerializeResourceReference(item, writer, summary);
                    writer.WriteEndArrayMember();
                }
                writer.WriteEndArrayElement();
            }

            // Serialize element custodian
            if (value.Custodian != null && !summary)
            {
                writer.WriteStartElement("custodian");
                ResourceReferenceSerializer.SerializeResourceReference(value.Custodian, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element authenticator
            if (value.Authenticator != null && !summary)
            {
                writer.WriteStartElement("authenticator");
                ResourceReferenceSerializer.SerializeResourceReference(value.Authenticator, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element created
            if (value.CreatedElement != null && !summary)
            {
                writer.WriteStartElement("created");
                FhirDateTimeSerializer.SerializeFhirDateTime(value.CreatedElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element indexed
            if (value.IndexedElement != null && !summary)
            {
                writer.WriteStartElement("indexed");
                InstantSerializer.SerializeInstant(value.IndexedElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element status
            if (value.StatusElement != null && !summary)
            {
                writer.WriteStartElement("status");
                CodeSerializer.SerializeCode <Hl7.Fhir.Model.DocumentReference.DocumentReferenceStatus>(value.StatusElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element docStatus
            if (value.DocStatus != null && !summary)
            {
                writer.WriteStartElement("docStatus");
                CodeableConceptSerializer.SerializeCodeableConcept(value.DocStatus, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element supercedes
            if (value.Supercedes != null && !summary)
            {
                writer.WriteStartElement("supercedes");
                ResourceReferenceSerializer.SerializeResourceReference(value.Supercedes, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element description
            if (value.DescriptionElement != null && !summary)
            {
                writer.WriteStartElement("description");
                FhirStringSerializer.SerializeFhirString(value.DescriptionElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element confidentiality
            if (value.Confidentiality != null && !summary)
            {
                writer.WriteStartElement("confidentiality");
                CodeableConceptSerializer.SerializeCodeableConcept(value.Confidentiality, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element primaryLanguage
            if (value.PrimaryLanguageElement != null && !summary)
            {
                writer.WriteStartElement("primaryLanguage");
                CodeSerializer.SerializeCode(value.PrimaryLanguageElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element mimeType
            if (value.MimeTypeElement != null && !summary)
            {
                writer.WriteStartElement("mimeType");
                CodeSerializer.SerializeCode(value.MimeTypeElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element format
            if (value.Format != null && !summary)
            {
                writer.WriteStartElement("format");
                CodeableConceptSerializer.SerializeCodeableConcept(value.Format, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element size
            if (value.SizeElement != null && !summary)
            {
                writer.WriteStartElement("size");
                IntegerSerializer.SerializeInteger(value.SizeElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element hash
            if (value.HashElement != null && !summary)
            {
                writer.WriteStartElement("hash");
                FhirStringSerializer.SerializeFhirString(value.HashElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element location
            if (value.LocationElement != null && !summary)
            {
                writer.WriteStartElement("location");
                FhirUriSerializer.SerializeFhirUri(value.LocationElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element service
            if (value.Service != null && !summary)
            {
                writer.WriteStartElement("service");
                DocumentReferenceSerializer.SerializeDocumentReferenceServiceComponent(value.Service, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element context
            if (value.Context != null && !summary)
            {
                writer.WriteStartElement("context");
                DocumentReferenceSerializer.SerializeDocumentReferenceContextComponent(value.Context, writer, summary);
                writer.WriteEndElement();
            }


            writer.WriteEndComplexContent();
            writer.WriteEndRootObject();
        }
        /// <summary>
        /// Parse DocumentReference
        /// </summary>
        public static Hl7.Fhir.Model.DocumentReference ParseDocumentReference(IFhirReader reader, ErrorList errors, Hl7.Fhir.Model.DocumentReference existingInstance = null)
        {
            Hl7.Fhir.Model.DocumentReference result = existingInstance != null ? existingInstance : new Hl7.Fhir.Model.DocumentReference();
            string currentElementName = reader.CurrentElementName;

            reader.EnterElement();

            while (reader.HasMoreElements())
            {
                var atName = reader.CurrentElementName;
                // Parse element extension
                if (atName == "extension")
                {
                    result.Extension = new List <Hl7.Fhir.Model.Extension>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "extension"))
                    {
                        result.Extension.Add(ExtensionParser.ParseExtension(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element language
                else if (atName == "language")
                {
                    result.LanguageElement = CodeParser.ParseCode(reader, errors);
                }

                // Parse element text
                else if (atName == "text")
                {
                    result.Text = NarrativeParser.ParseNarrative(reader, errors);
                }

                // Parse element contained
                else if (atName == "contained")
                {
                    result.Contained = new List <Hl7.Fhir.Model.Resource>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "contained"))
                    {
                        result.Contained.Add(ParserUtils.ParseContainedResource(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element _id
                else if (atName == "_id")
                {
                    result.LocalIdElement = Id.Parse(reader.ReadPrimitiveContents(typeof(Id)));
                }

                // Parse element masterIdentifier
                else if (atName == "masterIdentifier")
                {
                    result.MasterIdentifier = IdentifierParser.ParseIdentifier(reader, errors);
                }

                // Parse element identifier
                else if (atName == "identifier")
                {
                    result.Identifier = new List <Hl7.Fhir.Model.Identifier>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "identifier"))
                    {
                        result.Identifier.Add(IdentifierParser.ParseIdentifier(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element subject
                else if (atName == "subject")
                {
                    result.Subject = ResourceReferenceParser.ParseResourceReference(reader, errors);
                }

                // Parse element type
                else if (atName == "type")
                {
                    result.Type = CodeableConceptParser.ParseCodeableConcept(reader, errors);
                }

                // Parse element subtype
                else if (atName == "subtype")
                {
                    result.Subtype = CodeableConceptParser.ParseCodeableConcept(reader, errors);
                }

                // Parse element author
                else if (atName == "author")
                {
                    result.Author = new List <Hl7.Fhir.Model.ResourceReference>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "author"))
                    {
                        result.Author.Add(ResourceReferenceParser.ParseResourceReference(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element custodian
                else if (atName == "custodian")
                {
                    result.Custodian = ResourceReferenceParser.ParseResourceReference(reader, errors);
                }

                // Parse element authenticator
                else if (atName == "authenticator")
                {
                    result.Authenticator = ResourceReferenceParser.ParseResourceReference(reader, errors);
                }

                // Parse element created
                else if (atName == "created")
                {
                    result.CreatedElement = FhirDateTimeParser.ParseFhirDateTime(reader, errors);
                }

                // Parse element indexed
                else if (atName == "indexed")
                {
                    result.IndexedElement = InstantParser.ParseInstant(reader, errors);
                }

                // Parse element status
                else if (atName == "status")
                {
                    result.StatusElement = CodeParser.ParseCode <Hl7.Fhir.Model.DocumentReference.DocumentReferenceStatus>(reader, errors);
                }

                // Parse element docStatus
                else if (atName == "docStatus")
                {
                    result.DocStatus = CodeableConceptParser.ParseCodeableConcept(reader, errors);
                }

                // Parse element supercedes
                else if (atName == "supercedes")
                {
                    result.Supercedes = ResourceReferenceParser.ParseResourceReference(reader, errors);
                }

                // Parse element description
                else if (atName == "description")
                {
                    result.DescriptionElement = FhirStringParser.ParseFhirString(reader, errors);
                }

                // Parse element confidentiality
                else if (atName == "confidentiality")
                {
                    result.Confidentiality = CodeableConceptParser.ParseCodeableConcept(reader, errors);
                }

                // Parse element primaryLanguage
                else if (atName == "primaryLanguage")
                {
                    result.PrimaryLanguageElement = CodeParser.ParseCode(reader, errors);
                }

                // Parse element mimeType
                else if (atName == "mimeType")
                {
                    result.MimeTypeElement = CodeParser.ParseCode(reader, errors);
                }

                // Parse element format
                else if (atName == "format")
                {
                    result.Format = CodeableConceptParser.ParseCodeableConcept(reader, errors);
                }

                // Parse element size
                else if (atName == "size")
                {
                    result.SizeElement = IntegerParser.ParseInteger(reader, errors);
                }

                // Parse element hash
                else if (atName == "hash")
                {
                    result.HashElement = FhirStringParser.ParseFhirString(reader, errors);
                }

                // Parse element location
                else if (atName == "location")
                {
                    result.LocationElement = FhirUriParser.ParseFhirUri(reader, errors);
                }

                // Parse element service
                else if (atName == "service")
                {
                    result.Service = DocumentReferenceParser.ParseDocumentReferenceServiceComponent(reader, errors);
                }

                // Parse element context
                else if (atName == "context")
                {
                    result.Context = DocumentReferenceParser.ParseDocumentReferenceContextComponent(reader, errors);
                }

                else
                {
                    errors.Add(String.Format("Encountered unknown element {0} while parsing {1}", reader.CurrentElementName, currentElementName), reader);
                    reader.SkipSubElementsFor(currentElementName);
                    result = null;
                }
            }

            reader.LeaveElement();
            return(result);
        }