public IProductRecipe ConvertRecipeBack(RecipeModel recipe, IProductType productType)
        {
            IProductRecipe productRecipe;

            if (recipe.Id == 0)
            {
                var type = ReflectionTool.GetPublicClasses <IProductRecipe>(t => t.Name == recipe.Type).First();
                productRecipe = (IProductRecipe)Activator.CreateInstance(type);
            }
            else
            {
                productRecipe = RecipeManagement.Get(recipe.Id);
            }

            productRecipe.Name     = recipe.Name;
            productRecipe.Revision = recipe.Revision;
            productRecipe.State    = recipe.State;

            // Only load workplan if it changed
            var workplanRecipe = productRecipe as IWorkplanRecipe;

            if (workplanRecipe != null && workplanRecipe.Workplan?.Id != recipe.WorkplanId)
            {
                workplanRecipe.Workplan = WorkplanManagement.LoadWorkplan(recipe.WorkplanId);
            }

            if (productRecipe.Product == null)
            {
                productRecipe.Product = productType;
            }

            switch (recipe.Classification)
            {
            case RecipeClassificationModel.Unset:
                productRecipe.Classification = RecipeClassification.Unset;
                break;

            case RecipeClassificationModel.Default:
                productRecipe.Classification = RecipeClassification.Default;
                break;

            case RecipeClassificationModel.Alternative:
                productRecipe.Classification = RecipeClassification.Alternative;
                break;

            case RecipeClassificationModel.Intermediate:
                productRecipe.Classification = RecipeClassification.Intermediate;
                break;

            case RecipeClassificationModel.Part:
                productRecipe.Classification = RecipeClassification.Part;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            EntryConvert.UpdateInstance(productRecipe, recipe.Properties, RecipeSerialization);
            return(productRecipe);
        }
示例#2
0
        public IProductType ConvertProductBack(ProductModel source, ProductType converted)
        {
            // Copy base values
            converted.Identity = new ProductIdentity(source.Identifier, source.Revision);
            converted.Name     = source.Name;
            converted.State    = source.State;

            // Save recipes
            var recipes = new List <IProductRecipe>(source.Recipes?.Length ?? 0);

            foreach (var recipeModel in source.Recipes ?? Enumerable.Empty <RecipeModel>())
            {
                IProductRecipe productRecipe;
                if (recipeModel.Id == 0)
                {
                    var type = ReflectionTool.GetPublicClasses <IProductRecipe>(t => t.Name == recipeModel.Type).First();
                    productRecipe = (IProductRecipe)Activator.CreateInstance(type);
                }
                else
                {
                    productRecipe = RecipeManagement.Get(recipeModel.Id);
                }

                ConvertRecipeBack(recipeModel, productRecipe, converted);
                recipes.Add(productRecipe);
            }
            if (recipes.Any())
            {
                RecipeManagement.Save(source.Id, recipes);
            }

            // Product is flat
            if (source.Properties is null)
            {
                return(converted);
            }

            // Copy extended properties
            var properties = converted.GetType().GetProperties();

            EntryConvert.UpdateInstance(converted, source.Properties, ProductSerialization);

            // Copy Files
            ConvertFilesBack(converted, source, properties);

            // Convert parts
            foreach (var partConnector in source.Parts ?? Enumerable.Empty <PartConnector>())
            {
                if (partConnector.Parts is null)
                {
                    continue;
                }

                var prop  = properties.First(p => p.Name == partConnector.Name);
                var value = prop.GetValue(converted);
                if (partConnector.IsCollection)
                {
                    if (value == null)
                    {
                        value = Activator.CreateInstance(typeof(List <>)
                                                         .MakeGenericType(prop.PropertyType.GetGenericArguments().First()));
                        prop.SetValue(converted, value);
                    }
                    UpdateCollection((IList)value, partConnector.Parts);
                }
                else if (partConnector.Parts.Length == 1)
                {
                    if (value == null)
                    {
                        value = Activator.CreateInstance(prop.PropertyType);
                        prop.SetValue(converted, value);
                    }
                    UpdateReference((IProductPartLink)value, partConnector.Parts[0]);
                }
                else if (partConnector.Parts.Length == 0)
                {
                    prop.SetValue(converted, null);
                }
            }

            return(converted);
        }
        public RecipeModel GetRecipe(long recipeId)
        {
            var recipe = RecipeManagement.Get(recipeId);

            return(ConvertRecipe(recipe));
        }