Exemplo n.º 1
0
 public static byte[] GetDocumentChunk(Guid documentID, int offset, int size, NetWorkSetting ns)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             var buffer = Task.Run(() => web.GetDocumentChunk(documentID, offset, size)).Result;
             if (buffer == null)
             {
                 return(Array.Empty <byte>());
             }
             return(buffer.ToArray());
         }
     }
     catch (AggregateException aex)
     {
         var sb = new System.Text.StringBuilder();
         sb.AppendLine($"There were one or more errors downloading document chunk. (DocumentID:{ documentID }, offset:{ offset }, size:{ size }).");
         foreach (Exception x in aex.InnerExceptions)
         {
             sb.AppendLine(ExceptionHelpers.UnwindException(x, true));
         }
         _log.Error(sb.ToString(), aex);
         throw;
     }
     catch (Exception ex)
     {
         _log.Error(string.Format("Unable to get Document Chunk for Document: {0}.", documentID), ex);
         throw new GetDocumentChunkFailed(ex);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Returns an array of model configurations identified by datamart id, model id and model processor id.
        /// </summary>
        /// <param name="ns">NetWorkSettings</param>
        /// <returns>HubConfiguration[]</returns>
        public static HubConfiguration[] GetDataMartModelConfigurations(NetWorkSetting ns)
        {
            try
            {
                using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                {
                    var datamarts = AsyncHelpers.RunSync <IQueryable <Lpp.Dns.DTO.DataMartClient.DataMart> >(() => web.GetDataMarts());
                    var models    = datamarts.Where(dm => dm.Models != null)
                                    .SelectMany(dm => dm.Models.Select(m => new HubConfiguration
                    {
                        DataMartId       = dm.ID,
                        UnattendedMode   = (Lpp.Dns.DataMart.Lib.Classes.UnattendedMode)(int) dm.UnattendedMode,
                        ModelId          = m.ID,
                        ModelProcessorId = m.ProcessorID,
                        Properties       = m.Properties
                    }));

                    return(models.ToArray());
                }
            }
            catch (Exception ex)
            {
                throw new GetModelsFailed(ex);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns an array of model configurations identified by datamart id, model id and model processor id.
        /// </summary>
        /// <param name="ns">NetWorkSettings</param>
        /// <returns>HubConfiguration[]</returns>
        public static HubConfiguration[] GetDataMartModelConfigurations(NetWorkSetting ns)
        {
            try
            {
                using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                {
                    var datamarts = Task.Run(() => web.GetDataMarts()).Result;
                    var models    = datamarts.Where(dm => dm.Models != null)
                                    .SelectMany(dm => dm.Models.Select(m => new HubConfiguration
                    {
                        DataMartId       = dm.ID,
                        UnattendedMode   = (Lpp.Dns.DataMart.Lib.Classes.UnattendedMode)(int) dm.UnattendedMode,
                        ModelId          = m.ID,
                        ModelProcessorId = m.ProcessorID,
                        Properties       = m.Properties
                    }));

                    return(models.ToArray());
                }
            }
            catch (Exception ex)
            {
                _log.Error(string.Format("Unable to get Datamart Models for Network: {0}.", ns.NetworkName), ex);
                throw new GetModelsFailed(ex);
            }
        }
Exemplo n.º 4
0
        public static void PostDocumentChunk(string uploadIdentifier, Dns.DTO.DataMartClient.Criteria.DocumentMetadata doc, Stream stream, NetWorkSetting ns)
        {
            _log.Debug($"{uploadIdentifier} - Posting response document to API for: {doc.Name} (Chunk Index: {doc.CurrentChunkIndex}, ID: {doc.ID.ToString("D") }); RequestID: {doc.RequestID}, DataMartID: {doc.DataMartID.ToString("D")}");

            using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
            {
                byte[] buffer = new byte[0x400000];
                int    index  = 0;
                int    bytesRead;
                while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    byte[] data = buffer;
                    if (bytesRead < data.Length)
                    {
                        data = new byte[bytesRead];
                        Buffer.BlockCopy(buffer, 0, data, 0, bytesRead);
                    }
                    try
                    {
                        doc.CurrentChunkIndex = index;

                        Task.Run(async() => await web.PostDocumentChunk(doc, data)).Wait(TimeSpan.FromDays(2));
                    }
                    catch (Exception ex)
                    {
                        _log.Error($"{uploadIdentifier} - Unable to post response document content for: {doc.Name} (Chunk Index: {doc.CurrentChunkIndex}, ID: { doc.ID.ToString("D") }); RequestID: {doc.RequestID }, DataMartID: {doc.DataMartID.ToString("D") }.", ex);
                        throw new PostResponseDocumentContentFailed(ex);
                    }

                    index++;
                }
            }

            _log.Debug($"{uploadIdentifier} - Finished posting document content to API for: {doc.Name} (ID: {doc.ID.ToString("D") }); RequestID: {doc.RequestID}, DataMartID: {doc.DataMartID.ToString("D")}");
        }
Exemplo n.º 5
0
 public static bool LogIn(NetWorkSetting ns)
 {
     try
     {
         try
         {
             using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
             {
                 var profile = Task.Run(() => web.GetProfile()).Result;
                 ns.Profile = new Profile
                 {
                     Email            = profile.Email,
                     FullName         = profile.FullName,
                     OrganizationName = profile.OrganizationName,
                     Username         = profile.Username
                 };
             }
             ns.IsAuthenticated = true;
             ns.NetworkStatus   = Util.ConnectionOKStatus;
             ns.NetworkMessage  = string.Empty;
             return(true);
         }
         catch (System.Net.Http.HttpRequestException rex)
         {
             //Unwrap the raised exception to the original exception - it gets wrapped in generic http exceptions.
             throw UnwrapException(rex);
         }
     }
     catch (System.Net.Http.HttpRequestException rex)
     {
         ns.NetworkStatus   = Util.ConnectionFailedStatus;
         ns.IsAuthenticated = false;
         ns.NetworkMessage  = rex.Message;
         if (string.Equals(ns.NetworkMessage, "Response status code does not indicate success: 401 (Unauthorized).", StringComparison.OrdinalIgnoreCase))
         {
             ns.NetworkMessage = "Authentication failed, please check your credentials are correct.";
         }
     }
     catch (System.Net.Sockets.SocketException sockEx)
     {
         ns.NetworkStatus   = Util.ConnectionFailedStatus;
         ns.IsAuthenticated = false;
         if (sockEx.ErrorCode == 10061)
         {
             ns.NetworkMessage = string.Format("Unable to connect, please confirm service url:{1}{1}{0}{1}{1}If the url is correct, and the problem persists please contact your network administrator.", ns.HubWebServiceUrl, Environment.NewLine);
         }
         else
         {
             ns.NetworkMessage = sockEx.Message;
         }
     }
     catch (Exception e)
     {
         ns.NetworkStatus   = Util.ConnectionFailedStatus;
         ns.IsAuthenticated = false;
         ns.NetworkMessage  = e.Message;
     }
     return(false);
 }
Exemplo n.º 6
0
 public static Lpp.Dns.DTO.DataMartClient.RequestTypeIdentifier GetRequestTypeIdentifier(NetWorkSetting ns, Guid modelID, Guid processorID)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             return(AsyncHelpers.RunSync <Lpp.Dns.DTO.DataMartClient.RequestTypeIdentifier>(() => web.GetRequestTypeIdentifier(modelID, processorID)));
         }
     }
     catch (Exception ex)
     {
         throw new GetRequestTypeIdentifierException(ex);
     }
 }
