示例#1
0
 public void EnsureAbsoluteUris(BundleEntry entry)
 {
     if (!entry.Id.IsAbsoluteUri)
         entry.Id = makeAbsolute(entry.Id);
     if (!entry.Links.SelfLink.IsAbsoluteUri)
         entry.Links.SelfLink = makeAbsolute(entry.Links.SelfLink);
 }
示例#2
0
文件: Index.cs 项目: TonyAbell/spark
 public void Process(BundleEntry entry)
 {
     if (entry is ResourceEntry)
         Put(entry as ResourceEntry);
     else if (entry is DeletedEntry)
         Delete(entry as DeletedEntry);
 }
        public static void WriteTo(BundleEntry entry, JsonWriter writer, bool summary = false)
        {
            if (entry == null) throw new ArgumentException("Entry cannot be null");

            var result = createEntry(entry, summary);

            result.WriteTo(writer);
        }
        public static void WriteTo(BundleEntry entry, XmlWriter writer, bool summary = false)
        {
            if (entry == null) throw new ArgumentException("Entry cannot be null");

            var result = createEntry(entry,summary);

            var doc = new XDocument(result);
            doc.WriteTo(writer);
        }
        private static XElement createEntry(BundleEntry entry, bool summary)
        {
            XElement result = null;

            if (entry is ResourceEntry)
            {
                ResourceEntry re = (ResourceEntry)entry;
                result = new XElement(BundleXmlParser.XATOMNS + BundleXmlParser.XATOM_ENTRY);

                if (!String.IsNullOrEmpty(re.Title)) result.Add(xmlCreateTitle(re.Title));
                if (SerializationUtil.UriHasValue(entry.Id)) result.Add(xmlCreateId(entry.Id));

                if (re.LastUpdated != null) result.Add(new XElement(BundleXmlParser.XATOMNS + BundleXmlParser.XATOM_UPDATED, re.LastUpdated.Value));
                if (re.Published != null) result.Add(new XElement(BundleXmlParser.XATOMNS + BundleXmlParser.XATOM_PUBLISHED, re.Published.Value));

                if (!String.IsNullOrWhiteSpace(re.AuthorName))
                    result.Add(xmlCreateAuthor(re.AuthorName, re.AuthorUri));
            }
            else
            {
                result = new XElement(BundleXmlParser.XTOMBSTONE + BundleXmlParser.XATOM_DELETED_ENTRY);
                if (SerializationUtil.UriHasValue(entry.Id))
                    result.Add(new XAttribute(BundleXmlParser.XATOM_DELETED_REF, entry.Id.ToString()));
                if (((DeletedEntry)entry).When != null)
                    result.Add(new XAttribute(BundleXmlParser.XATOM_DELETED_WHEN, ((DeletedEntry)entry).When));
            }

            if(entry.Links != null)
                foreach (var l in entry.Links)
                    if (l.Uri != null) result.Add(xmlCreateLink(l.Rel, l.Uri));

            if (entry.Tags != null)
                foreach (var tag in entry.Tags)
                    result.Add(TagListSerializer.CreateTagCategoryPropertyXml(tag));

            if(entry is ResourceEntry)
            {
                ResourceEntry re = (ResourceEntry)entry;
                if (re.Resource != null)
                    result.Add(new XElement(BundleXmlParser.XATOMNS + BundleXmlParser.XATOM_CONTENT,
                        new XAttribute(BundleXmlParser.XATOM_CONTENT_TYPE, "text/xml"),
                        getContentAsXElement(re.Resource, summary)));

                // Note: this is a read-only property, so it is serialized but never parsed
                if (entry.Summary != null)
                {
                    var xelem = XElement.Parse(entry.Summary);
                    xelem.Name = XHtml.XHTMLNS + xelem.Name.LocalName;

                    result.Add(new XElement(BundleXmlParser.XATOMNS + BundleXmlParser.XATOM_SUMMARY,
                            new XAttribute(BundleXmlParser.XATOM_CONTENT_TYPE, "xhtml"), xelem));
                }
            }

            return result;
        }
示例#6
0
        // The list of hosts + paths that we share id's with:
        // Any resource we import with an url starting with a prefix from this list, will be treated 
        // as an insert/update and use the id specified. Any resource that is not from the list will
        // get a new id assigned based at the first Url in this list.

        public List<Tag> AffixTags(BundleEntry entry, IEnumerable<Tag> tags)
        {
            // Only keep the FHIR tags.
            List<Tag> entryTags = entry.Tags.FilterOnFhirSchemes().ToList() ?? new List<Tag>();
            List<Tag> newTags = tags.ToList();

            // union from newTags ensures tag values on existing tags are replaced
            if (tags != null)
                newTags = newTags.Union(entryTags).ToList();

            return newTags;
        }
