Example #1
0
        public TagList TagsFromResource(string collection)
        {
            RequestValidator.ValidateCollectionName(collection);
            IEnumerable <Tag> tags = _store.ListTagsInCollection(collection);

            return(new TagList(tags));
        }
Example #2
0
        public Bundle Search(string collection, IEnumerable <Tuple <string, string> > parameters, int pageSize, string sortby)
        {
            RequestValidator.ValidateCollectionName(collection);
            Query query = FhirParser.ParseQueryFromUriParameters(collection, parameters);
            ICollection <string> includes = query.Includes;

            SearchResults results = index.Search(query);

            if (results.HasErrors)
            {
                throw new SparkException(HttpStatusCode.BadRequest, results.Outcome);
            }

            RestUrl  selfLink = new RestUrl(Endpoint).AddPath(collection).AddPath(results.UsedParameters);
            string   title    = String.Format("Search on resources in collection '{0}'", collection);
            Snapshot snapshot = Snapshot.Create(title, selfLink.Uri, results, sortby, includes);

            store.AddSnapshot(snapshot);

            Bundle bundle = pager.GetPage(snapshot, 0, pageSize);

            /*
             * if (results.HasIssues)
             * {
             *  var outcomeEntry = BundleEntryFactory.CreateFromResource(results.Outcome, new Uri("outcome/1", UriKind.Relative), DateTimeOffset.Now);
             *  outcomeEntry.SelfLink = outcomeEntry.Id;
             *  bundle.Entries.Add(outcomeEntry);
             * }
             */

            exporter.Externalize(bundle);
            return(bundle);
        }
Example #3
0
        /// <summary>
        /// Delete a resource.
        /// </summary>
        /// <param name="collection">The resource type, in lowercase</param>
        /// <param name="id">The id part of a Resource id</param>
        /// <remarks>
        /// Upon successful deletion the server should return
        ///   * 204 (No Content).
        ///   * If the resource does not exist on the server, the server must return 404 (Not found).
        ///   * Performing this operation on a resource that is already deleted has no effect, and should return 204 (No Content).
        /// </remarks>
        public void Delete(string collection, string id)
        {
            RequestValidator.ValidateCollectionName(collection);
            RequestValidator.ValidateId(id);

            // See if we already have a version on store

            BundleEntry current = findEntry(collection, id);

            //var identity = resourceidentity.build(collection, id);
            //bundleentry current = _store.findentrybyid(identity);

            if (current == null)
            {
                throw new SparkException(HttpStatusCode.NotFound,
                                         "No {0} resource with id {1} was found, so it cannot be deleted.", collection, id);
            }
            else if (!(current is DeletedEntry))
            {
                // Add a new deleted-entry to mark this entry as deleted
                _importer.QueueNewDeletedEntry(collection, id);
                BundleEntry deletedEntry = _importer.ImportQueued().First();

                _store.AddEntry(deletedEntry);
                _index.Process(deletedEntry);
            }
        }
Example #4
0
        public ResourceEntry <OperationOutcome> Validate(string collection, ResourceEntry entry, string id = null)
        {
            RequestValidator.ValidateCollectionName(collection);
            if (id != null)
            {
                RequestValidator.ValidateId(id);
            }

            if (entry == null)
            {
                throw new SparkException("Validate needs a Resource in the body payload");
            }

            entry.Title       = "Validation test entity";
            entry.LastUpdated = DateTime.Now;
            entry.Id          = id != null?ResourceIdentity.Build(Endpoint, collection, id) : null;

            RequestValidator.ValidateResourceBody(entry, collection);
            var result = RequestValidator.ValidateEntry(entry);

            if (result != null)
            {
                return(new ResourceEntry <OperationOutcome>(Key.NewUrn(), DateTimeOffset.Now, result));
            }
            else
            {
                return(null);
            }
        }
Example #5
0
        public ResourceEntry Update(string collection, string id, ResourceEntry entry, Uri updatedVersionUri = null)
        {
            RequestValidator.ValidateCollectionName(collection);
            RequestValidator.ValidateId(id);
            RequestValidator.ValidateResourceBody(entry, collection);

            BundleEntry current = findEntry(collection, id);

            if (current == null)
            {
                return(null);
            }

            // todo: this fails. Both selflink and updatedVersionUri can be empty, but this function requires both.
            // Check if update done against correct version, if applicable
            // RequestValidator.ValidateCorrectUpdate(entry.Links.SelfLink, updatedVersionUri); // can throw exception

            // Entry already exists, add a new ResourceEntry with the same id
            var identity = ResourceIdentity.Build(Endpoint, collection, id);

            ResourceEntry newEntry = _importer.Import(identity, entry);

            //_importer.QueueNewResourceEntry(identity, entry.Resource);
            //var newEntry = (ResourceEntry)_importer.ImportQueued().First();

            // Merge tags passed to the update with already existing tags.
            newEntry.Tags = _importer.AffixTags(current, newEntry);

            var newVersion = _store.AddEntry(newEntry);

            _index.Process(newVersion);

            _exporter.EnsureAbsoluteUris(newVersion);
            return((ResourceEntry)newVersion);
        }