Exemplo n.º 7
0
 public static Lpp.Dns.DTO.DataMartClient.RequestTypeIdentifier GetRequestTypeIdentifier(NetWorkSetting ns, Guid modelID, Guid processorID)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             return(Task.Run(() => web.GetRequestTypeIdentifier(modelID, processorID)).Result);
         }
     }
     catch (Exception ex)
     {
         _log.Error(string.Format("Unable to get requests identifier for Network: {0}.", ns.NetworkName), ex);
         throw new GetRequestTypeIdentifierException(ex);
     }
 }
Exemplo n.º 8
0
 public static byte[] GetDocumentChunk(Guid documentID, int offset, int size, NetWorkSetting ns)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             var buffer = Task.Run(() => web.GetDocumentChunk(documentID, offset, size)).Result;
             return(buffer.ToArray());
         }
     }
     catch (Exception ex)
     {
         _log.Error(string.Format("Unable to get Document Chunk for Document: {0}.", documentID), ex);
         throw new GetDocumentChunkFailed(ex);
     }
 }
Exemplo n.º 9
0
 public static byte[] GetDocumentChunk(Guid documentID, int offset, int size, NetWorkSetting ns)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             //var buffer = AsyncHelpers.RunSync<IEnumerable<byte>>(() => web.GetDocumentChunk(documentID, offset, size));
             var buffer = Task.Run(() => web.GetDocumentChunk(documentID, offset, size)).Result;
             return(buffer.ToArray());
         }
     }
     catch (Exception ex)
     {
         throw new GetDocumentChunkFailed(ex);
     }
 }
