Beispiel #1
0
 public static void AppsOnBuild(this PartyRelationship @this, ObjectOnBuild method)
 {
     if ([email protected])
     {
         @this.FromDate = DateTime.UtcNow;
     }
 }
Beispiel #2
0
        public void AddSameRoleIntoRelationship_FailTest()
        {
            var husbandType            = new PartyRoleType("Husband");
            var husbandRole            = new PartyRole(husbandType);
            var familyRelationshipType = new PartyRelationshipType("Family");
            var famalyCanHasHusband    = new PartyRelationshipConstraint(husbandType);

            familyRelationshipType.AddConstraint(famalyCanHasHusband);
            var familyCanHaveOnlyOneHusband = new PartyRelationshipConstraint(familyRelationshipType,
                                                                              new Func <PartyRelationship, PartyRole, bool>((relationship, role) =>
            {
                var c = relationship.Roles.Count(r => r.Id == role.Id);
                if (c == 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }));

            familyRelationshipType.AddConstraint(familyCanHaveOnlyOneHusband);


            var familyRelationship = new PartyRelationship(familyRelationshipType);

            familyRelationship.AddRole(husbandRole);
            familyRelationship.AddRole(husbandRole);
        }
 public static void BaseOnBuild(this PartyRelationship @this, ObjectOnBuild method)
 {
     if ([email protected])
     {
         @this.FromDate = @this.Strategy.Session.Now();
     }
 }
Beispiel #4
0
 public bool EditPartyRelationship(PartyRelationship partyRelationship)
 {
     using (PartyManager partyManager = new PartyManager())
     {
         return(partyManager.UpdatePartyRelationship(partyRelationship.Id, partyRelationship.Title, partyRelationship.Description, partyRelationship.StartDate, partyRelationship.EndDate, partyRelationship.Scope, partyRelationship.Permission));
     }
 }
Beispiel #5
0
        private List <PartyRelationship> ConvertDictionaryToPartyRelationships(Dictionary <string, string> partyRelationshipsDic)
        {
            var partyRelationships = new List <PartyRelationship>();

            foreach (var partyRelationshipDic in partyRelationshipsDic)
            {
                var key = partyRelationshipDic.Key.Split('_');
                if (key.Length != 3)
                {
                    continue;
                }
                int    id = int.Parse(key[1]);
                int    partyTypePairId   = int.Parse(key[2]);
                string fieldName         = key[0];
                var    partyRelationship = partyRelationships.FirstOrDefault(item => item.SecondParty.Id == id);
                //TODO: Why?
                if (partyRelationship == null)
                {
                    partyRelationship = new PartyRelationship();
                    partyRelationship.SecondParty.Id = id;
                    partyRelationships.Add(partyRelationship);
                }
                partyRelationship.PartyTypePair    = new PartyTypePair();
                partyRelationship.PartyTypePair.Id = partyTypePairId;
                if (!string.IsNullOrEmpty(partyRelationshipDic.Value))
                {
                    switch (fieldName.ToLower())
                    {
                    case "title":
                        partyRelationship.Title = partyRelationshipDic.Value;
                        break;

                    case "description":
                        partyRelationship.Description = partyRelationshipDic.Value;
                        break;

                    case "startdate":
                        partyRelationship.StartDate = Convert.ToDateTime(partyRelationshipDic.Value);
                        break;

                    case "enddate":
                        partyRelationship.EndDate = Convert.ToDateTime(partyRelationshipDic.Value);
                        break;

                    case "scope":
                        partyRelationship.Scope = partyRelationshipDic.Value;
                        break;

                    case "partyrelationshiptypeid":
                        partyRelationship.PartyRelationshipType.Id = int.Parse(partyRelationshipDic.Value);
                        break;
                    }
                }
            }
            return(partyRelationships);
        }
Beispiel #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="id">party relationship id</param>
        /// <returns></returns>
        public ActionResult LoadPartyRelationship(int id)
        {
            using (PartyManager partyManager = new PartyManager())
            {
                PartyRelationship partyRelation = partyManager.PartyRelationshipRepository.Get(id);
                ViewBag.viewMode = Request.Params["viewMode"] != null?Convert.ToBoolean(Request.Params["viewMode"]) : false;

                return(PartialView("_relationshipEditViewPartial", partyRelation));
            }
        }
Beispiel #7
0
        public PartyRelationshipEditPage SelectPartyRelationship(PartyRelationship partyRelationship)
        {
            this.PartyRelationshipPanel.Click();

            var row  = this.Table.FindRow(partyRelationship);
            var cell = row.FindCell("type");

            cell.Click();

            return(new PartyRelationshipEditPage(this.Driver));
        }
Beispiel #8
0
        public bool EditPartyRelationship(PartyRelationship partyRelationship)
        {
            PartyManager partyManager = null;

            try
            {
                partyManager = new PartyManager();
                return(partyManager.UpdatePartyRelationship(partyRelationship.Id, partyRelationship.Title, partyRelationship.Description, partyRelationship.StartDate, partyRelationship.EndDate, partyRelationship.Scope));
            }
            finally { partyManager?.Dispose(); }
        }
Beispiel #9
0
        public void ValidateRoleConstraint_SuccessTest()
        {
            var husbandCanBePersonOnly = new PartyRoleConstraint(typeof(Person));
            var husbandType            = new PartyRoleType("Husband");

            husbandType.AddConstraint(husbandCanBePersonOnly);
            var husbandRole = new PartyRole(husbandType);

            var whifeCanBePersonOnly = new PartyRoleConstraint(typeof(Person));
            var whifeType            = new PartyRoleType("Whife");

            whifeType.AddConstraint(whifeCanBePersonOnly);
            var whifeRole = new PartyRole(whifeType);

            var familyRelationshipTyp = new PartyRelationshipType("Family");
            var famalyCanHasHusband   = new PartyRelationshipConstraint(husbandType);

            familyRelationshipTyp.AddConstraint(famalyCanHasHusband);
            var famalyCanHasWhife = new PartyRelationshipConstraint(whifeType);

            familyRelationshipTyp.AddConstraint(famalyCanHasWhife);

            var familyRelationship = new PartyRelationship(familyRelationshipTyp);

            familyRelationship.AddRole(husbandRole);
            familyRelationship.AddRole(whifeRole);

            Assert.AreEqual(2, familyRelationship.Roles.Count());
            Assert.AreEqual(1, husbandRole.Type.RoleConstraints.Count());
            Assert.AreEqual(0, husbandRole.Type.Rules.Count());

            Assert.AreEqual(2, familyRelationship.Roles.Count());
            Assert.AreEqual(1, whifeRole.Type.RoleConstraints.Count());
            Assert.AreEqual(0, whifeRole.Type.Rules.Count());

            var john = new Person()
            {
                Birthdate = new DateTime(1972, 11, 4)
            };

            familyRelationship.Assign(husbandRole, john);
            var marry = new Person()
            {
                Birthdate = new DateTime(1976, 4, 16)
            };

            familyRelationship.Assign(whifeRole, marry);

            Assert.AreEqual(2, familyRelationship.Roles.Count());
            Assert.AreEqual(1, familyRelationship.GetRoles(john).Count());
            Assert.AreEqual(1, familyRelationship.GetRoles(marry).Count());
        }
Beispiel #10
0
        public void AddCustomer_SuccessTest()
        {
            var customerRoleType = new PartyRoleType("Customer");
            var customerRole     = new PartyRole(customerRoleType);

            var customerRelationshipType = new PartyRelationshipType("Customers relationship");
            var customerRelationship     = new PartyRelationship(customerRelationshipType);

            customerRelationship.AddRole(customerRole);

            var vlad = new Person();

            customerRelationship.Assign(customerRole, vlad);

            var ilona = new Person();

            customerRelationship.Assign(customerRole, ilona);
        }
Beispiel #11
0
 public bool RemovePartyRelationship(PartyRelationship partyRelationship)
 {
     Contract.Requires(partyRelationship != null);
     Contract.Requires(partyRelationship.Id >= 0, "Provided entity must have a permanent ID");
     using (IUnitOfWork uow = this.GetUnitOfWork())
     {
         IRepository <PartyRelationship> repoPR = uow.GetRepository <PartyRelationship>();
         partyRelationship = repoPR.Reload(partyRelationship);
         var cnt = repoPR.Query(item => (item.PartyRelationshipType != null && item.PartyRelationshipType.Id == partyRelationship.PartyRelationshipType.Id) &&
                                (item.FirstParty != null && item.FirstParty.Id == partyRelationship.FirstParty.Id) &&
                                (item.SecondParty != null && item.SecondParty.Id == partyRelationship.SecondParty.Id)).Count();
         if (partyRelationship.PartyRelationshipType.MinCardinality >= cnt)
         {
             BexisException.Throw(partyRelationship, String.Format("Atleast {0} party relation is required.", partyRelationship.PartyRelationshipType.MinCardinality), BexisException.ExceptionType.Delete);
         }
         var entity = repoPR.Reload(partyRelationship);
         repoPR.Delete(entity);
         uow.Commit();
         //Here we don't need to change temp status of party because minimum cardinality will be presereved in any way.
     }
     return(true);
 }
        public static int?PaymentNetDays(this PartyRelationship @this)
        {
            int?customerPaymentNetDays = null;

            foreach (Agreement agreement in @this.Agreements)
            {
                foreach (AgreementTerm term in agreement.AgreementTerms)
                {
                    if (term.TermType.Equals(new InvoiceTermTypes(@this.Strategy.Session).PaymentNetDays))
                    {
                        if (int.TryParse(term.TermValue, out var netDays))
                        {
                            customerPaymentNetDays = netDays;
                        }

                        return(customerPaymentNetDays);
                    }
                }
            }

            return(null);
        }
Beispiel #13
0
        public void AddRightRoleIntoRelationship_FailTest()
        {
            var husbandType = new PartyRoleType("Husband");
            var husbandRole = new PartyRole(husbandType);

            var whifeType = new PartyRoleType("Whife");
            var whifeRole = new PartyRole(whifeType);

            var familyRelationshipTyp = new PartyRelationshipType("Family");
            var famalyCanHasHusband   = new PartyRelationshipConstraint(husbandType);

            familyRelationshipTyp.AddConstraint(famalyCanHasHusband);
            var famalyCanHasWhife = new PartyRelationshipConstraint(whifeType);

            familyRelationshipTyp.AddConstraint(famalyCanHasWhife);

            var familyRelationship = new PartyRelationship(familyRelationshipTyp);

            familyRelationship.AddRole(husbandRole);
            familyRelationship.AddRole(whifeRole);

            var john = new Person()
            {
                Birthdate = new DateTime(1972, 11, 4)
            };

            familyRelationship.Assign(husbandRole, john);
            var marry = new Person()
            {
                Birthdate = new DateTime(1976, 4, 16)
            };

            familyRelationship.Assign(whifeRole, marry);

            Assert.AreEqual(2, familyRelationship.Roles.Count());
            Assert.AreEqual(1, familyRelationship.GetRoles(john).Count());
            Assert.AreEqual(1, familyRelationship.GetRoles(marry).Count());
        }
Beispiel #14
0
        public void AddWrongRoleIntoRelationship_FailTest()
        {
            var husbandType = new PartyRoleType("Husband");
            var husbandRole = new PartyRole(husbandType);

            var whifeType = new PartyRoleType("Whife");
            var whifeRole = new PartyRole(whifeType);

            var familyRelationshipTyp = new PartyRelationshipType("Family");
            var famalyCanHasHusband   = new PartyRelationshipConstraint(husbandType);

            familyRelationshipTyp.AddConstraint(famalyCanHasHusband);
            var famalyCanHasWhife = new PartyRelationshipConstraint(whifeType);

            familyRelationshipTyp.AddConstraint(famalyCanHasWhife);

            var familyRelationship = new PartyRelationship(familyRelationshipTyp);

            familyRelationship.AddRole(husbandRole);

            var familyRelationshipType2 = new PartyRelationshipType("Family2");
            var familyRelationship2     = new PartyRelationship(familyRelationshipType2);

            familyRelationship2.AddRole(whifeRole);

            var john = new Person()
            {
                Birthdate = new DateTime(1972, 11, 4)
            };

            familyRelationship.Assign(husbandRole, john);
            var marry = new Person()
            {
                Birthdate = new DateTime(1976, 4, 16)
            };

            familyRelationship.Assign(whifeRole, marry);
        }
Beispiel #15
0
        public SupplierRelationshipEditTest(TestFixture fixture)
            : base(fixture)
        {
            var allors   = new Organisations(this.Session).FindBy(M.Organisation.Name, "Allors BVBA");
            var supplier = new OrganisationBuilder(this.Session).WithName("supplier").Build();

            // Delete all existing for the new one to be in the first page of the list.
            foreach (PartyRelationship partyRelationship in allors.PartyRelationshipsWhereParty)
            {
                partyRelationship.Delete();
            }

            this.editPartyRelationship = new SupplierRelationshipBuilder(this.Session)
                                         .WithSupplier(supplier)
                                         .WithInternalOrganisation(allors)
                                         .Build();

            this.Session.Derive();
            this.Session.Commit();

            this.Login();
            this.organisations = this.Sidenav.NavigateToOrganisations();
        }
        public void AddCommunications_SuccessTest()
        {
            var customerRoleType         = new PartyRoleType("Customer");
            var customerRole             = new PartyRole(customerRoleType);
            var customerRelationshipType = new PartyRelationshipType("Customers relationship");
            var customerRelationship     = new PartyRelationship(customerRelationshipType);

            customerRelationship.AddRole(customerRole);

            var vlad = new Person();

            customerRelationship.Assign(customerRole, vlad);
            var ilona = new Person();

            customerRelationship.Assign(customerRole, ilona);

            var customerServiceRepresentativeRoleType = new PartyRoleType("CustomerServiceRepresentative");
            var customerServiceRepresentativeRole     = new PartyRole(customerServiceRepresentativeRoleType);
            var communicationRelationshipType         = new PartyRelationshipType("Communication relationship");
            var communicationRelationship             = new Communication(communicationRelationshipType);

            communicationRelationship.AddRole(customerServiceRepresentativeRole);

            communicationRelationship.Assign(customerServiceRepresentativeRole, vlad);
            communicationRelationship.Assign(customerServiceRepresentativeRole, ilona);

            var customerCommunicationManager = new CustomerCommunicationManager();
            var customerServiceCase          = new CustomerServiceCase(new CustomerServiceCaseIdentifier(Guid.NewGuid()));

            customerCommunicationManager.AddCustomerServiceCases(customerServiceCase);
            var thread1 = new CommunicationThread();

            customerServiceCase.AddThread(thread1);
            var communication = new Communication(communicationRelationshipType);

            thread1.AddCommunication(communicationRelationship);
        }
Beispiel #17
0
        public void ValidateEntityValueConstraine_SuccessTest()
        {
            var husbandType = new PartyRoleType("Husband");
            var husbendMustBeOlderThen18 = new RuleSet(new Func <object, bool>((x) =>
            {
                var person = x as Person;
                if (person.Age > 18)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }));

            husbandType.AddRule(husbendMustBeOlderThen18);
            var husbandRole = new PartyRole(husbandType);

            var whifeType = new PartyRoleType("Whife");
            var whifeMustBeOlderThen16 = new RuleSet(new Func <object, bool>((x) =>
            {
                var person = x as Person;
                if (person.Age > 16)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }));

            whifeType.AddRule(whifeMustBeOlderThen16);
            var whifeRole = new PartyRole(whifeType);

            var childrenType = new PartyRoleType("Children");
            var childrenMustBeYangerThenParents = new RuleSet(new Func <object, bool>((x) =>
            {
                return(true);
            }));

            childrenType.AddRule(childrenMustBeYangerThenParents);
            var childrenRole = new PartyRole(childrenType);

            var familyRelationshipTyp = new PartyRelationshipType("Family");
            var famalyCanHasHusband   = new PartyRelationshipConstraint(husbandType);

            familyRelationshipTyp.AddConstraint(famalyCanHasHusband);
            var famalyCanHasWhife = new PartyRelationshipConstraint(whifeType);

            familyRelationshipTyp.AddConstraint(famalyCanHasWhife);
            var famalyCanHasChildren = new PartyRelationshipConstraint(childrenType);

            familyRelationshipTyp.AddConstraint(famalyCanHasChildren);

            var familyRelationship = new PartyRelationship(familyRelationshipTyp);

            familyRelationship.AddRole(husbandRole);
            familyRelationship.AddRole(whifeRole);
            familyRelationship.AddRole(childrenRole);

            Assert.AreEqual(3, familyRelationship.Roles.Count());
            Assert.AreEqual(1, husbandRole.Type.Rules.Count());

            Assert.AreEqual(3, familyRelationship.Roles.Count());
            Assert.AreEqual(1, whifeRole.Type.Rules.Count());

            Assert.AreEqual(3, familyRelationship.Roles.Count());
            Assert.AreEqual(1, childrenRole.Type.Rules.Count());

            var john = new Person()
            {
                Birthdate = new DateTime(1972, 11, 4)
            };

            familyRelationship.Assign(husbandRole, john);
            var marry = new Person()
            {
                Birthdate = new DateTime(1976, 4, 16)
            };

            familyRelationship.Assign(whifeRole, marry);
            var gimmy = new Person()
            {
                Birthdate = new DateTime(1996, 4, 16)
            };

            familyRelationship.Assign(childrenRole, gimmy);

            Assert.AreEqual(3, familyRelationship.Roles.Count());
            Assert.AreEqual(1, familyRelationship.GetRoles(john).Count());
            Assert.AreEqual(1, familyRelationship.GetRoles(marry).Count());
            Assert.AreEqual(1, familyRelationship.GetRoles(gimmy).Count());
        }
Beispiel #18
0
        private List <PartyRelationship> ConvertDictionaryToPartyRelationships(Dictionary <string, string> partyRelationshipsDic, Party sourceParty, PartyManager partyManager)
        {
            var partyRelationships = new List <PartyRelationship>();

            foreach (var partyRelationshipDic in partyRelationshipsDic)
            {
                var key = partyRelationshipDic.Key.Split('_');
                if (key.Length != 3)
                {
                    continue;
                }
                int    id = int.Parse(key[1]);
                int    partyTypePairId   = int.Parse(key[2]);
                string fieldName         = key[0];
                var    partyRelationship = partyRelationships.FirstOrDefault(item => item.SourceParty.Id == id || item.TargetParty.Id == id);
                //In each iteratoin one field fills , so in the first iteration we need to do this
                if (partyRelationship == null)
                {
                    partyRelationship             = new PartyRelationship();
                    partyRelationship.TargetParty = partyManager.PartyRepository.Get(id);
                    partyRelationship.SourceParty = sourceParty;
                    partyRelationships.Add(partyRelationship);
                }
                partyRelationship.PartyTypePair    = new PartyTypePair();
                partyRelationship.PartyTypePair.Id = partyTypePairId;
                if (!string.IsNullOrEmpty(partyRelationshipDic.Value))
                {
                    switch (fieldName.ToLower())
                    {
                    case "title":
                        partyRelationship.Title = partyRelationshipDic.Value;
                        break;

                    case "description":
                        partyRelationship.Description = partyRelationshipDic.Value;
                        break;

                    case "startdate":
                        partyRelationship.StartDate = Convert.ToDateTime(partyRelationshipDic.Value);
                        break;

                    case "enddate":
                        partyRelationship.EndDate = Convert.ToDateTime(partyRelationshipDic.Value);
                        break;

                    case "scope":
                        partyRelationship.Scope = partyRelationshipDic.Value;
                        break;

                    case "partyrelationshiptypeid":
                        partyRelationship.PartyRelationshipType.Id = int.Parse(partyRelationshipDic.Value);
                        break;

                    //when relationship come vice versa
                    case "issource":
                        if (partyRelationshipDic.Value.ToLower().Equals("true"))
                        {
                            partyRelationship.SourceParty = partyManager.PartyRepository.Get(id);
                            partyRelationship.TargetParty = sourceParty;
                        }
                        break;
                    }
                }
            }
            return(partyRelationships);
        }
Beispiel #19
0
        public PartyRelationship AddPartyRelationship(PartyX firstParty, PartyX secondParty, PartyRelationshipType partyRelationshipType,
            string title, string description, DateTime? startDate = null, DateTime? endDate = null, string scope = "")
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(title));
            Contract.Requires(firstParty != null);
            Contract.Requires(firstParty.Id >= 0, "Provided first entity must have a permanent ID");
            Contract.Requires(secondParty != null);
            Contract.Requires(secondParty.Id >= 0, "Provided first entity must have a permanent ID");
            Contract.Requires(partyRelationshipType != null && partyRelationshipType.Id > 0);
            Contract.Ensures(Contract.Result<PartyRelationship>() != null && Contract.Result<PartyRelationship>().Id >= 0);
            if (startDate == null)
                startDate = DateTime.MinValue;
            if (endDate == null)
                endDate = DateTime.MaxValue;
            var entity = new PartyRelationship()
            {
                Description = description,
                EndDate = endDate.Value,
                FirstParty = firstParty,
                PartyRelationshipType = partyRelationshipType,
                Scope = scope,
                SecondParty = secondParty,
                StartDate = startDate.Value,
                Title = title
            };
            using (IUnitOfWork uow = this.GetUnitOfWork())
            {
                IRepository<PartyRelationship> repoPR = uow.GetRepository<PartyRelationship>();
                IRepository<PartyRelationshipType> repoRelType = uow.GetRepository<PartyRelationshipType>();
                partyRelationshipType = repoRelType.Reload(partyRelationshipType);
                //Check if there is another relationship
                var cnt = repoPR.Query(item => (item.PartyRelationshipType != null && item.PartyRelationshipType.Id == partyRelationshipType.Id)
                                        && (item.FirstParty != null && item.FirstParty.Id == firstParty.Id)
                                         && (item.SecondParty != null && item.SecondParty.Id == secondParty.Id)).Count();
                //if ( > 0)
                //    BexisException.Throw(entity, "This relationship is already exist in database.", BexisException.ExceptionType.Add);
                //Check maximun cardinality
                if (partyRelationshipType.MaxCardinality <= cnt)
                    BexisException.Throw(entity, string.Format("Maximum relations for this type of relation is {0}.", partyRelationshipType.MaxCardinality), BexisException.ExceptionType.Add);

                //Check if there is a relevant party type pair
                var alowedSource = partyRelationshipType.AssociatedPairs.FirstOrDefault(item => item.AlowedSource == firstParty.PartyType || item.AlowedSource == secondParty.PartyType);
                var alowedTarget = partyRelationshipType.AssociatedPairs.FirstOrDefault(item => item.AlowedTarget == firstParty.PartyType || item.AlowedTarget == secondParty.PartyType);
                if (alowedSource == null || alowedTarget == null)
                    BexisException.Throw(entity, string.Format("There is not relevant 'PartyTypePair' for these types of parties.", partyRelationshipType.MaxCardinality), BexisException.ExceptionType.Add);

                partyRelationshipType.PartyRelationships.Add(entity);
                repoPR.Put(entity);
                uow.Commit();
            }
            return (entity);
        }
