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);
        }
        public RecipeModel SaveRecipe(RecipeModel recipe)
        {
            var productionRecipe = ConvertRecipeBack(recipe, null);
            var savedId          = RecipeManagement.Save(productionRecipe);

            recipe.Id = savedId;

            return(recipe);
        }
        private IProductType ConvertProductBack(ProductModel product)
        {
            // Fetch instance and copy base values
            ProductType converted;

            if (product.Id == 0)
            {
                converted = (ProductType)ProductManager.CreateType(product.Type);
            }
            else
            {
                converted = (ProductType)ProductManager.LoadType(product.Id);
            }

            converted.Identity = new ProductIdentity(product.Identifier, product.Revision);
            converted.Name     = product.Name;
            converted.State    = product.State;

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

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

            ConvertFilesBack(converted, product, properties);

            // Save recipes
            var recipes = product.Recipes.Select(r => ConvertRecipeBack(r, converted)).ToList();

            RecipeManagement.Save(product.Id, recipes);

            // Convert parts
            foreach (var partConnector in product.Parts)
            {
                var prop  = properties.First(p => p.Name == partConnector.Name);
                var value = prop.GetValue(converted);
                if (partConnector.IsCollection)
                {
                    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);
        }
        private ProductModel ConvertProduct(IProductType productType, bool flat)
        {
            var converted = _productCache.FirstOrDefault(p => p.Id == productType.Id);

            if (converted != null)
            {
                return(converted);
            }

            // Base object
            var identity = (ProductIdentity)productType.Identity ?? EmptyIdentity;

            converted = new ProductModel
            {
                Id         = productType.Id,
                Type       = productType.GetType().Name,
                Name       = productType.Name,
                State      = productType.State,
                Identifier = identity.Identifier,
                Revision   = identity.Revision
            };

            if (flat)
            {
                return(converted);
            }

            // Properties
            var properties = productType.GetType().GetProperties();

            converted.Properties = EntryConvert.EncodeObject(productType, ProductSerialization);

            // Files
            converted.Files = ConvertFiles(productType, properties);

            // Recipes
            var recipes = RecipeManagement.GetAllByProduct(productType);

            converted.Recipes = recipes.Select(ConvertRecipe).ToArray();

            // Parts
            ConvertParts(productType, properties, converted);

            _productCache.Add(converted);
            return(converted);
        }
Beispiel #5
0
        public ProductModel ConvertProduct(IProductType productType, bool flat)
        {
            // Base object
            var identity  = (ProductIdentity)productType.Identity ?? EmptyIdentity;
            var converted = new ProductModel
            {
                Id         = productType.Id,
                Type       = productType.GetType().Name,
                Name       = productType.Name,
                State      = productType.State,
                Identifier = identity.Identifier,
                Revision   = identity.Revision
            };

            if (flat)
            {
                return(converted);
            }

            // Properties
            var properties = productType.GetType().GetProperties();

            converted.Properties = EntryConvert.EncodeObject(productType, ProductSerialization);

            // Files
            converted.Files = (from property in properties
                               where property.PropertyType == typeof(ProductFile)
                               select(ProductFile) property.GetValue(productType)).ToArray();
            converted.FileModels = ConvertFiles(productType, properties);

            // Recipes
            var recipes = RecipeManagement.GetAllByProduct(productType);

            converted.Recipes = recipes.Select(ConvertRecipe).ToArray();

            // Parts
            ConvertParts(productType, properties, converted);

            return(converted);
        }
Beispiel #6
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[] GetRecipes(long productId)
        {
            var product = ProductManager.LoadType(productId);

            return(RecipeManagement.GetAllByProduct(product).Select(ConvertRecipe).ToArray());
        }
        public RecipeModel GetRecipe(long recipeId)
        {
            var recipe = RecipeManagement.Get(recipeId);

            return(ConvertRecipe(recipe));
        }