Exemplo n.º 1
0
        public WebData_v2 <TSource, TData> Load(WebRequest_v2 <TSource> request)
        {
            //Trace.WriteLine();
            //Trace.WriteLine($"WebDataManager_v5.Load()");

            WebData_v2 <TSource, TData> webData = new WebData_v2 <TSource, TData>(request);
            bool dataExist = DataExist(webData);


            if (_store == null || !dataExist || request.ReloadFromWeb || request.RefreshDocumentStore)
            {
                if (!LoadFromWeb(webData))
                {
                    return(webData);
                }
            }
            else
            {
                LoadFromStore(webData);
            }

            if (_store != null && webData.DataLoadedFromWeb)
            {
                SaveToStore(webData);
            }

            LoadImages(webData);

            return(webData);
        }
Exemplo n.º 2
0
        protected BsonDocument Serialize(WebData_v2 <TSource, TData> webData)
        {
            BsonDocument document = new BsonDocument {
                _dataSerializer.Serialize(webData.Data)
            };
            //if (webData.HttpResults.Count == 1)
            //{
            //    document.Add(_webRequestSerializer.Serialize(webData.HttpResults.First().Value.Http.GetHttpLog()));
            //}
            //else
            //{
            BsonDocument httpDocument = new BsonDocument();

            foreach (KeyValuePair <string, HttpResult <string> > value in webData.HttpResults)
            {
                if (value.Value.Http != null)
                {
                    httpDocument.Add(new BsonElement(value.Key, _webRequestSerializer.SerializeAsDocument(value.Value.Http.GetHttpLog())));
                }
            }
            string itemName = _webRequestSerializer.ItemName;

            if (itemName == null)
            {
                throw new PBException("web request serializer item name is not defined");
            }
            document.Add(new BsonElement(itemName, httpDocument));
            //}
            return(document);
        }
Exemplo n.º 3
0
        protected void LoadImages(WebData_v2 <TSource, TData> webData)
        {
            WebImageRequest imageRequest = webData.Request.ImageRequest;

            if (imageRequest == null)
            {
                return;
            }
            if ((webData.DataLoadedFromWeb && (imageRequest.LoadImageFromWeb || imageRequest.RefreshImage)) ||
                (!webData.DataLoadedFromWeb && imageRequest.LoadMissingImageFromWeb))
            {
                if (webData.Data is IGetWebImages)
                {
                    if (_webLoadImageManager.LoadImagesFromWeb(imageRequest, ((IGetWebImages)webData.Data).GetWebImages(), _getImageSubDirectory?.Invoke(webData)))
                    {
                        SaveToStore(webData);
                    }
                }
            }

            if (imageRequest.LoadImageToData)
            {
                _webLoadImageManager.LoadImagesToData(webData.Data);
            }
        }
Exemplo n.º 4
0
        protected bool LoadFromWeb(WebData_v2 <TSource, TData> webData)
        {
            //Trace.WriteLine($"WebDataManager_v5.LoadFromWeb()");

            TData   data    = webData.Data = _createData();
            TSource source  = webData.Request.Data;
            bool    success = true;

            foreach (KeyValuePair <string, Action <TData, HttpResult <string> > > value in _getDatas)
            {
                HttpRequest httpRequest = GetHttpRequest(source, value.Key);
                httpRequest.ReloadFromWeb = webData.Request.ReloadFromWeb;
                HttpResult <string> httpResult = _httpManager.LoadText(httpRequest);
                if (httpResult.Success)
                {
                    value.Value(data, httpResult);
                }
                else
                {
                    success = false;
                }
                webData.HttpResults.Add(value.Key, httpResult);
            }
            webData.DataLoaded        = true;
            webData.DataLoadedFromWeb = true;

            if (webData.Id != null)
            {
                webData.Data.zSetId(webData.Id);
            }

            return(success);
        }
Exemplo n.º 5
0
        protected void LoadFromStore(WebData_v2 <TSource, TData> webData)
        {
            //Trace.WriteLine($"WebDataManager_v5.LoadFromStore()");

            BsonDocument document = _store.LoadFromKey(webData.Key);

            Deserialize(webData, document);
            webData.DataLoaded          = true;
            webData.DataLoadedFromStore = true;
        }
