/// <summary>
        /// Converts a PharmanetMessage to an HL7 FHIR DocumentReference model.
        /// </summary>
        /// <param name="messageModel">The PharmanetMessage to convert from.</param>
        /// <param name="related">The optional ResourceReference representing a reference to the request message.</param>
        /// <returns>Returns a new DocumentReference from the PharmanetMessage.
        /// If the requestDocumentReference is provided, it used that to fill out cross-referenced data.
        /// </returns>
        public static DocumentReference ToDocumentReference(PharmanetMessageModel messageModel, ResourceReference?related = null)
        {
            DocumentReference documentReference = new DocumentReference();

            documentReference.Status           = DocumentReferenceStatus.Current;
            documentReference.Date             = DateTime.UtcNow;
            documentReference.Id               = messageModel.TransactionId; // set the GUID as the base artefact ID.
            documentReference.MasterIdentifier = new Identifier(MasterIdentifierNamespace, MasterIdentifierUrnPrefix + messageModel.TransactionId);
            DocumentReference.ContentComponent item = new DocumentReference.ContentComponent();
            item.Attachment = new Attachment();
            string hl7Message = Encoding.UTF8.GetString(Convert.FromBase64String(messageModel.Hl7Message));

            item.Attachment.Data        = Encoding.UTF8.GetBytes(hl7Message);
            item.Attachment.ContentType = HL7v2ContentType;

            documentReference.Content.Add(item);
            if (related != null)
            {
                documentReference.Context         = new DocumentReference.ContextComponent();
                documentReference.Context.Related = new List <ResourceReference>();
                documentReference.Context.Related.Add(related);
            }

            return(documentReference);
        }
        /// <summary>
        /// Converts an HL7 FHIR DocumentReference model to a PharmanetMessage model.
        /// </summary>
        /// <param name="documentReference">The DocumentReference to convert from.</param>
        /// <param name="base64Encode">A value indicating whether to base64 encode the HL7v2.</param>
        /// <returns>Returns a new PharmanetMessage mapped from the provided DocumentReference.</returns>
        public static PharmanetMessageModel ToPharmanetMessageModel(DocumentReference documentReference, bool base64Encode)
        {
            PharmanetMessageModel messageModel = new PharmanetMessageModel();

            // HL7 FHIR spec for GUID/UUID has this mandatory prefix in the value field.
            foreach (Match?m in Regex.Matches(documentReference.MasterIdentifier.Value, MasterIdentifierPattern))
            {
                GroupCollection groups = m !.Groups;
                string          value  = groups[1].Value;
                messageModel.TransactionId = value; // The GUID/UUID
                break;
            }

            DocumentReference.ContentComponent[] content = documentReference.Content.ToArray();

            byte[] data        = content[0].Attachment.Data;
            string contentType = content[0].Attachment.ContentType;
            bool   good        = (data.Length > 0) && contentType.Equals(HL7v2ContentType, System.StringComparison.Ordinal);

            messageModel.Hl7Message = good ? (base64Encode ? Convert.ToBase64String(data, Base64FormattingOptions.None) : Encoding.UTF8.GetString(data)) : string.Empty;

            return(messageModel);
        }