Exemplo n.º 10
0
 public static void SetRequestStatus(HubRequest request, HubRequestStatus status, IDictionary <string, string> requestProperties, NetWorkSetting ns)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             Task.Run(() => web.SetRequestStatus(request.Source.ID, request.DataMartId, status.Code, status.Message, requestProperties.EmptyIfNull().Select(p => new DTO.DataMartClient.RoutingProperty {
                 Name = p.Key, Value = p.Value
             }).ToArray())).Wait();
         }
     }
     catch (Exception ex)
     {
         _log.Error(string.Format("Unable to set request status for requestID: {0}.", request.Source.ID), ex);
         throw new SetRequestStatusFailed(ex);
     }
 }
Exemplo n.º 11
0
 public static Guid[] PostResponseDocuments(string requestId, Guid dataMartId, Lpp.Dns.DataMart.Model.Document[] documents, NetWorkSetting ns)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             var result = AsyncHelpers.RunSync <IEnumerable <Guid> >(() => web.PostResponseDocuments(new Guid(requestId), dataMartId, documents.Select(d => new Lpp.Dns.DTO.DataMartClient.Document {
                 Name = d.Filename, MimeType = d.MimeType, Size = d.Size, IsViewable = d.IsViewable
             })));
             return(result.ToArray());
         }
     }
     catch (Exception ex)
     {
         throw new PostResponseDocumentsFailed(ex);
     }
 }
Exemplo n.º 12
0
        /// <summary>
        /// Starts sending the document data to the portal and returns a stream of "bytes written so far" values
        /// TODO: Replace IObservable[int] with a specific type that would reflect the semantics
        /// </summary>
        public static IObservable <int> PostResponseDocumentContent(string uploadIdentifier, string requestID, Guid dataMartID, Guid documentId, string documentName, Stream stream, NetWorkSetting ns)
        {
            _log.Debug($"{uploadIdentifier} - Posting document content to API for: {documentName} (ID: {documentId.ToString("D") }); RequestID: {requestID}, DataMartID: {dataMartID.ToString("D")}");

            byte[] buffer = new byte[0x400000];
            return(Observable.Generate(
                       new { offset = 0, finish = false },
                       st => !st.finish,
                       st =>
            {
                int bytesRead = stream.Read(buffer, 0, buffer.Length);
                if (bytesRead == 0)
                {
                    _log.Debug($"{uploadIdentifier} - Finished posting document content to API for: {documentName} (ID: {documentId.ToString("D") }); RequestID: {requestID}, DataMartID: {dataMartID.ToString("D")}");
                    return new { st.offset, finish = true };
                }

                byte[] data = buffer;
                if (bytesRead < data.Length)
                {
                    data = new byte[bytesRead];
                    Buffer.BlockCopy(buffer, 0, data, 0, bytesRead);
                }

                try
                {
                    using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                    {
                        Task.Run(() => web.PostResponseDocumentChunk(documentId, data)).Wait();
                    }
                }
                catch (Exception ex)
                {
                    _log.Error(string.Format("Unable to post Repsonse Doucument Content for Document: {0}.", documentId), ex);
                    throw new PostResponseDocumentContentFailed(ex);
                }

                return new { offset = st.offset + bytesRead, finish = false };
            },
                       st => st.offset
                       ));
        }
Exemplo n.º 13
0
 public static HubDataMart[] GetDataMarts(NetWorkSetting ns)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             var datamarts = AsyncHelpers.RunSync <IQueryable <Lpp.Dns.DTO.DataMartClient.DataMart> >(() => web.GetDataMarts());
             return(datamarts.Select(dm => new HubDataMart {
                 DataMartId = dm.ID,
                 DataMartName = dm.Name,
                 OrganizationId = dm.OrganizationID.ToString("D"),
                 OrganizationName = dm.OrganizationName
             }).ToArray());
         }
     }
     catch (Exception ex)
     {
         throw new GetDataMartsFailed(ex);
     }
 }