示例#7
0
 public void Add(BundleEntry entry)
 {
     BsonDocument document = BundleEntryToBson(entry);
     try
     {
         transaction.Begin();
         transaction.Insert(document);
         transaction.Commit();
     }
     catch
     {
         transaction.Rollback();
         throw;
     }
 }
示例#8
0
        public void PostBundle()
        {
            var bundle = DemoData.GetDemoXdsBundle();

            postResult = client.Transaction(bundle);
            HttpTests.AssertEntryIdsArePresentAndAbsoluteUrls(postResult);
            if (postResult.Entries.Count != 5)
                TestResult.Fail(String.Format("Bundle response contained {0} entries in stead of 5", postResult.Entries.Count));

            postResult = client.RefreshBundle(postResult);
            HttpTests.AssertEntryIdsArePresentAndAbsoluteUrls(postResult);
            var entries = postResult.Entries.ToList();
            
            connDoc = entries[0];
            
            if (new ResourceIdentity(connDoc.Id).Id==null) TestResult.Fail("failed to assign id to new xds document");
            if (new ResourceIdentity(connDoc.SelfLink).VersionId == null) TestResult.Fail("failed to assign a version id to new xds document");

            patDoc = entries[1];
            if (new ResourceIdentity(patDoc.Id) == null) TestResult.Fail("failed to assign id to new patient");

            prac1Doc = entries[2];
            if (new ResourceIdentity(prac1Doc.Id).Id == null) TestResult.Fail("failed to assign id to new practitioner (#1)");
            if (new ResourceIdentity(prac1Doc.SelfLink).VersionId == null) TestResult.Fail("failed to assign a version id to new practitioner (#1)");

            prac2Doc = entries[3];
            if (new ResourceIdentity(prac2Doc.Id).Id == null) TestResult.Fail("failed to assign id to new practitioner (#2)");
            if (new ResourceIdentity(prac2Doc.SelfLink).VersionId == null) TestResult.Fail("failed to assign a version id to new practitioner (#2)");

            binDoc = entries[4];
            if (new ResourceIdentity(binDoc.Id).Id == null) TestResult.Fail("failed to assign id to new binary");
            if (new ResourceIdentity(binDoc.SelfLink).VersionId == null)
                TestResult.Fail("failed to assign a version id to new binary");

            var docResource = ((ResourceEntry<DocumentReference>)connDoc).Resource;

            if (!prac1Doc.Id.ToString().Contains(docResource.Author[0].Reference))
                TestResult.Fail("doc reference's author[0] does not reference newly created practitioner #1");
            if (!prac2Doc.Id.ToString().Contains(docResource.Author[1].Reference))
                TestResult.Fail("doc reference's author[1] does not reference newly created practitioner #2");

            var binRl = new ResourceIdentity(binDoc.Id);

            if (!docResource.Text.Div.Contains(binRl.OperationPath.ToString()))
                TestResult.Fail("href in narrative was not fixed to point to newly created binary");
        }
        private static JObject createEntry(BundleEntry entry, bool summary)
        {
            JObject result = new JObject();

            if (entry is ResourceEntry)
            {
                ResourceEntry re = (ResourceEntry)entry;
                if (!String.IsNullOrEmpty(re.Title)) result.Add(new JProperty(BundleXmlParser.XATOM_TITLE, re.Title));
                if (SerializationUtil.UriHasValue(entry.Id)) result.Add(new JProperty(BundleXmlParser.XATOM_ID, entry.Id.ToString()));

                if (re.LastUpdated != null) result.Add(new JProperty(BundleXmlParser.XATOM_UPDATED, re.LastUpdated));
                if (re.Published != null) result.Add(new JProperty(BundleXmlParser.XATOM_PUBLISHED, re.Published));

                if (!String.IsNullOrWhiteSpace(re.AuthorName))
                    result.Add(jsonCreateAuthor(re.AuthorName, re.AuthorUri));
            }
            else
            {
                DeletedEntry de = (DeletedEntry)entry;
                if (de.When != null) result.Add(new JProperty(BundleJsonParser.JATOM_DELETED, de.When));
                if (SerializationUtil.UriHasValue(entry.Id)) result.Add(new JProperty(BundleXmlParser.XATOM_ID, entry.Id.ToString()));
            }

            if(entry.Links != null && entry.Links.Count() > 0)
                result.Add(new JProperty(BundleXmlParser.XATOM_LINK, jsonCreateLinkArray(entry.Links)));

            if (entry.Tags != null && entry.Tags.Count() > 0) 
                result.Add(TagListSerializer.CreateTagCategoryPropertyJson(entry.Tags));

            if(entry is ResourceEntry)
            {
                ResourceEntry re = (ResourceEntry)entry;
                if (re.Resource != null)
                    result.Add(new JProperty(BundleXmlParser.XATOM_CONTENT, 
                        getContentsAsJObject(re.Resource, summary)));

                // Note: this is a read-only property, so it is serialized but never parsed
                if (entry.Summary != null)
                    result.Add(new JProperty(BundleXmlParser.XATOM_SUMMARY, entry.Summary));
            }

            return result;
        }
