Skip to content

Commit ad569fb

Browse files
Added file version management to download section
1 parent 5883a64 commit ad569fb

27 files changed

+1163
-334
lines changed

src/Libraries/SmartStore.Core/Domain/Catalog/Product.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public static HashSet<string> GetVisibilityAffectingPropertyNames()
6565
private ICollection<TierPrice> _tierPrices;
6666
private ICollection<Discount> _appliedDiscounts;
6767
private ICollection<ProductBundleItem> _productBundleItems;
68-
69-
private int _stockQuantity;
68+
69+
private int _stockQuantity;
7070
private int _backorderModeId;
7171
private string _sku;
7272
private string _gtin;
@@ -297,8 +297,9 @@ public string Gtin
297297
/// <summary>
298298
/// Gets or sets the download identifier
299299
/// </summary>
300-
[DataMember]
301-
public int DownloadId { get; set; }
300+
[Obsolete]
301+
[DataMember]
302+
public int DownloadId { get; set; }
302303

303304
/// <summary>
304305
/// Gets or sets a value indicating whether this downloadable product can be downloaded unlimited number of times
@@ -1138,5 +1139,5 @@ public virtual ICollection<ProductBundleItem> ProductBundleItems
11381139
get { return _productBundleItems ?? (_productBundleItems = new HashSet<ProductBundleItem>()); }
11391140
protected set { _productBundleItems = value; }
11401141
}
1141-
}
1142+
}
11421143
}

src/Libraries/SmartStore.Core/Domain/Media/Download.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
using NuGet;
2+
using SmartStore.Core.Domain.Catalog;
13
using System;
4+
using System.ComponentModel.DataAnnotations;
25
using System.ComponentModel.DataAnnotations.Schema;
36
using System.Runtime.Serialization;
47

@@ -83,5 +86,33 @@ public partial class Download : BaseEntity, ITransient, IHasMedia
8386
/// Gets or sets the media storage
8487
/// </summary>
8588
public virtual MediaStorage MediaStorage { get; set; }
86-
}
89+
90+
/// <summary>
91+
/// Gets or sets a value indicating the corresponding entity id
92+
/// </summary>
93+
[DataMember]
94+
[Index("IX_EntityId_EntityName", 0)]
95+
public int EntityId { get; set; }
96+
97+
/// <summary>
98+
/// Gets or sets a value indicating the corresponding entity name
99+
/// </summary>
100+
[DataMember]
101+
[Index("IX_EntityId_EntityName", 1)]
102+
[StringLength(100)]
103+
public string EntityName { get; set; }
104+
105+
/// <summary>
106+
/// Gets or sets a value the verion info
107+
/// </summary>
108+
[DataMember]
109+
[StringLength(30)]
110+
public string FileVersion { get; set; }
111+
112+
/// <summary>
113+
/// Gets or sets a value which contains information about changes of the current download version
114+
/// </summary>
115+
[DataMember]
116+
public string Changelog { get; set; }
117+
}
87118
}

src/Libraries/SmartStore.Data/Migrations/201807201157391_DownloadVersions.Designer.cs

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace SmartStore.Data.Migrations
2+
{
3+
using SmartStore.Core.Data;
4+
using SmartStore.Core.Domain.Catalog;
5+
using SmartStore.Data.Setup;
6+
using SmartStore.Data.Utilities;
7+
using System;
8+
using System.Data.Entity.Migrations;
9+
using System.Linq;
10+
using System.Web.Hosting;
11+
12+
public partial class DownloadVersions : DbMigration, IDataSeeder<SmartObjectContext>
13+
{
14+
public override void Up()
15+
{
16+
int entityId = 0;
17+
18+
if (DataSettings.DatabaseIsInstalled() && HostingEnvironment.IsHosted)
19+
{
20+
var ctx = new SmartObjectContext();
21+
entityId = ctx.Set<Product>().Select(x => x.Id).FirstOrDefault();
22+
}
23+
24+
AddColumn("dbo.Download", "EntityId", c => c.Int(nullable: false, defaultValue: entityId));
25+
AddColumn("dbo.Download", "EntityName", c => c.String(nullable: false, maxLength: 100));
26+
AddColumn("dbo.Download", "FileVersion", c => c.String(maxLength: 30));
27+
AddColumn("dbo.Download", "Changelog", c => c.String());
28+
CreateIndex("dbo.Download", new[] { "EntityId", "EntityName" });
29+
}
30+
31+
public override void Down()
32+
{
33+
DropIndex("dbo.Download", new[] { "EntityId", "EntityName" });
34+
DropColumn("dbo.Download", "Changelog");
35+
DropColumn("dbo.Download", "FileVersion");
36+
DropColumn("dbo.Download", "EntityName");
37+
DropColumn("dbo.Download", "EntityId");
38+
}
39+
40+
public bool RollbackOnFailure
41+
{
42+
get { return true; }
43+
}
44+
45+
public void Seed(SmartObjectContext context)
46+
{
47+
if (!HostingEnvironment.IsHosted)
48+
return;
49+
50+
DataMigrator.SetDownloadProductId(context);
51+
}
52+
}
53+
}

