protected async Task <BatchOperationResultDetail <T> > FetchDetail(Func <string[], Task <IEnumerable <T> > > fetchEntities, CancellationToken cancellationToken = default(CancellationToken))
        {
            BusinessProcessDetailsDataBatchResponse batchResult;

            if (!string.IsNullOrEmpty(BusinessProcessId))
            {
                var details = await _businessProcessClient.GetDetailsAsync(BusinessProcessId, cancellationToken).ConfigureAwait(false);

                batchResult = details.Details as BusinessProcessDetailsDataBatchResponse;
            }
            else
            {
                batchResult = new BusinessProcessDetailsDataBatchResponse()
                {
                    Response = new BatchResponse
                    {
                        Rows = new List <BatchResponseRow>()
                    }
                };
            }

            if (batchResult == null)
            {
                throw new InvalidOperationException("BusinessProcess did not return a BatchResponse");
            }

            return(new BatchOperationResultDetail <T>(batchResult, fetchEntities));
        }
Esempio n. 2
0
        /// <summary>Creates multiple <see cref="ListItem"/>s.</summary>
        /// <param name="createManyRequest">The create many request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The created <see cref="ListItem"/>s.</returns>
        /// <exception cref="ApiException">A server side error occurred.</exception>
        /// <exception cref="PictureparkException">The business process has not been completed.</exception>
        public async Task <IEnumerable <ListItem> > CreateManyAsync(ListItemCreateManyRequest createManyRequest, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!createManyRequest.Requests.Any())
            {
                return(new List <ListItem>());
            }

            var businessProcess = await CreateManyCoreAsync(createManyRequest, cancellationToken).ConfigureAwait(false);

            var waitResult = await _businessProcessClient.WaitForCompletionAsync(businessProcess.Id, null, cancellationToken).ConfigureAwait(false);

            if (waitResult.HasLifeCycleHit)
            {
                var details = await _businessProcessClient.GetDetailsAsync(businessProcess.Id, cancellationToken).ConfigureAwait(false);

                if (details.LifeCycle == BusinessProcessLifeCycle.Failed)
                {
                    // TODO: ListItemClient.CreateManyAsync: Should we check for Succeeded here?
                    throw new Exception("The business process failed to execute.");
                }

                var bulkResult = (BusinessProcessDetailsDataBulkResponse)details.Details;
                if (bulkResult.Response.Rows.Any(i => i.Succeeded == false))
                {
                    // TODO: ListItemClient.CreateManyAsync: Use better exception classes in this method.
                    throw new Exception("Could not save all objects.");
                }

                // Fetch created objects
                var searchRequest = new ListItemSearchRequest
                {
                    Start  = 0,
                    Limit  = 1000,
                    Filter = new TermsFilter
                    {
                        Field = "id",
                        Terms = bulkResult.Response.Rows.Select(i => i.Id).ToList()
                    }
                };

                var searchResult = await SearchAsync(searchRequest, cancellationToken).ConfigureAwait(false);

                return(searchResult.Results);
            }
            else
            {
                throw new Exception("The business process has not been completed.");
            }
        }