public async Task <List <DataObjectViewModel> > Load( IEnumerable <DataReference> dataReferences, Action <DataObjectViewModel> deletionCallback) { var dataObjects = new List <DataObjectViewModel>(); foreach (var dataReference in dataReferences) { try { var json = await dataApiClient.GetAsync( dataReference.DataType, dataReference.Id); var dataObject = new DataObjectViewModel( new JObjectViewModel(JObject.Parse(json), jsonViewModelFactory), dataReference.DataType, dataReference.Id, dataApiClient, deletionCallback); dataObjects.Add(dataObject); } catch (ApiException apiException) { if (apiException.StatusCode == HttpStatusCode.NotFound) { continue; // Ignore } throw; } } return(dataObjects); }
public static async Task CreateDataProject( IDataApiClient dataApiClient, string dataProjectId, string projectSourceSystem, string protocolName, Dictionary <string, string> protocolParameterResponses) { var protocol = await dataApiClient.GetAsync <DataCollectionProtocol>(protocolName); var mandatoryResponses = protocol.Parameters .Where(x => x.IsMandatory); var missingResponses = mandatoryResponses .Where(x => !protocolParameterResponses.ContainsKey(x.Name)) .ToList(); if (missingResponses.Any()) { var aggregatedMissingParameters = string.Join(", ", missingResponses.Select(x => x.Name)); throw new ArgumentException($"You are missing parameter responses for the following mandatory parameters: {aggregatedMissingParameters}"); } var globalizedProjectId = dataProjectId; try { var detectedSourceSystem = IdGenerator.GetSourceSystem(dataProjectId); if (detectedSourceSystem != projectSourceSystem) { throw new ArgumentException($"The provided data project ID has a prefix that is associated with " + $"source system '{detectedSourceSystem}', but '{projectSourceSystem}' was specified. " + $"This either means that the '{projectSourceSystem}' system uses an ID-pattern that " + $"clashes with the ID-convention (PREFIX.<LocalID>)" + $"or you specified the wrong ID/source system."); } } catch (KeyNotFoundException) { if (projectSourceSystem != "SelfAssigned") { globalizedProjectId = IdGenerator.GlobalizeLocalId(projectSourceSystem, dataProjectId); } } var dataProject = new DataProject( globalizedProjectId, projectSourceSystem, protocol, protocolParameterResponses); await dataApiClient.InsertAsync(dataProject, dataProjectId); }
public async Task <JObject> GetFromIdAsync(string id) { EnsureLoggedIn(); if (cachedItems.TryGetValue(id, out var item)) { return(item); } try { item = JObject.Parse(await dataApiClient.GetAsync(CollectionName, id)); if (item != null) { cachedItems.AddOrUpdate(GetId(item), item, (key, existingItem) => item); } return(item); } catch (ApiException apiException) { if (apiException.StatusCode == HttpStatusCode.NotFound) { return(default);
private async Task <string> LoadObject(SubscriptionNotification subscriptionNotification) { return(await dataApiClient.GetAsync( subscriptionNotification.DataType, subscriptionNotification.DataObjectId)); }
private async Task <ProcessingStatus> RunPostponedObject(PostponedProcessingObject postponedObject) { var processor = processors[postponedObject.ProcessorName]; var inputObjectJson = await dataApiClient.GetAsync(postponedObject.DataType, postponedObject.DataId); //await processorRunner.LogExecutionStarting(processor); bool isSuccess; bool isWorkDone; string summary; ProcessingStatus status; var stopWatch = Stopwatch.StartNew(); try { var processorResult = await processor.Process(postponedObject.ModificationType, postponedObject.DataType, postponedObject.DataId, inputObjectJson); stopWatch.Stop(); status = processorResult.Status; if (status == ProcessingStatus.NotInterested) { return(status); } else if (status == ProcessingStatus.Postponed) { postponedObject.LastAttempt = DateTime.UtcNow; postponedObject.RemainingAttempts--; await dataApiClient.ReplaceAsync(postponedObject, postponedObject.Id); // Indirectly updated postponedObjects return(status); } else if (status == ProcessingStatus.Error) { var errorProcessorResult = (ErrorProcessorResult)processorResult; isSuccess = false; isWorkDone = false; summary = errorProcessorResult.ErrorMessage; } else if (status == ProcessingStatus.Success) { var successProcessorResult = (SuccessProcessorResult)processorResult; foreach (var obj in successProcessorResult.Objects) { await processorRunner.StoreResult(obj); } isSuccess = true; isWorkDone = successProcessorResult.IsWorkDone; summary = successProcessorResult.Summary; } else { throw new InvalidEnumArgumentException($"Invalid enum value '{status}' for {nameof(ProcessingStatus)}"); } } catch (Exception e) { isSuccess = false; isWorkDone = false; status = ProcessingStatus.Error; summary = e.InnermostException().Message; if (string.IsNullOrWhiteSpace(summary)) { summary = e.Message; } } await processorRunner.LogExecutionFinished( processor, (processor as ISingleOutputProcessor)?.OutputTypeName, postponedObject.DataId, summary, isSuccess, isWorkDone, stopWatch); return(status); }
public static async Task Download(DataReference dataReference, IDataApiClient dataApiClient) { try { var json = await dataApiClient.GetAsync(dataReference.DataType, dataReference.Id); if (dataReference.DataType == nameof(DataBlob)) { var dataBlob = JsonConvert.DeserializeObject <DataBlob>(json); var extension = dataBlob.Filename != null ? Path.GetExtension(dataBlob.Filename) : ".bin"; var fileDialog = new VistaSaveFileDialog { AddExtension = true, DefaultExt = extension, OverwritePrompt = true, CheckPathExists = true, Filter = @"All files (*.*)|*.*", FileName = dataBlob.Filename }; if (fileDialog.ShowDialog() != true) { return; } var outputFilePath = fileDialog.FileName; File.WriteAllBytes(outputFilePath, dataBlob.Data); } else if (dataReference.DataType == nameof(Image)) { var image = JsonConvert.DeserializeObject <Image>(json); var extension = image.Extension; var fileDialog = new VistaSaveFileDialog { AddExtension = true, DefaultExt = extension, OverwritePrompt = true, CheckPathExists = true, Filter = @"All files (*.*)|*.*", FileName = image.Filename ?? $"{image.Id}{extension}" }; if (fileDialog.ShowDialog() != true) { return; } var outputFilePath = fileDialog.FileName; File.WriteAllBytes(outputFilePath, image.Data); } else { var fileDialog = new VistaSaveFileDialog { AddExtension = true, DefaultExt = ".json", OverwritePrompt = true, CheckPathExists = true, Filter = @"JSON (*.json)|*.json|All files (*.*)|*.*", FileName = $"{dataReference.DataType}_{dataReference.Id}.json" }; if (fileDialog.ShowDialog() != true) { return; } var outputFilePath = fileDialog.FileName; File.WriteAllText(outputFilePath, json); } } catch (Exception e) { var errorText = e.InnermostException().Message; if (string.IsNullOrEmpty(errorText) && e is ApiException apiException) { errorText = apiException.StatusCode.ToString(); } StaticMessageBoxSpawner.Show($"Could not download data: {errorText}"); } }