Exemplo n.º 14
0
 public static HubDataMart[] GetDataMarts(NetWorkSetting ns)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             var datamarts = Task.Run(() => web.GetDataMarts()).Result;
             return(datamarts.Select(dm => new HubDataMart {
                 DataMartId = dm.ID,
                 DataMartName = dm.Name,
                 OrganizationId = dm.OrganizationID.ToString("D"),
                 OrganizationName = dm.OrganizationName
             }).ToArray());
         }
     }
     catch (Exception ex)
     {
         _log.Error(string.Format("Unable to get Datamarts for Network: {0}.", ns.NetworkName), ex);
         throw new GetDataMartsFailed(ex);
     }
 }
Exemplo n.º 15
0
 public static void DownloadPackage(NetWorkSetting ns, RequestTypeIdentifier packageIdentifier)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             string filepath = System.IO.Path.Combine(Lpp.Dns.DataMart.Client.Utils.Configuration.PackagesFolderPath, packageIdentifier.PackageName());
             using (var stream = AsyncHelpers.RunSync <System.IO.Stream>(() => web.GetPackage(packageIdentifier)))
             {
                 using (var filestream = new System.IO.FileStream(filepath, System.IO.FileMode.Create))
                 {
                     stream.CopyTo(filestream);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 16
0
        /// <summary>
        /// Starts sending the document data to the portal and returns a stream of "bytes written so far" values
        /// TODO: Replace IObservable[int] with a specific type that would reflect the semantics
        /// </summary>
        public static IObservable <int> PostResponseDocumentContent(Guid documentId, Stream stream, NetWorkSetting ns)
        {
            byte[] buffer = new byte[0x400000];
            return(Observable.Generate(
                       new { offset = 0, finish = false },
                       st => !st.finish,
                       st =>
            {
                int bytesRead = stream.Read(buffer, 0, buffer.Length);
                if (bytesRead == 0)
                {
                    return new { st.offset, finish = true }
                }
                ;

                byte[] data = buffer;
                if (bytesRead < data.Length)
                {
                    data = new byte[bytesRead];

                    Buffer.BlockCopy(buffer, 0, data, 0, bytesRead);
                }

                try
                {
                    using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                    {
                        AsyncHelpers.RunSync(() => web.PostResponseDocumentChunk(documentId, data));
                    }
                }
                catch (Exception ex)
                {
                    throw new PostResponseDocumentContentFailed(ex);
                }

                return new { offset = st.offset + bytesRead, finish = false };
            },
                       st => st.offset
                       ));
        }
Exemplo n.º 17
0
 public static void DownloadPackage(NetWorkSetting ns, RequestTypeIdentifier packageIdentifier)
 {
     try
     {
         using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
         {
             string filepath = System.IO.Path.Combine(Lpp.Dns.DataMart.Client.Utils.Configuration.PackagesFolderPath, packageIdentifier.PackageName());
             using (var stream = Task.Run(() => web.GetPackage(packageIdentifier)).Result)
             {
                 using (var filestream = new System.IO.FileStream(filepath, System.IO.FileMode.Create))
                 {
                     stream.CopyTo(filestream);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         _log.Error(string.Format("Unable to download package {1} for Network: {0}.", ns.NetworkName, packageIdentifier), ex);
         throw ex;
     }
 }
Exemplo n.º 18
0
        public static void SetRequestStatus(HubRequest request, HubRequestStatus status, IDictionary <string, string> requestProperties, NetWorkSetting ns)
        {
            // BMS: Don't report inprocess or awaitingresponseapproval until portal can display status in routings.
            //if (status.Code == DTO.DataMartClient.Enums.DMCRoutingStatus.AwaitingResponseApproval)
            //{
            //    return;
            //}

            try
            {
                using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                {
                    AsyncHelpers.RunSync(() => web.SetRequestStatus(request.Source.ID, request.DataMartId, status.Code, status.Message, requestProperties.EmptyIfNull().Select(p => new DTO.DataMartClient.RoutingProperty {
                        Name = p.Key, Value = p.Value
                    }).ToArray()));
                }
            }
            catch (Exception ex)
            {
                throw new SetRequestStatusFailed(ex);
            }
        }
Exemplo n.º 19
0
        public static HubModel[] GetModels(Guid dataMartID, NetWorkSetting ns)
        {
            try
            {
                using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                {
                    var datamarts = AsyncHelpers.RunSync <IQueryable <Lpp.Dns.DTO.DataMartClient.DataMart> >(() => web.GetDataMarts());

                    var models = datamarts.Where(dm => dm.ID == dataMartID && dm.Models != null)
                                 .Select(dm => dm.Models.Select(m => new HubModel
                    {
                        Id               = m.ID,
                        Name             = m.Name,
                        ModelProcessorId = m.ProcessorID
                    })).FirstOrDefault();

                    return(models.ToArray());
                }
            }
            catch (Exception ex)
            {
                throw new GetModelsFailed(ex);
            }
        }
Exemplo n.º 20
0
        public static Guid[] PostResponseDocuments(string uploadIdentifier, string requestId, Guid dataMartId, Lpp.Dns.DataMart.Model.Document[] documents, NetWorkSetting ns)
        {
            string docString = string.Join(", ", documents.Select(x => string.Format("'{0:D}'", x.Filename)));

            try
            {
                using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                {
                    _log.Debug($"{uploadIdentifier} - Posting metadata for the following documents to API: {docString} for RequestID: {requestId}, DataMartID: {dataMartId.ToString("D")}");

                    var result = Task.Run(() => web.PostResponseDocuments(new Guid(requestId), dataMartId, documents.Select(d => new Lpp.Dns.DTO.DataMartClient.Document {
                        Name = d.Filename, MimeType = d.MimeType, Size = d.Size, IsViewable = d.IsViewable, Kind = d.Kind
                    }).ToArray())).Result;

                    _log.Debug($"{uploadIdentifier} - Posting metadata complete for the following documents to API: {docString} for RequestID: {requestId}, DataMartID: {dataMartId.ToString("D")}");
                    return(result.ToArray());
                }
            }
            catch (Exception ex)
            {
                _log.Error(string.Format("{2} - Unable to post document metadata for the following documents: {1} for RequestID: {0}.", requestId, docString, uploadIdentifier), ex);
                throw new PostResponseDocumentsFailed(ex);
            }
        }
Exemplo n.º 21
0
        public static IObservable <RequestList> GetRequestList(string queryDescription, NetWorkSetting ns, int startIndex, int count, RequestFilter filter, RequestSortColumn?sortColumn, bool?sortAscending)
        {
            var nullReqs = new RequestList();
            var noError  = (Exception)null;

            if (count < 0)
            {
                count = int.MaxValue;
            }

            return
                (Observable.Generate(new
            {
                reqs = nullReqs,
                nextIndex = startIndex,
                pageSize = 100,
                count,
                finish = false,
                error = noError
            },
                                     st => !st.finish,
                                     st =>
            {
                try
                {
                    if (st.error != null || st.count == 0)
                    {
                        return new { st.reqs, st.nextIndex, st.pageSize, st.count, finish = true, st.error };
                    }

                    Lpp.Dns.DTO.DataMartClient.RequestList list = null;
                    using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                    {
                        list = Task.Run(() => web.GetRequestList(queryDescription, filter.EffectiveFromDate, filter.EffectiveToDate, filter.DataMartIds, filter.Statuses, (Lpp.Dns.DTO.DataMartClient.RequestSortColumn?)(int?) sortColumn, sortAscending, st.nextIndex, Math.Min(st.count, st.pageSize))).Result;
                    }

                    RequestList reqs = null;
                    if (list == null)
                    {
                        reqs = new RequestList
                        {
                            Segment = Enumerable.Empty <RequestListRow>().ToArray(),
                            SortedAscending = true,
                            SortedByColumn = RequestSortColumn.RequestTime,
                            StartIndex = 0,
                            TotalCount = 0
                        };
                    }
                    else
                    {
                        reqs = new RequestList
                        {
                            Segment = list.Segment,
                            SortedAscending = list.SortedAscending,
                            SortedByColumn = list.SortedByColumn,
                            StartIndex = list.StartIndex,
                            TotalCount = list.TotalCount
                        };
                    }

                    _lastSucessfulPageSize = st.pageSize;


                    return new
                    {
                        reqs,
                        nextIndex = st.nextIndex + reqs.Segment.Count(),
                        st.pageSize,
                        count = Math.Min(st.count, reqs.TotalCount - reqs.StartIndex) - reqs.Segment.Count(),
                        finish = false,
                        error = noError
                    };
                }
                catch (CommunicationException ex)
                {
                    if (ex.Message.ToLower().Contains("size quota"))
                    {
                        return st.pageSize > 1 ?
                        new { reqs = nullReqs, st.nextIndex, pageSize = st.pageSize / 2, st.count, finish = false, error = noError } :
                        new { reqs = nullReqs, st.nextIndex, st.pageSize, st.count, finish = false, error = (Exception)ex };
                    }

                    throw;
                }
            },
                                     st => st.error == null ? Observable.Return(st.reqs) : Observable.Throw <RequestList>(st.error),
                                     System.Reactive.Concurrency.Scheduler.Default
                                     )
                 .Catch(( Exception ex ) =>
            {
                _log.Error(string.Format("Unable to get requests for Network: {0}.", ns.NetworkName), ex);
                return Observable.Throw <IObservable <RequestList> >(new GetRequestsFailed(ex));
            })
                 .SelectMany(ls => ls)
                 .Skip(1)
                 .Aggregate((l1, l2) => new RequestList
            {
                Segment = l1.Segment.EmptyIfNull().Concat(l2.Segment.EmptyIfNull()).ToArray(),
                SortedAscending = l1.SortedAscending,
                SortedByColumn = l1.SortedByColumn,
                StartIndex = l1.StartIndex,
                TotalCount = l1.TotalCount
            }));
        }
Exemplo n.º 22
0
        public static IObservable <Request> GetRequests(NetWorkSetting ns, Guid[] ids, Guid dmID)
        {
            if (ids.NullOrEmpty())
            {
                return(Observable.Empty <Request>());
            }

            var nullReqs = (Request[])null;
            var noError  = (Exception)null;

            return
                (Observable.Generate(
                     new { reqs = nullReqs, idss = new[] { ids }.AsEnumerable(), DataMartID = dmID, finish = false, error = noError },
                     st => !st.finish,
                     st =>
            {
                try
                {
                    if (st.error != null)
                    {
                        return new { st.reqs, st.idss, st.DataMartID, finish = true, st.error }
                    }
                    ;

                    var i = st.idss.FirstOrDefault();
                    var dm = st.DataMartID;
                    if (i == null)
                    {
                        return new { st.reqs, idss = st.idss, DataMartID = st.DataMartID, finish = true, error = noError }
                    }
                    ;

                    Request[] reqs = null;
                    using (var web = new Lpp.Dns.DataMart.Client.Lib.DnsApiClient(ns, FindCert(ns)))
                    {
                        var requests = AsyncHelpers.RunSync <IQueryable <DTO.DataMartClient.Request> >(() => web.GetRequests(i.Select(x => x).ToArray(), dm));

                        reqs = requests.ToArray();
                    }

                    return new { reqs, idss = st.idss.Skip(1), DataMartID = st.DataMartID, finish = false, error = noError };
                }
                catch (CommunicationException ex)
                {
                    if (ex.Message.ToLower().Contains("size quota"))
                    {
                        return st.idss.First().Length > 1         // When the next segment can be subdivided further, do it
                                    ? new
                        {
                            reqs = nullReqs,
                            idss = st.idss
                                   .SelectMany(i => new[] { i.Take(i.Length / 2).ToArray(), i.Skip(i.Length / 2).ToArray() })
                                   .Where(i => i.Any()),
                            DataMartID = st.DataMartID,
                            finish = false,
                            error = noError
                        }
                        // Otherwise, return error
                                    : new { reqs = nullReqs, idss = st.idss, DataMartID = st.DataMartID, finish = false, error = (Exception)ex };
                    }

                    throw;
                }
            },
                     st => st.error == null ? st.reqs.EmptyIfNull().ToObservable() : Observable.Throw <Request>(st.error),
                     System.Reactive.Concurrency.Scheduler.Default
                     )
                 .Catch(( Exception ex ) =>
            {
                _log.Error(string.Format("Unable to get requests for Network: {0}.", ns.NetworkName), ex);
                return Observable.Throw <IObservable <Request> >(new GetRequestsFailed(ex));
            })
                 .SelectMany(rs => rs));
        }