/// <summary> /// Create the specified resource. /// </summary> /// <param name="target">The target.</param> /// <param name="mode">The mode.</param> /// <returns>FhirOperationResult.</returns> /// <exception cref="System.ArgumentNullException">target</exception> /// <exception cref="System.IO.InvalidDataException"></exception> /// <exception cref="System.Data.SyntaxErrorException"></exception> public virtual FhirOperationResult Create(DomainResourceBase target, TransactionMode mode) { this.traceSource.TraceInformation("Creating resource {0} ({1})", this.ResourceName, target); if (target == null) { throw new ArgumentNullException(nameof(target)); } else if (!(target is TFhirResource)) { throw new InvalidDataException(); } // We want to map from TFhirResource to TModel var modelInstance = this.MapToModel(target as TFhirResource, WebOperationContext.Current); if (modelInstance == null) { throw new SyntaxErrorException(ApplicationContext.Current.GetLocaleString("MSGE001")); } List <IResultDetail> issues = new List <IResultDetail>(); var result = this.Create(modelInstance, issues, mode); // Return fhir operation result return(new FhirOperationResult() { Results = new List <DomainResourceBase>() { this.MapToFhir(result, WebOperationContext.Current) }, Details = issues, Outcome = issues.Exists(o => o.Type == MARC.Everest.Connectors.ResultDetailType.Error) ? ResultCode.Error : ResultCode.Accepted }); }
/// <summary> /// Updates the specified resource. /// </summary> /// <param name="id">The identifier.</param> /// <param name="target">The target.</param> /// <param name="mode">The mode.</param> /// <returns>Returns the FHIR operation result containing the updated resource.</returns> /// <exception cref="System.ArgumentNullException">target</exception> /// <exception cref="System.IO.InvalidDataException"></exception> /// <exception cref="System.Data.SyntaxErrorException"></exception> /// <exception cref="System.ArgumentException"></exception> /// <exception cref="System.Reflection.AmbiguousMatchException"></exception> /// <exception cref="System.Collections.Generic.KeyNotFoundException"></exception> public FhirOperationResult Update(string id, DomainResourceBase target, TransactionMode mode) { this.traceSource.TraceInformation("Updating resource {0}/{1} ({2})", this.ResourceName, id, target); if (target == null) { throw new ArgumentNullException(nameof(target)); } else if (!(target is TFhirResource)) { throw new InvalidDataException(); } // We want to map from TFhirResource to TModel var modelInstance = this.MapToModel(target as TFhirResource, WebOperationContext.Current); if (modelInstance == null) { throw new SyntaxErrorException(ApplicationContext.Current.GetLocaleString("MSGE001")); } // Guid identifier var guidId = Guid.Empty; if (!Guid.TryParse(id, out guidId)) { throw new ArgumentException(ApplicationContext.Current.GetLocaleString("MSGE002")); } // Model instance key does not equal path if (modelInstance.Key != Guid.Empty && modelInstance.Key != guidId) { throw new AmbiguousMatchException(ApplicationContext.Current.GetLocaleString("MSGE003")); } else if (modelInstance.Key == Guid.Empty) { modelInstance.Key = guidId; } else { throw new KeyNotFoundException(); } List <IResultDetail> issues = new List <IResultDetail>(); var result = this.Update(modelInstance, issues, mode); // Return fhir operation result return(new FhirOperationResult { Results = new List <DomainResourceBase> { this.MapToFhir(result, WebOperationContext.Current) }, Details = issues, Outcome = issues.Exists(o => o.Type == MARC.Everest.Connectors.ResultDetailType.Error) ? ResultCode.Error : ResultCode.Accepted }); }
/// <summary> /// Validate a resource (really an update with debugging / non comit) /// </summary> public OperationOutcome ValidateResource(string resourceType, string id, DomainResourceBase target) { this.ThrowIfNotReady(); FhirOperationResult result = null; try { // Setup outgoing content // Create or update? var handler = FhirResourceHandlerUtil.GetResourceHandler(resourceType); if (handler == null) { throw new FileNotFoundException(); // endpoint not found! } result = handler.Update(id, target, TransactionMode.Rollback); if (result == null || result.Results.Count == 0) // Create { result = handler.Create(target, TransactionMode.Rollback); WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Created; } if (result == null || result.Outcome == ResultCode.Rejected) { throw new InvalidDataException("Resource structure is not valid"); } else if (result.Outcome == ResultCode.AcceptedNonConformant) { throw new ConstraintException("Resource not conformant"); } else if (result.Outcome == ResultCode.TypeNotAvailable) { throw new FileNotFoundException(String.Format("Resource {0} not found", WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri)); } else if (result.Outcome != ResultCode.Accepted) { throw new DataException("Validate failed"); } // Return constraint return(MessageUtil.CreateOutcomeResource(result)); } catch (Exception e) { return(this.ErrorHelper(e, result, false) as OperationOutcome); } }
/// <summary> /// Create or update /// </summary> public DomainResourceBase CreateUpdateResource(string resourceType, string id, string mimeType, DomainResourceBase target) { return(this.UpdateResource(resourceType, id, mimeType, target)); }
/// <summary> /// Create a resource /// </summary> public DomainResourceBase CreateResource(string resourceType, string mimeType, DomainResourceBase target) { this.ThrowIfNotReady(); FhirOperationResult result = null; AuditData audit = null; IAuditorService auditService = ApplicationContext.Current.GetService(typeof(IAuditorService)) as IAuditorService; try { // Setup outgoing content // Create or update? var handler = FhirResourceHandlerUtil.GetResourceHandler(resourceType); if (handler == null) { throw new FileNotFoundException(); // endpoint not found! } result = handler.Create(target, TransactionMode.Commit); WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Created; if (result == null || result.Outcome == ResultCode.Rejected) { throw new InvalidDataException("Resource structure is not valid"); } else if (result.Outcome == ResultCode.AcceptedNonConformant) { throw new ConstraintException("Resource not conformant"); } else if (result.Outcome == ResultCode.TypeNotAvailable) { throw new FileNotFoundException(String.Format("Resource {0} not found", WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri)); } else if (result.Outcome != ResultCode.Accepted) { throw new DataException("Create failed"); } audit = AuditUtil.CreateAuditData(result.Results); String baseUri = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.BaseUri.AbsoluteUri; WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Location", String.Format("{0}{1}/{2}/_history/{3}", baseUri, resourceType, result.Results[0].Id, result.Results[0].VersionId)); WebOperationContext.Current.OutgoingResponse.LastModified = result.Results[0].Timestamp; WebOperationContext.Current.OutgoingResponse.ETag = result.Results[0].VersionId; return(result.Results[0]); } catch (Exception e) { audit = AuditUtil.CreateAuditData(null); audit.Outcome = OutcomeIndicator.EpicFail; return(this.ErrorHelper(e, result, false) as DomainResourceBase); } finally { if (auditService != null) { auditService.SendAudit(audit); } } }
/// <summary> /// Create the specified definition /// </summary> public FhirOperationResult Create(DomainResourceBase target, Core.Services.TransactionMode mode) { throw new NotSupportedException(); }
/// <summary> /// Creates a new instance of the resource bunlde /// </summary> /// <param name="r"></param> public BundleResrouce(DomainResourceBase r) { this.Resource = r; }