Skip to content

Commit

Permalink
Fix order operation tree
Browse files Browse the repository at this point in the history
  • Loading branch information
tatarincev committed Jan 18, 2017
1 parent b941891 commit 10a7620
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion VirtoCommerce.OrderModule.Data/Model/OperationEntity.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -43,7 +45,7 @@ public virtual OrderOperation ToModel(OrderOperation orderOperation)

orderOperation.InjectFrom(this);

orderOperation.ChildrenOperations = orderOperation.GetFlatObjectsListWithInterface<IOperation>().Except(new[] { orderOperation }).ToList();
orderOperation.ChildrenOperations = GetAllChildOperations(orderOperation);
return orderOperation;
}

Expand Down Expand Up @@ -75,5 +77,36 @@ public virtual void Patch(OperationEntity target)
target.Sum = this.Sum;

}

private static IEnumerable<IOperation> GetAllChildOperations(IOperation operation)
{
var retVal = new List<IOperation>();
var objectType = operation.GetType();

var properties = objectType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

var childOperations = properties.Where(x => x.PropertyType.GetInterface(typeof(IOperation).Name) != null)
.Select(x => (IOperation)x.GetValue(operation)).Where(x=> x != null).ToList();

foreach (var childOperation in childOperations.OfType<IOperation>())
{
retVal.Add(childOperation);
}

//Handle collection and arrays
var collections = properties.Where(p => p.GetIndexParameters().Length == 0)
.Select(x => x.GetValue(operation, null))
.Where(x => x is IEnumerable && !(x is String))
.Cast<IEnumerable>();

foreach (var collection in collections)
{
foreach (var childOperation in collection.OfType<IOperation>())
{
retVal.Add(childOperation);
}
}
return retVal;
}
}
}

0 comments on commit 10a7620

Please sign in to comment.