Skip to content

Commit

Permalink
Merge pull request #435 from PHOENIXCONTACT/fix/type-changed-event
Browse files Browse the repository at this point in the history
Port PR #434 to moryx 8
  • Loading branch information
Toxantron authored Aug 5, 2024
2 parents 7684fcb + 5f0457e commit 3365357
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
25 changes: 15 additions & 10 deletions src/Moryx.AbstractionLayer.Products.Endpoints/ProductConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public IProductType ConvertProductBack(ProductModel source, ProductType converte
private void UpdateCollection(IList value, IEnumerable<PartModel> parts)
{
// Track which part links are still represented by the models
var unused = new List<IProductPartLink>(value.OfType<IProductPartLink>());
var oldParts = new List<IProductPartLink>(value.OfType<IProductPartLink>());
// Iterate over the part models
// Create or update the part links
var elemType = value.GetType().GetInterfaces()
Expand All @@ -269,22 +269,27 @@ private void UpdateCollection(IList value, IEnumerable<PartModel> parts)
if (partModel is null)
continue;

var match = unused.Find(r => r.Id == partModel?.Id);
if (match == null)
var oldPartMatch = oldParts.Find(r => r.Id == partModel.Id);
// new partlink
if (oldPartMatch == null)
{
match = (IProductPartLink)Activator.CreateInstance(elemType);
value.Add(match);
oldPartMatch = (IProductPartLink)Activator.CreateInstance(elemType);
oldPartMatch.Product = _productManagement.LoadType(partModel.Product.Id);
value.Add(oldPartMatch);
}
//modified reference
else if (oldPartMatch.Product.Id != partModel.Product.Id)
oldPartMatch.Product = _productManagement.LoadType(partModel.Product.Id);
else
unused.Remove(match);
// existing unchanged partlink: do not delete at the end
oldParts.Remove(oldPartMatch);

EntryConvert.UpdateInstance(match, partModel.Properties);
match.Product = _productManagement.LoadType(partModel.Product.Id);
EntryConvert.UpdateInstance(oldPartMatch, partModel.Properties);
}

// Clear all values no longer present in the model
foreach (var link in unused)
value.Remove(link);
foreach (var part in oldParts)
value.Remove(part);
}

private void UpdateReference(IProductPartLink value, PartModel part)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ public IProductType LoadType(ProductIdentity identity)
public long SaveType(IProductType modifiedInstance)
{
var saved = Storage.SaveType(modifiedInstance);
RaiseProductChanged(modifiedInstance);
//reload the object for correct references
var loadedType = Storage.LoadType(saved);
RaiseProductChanged(loadedType);

return saved;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ public void ForwardBackwardProductConversionWithoutInformationLoss(DummyProductT
// - If there are ProductPartLinks the ProductManagement should be called
var targetDummyTypeWithParts = recoveredOriginal as DummyProductTypeWithParts;
if (targetDummyTypeWithParts?.ProductPartLink?.Product is not null)
{
_productManagerMock.Verify(pm => pm.LoadType(targetDummyTypeWithParts.ProductPartLink.Product.Id));
_productManagerMock.Verify(pm => pm.LoadType(targetDummyTypeWithParts.ProductPartLinkEnumerable.First().Product.Id));
}
}

private static bool HasChangedProperties<T>(object A, object B)
Expand Down

0 comments on commit 3365357

Please sign in to comment.