Beispiel #1
0
        /// <summary>
        /// Save a product to the database
        /// </summary>
        public long SaveType(IProductType modifiedInstance)
        {
            using (var uow = Factory.Create())
            {
                var productSaverContext = new ProductPartsSaverContext(uow);
                var entity = SaveProduct(productSaverContext, modifiedInstance);

                uow.SaveChanges();

                return(entity.Id);
            }
        }
Beispiel #2
0
        private ProductTypeEntity GetPartEntity(ProductPartsSaverContext saverContext, IProductPartLink link)
        {
            if (saverContext.EntityCache.ContainsKey((ProductIdentity)link.Product.Identity))
            {
                var part = saverContext.EntityCache[(ProductIdentity)link.Product.Identity];
                EntityIdListener.Listen(part, link.Product);
                return(part);
            }

            if (link.Product.Id == 0)
            {
                return(SaveProduct(saverContext, link.Product));
            }
            return(saverContext.UnitOfWork.GetEntity <ProductTypeEntity>(link.Product));
        }
Beispiel #3
0
        private ProductTypeEntity SaveProduct(ProductPartsSaverContext saverContext, IProductType modifiedInstance)
        {
            var strategy = TypeStrategies[modifiedInstance.GetType().Name];

            // Get or create entity
            var repo     = saverContext.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 = saverContext.GetRepository <IProductPropertiesRepository>().Create();
                version.State = (int)modifiedInstance.State;
                typeEntity.SetCurrentVersion(version);
            }

            strategy.SaveType(modifiedInstance, typeEntity.CurrentVersion);
            saverContext.EntityCache.Add(new ProductIdentity(typeEntity.Identifier, typeEntity.Revision), typeEntity);

            // And nasty again!
            var type     = modifiedInstance.GetType();
            var linkRepo = saverContext.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(saverContext, 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(saverContext, 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(saverContext, link);
                    }
                }
            }

            return(typeEntity);
        }