Skip to content

Commit

Permalink
Merge pull request #221 from PHOENIXCONTACT/fix/get-instance-by-ident…
Browse files Browse the repository at this point in the history
…ifier

Fix GetInstance(IIdentity) resulting in an exception because of a wrong if clause
  • Loading branch information
Toxantron authored Jan 24, 2024
2 parents c9835a1 + 0c8c6de commit af9bf2d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/Moryx.AbstractionLayer/Products/IProductManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public interface IProductManagement : IRecipeProvider, IWorkplans
/// <summary>
/// Get an instance with this identity
/// </summary>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="identity"/> is null</exception>
/// <exception cref="InvalidOperationException">Thrown when there is more than one product with the given <paramref name="identity"/></exception>
ProductInstance GetInstance(IIdentity identity);

/// <summary>
Expand Down
18 changes: 13 additions & 5 deletions src/Moryx.Products.Management/Facades/ProductManagementFacade.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022, Phoenix Contact GmbH & Co. KG
// Copyright (c) 2024, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using System;
Expand All @@ -9,6 +9,7 @@
using Moryx.AbstractionLayer.Identity;
using Moryx.AbstractionLayer.Products;
using Moryx.AbstractionLayer.Recipes;
using Moryx.Logging;
using Moryx.Runtime.Modules;
using Moryx.Workflows;

Expand All @@ -31,6 +32,8 @@ internal class ProductManagementFacade : IFacadeControl, IWorkplansVersions, IPr

public ModuleConfig Config { get; set; }

public IModuleLogger Logger { get; set; }

#endregion

public void Activate()
Expand Down Expand Up @@ -219,10 +222,15 @@ public ProductInstance GetInstance(IIdentity identity)
if (identity == null)
throw new ArgumentNullException(nameof(identity));

var instance = ProductManager
.GetInstances<IIdentifiableObject>(i => identity.Equals(i.Identity))
.SingleOrDefault();
return (ProductInstance) instance;
var instances = ProductManager
.GetInstances<IIdentifiableObject>(i => identity.Equals(i.Identity));
if (instances.Count > 1)
{
Logger.Log(LogLevel.Error, "ProductManagement contains more than one {0} with the identity {1}.", nameof(ProductInstance), identity);
throw new InvalidOperationException();
}

return (ProductInstance) instances.SingleOrDefault(); ;
}

public TInstance GetInstance<TInstance>(Expression<Func<TInstance, bool>> selector)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Moryx.Model.Repositories;
using Moryx.Tools;
using static Moryx.Products.Management.ProductExpressionHelpers;
using Moryx.Logging;

namespace Moryx.Products.Management
{
Expand Down Expand Up @@ -88,6 +89,11 @@ internal class ProductStorage : IProductRemoveRecipeStorage, IConfiguredTypesPro
/// </summary>
public ModuleConfig Config { get; set; }

/// <summary>
/// Logger for the product manager module
/// </summary>
public IModuleLogger Logger { get; set; }

/// <summary>
/// Start the storage and load the type strategies
/// </summary>
Expand Down Expand Up @@ -773,6 +779,8 @@ private IReadOnlyList<TInstance> LoadInstancesByType<TInstance>(Expression<Func<
else
{
// TODO: Filter by type specific properties
Logger.Log(LogLevel.Warning, "You tried to load an instance filtering a property ({0}) of the custom type {1}. " +
"This is not supported yet and will always return a negative result.", typeProperty.Name, typeProperty.ReflectedType.Name);
var productType = typeProperty.ReflectedType;
instanceSelector = i => false;
}
Expand Down Expand Up @@ -881,6 +889,13 @@ private void TransformInstance(IUnitOfWork uow, ProductInstanceEntity entity, Pr
// Update all parts that are also present as entities
foreach (var partEntity in partEntityGroups[partGroup.Key.Name])
{
if (!partGroup.Value.Any())
{
Logger.Log(LogLevel.Warning, "No reconstruction of the property {1} possible. You have configured the {0} strategy, but the property was null." +
"Please initialize the property in the Initialize method or select the {2} strategy.",
nameof(PartSourceStrategy.FromPartlink), partGroup.Key.Name, nameof(PartSourceStrategy.FromEntities));
continue;
}
var part = partGroup.Value.First(p => p.PartLink.Id == partEntity.PartLinkId);
TransformInstance(uow, partEntity, part);
}
Expand All @@ -895,7 +910,7 @@ private void TransformInstance(IUnitOfWork uow, ProductInstanceEntity entity, Pr
partArticles[index].PartLink = partLinks.Find(pl => pl?.Id == partCollection[index].PartLinkId.Value);
}

if (typeof(ProductInstance).IsAssignableFrom(partGroup.Key.PropertyType) && partArticles.Length == 0)
if (typeof(ProductInstance).IsAssignableFrom(partGroup.Key.PropertyType) && partArticles.Length == 1)
{
partGroup.Key.SetValue(productInstance, partArticles[0]);
}
Expand Down

0 comments on commit af9bf2d

Please sign in to comment.