Ejemplo n.º 1
0
        // ---------------------------------------------------------------------------------------------------------------------------------------------------------
        public static OperationResult <Relationship> CreateRelationship(Idea DestinationComposite, RelationshipDefinition Definitor, View TargetView,
                                                                        VisualRepresentation OriginRepresentation, Point OriginPosition,
                                                                        VisualRepresentation TargetRepresentation, Point TargetPosition,
                                                                        bool ExtendFromOriginRelationship = true, // (Shift not pressed) Applicable only when origin and target are both relationships. Else (Shift pressed) create Rel. from Target to Origin.
                                                                        bool ExtendExistingRelationship   = true) // (Right-Alt not pressed) Applicable only when origin is a not (simple and hiding-central-symbol) relationship. Else (Right-Alt pressed) created Rel. from Rel.
        {
            bool EditInPlace = false;

            General.ContractRequiresNotNull(DestinationComposite, Definitor, TargetView, OriginRepresentation);

            var MaxQuota = AppExec.CurrentLicenseEdition.TechName.SelectCorresponding(LicensingConfig.IdeasCreationQuotas);

            if (!ProductDirector.ValidateEditionLimit(DestinationComposite.OwnerComposition.DeclaredIdeas.Count() + 1, MaxQuota, "create", "Ideas (Concepts + Relationships)"))
            {
                return(OperationResult.Failure <Relationship>("This product edition cannot create more than " + MaxQuota + " Ideas."));
            }

            if (!ExtendFromOriginRelationship && (TargetRepresentation == null || !(TargetRepresentation.RepresentedIdea is Relationship)))
            {
                ExtendFromOriginRelationship = true;
            }

            var  PotentialOriginRelationship = OriginRepresentation.RepresentedIdea as Relationship;
            var  PotentialTargetRelationship = (TargetRepresentation == null ? (Relationship)null : TargetRepresentation.RepresentedIdea as Relationship);
            bool CreatingNewRelationship     = (/*?*/ !ExtendExistingRelationship || /*?*/ !((PotentialOriginRelationship != null && PotentialOriginRelationship.RelationshipDefinitor.Value == Definitor) ||
                                                                                             (PotentialTargetRelationship != null && PotentialTargetRelationship.RelationshipDefinitor.Value == Definitor)));
            bool?ExtensionFromOriginRelationship = (CreatingNewRelationship ? (bool?)null : (PotentialOriginRelationship != null && ExtendFromOriginRelationship));

            if (!CreatingNewRelationship && TargetRepresentation == null)
            {
                if (OriginRepresentation is RelationshipVisualRepresentation)
                {
                    return(OperationResult.Success(((RelationshipVisualRepresentation)OriginRepresentation).RepresentedRelationship, "Creating new Relationship."));
                }
                else
                {
                    return(OperationResult.Failure <Relationship>("There is no targeted Idea to relate."));
                }
            }

            if (DestinationComposite.IdeaDefinitor.CompositeContentDomain == null)
            {
                return(OperationResult.Failure <Relationship>("Destination Container doest not accept Composite-Content.", DestinationComposite));
            }

            if (DestinationComposite.CompositeContentDomain.GlobalId != Definitor.OwnerDomain.GlobalId)
            {
                return(OperationResult.Failure <Relationship>("The destination container only accepts content of the '" + DestinationComposite.CompositeContentDomain.Name + "' Domain."));
            }

            Relationship       WorkingRelationship = null;
            LinkRoleDefinition OriginLinkRoleDef   = null;
            RoleBasedLink      OriginLink          = null;

            DestinationComposite.EditEngine.StartCommandVariation("Create Relationship");

            if (CreatingNewRelationship)
            {
                if (TargetRepresentation != null)
                {
                    var CanLink = Definitor.CanLink(OriginRepresentation.RepresentedIdea.IdeaDefinitor, TargetRepresentation.RepresentedIdea.IdeaDefinitor);

                    if (!CanLink.Result)
                    {
                        DestinationComposite.EditEngine.DiscardCommandVariation();
                        return(OperationResult.Failure <Relationship>(CanLink.Message));
                    }
                }

                string NewName = Definitor.Name;

                var AppendNumToName = AppExec.GetConfiguration <bool>("IdeaEditing", "Relationship.OnCreationAppendDefNameAndNumber", true);
                if (AppendNumToName)
                {
                    NewName = NewName + " (Idea " + (DestinationComposite.CompositeIdeas.Count + 1).ToString() + ")";
                }

                WorkingRelationship = new Relationship(DestinationComposite.OwnerComposition, Definitor, NewName, NewName.TextToIdentifier());

                if (Definitor.IsVersionable)
                {
                    WorkingRelationship.Version = new VersionCard();
                }

                WorkingRelationship.AddToComposite(DestinationComposite);

                OriginLinkRoleDef = Definitor.GetLinkForRole(ERoleType.Origin);
                OriginLink        = new RoleBasedLink(WorkingRelationship, OriginRepresentation.RepresentedIdea, OriginLinkRoleDef, OriginLinkRoleDef.AllowedVariants[0]);
                WorkingRelationship.AddLink(OriginLink);

                PotentialOriginRelationship = WorkingRelationship;
                EditInPlace = true;
            }
            else
            {
                OperationResult <bool> CanLink = OperationResult.Success(true);

                if (ExtensionFromOriginRelationship.IsTrue())
                {
                    WorkingRelationship = PotentialOriginRelationship;
                }
                else
                {
                    WorkingRelationship = (PotentialTargetRelationship != null ? PotentialTargetRelationship : PotentialOriginRelationship);
                }

                if (TargetRepresentation != null)
                {
                    CanLink = EvaluateLinkability(Definitor, OriginRepresentation, TargetRepresentation,
                                                  PotentialOriginRelationship, PotentialTargetRelationship, WorkingRelationship);

                    if (!CanLink.Result)
                    {
                        DestinationComposite.EditEngine.DiscardCommandVariation();
                        return(OperationResult.Failure <Relationship>("Cannot create/extend Relationship. Cause: " + CanLink.Message));
                    }
                }
            }

            LinkRoleDefinition WorkingLinkRoleDef = null;
            RoleBasedLink      WorkingLink        = null;

            if (TargetRepresentation != null && (CreatingNewRelationship || (WorkingRelationship == PotentialOriginRelationship &&
                                                                             ExtensionFromOriginRelationship.IsTrue())))
            {
                WorkingLinkRoleDef = Definitor.GetLinkForRole(ERoleType.Target);
                if (WorkingRelationship.Links.Where(link => link.RoleDefinitor == WorkingLinkRoleDef &&
                                                    link.AssociatedIdea == TargetRepresentation.RepresentedIdea).Any())
                {
                    DestinationComposite.EditEngine.DiscardCommandVariation();
                    return(OperationResult.Failure <Relationship>("Cannot create Target Link. Cause: Already exists one of the same role-type associating the same Idea."));
                }

                WorkingLink = new RoleBasedLink(WorkingRelationship, TargetRepresentation.RepresentedIdea, WorkingLinkRoleDef, WorkingLinkRoleDef.AllowedVariants[0]);
                WorkingRelationship.AddLink(WorkingLink);
            }

            if (OriginRepresentation != null && !CreatingNewRelationship &&
                WorkingRelationship == PotentialTargetRelationship && !ExtensionFromOriginRelationship.IsTrue())
            {
                WorkingLinkRoleDef = Definitor.GetLinkForRole(ERoleType.Origin);
                if (WorkingRelationship.Links.Where(link => link.RoleDefinitor == WorkingLinkRoleDef &&
                                                    link.AssociatedIdea == OriginRepresentation.RepresentedIdea).Any())
                {
                    DestinationComposite.EditEngine.DiscardCommandVariation();
                    return(OperationResult.Failure <Relationship>("Cannot create Origin Link. Cause: Already exists one of the same role-type associating the same Idea."));
                }

                WorkingLink = new RoleBasedLink(WorkingRelationship, OriginRepresentation.RepresentedIdea, WorkingLinkRoleDef, WorkingLinkRoleDef.AllowedVariants[0]);
                WorkingRelationship.AddLink(WorkingLink);
            }

            RelationshipVisualRepresentation Representator = null;
            VisualSymbol CentralSymbol = null;

            if (TargetPosition != Display.NULL_POINT &&
                OriginPosition != Display.NULL_POINT)
            {
                VisualConnector OriginConnector = null;

                var InitialPosition    = OriginPosition;
                var EdgeOriginPosition = OriginPosition;
                var EdgeTargetPosition = TargetPosition;

                if (CreatingNewRelationship)
                {
                    // Force connect from Symbols' centers.
                    if (OriginRepresentation != null)
                    {
                        EdgeOriginPosition = OriginPosition.DetermineNearestIntersectingPoint(TargetPosition, TargetView.Presenter,
                                                                                              OriginRepresentation.MainSymbol.Graphic,
                                                                                              TargetView.VisualHitTestFilter);
                    }

                    if (TargetRepresentation == null)
                    {
                        InitialPosition = TargetPosition;
                    }
                    else
                    {
                        EdgeTargetPosition = TargetPosition.DetermineNearestIntersectingPoint(OriginPosition, TargetView.Presenter,
                                                                                              TargetRepresentation.MainSymbol.Graphic,
                                                                                              TargetView.VisualHitTestFilter);
                        InitialPosition = EdgeOriginPosition.DetermineCenterRespect(EdgeTargetPosition);
                    }

                    // Create representation
                    Representator = CreateRelationshipVisualRepresentation(WorkingRelationship, TargetView, InitialPosition);
                    CentralSymbol = Representator.MainSymbol;

                    // Notice that here the Origin connector is being drawn, so the Target plug is empty/none (because is connected to the Relationship Central/Main Symbol).
                    OriginConnector = new VisualConnector(Representator, OriginLink, OriginRepresentation.MainSymbol, CentralSymbol, OriginPosition, CentralSymbol.BaseCenter);
                }
                else
                {
                    if (WorkingRelationship == PotentialOriginRelationship)
                    {
                        Representator = (RelationshipVisualRepresentation)OriginRepresentation;
                    }
                    else
                    {
                        Representator = (RelationshipVisualRepresentation)TargetRepresentation;
                    }

                    CentralSymbol   = OriginRepresentation.MainSymbol;
                    InitialPosition = CentralSymbol.BaseCenter;
                }

                VisualConnector TargetConnector        = null;
                VisualConnector OriginAutoRefConnector = null;

                if (TargetRepresentation != null)
                {
                    TargetConnector = new VisualConnector(Representator, WorkingLink, CentralSymbol, TargetRepresentation.MainSymbol, InitialPosition, TargetPosition);

                    if (WorkingRelationship == PotentialOriginRelationship)
                    {
                        OriginAutoRefConnector = CentralSymbol.OriginConnections.FirstOrDefault(conn => conn.OriginSymbol == TargetRepresentation.MainSymbol);
                    }
                }

                if (CreatingNewRelationship)
                {
                    Representator.AddVisualPart(OriginConnector);
                }

                if (TargetConnector != null)
                {
                    Representator.AddVisualPart(TargetConnector);
                }

                if (OriginAutoRefConnector != null)
                {
                    Representator.BendAutoRefConnectors(OriginAutoRefConnector, TargetConnector);
                }

                Representator.Render();
            }

            DestinationComposite.UpdateVersion();
            DestinationComposite.EditEngine.CompleteCommandVariation();

            var InformedRelationship = WorkingRelationship;

            if (WorkingRelationship == PotentialTargetRelationship &&
                PotentialOriginRelationship != null)
            {
                InformedRelationship = PotentialOriginRelationship;
            }

            return(OperationResult.Success(InformedRelationship, null, null, CentralSymbol.OwnerRepresentation, EditInPlace));
        }