Example #6
0
        public TagList TagsFromResource(string resourcetype)
        {
            RequestValidator.ValidateCollectionName(resourcetype);
            IEnumerable <Tag> tags = tagstore.Tags(resourcetype);

            return(new TagList(tags));
        }
Example #7
0
        public Uri BuildLocation(string collection, string id, string vid = null)
        {
            RequestValidator.ValidateCollectionName(collection);
            RequestValidator.ValidateId(id);
            if (vid != null)
            {
                RequestValidator.ValidateVersionId(vid);
            }
            Uri uri = ResourceIdentity.Build(Endpoint, collection, id, vid);

            return(uri);
        }
Example #8
0
        public void Validate(string collection, ResourceEntry entry)
        {
            RequestValidator.ValidateCollectionName(collection);
            if (entry == null)
            {
                throw new SparkException("Validate needs a Resource in the body payload");
            }

            entry.Title       = "Validation test entity";
            entry.LastUpdated = DateTime.Now;
            entry.Id          = ResourceIdentity.Build(Endpoint, collection, getNewId());
            RequestValidator.ValidateEntry(entry);
        }
Example #9
0
        public void AffixTags(string collection, string id, IEnumerable <Tag> tags)
        {
            RequestValidator.ValidateCollectionName(collection);
            RequestValidator.ValidateId(id);
            if (tags == null)
            {
                throw new SparkException("No tags specified on the request");
            }

            ResourceEntry existing = this.internalRead(collection, id);

            existing.Tags = _importer.AffixTags(existing, tags);
            _store.ReplaceEntry(existing);
        }
Example #10
0
        public TagList TagsFromInstance(string collection, string id)
        {
            RequestValidator.ValidateCollectionName(collection);
            RequestValidator.ValidateId(id);

            Uri         uri   = ResourceIdentity.Build(collection, id);
            BundleEntry entry = _store.FindEntryById(uri);

            if (entry == null)
            {
                throwNotFound("Cannot retrieve tags", collection, id);
            }

            return(new TagList(entry.Tags));
        }
Example #11
0
        public Bundle History(string collection, DateTimeOffset?since)
        {
            RequestValidator.ValidateCollectionName(collection);
            if (since == null)
            {
                since = DateTimeOffset.MinValue;
            }
            string  title = String.Format("Full server-wide history for updates since {0}", since);
            RestUrl self  = new RestUrl(this.Endpoint).AddPath(collection, RestOperation.HISTORY);

            IEnumerable <BundleEntry> entries = _store.ListVersionsInCollection(collection, since, Const.MAX_HISTORY_RESULT_SIZE);
            Bundle bundle = BundleEntryFactory.CreateBundleWithEntries(title, self.Uri, Const.AUTHOR, Settings.AuthorUri, entries);

            return(exportPagedBundle(bundle));
        }
Example #12
0
        public Bundle History(string collection, DateTimeOffset?since, string sortby)
        {
            RequestValidator.ValidateCollectionName(collection);
            string  title = String.Format("Full server-wide history for updates since {0}", since);
            RestUrl self  = new RestUrl(this.Endpoint).AddPath(collection, RestOperation.HISTORY);

            IEnumerable <Uri> keys     = store.History(collection, since);
            Snapshot          snapshot = Snapshot.Create(title, self.Uri, keys, sortby);

            store.AddSnapshot(snapshot);

            Bundle bundle = pager.GetPage(snapshot);

            exporter.Externalize(bundle);
            return(bundle);
        }
Example #13
0
        public Bundle Search(string collection, IEnumerable <Tuple <string, string> > parameters, int pageSize)
        {
            RequestValidator.ValidateCollectionName(collection);

            string title = String.Format("Search on resources in collection '{0}'", collection);

            RestUrl selfLink = new RestUrl(Endpoint).AddPath(collection);

            SearchResults results  = _index.Search(collection, parameters);
            Snapshot      snapshot = Snapshot.Create(title, selfLink.Uri, results, results.MatchCount);

            Bundle bundle = _pager.FirstPage(snapshot, pageSize);

            _exporter.EnsureAbsoluteUris(bundle);
            return(bundle);
        }
