/
PublishedContentProductInformationExtractor.cs
287 lines (226 loc) · 11.9 KB
/
PublishedContentProductInformationExtractor.cs
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
using System;
using System.Globalization;
using System.Linq;
using TeaCommerce.Api.Common;
using TeaCommerce.Api.InformationExtractors;
using TeaCommerce.Api.Models;
using TeaCommerce.Api.Services;
using TeaCommerce.Umbraco.Configuration.Services;
using TeaCommerce.Umbraco.Configuration.Variants.Models;
using TeaCommerce.Umbraco.Configuration.Variants.Services;
using umbraco;
using Umbraco.Core.Models;
using Umbraco.Web;
using Constants = TeaCommerce.Api.Constants;
namespace TeaCommerce.Umbraco.Configuration.InformationExtractors {
public class PublishedContentProductInformationExtractor : IProductInformationExtractor, IProductInformationExtractor<IPublishedContent, VariantPublishedContent>, IPublishedContentProductInformationExtractor, IProductInformationExtractor<IPublishedContent> {
private UmbracoHelper _umbracoHelper;
protected UmbracoHelper UmbracoHelper {
get {
return _umbracoHelper = _umbracoHelper ?? new UmbracoHelper( UmbracoContext.Current );
}
}
protected readonly IStoreService StoreService;
protected readonly ICurrencyService CurrencyService;
protected readonly IVatGroupService VatGroupService;
protected readonly IVariantService<IPublishedContent, VariantPublishedContent> VariantService;
public PublishedContentProductInformationExtractor( IStoreService storeService, ICurrencyService currencyService, IVatGroupService vatGroupService, IVariantService<IPublishedContent, VariantPublishedContent> variantService ) {
StoreService = storeService;
CurrencyService = currencyService;
VatGroupService = vatGroupService;
VariantService = variantService;
}
#region IProductInformationExtractor
public virtual long GetStoreId( string productIdentifier ) {
ProductIdentifier productIdentifierObj = new ProductIdentifier( productIdentifier );
return GetStoreId( UmbracoHelper.TypedContent( productIdentifierObj.NodeId ) );
}
public virtual string GetSku( string productIdentifier ) {
ProductIdentifier productIdentifierObj = new ProductIdentifier( productIdentifier );
IPublishedContent content = UmbracoHelper.TypedContent( productIdentifierObj.NodeId );
long storeId = GetStoreId( content );
VariantPublishedContent variant = VariantService.GetVariant( storeId, content, productIdentifierObj.VariantId );
return GetSku( content, variant );
}
public virtual long? GetVatGroupId( string productIdentifier ) {
ProductIdentifier productIdentifierObj = new ProductIdentifier( productIdentifier );
IPublishedContent content = UmbracoHelper.TypedContent( productIdentifierObj.NodeId );
long storeId = GetStoreId( content );
VariantPublishedContent variant = VariantService.GetVariant( storeId, content, productIdentifierObj.VariantId );
return GetVatGroupId( content, variant );
}
public virtual OriginalUnitPriceCollection GetOriginalUnitPrices( string productIdentifier ) {
ProductIdentifier productIdentifierObj = new ProductIdentifier( productIdentifier );
IPublishedContent content = UmbracoHelper.TypedContent( productIdentifierObj.NodeId );
long storeId = GetStoreId( content );
VariantPublishedContent variant = VariantService.GetVariant( storeId, content, productIdentifierObj.VariantId );
return GetOriginalUnitPrices( content, variant );
}
public virtual ProductSnapshot GetSnapshot( string productIdentifier ) {
ProductIdentifier productIdentifierObj = new ProductIdentifier( productIdentifier );
IPublishedContent content = UmbracoHelper.TypedContent( productIdentifierObj.NodeId );
long storeId = GetStoreId( content );
VariantPublishedContent variant = VariantService.GetVariant( storeId, content, productIdentifierObj.VariantId );
ProductSnapshot snapshot = new ProductSnapshot( storeId, productIdentifier ) {
Sku = GetSku( content, variant ),
Name = GetName( content, variant ),
VatGroupId = GetVatGroupId( content, variant ),
LanguageId = GetLanguageId( content ),
OriginalUnitPrices = GetOriginalUnitPrices( content, variant ),
Properties = GetProperties( content, variant )
};
return snapshot;
}
public virtual bool HasAccess( long storeId, string productIdentifier ) {
ProductIdentifier productIdentifierObj = new ProductIdentifier( productIdentifier );
IPublishedContent content = UmbracoHelper.TypedContent( productIdentifierObj.NodeId );
return storeId == GetStoreId( content ) && library.HasAccess( content.Id, content.Path );
}
#endregion
#region IProductInformationExtractor<IPublishedContent, VariantPublishedContent>
public virtual long GetStoreId( IPublishedContent product, VariantPublishedContent variant ) {
long? storeId = GetPropertyValue<long?>( product, Constants.ProductPropertyAliases.StorePropertyAlias );
if ( storeId == null ) {
throw new ArgumentException( "The product doesn't have a store id associated with it - remember to add the Tea Commerce store picker to your Umbraco content tree" );
}
return storeId.Value;
}
public virtual string GetSku( IPublishedContent product, VariantPublishedContent variant = null ) {
string sku = GetPropertyValue<string>( product, Constants.ProductPropertyAliases.SkuPropertyAlias, variant, recursive: variant == null );
//If no sku is found - default to umbraco node id
if ( string.IsNullOrEmpty( sku ) ) {
sku = product.Id.ToString( CultureInfo.InvariantCulture ) + ( variant != null ? "_" + variant.VariantIdentifier : "" );
}
return sku;
}
public virtual string GetName( IPublishedContent product, VariantPublishedContent variant = null ) {
string name = GetPropertyValue<string>( product, Constants.ProductPropertyAliases.NamePropertyAlias, variant, recursive: variant == null );
//If no name is found - default to the umbraco node name
if ( string.IsNullOrEmpty( name ) ) {
if ( variant != null ) {
name = GetPropertyValue<string>( product, Constants.ProductPropertyAliases.NamePropertyAlias );
}
if ( variant == null ) {
name = product.Name;
}
if ( variant != null ) {
name += " - " + variant.Name;
}
}
return name;
}
public virtual long? GetVatGroupId( IPublishedContent product, VariantPublishedContent variant = null ) {
long storeId = GetStoreId( product );
long? vatGroupId = GetPropertyValue<long?>( product, Constants.ProductPropertyAliases.VatGroupPropertyAlias, variant );
//In umbraco a product can have a relation to a delete marked vat group
if ( vatGroupId != null ) {
VatGroup vatGroup = VatGroupService.Get( storeId, vatGroupId.Value );
if ( vatGroup == null || vatGroup.IsDeleted ) {
vatGroupId = null;
}
}
return vatGroupId;
}
public virtual OriginalUnitPriceCollection GetOriginalUnitPrices( IPublishedContent product, VariantPublishedContent variant = null ) {
OriginalUnitPriceCollection prices = new OriginalUnitPriceCollection();
foreach ( Currency currency in CurrencyService.GetAll( GetStoreId( product ) ) ) {
prices.Add( new OriginalUnitPrice( GetPropertyValue<string>( product, currency.PricePropertyAlias, variant ).ParseToDecimal() ?? 0M, currency.Id ) );
}
return prices;
}
public virtual long? GetLanguageId( IPublishedContent product, VariantPublishedContent variant ) {
return LanguageService.Instance.GetLanguageIdByNodePath( product.Path );
}
public virtual CustomPropertyCollection GetProperties( IPublishedContent product, VariantPublishedContent variant = null ) {
CustomPropertyCollection properties = new CustomPropertyCollection();
foreach ( string productPropertyAlias in StoreService.Get( GetStoreId( product ) ).ProductSettings.ProductPropertyAliases ) {
properties.Add( new CustomProperty( productPropertyAlias, GetPropertyValue<string>( product, productPropertyAlias, variant ) ) { IsReadOnly = true } );
}
return properties;
}
public virtual string GetPropertyValue( IPublishedContent product, string propertyAlias, VariantPublishedContent variant = null ) {
return GetPropertyValue<string>( product, propertyAlias, variant );
}
#endregion
#region IProductInformationExtractor<IPublishedContent>
public long GetStoreId( IPublishedContent product ) {
return GetStoreId( product, null );
}
public string GetSku( IPublishedContent product ) {
return GetSku( product, null );
}
public string GetName( IPublishedContent product ) {
return GetName( product, null );
}
public long? GetVatGroupId( IPublishedContent product ) {
return GetVatGroupId( product, null );
}
public OriginalUnitPriceCollection GetOriginalUnitPrices( IPublishedContent product ) {
return GetOriginalUnitPrices( product, null );
}
public long? GetLanguageId( IPublishedContent product ) {
return GetLanguageId( product, null );
}
public CustomPropertyCollection GetProperties( IPublishedContent product ) {
return GetProperties( product, null );
}
public string GetPropertyValue( IPublishedContent product, string propertyAlias ) {
return GetPropertyValue( product, propertyAlias, null );
}
#endregion
public virtual T GetPropertyValue<T>( IPublishedContent product, string propertyAlias, VariantPublishedContent variant = null, Func<IPublishedContent, bool> func = null, bool recursive = true ) {
T rtnValue = default( T );
if ( product != null && !string.IsNullOrEmpty( propertyAlias ) ) {
if ( variant != null ) {
rtnValue = variant.GetPropertyValue<T>( propertyAlias );
}
if ( ( variant == null || recursive ) && CheckNullOrEmpty( rtnValue ) ) {
//Check if this node or ancestor has it
IPublishedContent currentNode = func != null ? product.AncestorsOrSelf().FirstOrDefault( func ) : product;
if ( currentNode != null ) {
rtnValue = GetPropertyValueInternal<T>( currentNode, propertyAlias, recursive && func == null );
}
//Check if we found the value
if ( CheckNullOrEmpty( rtnValue ) ) {
//Check if we can find a master relation
string masterRelationNodeId = GetPropertyValueInternal<string>( product, Constants.ProductPropertyAliases.MasterRelationPropertyAlias, recursive );
if ( !string.IsNullOrEmpty( masterRelationNodeId ) && UmbracoHelper != null ) {
rtnValue = GetPropertyValue<T>( UmbracoHelper.TypedContent( masterRelationNodeId ), propertyAlias,
variant, func );
}
}
}
}
return rtnValue;
}
protected virtual T GetPropertyValueInternal<T>( IPublishedContent product, string propertyAlias, bool recursive ) {
T rtnValue = default( T );
if ( product != null && !string.IsNullOrEmpty( propertyAlias ) ) {
if ( !recursive ) {
rtnValue = product.GetPropertyValue<T>( propertyAlias );
} else {
IPublishedContent tempproduct = product;
T tempProperty = tempproduct.GetPropertyValue<T>( propertyAlias );
if ( !CheckNullOrEmpty( tempProperty ) ) {
rtnValue = tempProperty;
}
while ( CheckNullOrEmpty( rtnValue ) && tempproduct != null && tempproduct.Id > 0 ) {
tempproduct = tempproduct.Parent;
if ( tempproduct != null ) {
tempProperty = tempproduct.GetPropertyValue<T>( propertyAlias );
if ( !CheckNullOrEmpty( tempProperty ) ) {
rtnValue = tempProperty;
}
}
}
}
}
return rtnValue;
}
protected bool CheckNullOrEmpty<T>( T value ) {
if ( typeof( T ) == typeof( string ) )
return string.IsNullOrEmpty( value as string );
return value == null || value.Equals( default( T ) );
}
}
}