Exemplo n.º 6
0
        private LoadNewDocumentsResult _LoadNewDocuments(IEnumerable <THeaderData> headers, int maxDocumentsLoadedFromStore = 5, int maxDocumentsLoaded = 0, WebImageRequest imageRequest = null)
        {
            bool refreshDocumentStore = false;    // obligatoire sinon nbDocumentLoadedFromStore reste à 0

            if (imageRequest == null)
            {
                imageRequest = new WebImageRequest {
                    LoadImageFromWeb = true
                }
            }
            ;
            int nbDocumentsLoaded          = 0;
            int nbDocumentsLoadedFromStore = 0;
            int nbDocumentsLoadedFromWeb   = 0;

            foreach (THeaderData header in headers)
            {
                //if (!(header is IHeaderData))
                //    throw new PBException("type {0} is not IHeaderData", header.GetType().zGetTypeName());
                //WebData<TDetailData> webData = _detailDataManager.Load(new WebRequest { HttpRequest = ((IHeaderData)header).GetHttpRequestDetail(), ReloadFromWeb = false, ImageRequest = imageRequest, RefreshDocumentStore = refreshDocumentStore });
                WebData_v2 <THeaderData, TDetailData> webData = _detailDataManager.Load(new WebRequest_v2 <THeaderData> {
                    Data = header, RefreshDocumentStore = refreshDocumentStore, ImageRequest = imageRequest
                });
                nbDocumentsLoaded++;
                if (webData.DataLoadedFromStore)
                {
                    nbDocumentsLoadedFromStore++;
                }
                if (webData.DataLoadedFromWeb)
                {
                    nbDocumentsLoadedFromWeb++;
                }

                _onDocumentLoaded?.Invoke(webData);
                if (maxDocumentsLoadedFromStore != 0 && nbDocumentsLoadedFromStore == maxDocumentsLoadedFromStore)
                {
                    break;
                }
                if (maxDocumentsLoaded != 0 && nbDocumentsLoaded == maxDocumentsLoaded)
                {
                    break;
                }
            }
            return(new LoadNewDocumentsResult {
                NbDocumentsLoadedFromWeb = nbDocumentsLoadedFromWeb, NbDocumentsLoadedFromStore = nbDocumentsLoadedFromStore
            });
        }
    }
Exemplo n.º 7
0
        protected void SaveToStore(WebData_v2 <TSource, TData> webData)
        {
            //Trace.WriteLine($"WebDataManager_v5.SaveToStore()");

            BsonDocument document = Serialize(webData);

            if (_store.GenerateId)
            {
                BsonValue id = webData.Id;
                if (id == null)
                {
                    webData.Id = id = _store.GetNewId();
                    webData.Data.zSetId(id);
                }
                _store.SaveWithId(id, document);
            }
            else
            {
                _store.SaveWithKey(webData.Key, document);
            }
        }
Exemplo n.º 8
0
        protected void Deserialize(WebData_v2 <TSource, TData> webData, BsonDocument document)
        {
            webData.Data = _dataSerializer.Deserialize(document);

            string itemName = _webRequestSerializer.ItemName;

            if (itemName == null)
            {
                throw new PBException("web request serializer item name is not defined");
            }
            if (!document.Contains(itemName))
            {
                throw new PBException($"deserialize web request : serializer document does'nt contain element \"{itemName}\"");
            }
            var element = document[itemName];

            if (element == null)
            {
                throw new PBException($"deserialize web request : element \"{itemName}\" is null");
            }
            if (!(element is BsonDocument))
            {
                throw new PBException($"deserialize web request : element \"{itemName}\" is not a document");
            }
            BsonDocument httpDocument = element as BsonDocument;

            foreach (BsonElement httpElement in httpDocument)
            {
                if (!(httpElement.Value is BsonDocument))
                {
                    throw new PBException($"deserialize web request : element \"{httpElement.Name}\" is not a document");
                }
                HttpLog httpLog = _webRequestSerializer.DeserializeData((BsonDocument)httpElement.Value);
                webData.HttpResults.Add(httpElement.Name, new HttpResult <string> {
                    Http = new Http_v2(httpLog)
                });
            }
        }
Exemplo n.º 9
0
        protected bool DataExist(WebData_v2 <TSource, TData> webData)
        {
            bool dataExists       = false;
            bool useDocumentStore = _store != null && !_desactivateDocumentStore;

            if (useDocumentStore)
            {
                webData.Key = GetDataKey(webData.Request.Data);
                if (_store.GenerateId)
                {
                    webData.Id = _store.GetId(webData.Key);
                    if (webData.Id != null)
                    {
                        dataExists = true;
                    }
                }
                else
                {
                    dataExists = _store.Exists(webData.Key);
                }
            }
            return(dataExists);
        }