private static void SaveRouteAssetsBulkToDatabase(RouteModel route,
      List<RouteAssetsModel> routeAssets)
      {
      // Step 1: add all assets tot he assets table
      // Step 2: insert provider products in the table
      // Step 3: get for each asset the id from the assets table (all assets will be there)
      // Step 4: insert route Id and asset id in the 

      List<AssetModel> assets = routeAssets.Select(x => x.Asset).ToList();
      List<ProviderProductModel> providerProducts =
        routeAssets.Select(x => x.Asset.ProviderProduct).DistinctBy(x => x.ProviderProduct)
          .ToList();
      ProviderProductDatabaseCollectionModel.SaveProviderProductsBulk(providerProducts);
      AssetDatabaseCollectionModel.SaveAssetsBulk(assets);

      providerProducts = assets.Select(x => x.ProviderProduct).ToList();
      // make sure all providerProducts have an Id 
      ProviderProductDatabaseCollectionModel.UpdateListIdsFromDatabase(providerProducts);
      // Bulk insert
      using (IDbConnection connection =
        new SQLiteConnection(AssetDatabaseAccess.GetConnectionString()))
        {
        connection.Open();
        using (IDbTransaction transaction = connection.BeginTransaction())
          {
          string sqlStatement = @$"INSERT OR IGNORE INTO RouteAssets (RouteId,AssetId)
                                    SELECT Routes.Id, Assets.Id FROM Routes, Assets
	                                  WHERE Routes.Id= (SELECT Id FROM Routes WHERE RouteGuid= @RouteGuid) AND
                                    Assets.Id= (SELECT Id FROM Assets WHERE BlueprintPath=@BluePrintPath AND 
                                    Assets.ProvProdId=@Id); ";
          try
            {
            foreach (var item in routeAssets)
              {
              connection.Execute(sqlStatement, new {item.Route.RouteGuid, item.Asset.BluePrintPath, item.Asset.ProviderProduct.Id }, transaction);
              }

            transaction.Commit();
            }
          catch (Exception e)
            {
            transaction.Rollback();
            Log.Trace($"Bulk save in database for assets failed, rolled back", e,
              LogEventType.Error);
            }
          }
        }
      }
Example #2
0
        public void SaveAllUnpackedAssets()
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            try
            {
                List <ProviderProductModel> ProviderProductList = ProviderProductDatabaseCollectionModel.ReadAllProviderProductsFromDatabase();
                ProviderProductList = ProviderProductList.Where(x => x.Pack.Length == 0).ToList();

                ProgressPercentage progress = new ProgressPercentage()
                {
                    TotalWork       = ProviderProductList.Count,
                    CurrentProgress = 0,
                    Message         = "Saving assets progress"
                };

                foreach (ProviderProductModel providerProduct in ProviderProductList)
                {
                    Int32  providerProductId = providerProduct.GetDatabaseRecordId();
                    String tempPath          = $@"{AssetBasePath}{providerProduct.ArchiveFileName}";
                    if (Directory.Exists(tempPath))
                    {
                        DirectoryInfo providerProductDir = new DirectoryInfo(tempPath);
                        var           assets             =
                            ReadAllAssetsFromDirectory(providerProductDir, providerProduct, tempPath.Length);
                        SaveAssetsBulk(assets, providerProductId);
                        progress.CurrentProgress++;
                        SaveAssetProgressReporter?.Invoke(this, progress);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Trace($"Saving unpacked assets failed ", ex, LogEventType.Error);
            }
        }