src/Libraries/SmartStore.Data/Migrations/201807201157391_DownloadVersions.resx

Lines changed: 126 additions & 0 deletions
Large diffs are not rendered by default.

src/Libraries/SmartStore.Data/Migrations/MigrationsConfiguration.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,20 @@ public void MigrateLocaleResources(LocaleResourcesBuilder builder)
339339
"Intro",
340340
"Optional. Short introduction / teaser.",
341341
"Optional. Einleitung / Teaser.");
342-
}
342+
343+
builder.AddOrUpdate("Common.Download.Versions", "Versions", "Versionen");
344+
builder.AddOrUpdate("Common.Download.Version", "Version", "Version");
345+
builder.AddOrUpdate("Common.Download.Delete", "Delete download", "Download löschen");
346+
347+
builder.AddOrUpdate("Admin.Catalog.Products.Fields.NewVersionDownloadId",
348+
"New download version",
349+
"Neue Version des Downloads",
350+
"Upload a new version of the download file here.",
351+
"Laden Sie hier eine neue Version der Download-Datei hoch.");
352+
353+
builder.AddOrUpdate("Admin.Catalog.Products.Download.VersionDelete", "Delete this file version of the download.", "Diese Dateiversion des Downloads löschen.");
354+
builder.AddOrUpdate("Admin.Catalog.Products.Download.AddChangelog", "Edit changelog", "Changelog bearbeiten");
355+
builder.AddOrUpdate("Customer.Downloads.NoChangelogAvailable", "No changelog information available.", "Keine Changelog-Informationen verfügbar.");
356+
}
343357
}
344358
}

src/Libraries/SmartStore.Data/SmartStore.Data.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,9 @@
606606
<Compile Include="Migrations\201807191020207_OrderItemDeliveryTime.cs" />
607607
<Compile Include="Migrations\201807191020207_OrderItemDeliveryTime.Designer.cs">
608608
<DependentUpon>201807191020207_OrderItemDeliveryTime.cs</DependentUpon>
609+
<Compile Include="Migrations\201807201157391_DownloadVersions.cs" />
610+
<Compile Include="Migrations\201807201157391_DownloadVersions.Designer.cs">
611+
<DependentUpon>201807201157391_DownloadVersions.cs</DependentUpon>
609612
</Compile>
610613
<Compile Include="ObjectContextBase.SaveChanges.cs" />
611614
<Compile Include="Setup\Builder\ActivityLogTypeMigrator.cs" />
@@ -1091,6 +1094,8 @@
10911094
</EmbeddedResource>
10921095
<EmbeddedResource Include="Migrations\201807191020207_OrderItemDeliveryTime.resx">
10931096
<DependentUpon>201807191020207_OrderItemDeliveryTime.cs</DependentUpon>
1097+
<EmbeddedResource Include="Migrations\201807201157391_DownloadVersions.resx">
1098+
<DependentUpon>201807201157391_DownloadVersions.cs</DependentUpon>
10941099
</EmbeddedResource>
10951100
<EmbeddedResource Include="Sql\Indexes.sql" />
10961101
<EmbeddedResource Include="Sql\StoredProcedures.sql" />

src/Libraries/SmartStore.Data/Utilities/DataMigrator.cs

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,75 @@
1818
using System.Text.RegularExpressions;
1919
using SmartStore.Core.Domain.Common;
2020
using SmartStore.Core.Domain.Customers;
21+
using SmartStore.Core.Domain.Media;
22+
using System.Data.Entity.Migrations;
2123