Example #14
0
        public void RemoveTags(string collection, string id, IEnumerable <Tag> tags)
        {
            RequestValidator.ValidateCollectionName(collection);
            RequestValidator.ValidateId(id);
            if (tags == null)
            {
                throw new SparkException("No tags specified on the request");
            }

            ResourceEntry existing = this.internalRead(collection, id);

            if (existing.Tags != null)
            {
                existing.Tags = existing.Tags.Exclude(tags).ToList();
            }

            _store.ReplaceEntry(existing);
        }
Example #15
0
        /// <summary>
        /// Create a new resource with a server assigned id.
        /// </summary>
        /// <param name="collection">The resource type, in lowercase</param>
        /// <param name="resource">The data for the Resource to be created</param>
        /// <remarks>
        /// May return:
        ///     201 Created - on successful creation
        /// </remarks>
        public ResourceEntry Create(string collection, ResourceEntry entry, string newId = null)
        {
            RequestValidator.ValidateCollectionName(collection);
            RequestValidator.ValidateIdPattern(newId);
            RequestValidator.ValidateResourceBody(entry, collection);

            if (newId == null)
            {
                newId = getNewId();
            }

            ResourceIdentity identity = ResourceIdentity.Build(Endpoint, collection, newId);
            var newEntry = _importer.Import(identity, entry);

            ResourceEntry result = internalCreate(newEntry);

            _exporter.EnsureAbsoluteUris(result);

            return(result);
        }
Example #16
0
        private ResourceEntry internalRead(string collection, string id)
        {
            RequestValidator.ValidateCollectionName(collection);
            RequestValidator.ValidateId(id);

            Uri         uri   = ResourceIdentity.Build(collection, id);
            BundleEntry entry = _store.FindEntryById(uri);

            if (entry == null)
            {
                throwNotFound("Cannot read resource", collection, id);
            }
            else if (entry is DeletedEntry)
            {
                var deletedentry = (entry as DeletedEntry);
                var message      = String.Format("A {0} resource with id {1} existed, but was deleted on {2} (version {3}).",
                                                 collection, id, deletedentry.When, new ResourceIdentity(deletedentry.Links.SelfLink).VersionId);

                throw new SparkException(HttpStatusCode.Gone, message);
            }
            return((ResourceEntry)entry);
        }
Example #17
0
        public TagList TagsFromHistory(string collection, string id, string vid)
        {
            RequestValidator.ValidateCollectionName(collection);
            RequestValidator.ValidateId(id);
            RequestValidator.ValidateVersionId(vid);

            var         uri   = ResourceIdentity.Build(collection, id, vid);
            BundleEntry entry = _store.FindVersionByVersionId(uri);

            if (entry == null)
            {
                throwNotFound("Cannot retrieve tags", collection, id, vid);
            }
            else if (entry is DeletedEntry)
            {
                throw new SparkException(HttpStatusCode.Gone,
                                         "A {0} resource with version {1} and id {2} exists, but is a deletion (deleted on {3}).",
                                         collection, vid, id, (entry as DeletedEntry).When);
            }

            return(new TagList(entry.Tags));
        }
Example #18
0
        private ResourceEntry internalVRead(string collection, string id, string vid)
        {
            RequestValidator.ValidateCollectionName(collection);
            RequestValidator.ValidateId(id);
            RequestValidator.ValidateVersionId(vid);

            var versionUri = ResourceIdentity.Build(collection, id, vid);

            BundleEntry entry = _store.FindVersionByVersionId(versionUri);

            if (entry == null)
            {
                throwNotFound("Cannot read version of resource", collection, id, vid);
            }
            else if (entry is DeletedEntry)
            {
                throw new SparkException(HttpStatusCode.Gone,
                                         "A {0} resource with version {1} and id {2} exists, but is a deletion (deleted on {3}).",
                                         collection, vid, id, (entry as DeletedEntry).When);
            }

            return((ResourceEntry)entry);
        }
Example #19
0
        public Bundle History(string collection, string id, DateTimeOffset?since)
        {
            RequestValidator.ValidateCollectionName(collection);
            RequestValidator.ValidateId(id);

            if (since == null)
            {
                since = DateTimeOffset.MinValue;
            }
            string  title = String.Format("History for updates on '{0}' resource '{1}' since {2}", collection, id, since);
            RestUrl self  = new RestUrl(this.Endpoint).AddPath(collection, id, RestOperation.HISTORY);

            if (!entryExists(collection, id))
            {
                throw new SparkException(HttpStatusCode.NotFound, "There is no history because there is no {0} resource with id {1}.", collection, id);
            }

            var identity = ResourceIdentity.Build(collection, id).OperationPath;
            IEnumerable <BundleEntry> entries = _store.ListVersionsById(identity, since, Const.MAX_HISTORY_RESULT_SIZE);
            Bundle bundle = BundleEntryFactory.CreateBundleWithEntries(title, self.Uri, Const.AUTHOR, Settings.AuthorUri, entries);

            return(exportPagedBundle(bundle));
        }