示例#10
0
 public static IEnumerable<Tag> AffixTags(BundleEntry entry, BundleEntry other)
 {
     IEnumerable<Tag> entryTags = entry.Tags ?? Enumerable.Empty<Tag>();
     IEnumerable<Tag> otherTags = other.Tags ?? Enumerable.Empty<Tag>();
     return Affix(entryTags, otherTags);
 }
示例#11
0
        private static void checkSerDeser(BundleEntry be)
        {
            var errs = new ErrorList();

            FhirParser.ParseBundleEntryFromJson(FhirSerializer.SerializeBundleEntryToJson(be), errs);
            Assert.IsTrue(errs.Count == 0);

            FhirParser.ParseBundleEntryFromXml(FhirSerializer.SerializeBundleEntryToXml(be), errs);
            Assert.IsTrue(errs.Count == 0);
        }
示例#12
0
        private static DateTime? GetVersionDate(BundleEntry entry)
        {
            DateTimeOffset? result = (entry is ResourceEntry)
                ? ((ResourceEntry)entry).LastUpdated
                : ((DeletedEntry)entry).When;

            // todo: moet een ontbrekende version date niet in de service gevuld worden?
            //return (result != null) ? result.Value.UtcDateTime : null;
            return (result != null) ? result.Value.UtcDateTime : (DateTime?)null;
        }
示例#13
0
        public void QueueNewEntry(BundleEntry entry)
        {
            if (entry == null) throw new ArgumentNullException("entry");
            if (entry.Id == null) throw new ArgumentNullException("Entry's must have a non-null Id");
            if (!entry.Id.IsAbsoluteUri) throw new ArgumentException("Uri for new resource must be absolute");

           //  Clone entry so we won't be updating our source data
            
            var newEntry = FhirParser.ParseBundleEntryFromXml(FhirSerializer.SerializeBundleEntryToXml(entry));
            queue.Add(newEntry);
        }
示例#14
0
 public static string SerializeBundleEntryToJson(BundleEntry entry, bool summary=false)
 {
     return jsonWriterToString(jw=>BundleJsonSerializer.WriteTo(entry, jw, summary));
 }
示例#15
0
        private void localizeEntryIds(BundleEntry entry)
        {
          
            // Did we already reassign this entry.Id within this batch?
            if (!uriMap.ContainsKey(entry.Id))
            {
                Uri localUri;
                ResourceIdentity identity;

                identity = new ResourceIdentity(entry.Id);

                // If we shared this id space, use the relative path as id
                if (inSharedIdSpace(entry.Id) && (identity.Collection != null && identity.Id != null))
                {
                    localUri = identity.OperationPath; 

                    // If we're about to add an entry with a numerical id > than our current
                    // "new record counter", make sure the next new record gets an id 1 higher
                    // than this entries id.
                    int newIdNum = 0;
                    if (Int32.TryParse(identity.Id, out newIdNum))
                        _store.EnsureNextSequenceNumberHigherThan(newIdNum);
                
                }
                else
                {
                    // Otherwise, give it a new relative, local id
                    var newResourceId = _store.GenerateNewIdSequenceNumber();
                    
                    string collectionName = getCollectionNameFromEntry(entry);
                    localUri = ResourceIdentity.Build(collectionName, newResourceId.ToString());
                }

                uriMap.Add(entry.Id, localUri);
            }

            // Reassign the resultString to our new local resultString
            entry.Id = uriMap[entry.Id];

            // Now, build a new version-specific link (always, no reuse)
            string vid = _store.GenerateNewVersionSequenceNumber().ToString();
            var id = new ResourceIdentity(entry.Id).WithVersion(vid);
            

            // If the entry did carry an version-specific resultString originally,
            // keep it in the map so we can update references to it.
            if (entry.SelfLink != null)
                uriMap.Add(entry.SelfLink, id);

            // Assign a new version-specific link to entry
            entry.SelfLink = id;
        }
