Exemple #1
0
        public async Task FetchFullDownloadableDataIntoDatabase(
            string exhibitId, int idForRestApi, CancellationToken token, IProgressListener listener)
        {
            double totalSteps = await FetchNeededMediaForFullExhibit(idForRestApi);

            if (token.IsCancellationRequested)
            {
                return;
            }

            listener.SetMaxProgress(totalSteps);

            // We need to attach all images since the quiz download may download existing images
            var itemsToTrack = DbManager
                               .DataAccess
                               .GetItems <Image>()
                               .Where(image => image.Id != BackupData.BackupImage.Id && image.Id != BackupData.BackupImageTag.Id);
            await mediaDataFetcher.FetchMedias(requiredMedia, token, listener);

            var quizzes = await DownloadQuizes(idForRestApi, token);

            var mediaToFilePath = await mediaDataFetcher.WriteMediaToDiskAsync();

            DbManager.InTransaction(itemsToTrack, transaction =>
            {
                ProcessPages(exhibitId, token, transaction.DataAccess, mediaToFilePath);
                transaction.DataAccess.Quizzes().Add(quizzes);

                if (token.IsCancellationRequested)
                {
                    transaction.Rollback();
                }
            });
        }
Exemple #2
0
        public async Task FetchFullDownloadableDataIntoDatabase(
            string routeId, int idForRestApi, CancellationToken token, IProgressListener listener)
        {
            routeDto = (await routesApiAccess.GetRoutes(new List <int> {
                idForRestApi
            })).Items.First();
            if (token.IsCancellationRequested)
            {
                return;
            }

            route = DbManager.DataAccess.Routes().GetRoute(routeId);
            pagesAndMediaForMissingExhibits = new List <ExhibitPagesAndMediaContainer>();

            var allMissingExhibits = DbManager.DataAccess.Exhibits().GetExhibits().ToList().FindAll(x => !x.DetailsDataLoaded); // Exhibits not fully loaded yet

            missingExhibitsForRoute = allMissingExhibits.ToList().FindAll(x => routeDto.Exhibits.Contains(x.IdForRestApi));     // Select those part of the route

            double totalSteps = FetchNeededMediaForFullRoute();

            foreach (var exhibit in missingExhibitsForRoute) // Fetch media for missing exhibits and save them for later download
            {
                var pagesAndRequiredMediaForExhibit = await fullExhibitDataFetcher.FetchPagesAndMediaForExhibitFromRouteFetcher(exhibit.IdForRestApi);

                if (token.IsCancellationRequested)
                {
                    return;
                }

                pagesAndMediaForMissingExhibits.Add(pagesAndRequiredMediaForExhibit);
                totalSteps += pagesAndRequiredMediaForExhibit.RequiredMedia.Count;
            }

            listener.SetMaxProgress(totalSteps);

            await AddFullExhibitsToRoute(route, token, listener); // Download all missing exhibits

            if (token.IsCancellationRequested)
            {
                return;
            }

            await FetchMediaData(token, listener);

            var mediaToFilePath = await mediaDataFetcher.WriteMediaToDiskAsync();

            DbManager.InTransaction(transaction =>
            {
                var dataAccess = transaction.DataAccess;
                ProcessRoute(token, dataAccess, mediaToFilePath); // Download audio
                if (token.IsCancellationRequested)
                {
                    transaction.Rollback();
                }
            });
        }
Exemple #3
0
        public async Task FetchBaseDataIntoDatabase(CancellationToken token, IProgressListener listener)
        {
            var routes   = DbManager.DataAccess.Routes().GetRoutes().ToDictionary(x => x.IdForRestApi, x => x.Timestamp);
            var exhibits = DbManager.DataAccess.Exhibits().GetExhibits().ToDictionary(x => x.IdForRestApi, x => x.Timestamp);

            var totalSteps = await exhibitsBaseDataFetcher.FetchNeededDataForExhibits(exhibits);

            if (token.IsCancellationRequested)
            {
                return;
            }

            totalSteps += await routesBaseDataFetcher.FetchNeededDataForRoutes(routes);

            if (token.IsCancellationRequested)
            {
                return;
            }

            listener.SetMaxProgress(totalSteps);

            await exhibitsBaseDataFetcher.FetchMediaData(token, listener);

            if (token.IsCancellationRequested)
            {
                return;
            }

            await routesBaseDataFetcher.FetchMediaData(token, listener);

            if (token.IsCancellationRequested)
            {
                return;
            }

            var exhibitsMediaToFilePath = await exhibitsBaseDataFetcher.WriteMediaToDiskAsync();

            var routesMediaToFilePath = await routesBaseDataFetcher.WriteMediaToDiskAsync();

            var cancelled = DbManager.InTransaction(transaction =>
            {
                exhibitsBaseDataFetcher.ProcessExhibits(listener, transaction.DataAccess, exhibitsMediaToFilePath);
                if (token.IsCancellationRequested)
                {
                    transaction.Rollback();
                    return(true);
                }

                routesBaseDataFetcher.ProcessRoutes(listener, transaction.DataAccess, routesMediaToFilePath);
                if (token.IsCancellationRequested)
                {
                    transaction.Rollback();
                    return(true);
                }

                return(false);
            });

            if (!cancelled)
            {
                await dataToRemoveFetcher.FetchDataToDelete(token);

                DbManager.InTransaction(transaction => dataToRemoveFetcher.CleanupRemovedData(transaction.DataAccess));
                await dataToRemoveFetcher.PruneMediaFilesAsync(DbManager.DataAccess);
            }
        }