protected void CreateHistory(Action<OperationState> stateChangedCallback, Action completedCallback, CancellationToken cancellationToken) { //1. Group By Objects //2. For Every Group retrive group items //3. Select from group all but newest //4. Drop all but newest //5. CreateHistory items OperationState state = new OperationState(); var objects = Repositories.AdsRepository.GetAdsObjects<AdRealty>(); state.ProgressTotal = objects.Count(); stateChangedCallback(state); foreach (var obj in objects) { if (cancellationToken.IsCancellationRequested) { state.Canceled = true; stateChangedCallback(state); break; } var objAds = Repositories.AdsRepository.GetAdsForTheSameObject(obj).OrderByDescending(a => a.CollectDate); Repositories.AdsRepository.DeleteItems(objAds.Skip(1).Select(a => a.Id).ToList()); AdRealty newestAd = (AdRealty)objAds.First(); Repositories.AdHistoryItemsRepository.AddList(objAds.Skip(1) .Select(a => new AdHistoryItem() { AdId = newestAd.Id, AdCollectDate = a.CollectDate, AdPublishDate = a.PublishDate, Price = a.Price }) .ToList()); state.Progress++; stateChangedCallback(state); } completedCallback(); }
protected void FillDetails(Action<OperationState> stateCallback, Action completedCallback, CancellationToken cancelationToken, List<Ad> ads) { Dictionary<string, IConnector> connectorsCache = new Dictionary<string, IConnector>(); OperationState state = new OperationState(); state.ProgressTotal = ads.Count; foreach (var ad in ads) { IConnector connector; if (!connectorsCache.TryGetValue(ad.ConnectorId, out connector)) { connector = Managers.ConnectorsManager.GetById(ad.ConnectorId); connectorsCache.Add(ad.ConnectorId, connector); } try { if (connector.FillDetails(ad)) { if (cancelationToken.IsCancellationRequested) { state.Canceled = true; stateCallback(state); break; } ad.DetailsDownloadStatus = DetailsDownloadStatus.Downloaded; Repositories.AdsRepository.UpdateItem(ad); if (ad.Images != null && ad.Images.Count > 0) { Repositories.AdImagesRepository.AddList(ad.Images); } } state.Description = string.Format("Fill details {0}.", ad.ConnectorId); state.ProgressTotal++; stateCallback(state); } catch (Exception ex) { Managers.LogEntriesManager.AddItem(SeverityLevel.Error, string.Format("Fill details error. Connector: {0}; Ad: {1}.", connector.Id, ad.Id), string.Format("Exception: {0}\n Stack: {1}\n Url: {2}", ex.Message, ex.StackTrace, ad.Url)); } } completedCallback(); }