/// <summary> /// Processes the command /// </summary> /// <returns></returns> public bool Run() { if (IsValid) { int? ownerId = (int?)GetOptionValue("ownerId"); Guid?instanceId = (Guid?)GetOptionValue("instanceId"); Guid?dataId = (Guid?)GetOptionValue("dataId"); if ((ownerId.HasValue) && (instanceId != null) && (dataId != null)) { Stream stream = _clientWrapper.GetData((int)ownerId, (Guid)instanceId, (Guid)dataId); if (stream != null) { string fileFolder = $@"{ownerId}\{instanceId}"; CliFileWrapper.SaveToFile(fileFolder, ((Guid)dataId).ToString(), stream); } } else { GetDocumentFromInstances(); } } else { ErrorMessage = "Error in command parameter(s)\n"; _logger.LogInformation(ErrorMessage); } return(true); }
/// <summary> /// Fetch document from storage and save it to file. If the Next link is defined continue to read rest of instances and data /// </summary> /// <param name="instances">The instances for which the data shall be fetched</param> /// <param name="nextLink">The fetch of data elements is paged, the next link shall be used to fetch next page of instances</param> /// <param name="updateInstances"></param> private void FetchAndSaveData(Instance[] instances, Uri nextLink, string updateInstances) { foreach (Instance instance in instances) { int numberOfFiles = 0; foreach (DataElement data in instance.Data) { string url = data.SelfLinks.Platform; Stream responseData = _clientWrapper.GetData(url, data.ContentType); if (responseData != null) { string instanceGuidId = instance.Id.Split('/')[1]; string fileName = $"{data.DataType.ToString()}_{((string.IsNullOrEmpty(data.Filename)) ? data.Id : data.Filename)}"; string fileFolder = $@"{instance.InstanceOwner.PartyId}\{instanceGuidId}"; if (CliFileWrapper.SaveToFile(fileFolder, fileName, responseData)) { _logger.LogInformation($"File:{fileName} saved at {fileFolder}"); } numberOfFiles++; } } if (numberOfFiles == 0) { _logger.LogInformation($"No files received for instance:{instance.Id}"); } } if (nextLink != null) { InstanceResponseMessage responsMessage = _clientWrapper.GetInstanceMetaData(nextLink); _logger.LogInformation($"Fetched {responsMessage.Instances.Length} instances. Count={responsMessage.Count}"); FetchAndSaveData(responsMessage.Instances, responsMessage.Next, updateInstances); } }