Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port PR AL-221 to release 8 #366

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
20 changes: 15 additions & 5 deletions src/Moryx.Products.Management/Facades/ProductManagementFacade.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG
// Copyright (c) 2024, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Moryx.AbstractionLayer.Identity;
using Moryx.AbstractionLayer.Products;
using Moryx.AbstractionLayer.Recipes;
using Moryx.Logging;
using Moryx.Runtime.Modules;
using Moryx.Workplans;

Expand All @@ -31,6 +33,8 @@ internal class ProductManagementFacade : IFacadeControl, IProductManagement

public ModuleConfig Config { get; set; }

public IModuleLogger Logger { get; set; }

#endregion

public void Activate()
Expand Down Expand Up @@ -219,10 +223,16 @@ 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)
{
var ex = new InvalidOperationException($"ProductManagement contains more than one {nameof(ProductInstance)} with the identity {identity}.");
Logger.LogError(ex, "Please make sure that an identity is unique.");
throw ex;
}

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,7 +17,9 @@
using Moryx.Model.Repositories;
using Moryx.Tools;
using static Moryx.Products.Management.ProductExpressionHelpers;
using Moryx.Logging;
using Moryx.Products.Management.Implementation.Storage;
using Microsoft.Extensions.Logging;

namespace Moryx.Products.Management
{
Expand Down Expand Up @@ -71,6 +73,11 @@ internal class ProductStorage : IProductStorage, IConfiguredTypesProvider
/// </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 @@ -770,6 +777,8 @@ private IReadOnlyList<TInstance> LoadInstancesByType<TInstance>(Expression<Func<
else
{
// TODO: Filter by type specific properties
Logger.LogWarning("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 @@ -877,6 +886,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.LogWarning("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.PartLinkEntityId);
TransformInstance(uow, partEntity, part);
}
Expand All @@ -891,7 +907,7 @@ private void TransformInstance(IUnitOfWork uow, ProductInstanceEntity entity, Pr
partArticles[index].PartLink = partLinks.Find(pl => pl?.Id == partCollection[index].PartLinkEntityId.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
Loading