Skip to content
This repository has been archived by the owner on Oct 11, 2019. It is now read-only.

Export/dev VP-255 use export in do export in pricing #521

Open
wants to merge 22 commits into
base: export/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,51 @@ namespace VirtoCommerce.ExportModule.Data.Extensions
{
public static class ExportedTypeMetadataExtensions
{
private class ExportTypePropertyInfoEx
/// <summary>
/// Returns metadata for type plain (all except: Entity, IEnumerable &lt;Entity&gt;) properties.
/// </summary>
/// <param name="type">Type for getting metadata.</param>
/// <returns>Metadata for the T type, including all non-reference properties of type.</returns>
public static ExportedTypeMetadata GetPropertyNames(this Type type)
{
public ExportedTypePropertyInfo ExportedPropertyInfo { get; set; }
public bool IsReference { get; set; }
public PropertyInfo PropertyInfo { get; set; }
var result = new ExportedTypeMetadata
{
PropertyInfos = type.GetPropertyNames(type.Name).ToArray()
};

return result;
}

/// <summary>
/// Returns metadata about exportable entity type.
/// Returns metadata for type nested properties.
/// </summary>
/// <typeparam name="T">Type for getting metadata.</typeparam>
/// <param name="propertyPathsToInclude">Property full paths to include to metadata</param>
/// <returns>Metadata for the T type, including all non-reference properties of types: T and corresponding to the passed properties.</returns>
public static ExportedTypeMetadata GetPropertyNames(this Type type, params string[] propertyPathsToInclude)
/// <param name="type">Type for getting metadata.</param>
/// <param name="propertyPaths">Property paths, e.g. PropertyB.PropertyC </param>
/// <returns>Metadata with the nested property's property paths, e.g. [{ FullName: Id, Group : 'PropertyB.PropertyC' }, ...] </returns>
public static ExportedTypeMetadata GetNestedPropertyNames(this Type type, params string[] propertyPaths)
{
var result = new ExportedTypeMetadata
{
PropertyInfos = GetPropertyNames(type, type.Name, string.Empty, propertyPathsToInclude)
.Where(x => !x.IsReference)
.Select(x => x.ExportedPropertyInfo)
.ToArray()
PropertyInfos = propertyPaths.SelectMany(x =>
type.GetPropertyType(x).GetPropertyNames(x))
.ToArray()
};

return result;
}

private static Type GetPropertyType(Type type, string propertyName)
private static Type GetPropertyType(this Type type, string propertyPath)
{
return GetPropertyType(type, propertyName.Split('.'));
return type.GetPropertyType(propertyPath.Split('.'));
}

private static Type GetPropertyType(Type type, IEnumerable<string> propertyNames)
private static Type GetPropertyType(this Type type, IEnumerable<string> propertyNames)
{
Type result;
var nestedType = GetNestedType(type);
if (propertyNames.Any())
{
result = GetPropertyType(nestedType.GetProperty(propertyNames.First()).PropertyType, propertyNames.Skip(1));
result = nestedType.GetProperty(propertyNames.First()).PropertyType.GetPropertyType(propertyNames.Skip(1));
}
else
{
Expand All @@ -55,47 +62,33 @@ private static Type GetPropertyType(Type type, IEnumerable<string> propertyNames
return result;
}

private static ExportTypePropertyInfoEx[] GetPropertyNames(Type type, string groupName, string baseMemberName, string[] propertyPathsToInclude)
private static ExportedTypePropertyInfo[] GetPropertyNames(this Type type, string groupName)
{
var result = new List<ExportTypePropertyInfoEx>();
var result = new List<ExportedTypePropertyInfo>();
var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(x => x.CanRead && x.CanWrite);

foreach (var propertyInfo in properties)
{
var nestedType = GetNestedType(propertyInfo.PropertyType);
var isNested = nestedType.IsSubclassOf(typeof(Entity));
var memberName = propertyInfo.GetDerivedName(baseMemberName);
var memberName = propertyInfo.Name;

if (!isNested)
{
result.Add(new ExportTypePropertyInfoEx()
result.Add(new ExportedTypePropertyInfo()
{
ExportedPropertyInfo = new ExportedTypePropertyInfo
{
FullName = memberName,
DisplayName = memberName,
Group = groupName,
},
PropertyInfo = propertyInfo,
IsReference = isNested,
FullName = memberName,
DisplayName = memberName,
Group = groupName,
});
}
}
//Continue searching for members in every property path
foreach (var propertyPathToInclude in propertyPathsToInclude)
{
result.AddRange(GetPropertyNames(
GetPropertyType(type, propertyPathToInclude),
string.Format($@"{groupName}.{propertyPathToInclude}"),
propertyPathToInclude,
new string[] { }));
}

return result.ToArray();
}

/// <summary>
/// Adds baseName as a prefixe to the property name (i.e. "{baseName}.{Name}")
/// Adds baseName as a prefix to the property name (i.e. "{baseName}.{Name}")
/// </summary>
/// <param name="pi"></param>
/// <param name="baseName"></param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void Export(Stream stream, ExportDataRequest request, Action<ExportProgre

var objectBatch = pagedDataSource.FetchNextPage();

if (objectBatch == null)
if (objectBatch.IsNullOrEmpty())
{
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,10 @@ namespace VirtoCommerce.ExportModule.Tests
public class ExportedTypeMetadataTests
{
[Fact]
public Task GetFromType_Pricelist_NameBuiltCorrectly()
public Task GetPropertyNames_Plain_BuiltCorrectly()
{
var metadata = typeof(Pricelist).GetPropertyNames(nameof(Pricelist.Prices), nameof(Pricelist.Assignments));
var props = metadata.PropertyInfos.Select(x => x.FullName);

// Check if all own property detected
Assert.Contains("Name", props);
Assert.Contains("Description", props);
Assert.Contains("Currency", props);
Assert.Contains("CreatedDate", props);
Assert.Contains("ModifiedDate", props);
Assert.Contains("CreatedBy", props);
Assert.Contains("ModifiedBy", props);
Assert.Contains("Id", props);

// Reference properties
Assert.Contains("Prices.Id", props);
Assert.Contains("Prices.PricelistId", props);
Assert.Contains("Assignments.Id", props);
Assert.Contains("Assignments.PricelistId", props);

// Check it doesn't contains recursive links
Assert.DoesNotContain("Prices.Pricelist", props);
Assert.DoesNotContain("Prices.Pricelist.Id", props);
Assert.DoesNotContain("Assignments.Pricelist", props);
Assert.DoesNotContain("Assignments", props);

return Task.CompletedTask;
}

[Fact]
public Task GetFromType_Pricelist_ExportNameBuiltCorrectly()
{
var metadata = typeof(Pricelist).GetPropertyNames(nameof(Pricelist.Prices), nameof(Pricelist.Assignments));
var props = metadata.PropertyInfos.Select(x => x.DisplayName);
var metadata = typeof(Pricelist).GetPropertyNames();
var props = metadata.PropertyInfos.Select(x => x.FullName).ToArray();

// Check if all own property detected
Assert.Contains("Name", props);
Expand All @@ -54,44 +23,32 @@ public Task GetFromType_Pricelist_ExportNameBuiltCorrectly()
Assert.Contains("ModifiedBy", props);
Assert.Contains("Id", props);

// Reference properties
Assert.Contains("Prices.Id", props);
Assert.Contains("Prices.PricelistId", props);
Assert.Contains("Assignments.Id", props);
Assert.Contains("Assignments.PricelistId", props);

// Check it doesn't contains recursive links
Assert.DoesNotContain("Prices.Pricelist", props);
Assert.DoesNotContain("Prices.Pricelist.Id", props);
Assert.DoesNotContain("Assignments.Pricelist", props);
Assert.DoesNotContain("Assignments", props);
Assert.Equal(8, props.Length);

return Task.CompletedTask;
}


[Fact]
public Task GetFromType_Pricelist_WithoutReferences_BuiltCorrectly()
public Task GetNestedPropertyNames_BuiltCorrectly()
{
var metadata = typeof(Pricelist).GetPropertyNames();
var props = metadata.PropertyInfos.Select(x => x.FullName);
var metadata = typeof(Pricelist).GetNestedPropertyNames(nameof(Pricelist.Prices));
var props = metadata.PropertyInfos.Select(x => x.FullName).ToArray();

// Check if all own property detected
Assert.Contains("Name", props);
Assert.Contains("Description", props);
Assert.Contains("PricelistId", props);
Assert.Contains("Currency", props);
Assert.Contains("ProductId", props);
Assert.Contains("Sale", props);
Assert.Contains("List", props);
Assert.Contains("MinQuantity", props);
Assert.Contains("StartDate", props);
Assert.Contains("EndDate", props);
Assert.Contains("CreatedDate", props);
Assert.Contains("ModifiedDate", props);
Assert.Contains("CreatedBy", props);
Assert.Contains("ModifiedBy", props);
Assert.Contains("Id", props);

// And does not contain nested properties
Assert.DoesNotContain("Prices", props);
Assert.DoesNotContain("Prices.Id", props);
Assert.DoesNotContain("Prices.PricelistId", props);
Assert.DoesNotContain("Assignments.Id", props);
Assert.DoesNotContain("Assignments.PricelistId", props);
Assert.Equal(13, props.Length);

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.Linq;
using VirtoCommerce.ExportModule.Core.Model;
using VirtoCommerce.PricingModule.Core.Model;

namespace VirtoCommerce.PricingModule.Data.ExportImport
{
public class ExportablePricingFull : IExportable
{
public string Name { get; set; }
public string Code { get; set; }
public string ImageUrl { get; set; }
public string Parent { get; set; }
public string Type { get; set; }

public ICollection<Pricelist> Pricelists { get; set; }
public ICollection<Price> Prices { get; set; }
public ICollection<PricelistAssignment> Assignments { get; set; }


public object Clone()
{
var result = MemberwiseClone() as ExportablePricingFull;
result.Pricelists = Pricelists?.Select(x => x.Clone() as Pricelist).ToList();
result.Prices = Prices?.Select(x => x.Clone() as Price).ToList();
result.Assignments = Assignments?.Select(x => x.Clone() as PricelistAssignment).ToList();

return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using VirtoCommerce.ExportModule.Core.Model;

namespace VirtoCommerce.PricingModule.Data.ExportImport
{
public class PricingFullExportDataQuery : ExportDataQuery
{

}
}
Loading