Beispiel #20
0
 public bool RemovePartyRelationship(PartyRelationship partyRelationship)
 {
     Contract.Requires(partyRelationship != null);
     Contract.Requires(partyRelationship.Id >= 0, "Provided entity must have a permanent ID");
     using (IUnitOfWork uow = this.GetUnitOfWork())
     {
         IRepository<PartyRelationship> repoPR = uow.GetRepository<PartyRelationship>();
         partyRelationship = repoPR.Reload(partyRelationship);
         var cnt = repoPR.Query(item => (item.PartyRelationshipType != null && item.PartyRelationshipType.Id == partyRelationship.PartyRelationshipType.Id)
                               && (item.FirstParty != null && item.FirstParty.Id == partyRelationship.FirstParty.Id)
                                && (item.SecondParty != null && item.SecondParty.Id == partyRelationship.SecondParty.Id)).Count();
         if (partyRelationship.PartyRelationshipType.MinCardinality >= cnt)
             BexisException.Throw(partyRelationship, String.Format("Atleast {0} party relation is required.", partyRelationship.PartyRelationshipType.MinCardinality), BexisException.ExceptionType.Delete);
         var entity = repoPR.Reload(partyRelationship);
         repoPR.Delete(entity);
         uow.Commit();
     }
     return (true);
 }
