public Result PlaceNodeContainerInRouteNetworkNode(
            CommandContext cmdContext,
            LookupCollection <NodeContainer> nodeContainers,
            LookupCollection <NodeContainerSpecification> nodeContainerSpecifications,
            Guid nodeContainerId,
            Guid nodeContainerSpecificationId,
            RouteNetworkInterest nodeOfInterest,
            NamingInfo?namingInfo,
            LifecycleInfo?lifecycleInfo,
            Guid?manufacturerId
            )
        {
            this.Id = nodeContainerId;

            if (nodeContainerId == Guid.Empty)
            {
                return(Result.Fail(new PlaceNodeContainerInRouteNetworkError(PlaceNodeContainerInRouteNetworkErrorCodes.INVALID_NODE_CONTAINER_ID_CANNOT_BE_EMPTY, "Node container id cannot be empty. A unique id must be provided by client.")));
            }

            if (nodeContainers.ContainsKey(nodeContainerId))
            {
                return(Result.Fail(new PlaceNodeContainerInRouteNetworkError(PlaceNodeContainerInRouteNetworkErrorCodes.INVALID_NODE_CONTAINER_ID_ALREADY_EXISTS, $"A node container with id: {nodeContainerId} already exists.")));
            }

            if (nodeOfInterest.Kind != RouteNetworkInterestKindEnum.NodeOfInterest)
            {
                return(Result.Fail(new PlaceNodeContainerInRouteNetworkError(PlaceNodeContainerInRouteNetworkErrorCodes.INVALID_INTEREST_KIND_MUST_BE_NODE_OF_INTEREST, "Interest kind must be NodeOfInterest. You can only put node container into route nodes!")));
            }

            if (!nodeContainerSpecifications.ContainsKey(nodeContainerSpecificationId))
            {
                return(Result.Fail(new PlaceNodeContainerInRouteNetworkError(PlaceNodeContainerInRouteNetworkErrorCodes.INVALID_NODE_CONTAINER_SPECIFICATION_ID_NOT_FOUND, $"Cannot find node container specification with id: {nodeContainerSpecificationId}")));
            }

            if (nodeContainers.Any(n => n.RouteNodeId == nodeOfInterest.RouteNetworkElementRefs[0]))
            {
                return(Result.Fail(new PlaceNodeContainerInRouteNetworkError(PlaceNodeContainerInRouteNetworkErrorCodes.NODE_CONTAINER_ALREADY_EXISTS_IN_ROUTE_NODE, $"A node container already exist in the route node with id: {nodeOfInterest.RouteNetworkElementRefs[0]} Only one node container is allowed per route node.")));
            }

            var nodeContainer = new NodeContainer(nodeContainerId, nodeContainerSpecificationId, nodeOfInterest.Id, nodeOfInterest.RouteNetworkElementRefs[0])
            {
                ManufacturerId = manufacturerId,
                NamingInfo     = namingInfo,
                LifecycleInfo  = lifecycleInfo
            };

            var nodeContainerPlaceInRouteNetworkEvent = new NodeContainerPlacedInRouteNetwork(nodeContainer)
            {
                CorrelationId = cmdContext.CorrelationId,
                IncitingCmdId = cmdContext.CmdId,
                UserName      = cmdContext.UserContext?.UserName,
                WorkTaskId    = cmdContext.UserContext?.WorkTaskId
            };

            RaiseEvent(nodeContainerPlaceInRouteNetworkEvent);

            return(Result.Ok());
        }
 private static void ValidateSpanStructureSpecificationReferences(SpanEquipmentSpecification spanEquipmentSpecification, LookupCollection <SpanStructureSpecification> spanStructureSpecifications)
 {
     // Checkthat all span structures references exists
     foreach (var spanStructureTemplate in spanEquipmentSpecification.RootTemplate.GetAllSpanStructureTemplatesRecursive())
     {
         if (!spanStructureSpecifications.ContainsKey(spanStructureTemplate.SpanStructureSpecificationId))
         {
             throw new ArgumentException($"Cannot find span structure specification with id: {spanStructureTemplate.SpanStructureSpecificationId}");
         }
     }
 }
        public void AddSpecification(CommandContext cmdContext, SpanEquipmentSpecification spanEquipmentSpecification, LookupCollection <SpanStructureSpecification> spanStructureSpecifications, LookupCollection <Manufacturer> manufacturer)
        {
            if (_spanEquipmentSpecifications.ContainsKey(spanEquipmentSpecification.Id))
            {
                throw new ArgumentException($"A span equipment specification with id: {spanEquipmentSpecification.Id} already exists");
            }

            ValidateSpanStructureSpecificationReferences(spanEquipmentSpecification, spanStructureSpecifications);
            ValidateManufacturerReferences(spanEquipmentSpecification, manufacturer);
            ValidateSpanStructureLevelAndPosition(spanEquipmentSpecification, spanStructureSpecifications);

            RaiseEvent(
                new SpanEquipmentSpecificationAdded(spanEquipmentSpecification)
            {
                CorrelationId = cmdContext.CorrelationId,
                IncitingCmdId = cmdContext.CmdId,
                UserName      = cmdContext.UserContext?.UserName,
                WorkTaskId    = cmdContext.UserContext?.WorkTaskId
            }
                );
        }
 private static void ValidateManufacturerReferences(SpanEquipmentSpecification spanEquipmentSpecification, LookupCollection <Manufacturer> manufacturer)
 {
     // Check that all manufacturer exists if provided
     if (spanEquipmentSpecification.ManufacturerRefs != null)
     {
         foreach (var manufacturerId in spanEquipmentSpecification.ManufacturerRefs)
         {
             if (!manufacturer.ContainsKey(manufacturerId))
             {
                 throw new ArgumentException($"Cannot find manufaturer with id: {manufacturerId}");
             }
         }
     }
 }
        public void AddSpecification(CommandContext cmdContext, NodeContainerSpecification nodeContainerSpecifiation, LookupCollection <Manufacturer> manufacturer)
        {
            if (_nodeContainerSpecifications.ContainsKey(nodeContainerSpecifiation.Id))
            {
                throw new ArgumentException($"A node container specification with id: {nodeContainerSpecifiation.Id} already exists");
            }

            RaiseEvent(
                new NodeContainerSpecificationAdded(nodeContainerSpecifiation)
            {
                CorrelationId = cmdContext.CorrelationId,
                IncitingCmdId = cmdContext.CmdId,
                UserName      = cmdContext.UserContext?.UserName,
                WorkTaskId    = cmdContext.UserContext?.WorkTaskId
            }
                );
        }