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);
        }
예제 #2
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);
        }
예제 #3
0
        /// <summary>
        /// Load all parts of the product
        /// </summary>
        private void LoadParts(IUnitOfWork uow, ProductTypeEntity typeEntity, IProductType productType, IDictionary <long, IProductType> loadedProducts)
        {
            // Let's get nasty!
            // Load children
            var type = productType.GetType();

            foreach (var part in LinkStrategies[type.Name].Values)
            {
                object value    = null;
                var    property = type.GetProperty(part.PropertyName);
                if (typeof(IProductPartLink).IsAssignableFrom(property.PropertyType))
                {
                    var linkEntity = FindLink(part.PropertyName, typeEntity);
                    if (linkEntity != null)
                    {
                        var link = LinkConstructors[$"{type.Name}.{property.Name}"]();
                        link.Id = linkEntity.Id;
                        part.LoadPartLink(linkEntity, link);
                        link.Product = (ProductType)Transform(uow, linkEntity.Child, true, loadedProducts, link);
                        value        = link;
                    }
                }
                else if (typeof(IList).IsAssignableFrom(property.PropertyType))
                {
                    var linkEntities = FindLinks(part.PropertyName, typeEntity);
                    var links        = (IList)Activator.CreateInstance(property.PropertyType);
                    foreach (var linkEntity in linkEntities)
                    {
                        var link = LinkConstructors[$"{type.Name}.{property.Name}"]();
                        link.Id = linkEntity.Id;
                        part.LoadPartLink(linkEntity, link);
                        link.Product = (ProductType)Transform(uow, linkEntity.Child, true, loadedProducts, link);
                        links.Add(link);
                    }
                    value = links;
                }
                property.SetValue(productType, value);
            }
        }
예제 #4
0
        private ProductTypeEntity SaveProduct(IUnitOfWork uow, IProductType modifiedInstance)
        {
            var strategy = TypeStrategies[modifiedInstance.GetType().Name];

            // Get or create entity
            var repo     = uow.GetRepository <IProductTypeEntityRepository>();
            var identity = (ProductIdentity)modifiedInstance.Identity;
            ProductTypeEntity typeEntity;
            var entities = repo.Linq
                           .Where(p => p.Identifier == identity.Identifier && p.Revision == identity.Revision)
                           .ToList();

            // If entity does not exist or was deleted, create a new one
            if (entities.All(p => p.Deleted != null))
            {
                typeEntity = repo.Create(identity.Identifier, identity.Revision, modifiedInstance.Name, modifiedInstance.GetType().Name);
                EntityIdListener.Listen(typeEntity, modifiedInstance);
            }
            else
            {
                typeEntity      = entities.First(p => p.Deleted == null);
                typeEntity.Name = modifiedInstance.Name;
                // Set id in case it was imported under existing material and revision
                modifiedInstance.Id = typeEntity.Id;
            }
            // Check if we need to create a new version
            if (typeEntity.CurrentVersion == null || typeEntity.CurrentVersion.State != (int)modifiedInstance.State || strategy.HasChanged(modifiedInstance, typeEntity.CurrentVersion))
            {
                var version = uow.GetRepository <IProductPropertiesRepository>().Create();
                version.State = (int)modifiedInstance.State;
                typeEntity.SetCurrentVersion(version);
            }

            strategy.SaveType(modifiedInstance, typeEntity.CurrentVersion);

            // And nasty again!
            var type     = modifiedInstance.GetType();
            var linkRepo = uow.GetRepository <IPartLinkRepository>();

            foreach (var linkStrategy in LinkStrategies[strategy.TargetType.Name].Values)
            {
                var property = type.GetProperty(linkStrategy.PropertyName);
                var value    = property.GetValue(modifiedInstance);
                if (typeof(IProductPartLink).IsAssignableFrom(property.PropertyType))
                {
                    var link       = (IProductPartLink)value;
                    var linkEntity = FindLink(linkStrategy.PropertyName, typeEntity);
                    if (linkEntity == null && link != null) // link is new
                    {
                        linkEntity        = linkRepo.Create(linkStrategy.PropertyName);
                        linkEntity.Parent = typeEntity;
                        linkStrategy.SavePartLink(link, linkEntity);
                        EntityIdListener.Listen(linkEntity, link);
                        linkEntity.Child = GetPartEntity(uow, link);
                    }
                    else if (linkEntity != null && link == null) // link was removed
                    {
                        linkStrategy.DeletePartLink(new IGenericColumns[] { linkEntity });
                        linkRepo.Remove(linkEntity);
                    }
                    else if (linkEntity != null && link != null) // link was modified
                    {
                        linkStrategy.SavePartLink(link, linkEntity);
                        linkEntity.Child = GetPartEntity(uow, link);
                    }
                    // else: link was null and is still null
                }
                else if (typeof(IEnumerable <IProductPartLink>).IsAssignableFrom(property.PropertyType))
                {
                    var links = (IEnumerable <IProductPartLink>)value;
                    // Delete the removed ones
                    var toDelete = (from link in typeEntity.Parts
                                    where link.PropertyName == linkStrategy.PropertyName
                                    where links.All(l => l.Id != link.Id)
                                    select link).ToArray();
                    linkStrategy.DeletePartLink(toDelete);
                    linkRepo.RemoveRange(toDelete);

                    // Save those currently active
                    var currentEntities = FindLinks(linkStrategy.PropertyName, typeEntity).ToArray();
                    foreach (var link in links)
                    {
                        PartLink linkEntity;
                        if (link.Id == 0 || (linkEntity = currentEntities.FirstOrDefault(p => p.Id == link.Id)) == null)
                        {
                            linkEntity        = linkRepo.Create(linkStrategy.PropertyName);
                            linkEntity.Parent = typeEntity;
                            EntityIdListener.Listen(linkEntity, link);
                        }
                        else
                        {
                            linkEntity = typeEntity.Parts.First(p => p.Id == link.Id);
                        }
                        linkStrategy.SavePartLink(link, linkEntity);
                        linkEntity.Child = GetPartEntity(uow, link);
                    }
                }
            }

            return(typeEntity);
        }