示例#16
0
        public static void ValidateVersion(BundleEntry proposed, BundleEntry current)
        {
            if (requiresVersionAwareUpdate(proposed))
            {
                if (proposed.SelfLink == null)
                    throw new SparkException(HttpStatusCode.PreconditionFailed,
                    "This resource requires version-aware updates and no Content-Location was given");

                var _proposed = new ResourceIdentity(proposed.SelfLink);
                var _current = new ResourceIdentity(current.SelfLink);

                if (_proposed.VersionId != _current.VersionId)
                {
                    throw new SparkException(HttpStatusCode.Conflict, "There is an update conflict: update referred to version {0}, but current version is {1}", _proposed, _current);
                }
            }
        }
示例#17
0
 private static void AddMetaData(BsonDocument document, BundleEntry entry)
 {
     document[Field.VERSIONID] = entry.Links.SelfLink.ToString();
     document[Field.ENTRYTYPE] = entry.TypeName();
      document[Field.COLLECTION] = entry.GetResourceTypeName();
     document[Field.VERSIONDATE] = GetVersionDate(entry) ?? DateTime.UtcNow;
 }
示例#18
0
 public static bool HasValidLocalKey(BundleEntry entry)
 {
     string type = entry.GetResourceTypeName();
     Uri id = entry.Id;
     return Key.IsValidLocalKey(id, type);
 }
示例#19
0
 public static byte[] SerializeBundleEntryToJsonBytes(BundleEntry entry, bool summary=false)
 {
     return jsonWriterToBytes(jw => BundleJsonSerializer.WriteTo(entry, jw, summary));
 }
示例#20
0
 public void Externalize(BundleEntry entry)
 {
     ensureAbsoluteUris(entry);
 }
示例#21
0
 public List<Tag> AffixTags(BundleEntry entry, BundleEntry other)
 {
     return AffixTags(entry, other.Tags);
 }
示例#22
0
 private void moveIdToRelated(BundleEntry entry)
 {
     entry.Links.Alternate = entry.Id;
 }
示例#23
0
 private static bool requiresVersionAwareUpdate(BundleEntry entry)
 {
     // todo: question: Should this not be implemented somewhere else? (metadata?) /mh
     // answer: move to Config file.
     string collection = entry.GetResourceTypeName();
     return (collection == "Organization");
 }
示例#24
0
 private static void AddVersionDate(BundleEntry entry, DateTime stamp)
 {
     if (entry is ResourceEntry)
     {
         (entry as ResourceEntry).LastUpdated = stamp;
     }
     if (entry is DeletedEntry)
     {
         (entry as DeletedEntry).When = stamp;
     }
 }
示例#25
0
 public static void SerializeBundleEntry(BundleEntry entry, XmlWriter writer, bool summary=false)
 {
     BundleXmlSerializer.WriteTo(entry, writer,summary);
 }
示例#26
0
 private static BsonDocument BundleEntryToBson(BundleEntry entry)
 {
     string json = FhirSerializer.SerializeBundleEntryToJson(entry);
     BsonDocument document = BsonDocument.Parse(json);
     AddMetaData(document, entry);
     return document;
 }
示例#27
0
 public static string SerializeBundleEntryToXml(BundleEntry entry, bool summary=false)
 {
     return xmlWriterToString(xw => BundleXmlSerializer.WriteTo(entry, xw, summary));
 }
示例#28
0
        private string getCollectionNameFromEntry(BundleEntry entry)
        {
            ResourceIdentity identity;
 
            if (entry.Id.Scheme == Uri.UriSchemeHttp)
            {
                identity = new ResourceIdentity(entry.Id);

                if (identity.Collection != null)
                    return identity.Collection;
            }

            if (entry.SelfLink != null && entry.SelfLink.Scheme == Uri.UriSchemeHttp)
            {
                identity = new ResourceIdentity(entry.SelfLink);

                if (identity.Collection != null)
                    return identity.Collection;
                
            }

            if (entry is ResourceEntry)
                return (entry as ResourceEntry).Resource.GetCollectionName();
                //return ((ResourceEntry)entry).Resource.GetType().Name.ToLower();

            throw new InvalidOperationException("Encountered a entry without an id, self-link or content that indicates the resource's type");
        }
示例#29
0
 public static byte[] SerializeBundleEntryToXmlBytes(BundleEntry entry, bool summary=false)
 {
     return xmlWriterToBytes(xw => BundleXmlSerializer.WriteTo(entry, xw, summary));
 }
示例#30
0
        public void Replace(BundleEntry entry)
        {
            string key = entry.SelfLink.ToString();

            IMongoQuery query = MonQ.Query.EQ(Field.VERSIONID, key);
            BsonDocument current = collection.FindOne(query);
            BsonDocument replacement = BundleEntryToBson(entry);
            TransferMetadata(current, replacement);

            IMongoUpdate update = MonQ.Update.Replace(replacement);
            collection.Update(query, update);
        }