Beispiel #21
0
        public PartyRelationship AddPartyRelationship(PartyX firstParty, PartyX secondParty, PartyRelationshipType partyRelationshipType,
                                                      string title, string description, PartyTypePair partyTypePair, DateTime?startDate = null, DateTime?endDate = null, string scope = "")
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(title));
            Contract.Requires(firstParty != null);
            Contract.Requires(firstParty.Id >= 0, "Provided first entity must have a permanent ID");
            Contract.Requires(secondParty != null);
            Contract.Requires(secondParty.Id >= 0, "Provided first entity must have a permanent ID");
            Contract.Requires(partyRelationshipType != null && partyRelationshipType.Id > 0);
            Contract.Ensures(Contract.Result <PartyRelationship>() != null && Contract.Result <PartyRelationship>().Id >= 0);
            if (startDate == null)
            {
                startDate = DateTime.MinValue;
            }
            if (endDate == null)
            {
                endDate = DateTime.MaxValue;
            }
            if (startDate > endDate)
            {
                BexisException.Throw(firstParty, "End date should be greater than start date.");
            }
            var entity = new PartyRelationship()
            {
                Description           = description,
                EndDate               = endDate.Value,
                FirstParty            = firstParty,
                PartyRelationshipType = partyRelationshipType,
                Scope       = scope,
                SecondParty = secondParty,
                StartDate   = startDate.Value,
                Title       = title
            };

            if (partyTypePair != null)
            {
                entity.PartyTypePair = partyTypePair;
            }
            using (IUnitOfWork uow = this.GetUnitOfWork())
            {
                IRepository <PartyX>                repoParty   = uow.GetRepository <PartyX>();
                IRepository <PartyRelationship>     repoPR      = uow.GetRepository <PartyRelationship>();
                IRepository <PartyRelationshipType> repoRelType = uow.GetRepository <PartyRelationshipType>();
                partyRelationshipType = repoRelType.Reload(partyRelationshipType);
                var cnt = repoPR.Query(item => (item.PartyRelationshipType != null && item.PartyRelationshipType.Id == partyRelationshipType.Id) &&
                                       (item.FirstParty != null && item.FirstParty.Id == firstParty.Id) &&
                                       (item.SecondParty != null && item.SecondParty.Id == secondParty.Id)).Where(item => item.EndDate > startDate).Count();
                //Check maximun cardinality
                if (partyRelationshipType.MaxCardinality != -1 && partyRelationshipType.MaxCardinality <= cnt)
                {
                    BexisException.Throw(entity, string.Format("Maximum relations for this type of relation is {0}.", partyRelationshipType.MaxCardinality), BexisException.ExceptionType.Add);
                }

                //Check if there is a relevant party type pair
                var alowedSource = partyRelationshipType.AssociatedPairs.FirstOrDefault(item => item.AllowedSource.Id == firstParty.PartyType.Id || item.AllowedSource.Id == secondParty.PartyType.Id);
                var alowedTarget = partyRelationshipType.AssociatedPairs.FirstOrDefault(item => item.AllowedTarget.Id == firstParty.PartyType.Id || item.AllowedTarget.Id == secondParty.PartyType.Id);
                if (alowedSource == null || alowedTarget == null)
                {
                    BexisException.Throw(entity, "There is not relevant 'PartyTypePair' for these types of parties.", BexisException.ExceptionType.Add);
                }
                partyRelationshipType.PartyRelationships.Add(entity);
                repoPR.Put(entity);
                uow.Commit();
                //update the source party to check if relationship rules are satisfied and changed the istemp field
                Update(entity.FirstParty);
            }
            return(entity);
        }