2224
namespace SmartStore.Data.Utilities
2325
{
2426
public static class DataMigrator
2527
{
26-
#region Product.MainPicture
27-
28-
/// <summary>
29-
/// Fixes 'MainPictureId' property of a single product entity
30-
/// </summary>
31-
/// <param name="context">Database context (must be <see cref="SmartObjectContext"/>)</param>
32-
/// <param name="entities">When <c>null</c>, Product.ProductPictures gets called.</param>
33-
/// <param name="product">Product to fix</param>
34-
/// <returns><c>true</c> when value was fixed</returns>
35-
public static bool FixProductMainPictureId(IDbContext context, Product product, IEnumerable<ProductPicture> entities = null)
28+
#region Download.ProductId
29+
30+
/// <summary>
31+
/// Sets EntityId & EntityName for download table
32+
/// </summary>
33+
/// <param name="context"></param>
34+
/// <returns></returns>
35+
public static int SetDownloadProductId(IDbContext context)
36+
{
37+
var ctx = context as SmartObjectContext;
38+
if (ctx == null)
39+
throw new ArgumentException("Passed context must be an instance of type '{0}'.".FormatInvariant(typeof(SmartObjectContext)), nameof(context));
40+
41+
#pragma warning disable 612, 618
42+
// Get all products with a download
43+
var productQuery = from p in ctx.Set<Product>().AsNoTracking()
44+
where (p.DownloadId != 0)
45+
orderby p.Id
46+
select new { p.Id, p.DownloadId };
47+
#pragma warning restore 612, 618
48+
49+
var downloads = context.Set<Download>().Select(x => x).ToDictionary(x => x.Id);
50+
51+
int pageIndex = -1;
52+
while (true)
53+
{
54+
var products = PagedList.Create(productQuery, ++pageIndex, 100);
55+
56+
foreach (var p in products)
57+
{
58+
try
59+
{
60+
if (downloads.TryGetValue(p.DownloadId, out var download))
61+
{
62+
download.EntityId = p.Id;
63+
download.EntityName = "Product";
64+
}
65+
}
66+
catch { }
67+
}
68+
69+
context.SaveChanges();
70+
71+
if (!products.HasNextPage)
72+
break;
73+
}
74+
75+
return 0;
76+
}
77+
78+
#endregion
79+
80+
#region Product.MainPicture
81+
82+
/// <summary>
83+
/// Fixes 'MainPictureId' property of a single product entity
84+
/// </summary>
85+
/// <param name="context">Database context (must be <see cref="SmartObjectContext"/>)</param>
86+
/// <param name="entities">When <c>null</c>, Product.ProductPictures gets called.</param>
87+
/// <param name="product">Product to fix</param>
88+
/// <returns><c>true</c> when value was fixed</returns>
89+
public static bool FixProductMainPictureId(IDbContext context, Product product, IEnumerable<ProductPicture> entities = null)
3690
{
3791
Guard.NotNull(product, nameof(product));
3892

src/Libraries/SmartStore.Services/Catalog/CopyProductService.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public virtual Product CopyProduct(
105105
{
106106
if (product.IsDownload)
107107
{
108-
downloadId = CopyDownload(product.DownloadId)?.Id;
108+
downloadId = CopyDownload(product.Id, "Product")?.Id;
109109
}
110110

111111
if (product.HasSampleDownload)
@@ -145,8 +145,8 @@ public virtual Product CopyProduct(
145145
RequiredProductIds = product.RequiredProductIds,
146146
AutomaticallyAddRequiredProducts = product.AutomaticallyAddRequiredProducts,
147147
IsDownload = product.IsDownload,
148-
DownloadId = downloadId ?? 0,
149-
UnlimitedDownloads = product.UnlimitedDownloads,
148+
//DownloadId = downloadId ?? 0,
149+
//UnlimitedDownloads = product.UnlimitedDownloads,
150150
MaxNumberOfDownloads = product.MaxNumberOfDownloads,
151151
DownloadExpirationDays = product.DownloadExpirationDays,
152152
DownloadActivationType = product.DownloadActivationType,
@@ -364,9 +364,11 @@ private void Commit()
364364
_services.DbContext.SaveChanges();
365365
}
366366

367-
private Download CopyDownload(int downloadId)
367+
private Download CopyDownload(int productId, string entityName = "")
368368
{
369-
var download = _downloadService.GetDownloadById(downloadId);
369+
var download = _downloadService.GetDownloadsByEntityId(productId, entityName)
370+
.OrderByDescending(x => x.FileVersion)
371+
.FirstOrDefault();
370372

371373
if (download == null)
372374
{

src/Libraries/SmartStore.Services/Catalog/Importer/ProductImporter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ protected virtual int ProcessProducts(
354354
row.SetProperty(context.Result, (x) => x.RequiredProductIds); // TODO: global scope
355355
row.SetProperty(context.Result, (x) => x.AutomaticallyAddRequiredProducts);
356356
row.SetProperty(context.Result, (x) => x.IsDownload);
357-
row.SetProperty(context.Result, (x) => x.DownloadId);
358-
row.SetProperty(context.Result, (x) => x.UnlimitedDownloads, true);
357+
//row.SetProperty(context.Result, (x) => x.DownloadId);
358+
//row.SetProperty(context.Result, (x) => x.UnlimitedDownloads, true);
359359
row.SetProperty(context.Result, (x) => x.MaxNumberOfDownloads, 10);
360360
row.SetProperty(context.Result, (x) => x.DownloadExpirationDays);
361361
row.SetProperty(context.Result, (x) => x.DownloadActivationTypeId, 1);

0 commit comments

Comments
 (0)