public static ParsedResource CreateResource(ParsedResource resource) { var patientRepository = new SqlLitePatientRepository(); var referencedResource = patientRepository.GetResource(resource.PatientId); if ((referencedResource == null && resource.TypeName != "Patient") || (resource.TypeName != "Patient" && referencedResource != null && !referencedResource.IsIotaResource)) { throw new MissingReferenceException(); } if (resource.Resource.GetType().GetProperty("Subject") != null) { resource.Resource.GetType().GetProperty("Subject")?.SetValue( resource.Resource, new ResourceReference { Reference = $"did:iota:{referencedResource.Resource.Id}" }); } if (resource.Resource.GetType().GetProperty("Patient") != null) { resource.Resource.GetType().GetProperty("Patient")?.SetValue( resource.Resource, new ResourceReference { Reference = $"did:iota:{referencedResource.Resource.Id}" }); } var client = new RestClient("http://pactfhir.azurewebsites.net/"); var request = new RestRequest($"/api/fhir/create/{resource.TypeName}", Method.POST); request.AddHeader("Content-Type", "application/fhir+json"); request.AddHeader("Prefer", "representation"); request.AddParameter("application/fhir+json", resource.Json, ParameterType.RequestBody); var response = client.Execute(request); if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created) { var parsedResource = new ParsedResource { Id = resource.Id, PatientId = resource.PatientId, Resource = new FhirJsonParser().Parse <Resource>(response.Content) }; patientRepository.UpdateResource(parsedResource); return(parsedResource); } throw new ResourceException(new FhirJsonParser().Parse <OperationOutcome>(response.Content)); }
/// <inheritdoc /> public void UpdateResource(ParsedResource resource) { using (var connection = new SQLiteConnection($"Data Source={DatabaseFilename};Version=3;")) { connection.Open(); using (var command = new SQLiteCommand($"UPDATE Resource SET Payload=@payload WHERE Id='{resource.Id}'", connection)) { command.Parameters.AddWithValue("payload", resource.Json); command.ExecuteNonQuery(); } } }
private TreeViewItem CreateTreeViewItem(ParsedResource resource) { var treeViewItem = new TreeViewItem { Header = new Label { Content = resource.TypeName, Foreground = resource.IsIotaResource ? Brushes.DarkGreen : Brushes.Black, FontWeight = FontWeights.Bold } }; if (resource.CanBeUploaded) { var menuItem = new MenuItem { Header = "Upload to Tangle" }; treeViewItem.ContextMenu = new ContextMenu { ItemsSource = new List <MenuItem> { menuItem } }; menuItem.Click += (sender, args) => { if (sender is MenuItem) { this.UploadResource(treeViewItem, resource); } }; } else { var menuItem = new MenuItem { Header = "Upload of Resource Type currently not supported", IsEnabled = false }; treeViewItem.ContextMenu = new ContextMenu { ItemsSource = new List <MenuItem> { menuItem } }; } var jsonViewItem = new TreeViewItem { Header = "Json View" }; jsonViewItem.Items.Add(new TextBox { Text = resource.FormattedJson, IsReadOnly = true }); treeViewItem.Items.Add(jsonViewItem); if (resource.IsIotaResource) { var tangleDetails = new TreeViewItem { Header = "Tangle information" }; var resourceLink = $"http://pactfhir.azurewebsites.net/api/fhir/{resource.TypeName}/{resource.Resource.Id}"; tangleDetails.Items.Add( new TextBox { Text = resourceLink, IsReadOnly = true }); var clipboardButton = new Button { Content = new TextBlock { Text = "Copy to Clipboard" } }; clipboardButton.Click += (sender, args) => { Clipboard.SetText(resourceLink); }; tangleDetails.Items.Add(clipboardButton); treeViewItem.Items.Add(tangleDetails); } treeViewItem.Items.Add(resource); return(treeViewItem); }
private void UploadResource(TreeViewItem treeViewItem, ParsedResource resource) { try { var spinner = new LoadingSpinner(); spinner.SetText("Creating resource on Tangle"); ApplicationManager.SetContent(spinner); ApplicationManager.MainWindow.MainMenu.IsEnabled = false; spinner.Start(); Task.Factory.StartNew( () => { try { var updatedResource = FhirInteractor.CreateResource(resource); this.Dispatcher.BeginInvoke( new Action( () => { var newItem = this.CreateTreeViewItem(updatedResource); foreach (ParsedPatient patient in this.Patients.ItemsSource) { if (patient.Resources.First().PatientId != updatedResource.PatientId) { continue; } var resourceToUpdate = patient.Resources.First(r => r.Id == updatedResource.Id); if (resourceToUpdate != null) { resourceToUpdate.Resource = updatedResource.Resource; } } var selectedIndex = this.PatientDetails.Items.IndexOf(treeViewItem); this.PatientDetails.Items.RemoveAt(selectedIndex); this.PatientDetails.Items.Insert(selectedIndex, newItem); this.PatientDetails.Items.Refresh(); this.PatientDetails.UpdateLayout(); spinner.Stop(); })); } catch (MissingReferenceException) { this.Dispatcher.BeginInvoke( new Action( () => { MessageBox.Show("Referenced resource not uploaded yet. Upload Patient record first.", "Creation Failed"); })); } catch (ResourceException exception) { this.Dispatcher.BeginInvoke( new Action( () => { MessageBox.Show(exception.Outcome.ToFormattedJson(), "Creation Failed"); })); } catch (Exception exception) { this.Dispatcher.BeginInvoke( new Action( () => { MessageBox.Show(exception.StackTrace, exception.Message); })); } finally { this.Dispatcher.BeginInvoke( new Action( () => { ApplicationManager.MainWindow.MainMenu.IsEnabled = true; ApplicationManager.SetContent(this); })); } }); } catch { // ignored } }