-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductManager.cs
More file actions
520 lines (464 loc) · 25 KB
/
ProductManager.cs
File metadata and controls
520 lines (464 loc) · 25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
using Dynamicweb.Caching;
using Dynamicweb.Configuration;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Cache;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Connectors;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Extensions;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Logging;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.XmlGenerators;
using Dynamicweb.Ecommerce.Prices;
using Dynamicweb.Ecommerce.Products;
using Dynamicweb.Extensibility.AddIns;
using Dynamicweb.Security.UserManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Xml;
namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Products
{
/// <summary>
/// Static manager to handle product info requests.
/// </summary>
public static class ProductManager
{
private static readonly string ProductProviderCacheKey = $"{Constants.AssemblyVersion}DynamicwebLiveIntegrationProductProvider";
/// <summary>
/// Gets the product provider. Implement your own class deriving ProductProviderBase if you want to override the built-in behavior.
/// </summary>
/// <value>The product provider.</value>
internal static ProductProviderBase ProductProvider
{
get
{
ProductProviderBase provider;
try
{
Caching.Cache.Current.TryGet(ProductProviderCacheKey, out provider);
}
catch
{
provider = new ProductProviderBase();
Caching.Cache.Current[ProductProviderCacheKey] = provider;
}
if (provider is null)
{
foreach (Type addIn in AddInManager.GetTypes(typeof(ProductProviderBase)))
{
if (!ReferenceEquals(addIn, typeof(ProductProviderBase)))
{
provider = (ProductProviderBase)AddInManager.GetInstance(addIn);
break;
}
}
if (provider is null)
{
provider = new ProductProviderBase();
}
Caching.Cache.Current[ProductProviderCacheKey] = provider;
}
return provider;
}
}
/// <summary>
/// Fills the product values from the response.
/// </summary>
/// <param name="product">The product.</param>
/// <param name="quantity">The quantity.</param>
[Obsolete("Use FillProductValues(Settings settings, Product product, double quantity, LiveContext context, string unitId)")]
public static void FillProductValues(Settings settings, Product product, double quantity, LiveContext context)
{
FillProductValues(settings, product, quantity, context, null);
}
/// <summary>
/// Fills the product values from the response.
/// </summary>
/// <param name="product">The product.</param>
/// <param name="quantity">The quantity.</param>
public static void FillProductValues(Settings settings, Product product, double quantity, LiveContext context, string unitId)
{
ProductInfo productInfo = GetProductInfo(product, settings, context.User, context, unitId);
if (productInfo != null)
{
Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.ProductManager.FillProductValues START");
ProductProvider.FillProductValues(productInfo, product, settings, quantity, context);
Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.ProductManager.FillProductValues END");
}
}
public static void FillProductFieldValues(Product product, ProductInfo productInfo)
{
ProductProvider.FillProductFieldValues(product, productInfo);
}
/// <summary>
/// Gets the product prices list from the response.
/// </summary>
/// <param name="productInfo">The product info.</param>
public static List<Price> GetPrices(ProductInfo productInfo)
{
List<Price> prices = new List<Price>();
if (productInfo != null)
{
prices = ProductProvider.GetPrices(productInfo);
}
return prices;
}
/// <summary>
/// Fetches the product info from the ERP.
/// </summary>
/// <param name="products">The products with quantities.</param>
/// <param name="user">The user.</param>
/// <param name="updateCache">Update cache with products information retrieved.</param>
/// <returns><c>true</c> if product information was fetched, <c>false</c> otherwise.</returns>
internal static bool FetchProductInfos(List<PriceProductSelection> products, LiveContext context, Settings settings, Logger logger, bool doCurrencyCheck, SubmitType submitType, bool updateCache = true)
{
if (products == null || products.Count == 0)
{
return false;
}
settings = settings ?? SettingsManager.GetSettingsByShop(context.Shop?.Id);
if (!settings.EnableLivePrices)
{
string cacheKey = "DynamicwebLiveIntegration.LivePricesDisabled";
if (!Caching.Cache.Current.Contains(cacheKey))
{
Caching.Cache.Current.Set(cacheKey, string.Empty, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddDays(1) });
logger.Log(ErrorLevel.DebugInfo, "Live Prices are Disabled");
}
return false;
}
if (!Connector.IsWebServiceConnectionAvailable(settings, logger, submitType))
{
logger.Log(ErrorLevel.DebugInfo, "Live Prices are unavailable");
return false;
}
if (context.User == null && !settings.LiveProductInfoForAnonymousUsers)
{
logger.Log(ErrorLevel.DebugInfo, "No user is currently logged in. Anonymous user is not allowed.");
return false;
}
if (context.User != null && context.User.IsLivePricesDisabled)
{
logger.Log(ErrorLevel.DebugInfo, $"Calculated prices are not allowed for the user '{context.User.UserName}'.");
return false;
}
var productCacheLevel = settings.GetProductCacheLevel();
// Check for existence of the given products in the Cache
var productsForRequest = GetProductsForRequest(settings, productCacheLevel, products, logger, context, doCurrencyCheck);
if (productsForRequest.Count == 0)
{
return true;
}
if (settings.MaxProductsPerRequest == 0)
{
// don't split requests
return FetchProductInfosInternal(productsForRequest, productCacheLevel, context, settings, logger, updateCache);
}
var allProductsSuccessfullyFetched = true;
var numberOfRequests = Core.Converter.ToInt32(Math.Ceiling((double)productsForRequest.Count / (double)settings.MaxProductsPerRequest));
for (var i = 0; i < numberOfRequests; i++)
{
var currentSetFetched = FetchProductInfosInternal(productsForRequest.OrderBy(p => p.Product.Id).Skip(i * settings.MaxProductsPerRequest).Take(settings.MaxProductsPerRequest).ToList(), productCacheLevel, context, settings, logger, updateCache);
allProductsSuccessfullyFetched = allProductsSuccessfullyFetched ? currentSetFetched : allProductsSuccessfullyFetched;
}
return allProductsSuccessfullyFetched;
}
private static bool FetchProductInfosInternal(List<PriceProductSelection> productsForRequest, ResponseCacheLevel productCacheLevel, LiveContext context, Settings settings, Logger logger, bool updateCache = true)
{
Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.ProductManager.FetchProductInfos START");
string request = BuildProductRequest(settings, productsForRequest, context, logger);
string requestHash = null;
XmlDocument response = null;
bool isResponseCached = false;
if (productCacheLevel == ResponseCacheLevel.Page)
{
requestHash = Helpers.CalculateHash(request);
isResponseCached = Caching.Cache.Current.TryGet(requestHash, out response);
}
HttpStatusCode httpStatusCode = HttpStatusCode.OK;
if (!isResponseCached)
{
response = Connector.GetProductsInfo(settings, request, logger, out httpStatusCode, settings.MakeRetryForLiveProductInformation);
}
bool result = true;
if (response != null && !string.IsNullOrEmpty(response.InnerXml))
{
if (productCacheLevel == ResponseCacheLevel.Page && !string.IsNullOrEmpty(requestHash))
{
SaveLastResponse(requestHash, response);
}
// Parse the response
Dictionary<string, ProductInfo> prices = ProcessResponse(settings, response, logger, context);
if (prices != null)
{
if (updateCache)
{
Dictionary<string, ProductInfo> cachedProductInfos = ResponseCache.GetProductInfos(productCacheLevel, context.User);
if (cachedProductInfos != null)
{
// Cache prices
foreach (string productKey in prices.Keys)
{
cachedProductInfos.Remove(productKey);
cachedProductInfos.Add(productKey, prices[productKey]);
}
// Cache empty values for products that were in the request but were not returned in the response
foreach (var priceProductSelection in productsForRequest)
{
var productIdentifier = ProductProvider.GetProductIdentifier(settings, priceProductSelection.Product, priceProductSelection.UnitId);
cachedProductInfos.TryAdd(productIdentifier, null);
}
}
}
}
else
{
result = false;
if (httpStatusCode == HttpStatusCode.InternalServerError && productCacheLevel == ResponseCacheLevel.Page && !string.IsNullOrEmpty(requestHash))
{
SaveLastResponse(requestHash, new XmlDocument());
}
}
}
else
{
// error occurred
result = false;
if (httpStatusCode == HttpStatusCode.InternalServerError && productCacheLevel == ResponseCacheLevel.Page && !string.IsNullOrEmpty(requestHash))
{
SaveLastResponse(requestHash, new XmlDocument());
}
}
Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.ProductManager.FetchProductInfos END");
return result;
}
/// <summary>
/// Builds the product request.
/// </summary>
/// <param name="products">The products.</param>
/// <returns>System.String.</returns>
private static string BuildProductRequest(Settings settings, List<PriceProductSelection> products, LiveContext context, Logger logger)
{
var xmlGenerator = new ProductInfoXmlGenerator();
var xmlGeneratorSettings = new ProductInfoXmlGeneratorSettings
{
AddProductFieldsToRequest = settings.AddProductFieldsToRequest,
GetUnitPrices = settings.UseUnitPrices,
LiveIntegrationSubmitType = SubmitType.Live,
ReferenceName = "ProductInfoLive",
Context = context
};
var xml = xmlGenerator.GenerateProductInfoXml(
settings,
products,
xmlGeneratorSettings, logger);
return xml;
}
private static List<PriceProductSelection> GetProductsForRequest(Settings settings, ResponseCacheLevel productCacheLevel, List<PriceProductSelection> products, Logger logger, LiveContext context, bool doCurrencyCheck)
{
// Check for existence of the given products in the Cache
List<PriceProductSelection> productsForRequest = new List<PriceProductSelection>();
bool getProductInformationForAllVariants = settings.GetProductInformationForAllVariants;
bool showVariantDefault = SystemConfiguration.Instance.GetBoolean("/Globalsettings/Ecom/Product/ShowVariantDefault");
foreach (var productWithQuantity in products)
{
Product product = productWithQuantity.Product;
string productIdentifier = ProductProvider.GetProductIdentifier(settings, productWithQuantity.Product, productWithQuantity.UnitId);
bool isProductCached = ResponseCache.IsProductInCache(productCacheLevel, productIdentifier, context.User,
doCurrencyCheck ? context.Currency : null);
if (!isProductCached)
{
// setting to request all variants in request
if (getProductInformationForAllVariants)
{
var variants = Services.VariantCombinations.GetVariantCombinations(product.Id)?.Select(vc => Services.Products.GetProductById(vc.ProductId, vc.VariantId, false)).ToList();
if (variants != null)
{
variants = GetFilteredVariants(variants);
foreach (var variant in variants)
{
var variantProductIdentifier = ProductProvider.GetProductIdentifier(settings, variant, variant.DefaultUnitId);
var isVariantProductCached = ResponseCache.IsProductInCache(productCacheLevel, variantProductIdentifier, context.User,
doCurrencyCheck ? context.Currency : null);
if (!isVariantProductCached)
{
var isVariantProductAdded = productsForRequest.Any(p => p.Product == variant);
var isVariantProductLivePriceEnabled = ProductProvider.IsLivePriceEnabledForProduct(variant);
if (!isVariantProductAdded && isVariantProductLivePriceEnabled && variant.HasIdentifier(settings))
{
productsForRequest.Add(variant.GetPriceProductSelection(productWithQuantity.Quantity, null));
}
}
}
}
}
if (showVariantDefault)
{
product = ProductProvider.GetProductFromVariantComboId(product, logger);
}
if (string.IsNullOrEmpty(product.VariantId) || GetFilteredVariants(new List<Product>() { product }).Count != 0)
{
productIdentifier = ProductProvider.GetProductIdentifier(settings, product, productWithQuantity.UnitId);
isProductCached = ResponseCache.IsProductInCache(productCacheLevel, productIdentifier, context.User,
doCurrencyCheck ? context.Currency : null);
if (!isProductCached)
{
bool isProductAdded = productsForRequest.Any(p => p.Product == product);
bool isProductLivePriceEnabled = ProductProvider.IsLivePriceEnabledForProduct(product);
if (!isProductAdded && isProductLivePriceEnabled && product.HasIdentifier(settings))
{
productsForRequest.Add(product.GetPriceProductSelection(productWithQuantity.Quantity, productWithQuantity.UnitId));
}
}
}
}
}
return productsForRequest;
}
private static List<Product> GetFilteredVariants(List<Product> variants)
{
bool hideInactiveVariants = SystemConfiguration.Instance.GetBoolean("/Globalsettings/Ecom/Product/DontAllowLinksToVariantIfNotActive");
bool hideInactiveProducts = SystemConfiguration.Instance.GetBoolean("/Globalsettings/Ecom/Product/DontAllowLinksToProductIfNotActive");
if (hideInactiveVariants || hideInactiveProducts)
{
bool inactiveVariantsNotSet = string.IsNullOrEmpty(SystemConfiguration.Instance.GetValue("/Globalsettings/Ecom/Product/DontAllowLinksToVariantIfNotActive"));
List<Product> result = new List<Product>();
foreach (var variant in variants)
{
if (!variant.Active && (hideInactiveVariants || inactiveVariantsNotSet && hideInactiveProducts))
{
continue;
}
result.Add(variant);
}
return result;
}
else
{
return variants;
}
}
/// <summary>
/// Processes the response with product info.
/// </summary>
/// <param name="response">The response.</param>
/// <returns>Dictionary<System.String, ProductInfo>.</returns>
private static Dictionary<string, ProductInfo> ProcessResponse(Settings settings, XmlDocument response, Logger logger, LiveContext context)
{
if (response == null)
{
return null;
}
try
{
Dictionary<string, ProductInfo> infos = new Dictionary<string, ProductInfo>();
string priceLookupKey(string productId, string variantId) => string.Join("|", productId, variantId);
ILookup<string, ProductPrice> pricesLookup = ProductProvider.ExtractPrices(settings, response, logger).ToLookup(p => priceLookupKey(p.ProductId, p.ProductVariantId), StringComparer.OrdinalIgnoreCase);
var items = response.SelectNodes("//item [@table='EcomProducts']");
if (items != null)
{
foreach (XmlNode item in items)
{
string productId = item.SelectSingleNode("column [@columnName='ProductId']")?.InnerText ?? string.Empty;
string variantId = item.SelectSingleNode("column [@columnName='ProductVariantId']")?.InnerText ?? string.Empty;
string productIdentifier = item.SelectSingleNode("column [@columnName='ProductIdentifier']")?.InnerText ?? string.Empty;
string productPrice = item.SelectSingleNode("column [@columnName='ProductPrice']")?.InnerText ?? string.Empty;
string productPriceWithVat = item.SelectSingleNode("column [@columnName='ProductPriceWithVat']")?.InnerText ?? string.Empty;
ProductInfo productInfo = new ProductInfo
{
// Set ProductId
["ProductId"] = productIdentifier,
// Set price
["TotalPrice"] = !string.IsNullOrEmpty(productPrice) ? Helpers.ToDouble(settings, logger, productPrice) : (double?)null,
["TotalPriceWithVat"] = !string.IsNullOrEmpty(productPriceWithVat) ? Helpers.ToDouble(settings, logger, productPriceWithVat) : (double?)null,
["CurrencyCode"] = context?.Currency?.Code
};
// Set stock
var stock = item.SelectSingleNode("column [@columnName='ProductStock']")?.InnerText;
if (stock != null)
{
productInfo["Stock"] = Helpers.ToDouble(settings, logger, stock);
}
else
{
productInfo["Stock"] = null;
}
// Set prices
productInfo["Prices"] = pricesLookup[priceLookupKey(productId, variantId)].ToList();
// Set ProductCustomFields
if (settings.AddProductFieldsToRequest)
{
foreach (ProductField pf in ProductField.GetProductFields())
{
var fieldNode = item.SelectSingleNode($"column [@columnName='{pf.SystemName}']");
if (fieldNode != null)
{
productInfo[pf.SystemName] = fieldNode.InnerText;
}
}
}
if (settings.UseUnitPrices)
{
productInfo["ProductDefaultUnitId"] = item.SelectSingleNode("column [@columnName='ProductDefaultUnitId']")?.InnerText;
}
Dynamicweb.Extensibility.Notifications.NotificationManager.Notify(Notifications.ProductInfo.OnAfterProductInfoProcessResponse,
new Notifications.ProductInfo.OnAfterProductInfoProcessResponseArgs(productInfo, response, settings, logger));
// avoid exception to duplicate products in XML
infos.TryAdd(productIdentifier, productInfo);
}
}
return infos;
}
catch (Exception e)
{
logger.Log(ErrorLevel.Error, $"Response does not match schema: '{e.Message}'.");
}
return null;
}
/// <summary>
/// Saves the last response to cache for 30 seconds.
/// </summary>
private static void SaveLastResponse(string requestHash, XmlDocument response)
{
Caching.Cache.Current.Set(requestHash, response, new CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.AddSeconds(30) });
}
/// <summary>
/// Gets the ProductInfo from the ERP
/// </summary>
/// <param name="product">The product</param>
/// <returns></returns>
public static ProductInfo GetProductInfo(Product product, Settings settings, User user)
{
return GetProductInfo(product, settings, user, null, settings.UseUnitPrices ? product.DefaultUnitId : null);
}
/// <summary>
/// Gets the ProductInfo from the ERP
/// </summary>
/// <param name="product">The product</param>
/// <returns></returns>
[Obsolete("Use GetProductInfo(Product product, Settings settings, User user, LiveContext context, string unitId)")]
public static ProductInfo GetProductInfo(Product product, Settings settings, User user, LiveContext context)
{
return GetProductInfo(product, settings, user, context, null);
}
/// <summary>
/// Gets the ProductInfo from the ERP
/// </summary>
/// <param name="product">The product</param>
/// <returns></returns>
public static ProductInfo GetProductInfo(Product product, Settings settings, User user, LiveContext context, string unitId)
{
ProductInfo productInfo = null;
if (product != null)
{
string productIdentifier = ProductProvider.GetProductIdentifier(settings, product, unitId);
var productCacheLevel = settings.GetProductCacheLevel();
if (ResponseCache.IsProductInCache(productCacheLevel, productIdentifier, user, context?.Currency))
{
Dictionary<string, ProductInfo> productInfoCache = ResponseCache.GetProductInfos(productCacheLevel, user);
productInfoCache?.TryGetValue(productIdentifier, out productInfo);
}
}
return productInfo;
}
}
}