コード例 #1
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));
        }
コード例 #2
0
        /// <summary>
        /// Save a workplan to the database
        /// </summary>
        public static WorkplanEntity SaveWorkplan(IUnitOfWork uow, Workplan workplan)
        {
            var workplanRepo  = uow.GetRepository <IWorkplanEntityRepository>();
            var referenceRepo = uow.GetRepository <IWorkplanReferenceRepository>();

            // Try to get the current object
            var workplanEntity = workplanRepo.GetByKey(workplan.Id);

            // If it is a new plan we need a new object
            if (workplanEntity == null)
            {
                workplanEntity = workplanRepo.Create(workplan.Name, 1, (int)workplan.State);
                EntityIdListener.Listen(workplanEntity, workplan);
            }
            // If it was modified we write to a new entity and increment the version
            else
            {
                // Flag the previous version deleted, to hide it from the list but keep it accessible by old recipes
                workplanRepo.Remove(workplanEntity);

                // Create a reference link between old and new version
                var reference = referenceRepo.Create((int)WorkplanReferenceType.NewVersion);
                reference.Source = workplanEntity;
                reference.Target = workplanEntity = workplanRepo.Create(workplan.Name, workplanEntity.Version + 1, (int)workplan.State);
                EntityIdListener.Listen(workplanEntity, workplan);
            }

            // Set properties of the workplan entity
            workplanEntity.Name         = workplan.Name;
            workplanEntity.State        = (int)workplan.State;
            workplanEntity.MaxElementId = workplan.MaxElementId;

            // Save connectors and steps
            var steps      = SaveSteps(uow, workplanEntity, workplan);
            var connectors = SaveConnectors(uow, workplanEntity, workplan);

            LinkSteps(uow, workplan, steps, connectors);
            RemoveUnusedConnectors(uow, workplanEntity, workplan);

            return(workplanEntity);
        }
コード例 #3
0
        /// <summary>
        /// Save a workplan to the database
        /// </summary>
        public static WorkplanEntity SaveWorkplan(IUnitOfWork uow, Workplan workplan)
        {
            var workplanRepo  = uow.GetRepository <IWorkplanEntityRepository>();
            var referenceRepo = uow.GetRepository <IWorkplanReferenceRepository>();

            // Try to get the current object
            var workplanEntity = workplanRepo.GetByKey(workplan.Id);

            // If it is a new plan we need a new object
            if (workplanEntity == null)
            {
                workplanEntity = workplanRepo.Create(workplan.Name, workplan.Version, (int)workplan.State);
                EntityIdListener.Listen(workplanEntity, workplan);
            }
            // If it was modified or the version increased we create a new one and reference the old one
            else if (workplan.Version > workplanEntity.Version)
            {
                var reference = referenceRepo.Create((int)WorkplanReferenceType.NewVersion);
                reference.Source = workplanEntity;
                reference.Target = workplanEntity = workplanRepo.Create(workplan.Name, workplan.Version, (int)workplan.State);
                EntityIdListener.Listen(workplanEntity, workplan);
            }

            // Set properties of the workplan entity
            workplanEntity.Name         = workplan.Name;
            workplanEntity.State        = (int)workplan.State;
            workplanEntity.MaxElementId = workplan.MaxElementId;

            // Save connectors and steps
            var steps      = SaveSteps(uow, workplanEntity, workplan);
            var connectors = SaveConnectors(uow, workplanEntity, workplan);

            LinkSteps(uow, workplan, steps, connectors);
            RemoveUnusedConnectors(uow, workplanEntity, workplan);

            return(workplanEntity);
        }
コード例 #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);
        }