Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="id">party type id</param>
        /// <returns></returns>
        public ActionResult LoadPartyRelationshipType(int id)
        {
            using (PartyRelationshipTypeManager partyRelManager = new PartyRelationshipTypeManager())
                using (PartyManager partyManager = new PartyManager())
                {
                    var partyId = Request.Params["partyId"] != null?long.Parse(Request.Params["partyId"]) : 0;

                    Party party = partyManager.PartyRepository.Get(partyId);
                    ViewBag.sourceParty = party;
                    var partyRelationshipTypes    = partyRelManager.GetAllPartyRelationshipTypes(party.PartyType.Id, true);
                    var addpartyRelationshipModel = new List <AddRelationshipModel>();
                    // foreach (var partyRelationshipType in partyRelationshipTypes)
                    var addRelationshipModel = new List <AddRelationshipModel>();
                    addRelationshipModel.Add(new AddRelationshipModel()
                    {
                        PartyRelationshipTypes = partyRelationshipTypes.Where(cc => cc.AssociatedPairs.Any(item => item.SourcePartyType.Id == party.PartyType.Id)),
                        SourceParty            = party,
                        isAsSource             = false
                    });
                    addRelationshipModel.Add(new AddRelationshipModel()
                    {
                        PartyRelationshipTypes = partyRelationshipTypes.Where(cc => cc.AssociatedPairs.Any(item => item.TargetPartyType.Id == party.PartyType.Id)),
                        SourceParty            = party,
                        isAsSource             = true
                    });
                    return(PartialView("_addPartyRelationshipPartial", addRelationshipModel));
                }
        }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="description"></param>
        /// <param name="partyTypeId"></param>
        /// <param name="partyCustomAttributeValues">CustomAttributeName or Id as key</param>
        /// <returns></returns>
        internal static Party CreateParty(DateTime?startDate, DateTime?endDate, string description, long partyTypeId, Dictionary <string, string> partyCustomAttributeValuesDict)
        {
            using (PartyTypeManager partyTypeManager = new PartyTypeManager())
                using (PartyManager partyManager = new PartyManager())
                    using (PartyRelationshipTypeManager partyRelationshipTypeManager = new PartyRelationshipTypeManager())
                    {
                        var       newParty        = new Party();
                        PartyType partyType       = partyTypeManager.PartyTypeRepository.Get(partyTypeId);
                        var       partyStatusType = partyTypeManager.GetStatusType(partyType, "Created");
                        // save party as temp if the reationships are required
                        var requiredPartyRelationTypes = partyRelationshipTypeManager.GetAllPartyRelationshipTypes(partyType.Id).Where(cc => cc.MinCardinality > 0);
                        //Create party
                        newParty = partyManager.Create(partyType, "", description, startDate, endDate, partyStatusType, requiredPartyRelationTypes.Any());
                        partyManager.AddPartyCustomAttributeValues(newParty, toPartyCustomAttributeValues(partyCustomAttributeValuesDict, partyTypeId));
                        // partyManager.AddPartyRelationship(null,TargetPartyId,PartyTypePairid)
                        //var systemPartyTypePairs = GetSystemTypePairs(newParty.PartyType.Id);
                        ////add relationship to the all targets
                        //foreach (var systemPartyTypePair in systemPartyTypePairs)
                        //{
                        //    foreach (var targetParty in systemPartyTypePair.TargetPartyType.Parties)
                        //    {
                        //        PartyTypePair partyTypePair = partyRelationshipTypeManager.PartyTypePairRepository.Reload(systemPartyTypePair);
                        //        partyManager.AddPartyRelationship(partyManager.PartyRepository.Reload(newParty), targetParty,  "system", "", systemPartyTypePair, permission: systemPartyTypePair.PermissionTemplate);
                        //    }
                        //}

                        return(newParty);
                    }
        }
Exemple #3
0
        public ActionResult CreatePartyRelationships(int partyId, Dictionary <string, string> partyRelationshipsDic)
        {
            PartyManager partyManager = null;
            PartyRelationshipTypeManager partyRelationshipManager = null;

            try
            {
                partyManager = new PartyManager();
                var party = partyManager.PartyRepository.Get(partyId);
                var partyRelationships = ConvertDictionaryToPartyRelationships(partyRelationshipsDic, party, partyManager);
                partyRelationshipManager = new PartyRelationshipTypeManager();
                foreach (var partyRelationship in partyRelationships)
                {
                    // Party TargetParty = partyManager.PartyRepository.Get(partyRelationship.TargetParty.Id);
                    // PartyRelationshipType partyRelationshipType = partyRelationshipManager.PartyRelationshipTypeRepository.Get(partyRelationship.PartyRelationshipType.Id);
                    PartyTypePair partyTypePair = partyRelationshipManager.PartyTypePairRepository.Get(partyRelationship.PartyTypePair.Id);
                    //Min date value is sent from telerik date time element, if it was empty
                    if (partyRelationship.EndDate == DateTime.MinValue)
                    {
                        partyRelationship.EndDate = DateTime.MaxValue;
                    }
                    partyManager.AddPartyRelationship(partyRelationship.SourceParty, partyRelationship.TargetParty, partyRelationship.Title, partyRelationship.Description, partyTypePair, partyRelationship.StartDate, partyRelationship.EndDate, partyRelationship.Scope);
                }
                partyManager?.Dispose();
                return(RedirectToAction("CreateEdit", "party", new { id = partyId, relationTabAsDefault = true }));
            }
            finally
            {
                partyManager?.Dispose();
                partyRelationshipManager?.Dispose();
            }
        }
Exemple #4
0
        public ActionResult CreateEdit(PartyModel partyModel, Dictionary <string, string> partyCustomAttributeValues, IList <PartyRelationship> systemPartyRelationships)
        {
            PartyManager partyManager = null;
            PartyRelationshipTypeManager partyRelationshipTypeManager = null;

            try
            {
                partyManager = new PartyManager();
                var party = new Party();
                if (partyModel.Id == 0)
                {
                    party = Helper.CreateParty(partyModel, partyCustomAttributeValues);
                }
                else
                {
                    party = Helper.EditParty(partyModel, partyCustomAttributeValues, systemPartyRelationships);
                }

                if (party.IsTemp)
                {
                    return(RedirectToAction("CreateEdit", new { id = party.Id, relationTabAsDefault = true }));
                }
                else
                {
                    return(RedirectToAction("Index"));
                }
            }
            finally
            {
                partyManager?.Dispose();
                partyRelationshipTypeManager?.Dispose();
            }
        }
Exemple #5
0
        internal static Party EditParty(PartyModel partyModel, Dictionary <string, string> partyCustomAttributeValues, IList <PartyRelationship> systemPartyRelationships)
        {
            using (PartyTypeManager partyTypeManager = new PartyTypeManager())
                using (PartyManager partyManager = new PartyManager())
                    using (PartyRelationshipTypeManager partyRelationshipTypeManager = new PartyRelationshipTypeManager())
                    {
                        var party = new Party();

                        var newAddPartyCustomAttrValues = new Dictionary <PartyCustomAttribute, string>();
                        party = partyManager.Find(partyModel.Id);
                        //Update some fields
                        party.Description = partyModel.Description;
                        party.StartDate   = partyModel.StartDate.HasValue ? partyModel.StartDate.Value : DateTime.MinValue;
                        party.EndDate     = partyModel.EndDate.HasValue ? partyModel.EndDate.Value : DateTime.MaxValue;
                        party             = partyManager.Update(party);
                        foreach (var partyCustomAttributeValueString in partyCustomAttributeValues)
                        {
                            PartyCustomAttribute partyCustomAttribute = partyTypeManager.PartyCustomAttributeRepository.Get(int.Parse(partyCustomAttributeValueString.Key));
                            string value = string.IsNullOrEmpty(partyCustomAttributeValueString.Value) ? "" : partyCustomAttributeValueString.Value;
                            newAddPartyCustomAttrValues.Add(partyCustomAttribute, value);
                        }
                        party.CustomAttributeValues = partyManager.AddPartyCustomAttributeValues(party, partyCustomAttributeValues.ToDictionary(cc => long.Parse(cc.Key), cc => cc.Value)).ToList();

                        return(party);
                    }
        }
Exemple #6
0
        public ActionResult CreateUserParty(Party party, Dictionary <string, string> partyCustomAttributeValues, List <PartyRelationship> partyRelationships)
        {
            using (PartyTypeManager partyTypeManager = new PartyTypeManager())
                using (PartyManager partyManager = new PartyManager())
                    using (PartyRelationshipTypeManager partyRelationshipManager = new PartyRelationshipTypeManager())
                        using (UserManager userManager = new UserManager())
                            using (PartyRelationshipTypeManager partyRelationshipTypeManager = new PartyRelationshipTypeManager())
                            {
                                // check if
                                var userTask = userManager.FindByNameAsync(HttpContext.User.Identity.Name);
                                userTask.Wait();
                                var user = userTask.Result;

                                //check if the party blongs to the user
                                //Bind party if there is already a user associated to this party
                                var partyuser = partyManager.GetPartyByUser(user.Id);
                                if (partyuser == null)
                                {
                                    var partyType       = partyTypeManager.PartyTypeRepository.Get(party.PartyType.Id);
                                    var partyStatusType = partyTypeManager.GetStatusType(partyType, "Created");
                                    //Create party
                                    party = partyManager.Create(partyType, party.Description, null, null, partyCustomAttributeValues.ToDictionary(cc => long.Parse(cc.Key), cc => cc.Value));
                                    if (partyRelationships != null)
                                    {
                                        foreach (var partyRelationship in partyRelationships)
                                        {
                                            //the duration is from current datetime up to the end of target party date
                                            var TargetParty = partyManager.PartyRepository.Get(partyRelationship.TargetParty.Id);
                                            // var partyRelationshipType = partyRelationshipManager.PartyRelationshipTypeRepository.Get(partyRelationship.PartyRelationshipType.Id);
                                            var partyTypePair = partyRelationshipManager.PartyTypePairRepository.Get(partyRelationship.PartyTypePair.Id);
                                            partyManager.AddPartyRelationship(party, TargetParty, partyRelationship.Title, partyRelationship.Description, partyTypePair, DateTime.Now, TargetParty.EndDate, partyRelationship.Scope);
                                        }
                                    }

                                    partyManager.AddPartyUser(party, user.Id);

                                    //set FullName in user
                                    var    p           = partyManager.GetParty(party.Id);
                                    string displayName = String.Join(" ",
                                                                     p.CustomAttributeValues.
                                                                     Where(ca => ca.CustomAttribute.IsMain.Equals(true)).
                                                                     OrderBy(ca => ca.CustomAttribute.Id).
                                                                     Select(ca => ca.Value).ToArray());

                                    user.DisplayName = displayName;
                                    userManager.UpdateAsync(user);
                                }

                                return(RedirectToAction("Index"));
                            }
        }
Exemple #7
0
        public static IList <PartyTypePair> GetSystemTypePairs(long partyTypeId)
        {
            PartyRelationshipTypeManager partTypeManager = new PartyRelationshipTypeManager();

            try
            {
                var typePairs = partTypeManager.PartyTypePairRepository.Get(cc => cc.SourcePartyType.Id == partyTypeId && cc.TargetPartyType.SystemType);
                return(typePairs);
            }
            finally
            {
                partTypeManager?.Dispose();
            }
        }
Exemple #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="id">party type id</param>
        /// <returns></returns>
        public ActionResult LoadPartyRelationshipType(int id)
        {
            PartyRelationshipTypeManager partyRelManager = null;

            try
            {
                partyRelManager = new PartyRelationshipTypeManager();
                Party party = Request.Params["partyId"] != null ? new PartyManager().PartyRepository.Get(long.Parse(Request.Params["partyId"])) : null;
                ViewBag.sourceParty = party;
                var partyRelationshipTypes = partyRelManager.GetAllPartyRelationshipTypes(party.PartyType.Id);

                return(PartialView("_addPartyRelationshipPartial", partyRelationshipTypes.ToList()));
            }
            finally
            {
                partyRelManager?.Dispose();
            }
        }
Exemple #9
0
        public ActionResult CreateUserParty(Party party, Dictionary <string, string> partyCustomAttributeValues, List <PartyRelationship> partyRelationships)
        {
            PartyTypeManager             partyTypeManager         = null;
            PartyManager                 partyManager             = null;
            PartyRelationshipTypeManager partyRelationshipManager = null;
            UserManager userManager = null;

            try
            {
                //check if the party blongs to the user
                //Bind party if there is already a user associated to this party

                userManager              = new UserManager();
                partyTypeManager         = new PartyTypeManager();
                partyManager             = new PartyManager();
                partyRelationshipManager = new PartyRelationshipTypeManager();
                var partyType       = partyTypeManager.PartyTypeRepository.Get(party.PartyType.Id);
                var partyStatusType = partyTypeManager.GetStatusType(partyType, "Created");
                //Create party
                party = partyManager.Create(partyType, party.Description, null, null, partyCustomAttributeValues.ToDictionary(cc => long.Parse(cc.Key), cc => cc.Value));
                if (partyRelationships != null)
                {
                    foreach (var partyRelationship in partyRelationships)
                    {
                        //the duration is from current datetime up to the end of target party date
                        var secondParty           = partyManager.PartyRepository.Get(partyRelationship.SecondParty.Id);
                        var partyRelationshipType = partyRelationshipManager.PartyRelationshipTypeRepository.Get(partyRelationship.PartyRelationshipType.Id);
                        var partyTypePair         = partyRelationshipManager.PartyTypePairRepository.Get(partyRelationship.PartyTypePair.Id);
                        partyManager.AddPartyRelationship(party, secondParty, partyRelationshipType, partyRelationship.Title, partyRelationship.Description, partyTypePair, DateTime.Now, secondParty.EndDate, partyRelationship.Scope);
                    }
                }
                var userTask = userManager.FindByNameAsync(HttpContext.User.Identity.Name);
                userTask.Wait();
                var user = userTask.Result;
                partyManager.AddPartyUser(party, user.Id);
                return(RedirectToAction("Index"));
            }
            finally
            {
                partyTypeManager?.Dispose();
                partyManager?.Dispose();
                partyRelationshipManager?.Dispose();
            }
        }
Exemple #10
0
        public void AddPartyRelationshipTest()
        {
            PartyManager     partyManager     = new PartyManager();
            PartyTypeManager partyTypeManager = new PartyTypeManager();
            var partyStatusTypes = new List <PartyStatusType>();

            partyStatusTypes.Add(new PartyStatusType()
            {
                Name = "Created", Description = ""
            });
            var partyTypeTest   = partyTypeManager.Create("partyTypeTitle", "", "", partyStatusTypes);
            var partyTypeTest2  = partyTypeManager.Create("partyTypeTitle2", "", "", partyStatusTypes);
            var partyStatusType = partyTypeManager.GetStatusType(partyTypeTest, "Created");
            //create cstom attributes
            var partyCustomAttribute1 = partyTypeManager.CreatePartyCustomAttribute(partyTypeTest, "String", "FirstName", "", "", "", isMain: true);
            var partyCustomAttribute2 = partyTypeManager.CreatePartyCustomAttribute(partyTypeTest, "String", "LastName", "", "", "", isMain: true);
            var partyCustomAttribute3 = partyTypeManager.CreatePartyCustomAttribute(partyTypeTest, "Int", "Age", "", "", "");
            //create with CustomAttributeValues<Id,value>
            Dictionary <long, String> customAttributeValues = new Dictionary <long, string>();

            customAttributeValues.Add(partyCustomAttribute1.Id, "Masoud");
            customAttributeValues.Add(partyCustomAttribute2.Id, "Allahyari");
            customAttributeValues.Add(partyCustomAttribute3.Id, "31");
            var party = partyManager.Create(partyTypeTest2, "description test", DateTime.Now.AddDays(-10), DateTime.Now.AddDays(10), customAttributeValues);

            //create with CustomAttributeValues<Id,value>
            customAttributeValues = new Dictionary <long, string>();
            customAttributeValues.Add(partyCustomAttribute1.Id, "Ali");
            customAttributeValues.Add(partyCustomAttribute2.Id, "Wandern");
            customAttributeValues.Add(partyCustomAttribute3.Id, "37");
            var party2 = partyManager.Create(partyTypeTest, "description test", DateTime.Now.AddDays(-10), DateTime.Now.AddDays(10), customAttributeValues);

            //party type pair
            var prtManager = new PartyRelationshipTypeManager();

            prtManager.Create("relationship test", "", "", true, 10, 0, true, partyTypeTest, partyTypeTest2, "type pair test", "", "", "", 0);

            // partyManager.AddPartyRelationship(party,party2,"relation test","",)
            //add relationship

            //test maximun and minimum cardinality
        }
Exemple #11
0
        internal static Party CreateParty(PartyModel partyModel, Dictionary <string, string> partyCustomAttributeValues)
        {
            PartyTypeManager partyTypeManager = new PartyTypeManager();
            PartyManager     partyManager     = new PartyManager();
            var party = new Party();

            try
            {
                PartyType partyType       = partyTypeManager.PartyTypeRepository.Get(partyModel.PartyType.Id);
                var       partyStatusType = partyTypeManager.GetStatusType(partyType, "Created");
                // save party as temp if the reationships are required
                var requiredPartyRelationTypes = new PartyRelationshipTypeManager().GetAllPartyRelationshipTypes(partyType.Id).Where(cc => cc.MinCardinality > 0);
                //Create party
                party = partyManager.Create(partyType, "", partyModel.Description, partyModel.StartDate, partyModel.EndDate, partyStatusType, requiredPartyRelationTypes.Any());
                partyManager.AddPartyCustomAttributeValues(party, partyCustomAttributeValues.ToDictionary(cc => long.Parse(cc.Key), cc => cc.Value));
            }
            finally
            {
                partyTypeManager?.Dispose();
                partyManager?.Dispose();
            }
            return(party);
        }
Exemple #12
0
        public ActionResult CreatePartyRelationships(int partyId, Dictionary <string, string> partyRelationshipsDic)
        {
            PartyManager partyManager = null;

            try
            {
                partyManager = new PartyManager();
                Party party = partyManager.PartyRepository.Get(partyId);
                List <PartyRelationship> partyRelationships = ConvertDictionaryToPartyRelationships(partyRelationshipsDic);
                var partyRelationshipManager = new PartyRelationshipTypeManager();
                foreach (var partyRelationship in partyRelationships)
                {
                    Party secondParty = partyManager.PartyRepository.Get(partyRelationship.SecondParty.Id);
                    PartyRelationshipType partyRelationshipType = partyRelationshipManager.PartyRelationshipTypeRepository.Get(partyRelationship.PartyRelationshipType.Id);
                    PartyTypePair         partyTypePair         = partyRelationshipManager.PartyTypePairRepository.Get(partyRelationship.PartyTypePair.Id);
                    //Min date value is sent from telerik date time element, if it was empty
                    if (partyRelationship.EndDate == DateTime.MinValue)
                    {
                        partyRelationship.EndDate = DateTime.MaxValue;
                    }
                    partyManager.AddPartyRelationship(party, secondParty, partyRelationshipType, partyRelationship.Title, partyRelationship.Description, partyTypePair, partyRelationship.StartDate, partyRelationship.EndDate, partyRelationship.Scope);
                }
                partyManager?.Dispose();
                //partyManager = new PartyManager();
                ////if relationship rules are satisfied, it is not temp
                //  if (string.IsNullOrWhiteSpace(Helpers.Helper.ValidateRelationships(party.Id)))
                //    party.IsTemp = false;
                //else
                //    party.IsTemp = true;
                //partyManager.Update(party);
                return(RedirectToAction("CreateEdit", "party", new { id = partyId, relationTabAsDefault = true }));
            }
            finally
            {
                partyManager?.Dispose();
            }
        }
Exemple #13
0
        /// <summary>
        /// Update rules:
        /// Comparison for update is by the title of elements: title of elements are not editable
        /// if title of an element is changed because remove is forbiden here ,  it adds it as a new element and the old one will remain there
        ///
        /// </summary>
        private void ImportPartyTypes()
        {
            PartyTypeManager partyTypeManager = new PartyTypeManager();
            var         filePath = Path.Combine(AppConfiguration.GetModuleWorkspacePath("BAM"), "partyTypes.xml");
            XDocument   xDoc     = XDocument.Load(filePath);
            XmlDocument xmlDoc   = new XmlDocument();

            xmlDoc.Load(xDoc.CreateReader());
            var partyTypesNodeList = xmlDoc.SelectNodes("//PartyTypes");

            if (partyTypesNodeList.Count > 0)
            {
                foreach (XmlNode partyTypeNode in partyTypesNodeList[0].ChildNodes)
                {
                    //Convert xmAttributeCollection to list to skipt the case sensitive and null problems
                    var attributes = new List <XmlAttribute>();
                    foreach (XmlAttribute att in partyTypeNode.Attributes)
                    {
                        attributes.Add(att);
                    }
                    var title       = GetAttributeValue(attributes, "Name", true);
                    var displayName = GetAttributeValue(attributes, "DisplayName", false);
                    var partyType   = partyTypeManager.PartyTypeRepository.Get(item => item.Title == title).FirstOrDefault();
                    //If there is not such a party type
                    if (partyType == null)
                    {
                        var partyStatusTypes = new List <PartyStatusType>();
                        partyStatusTypes.Add(new PartyStatusType()
                        {
                            Name = "Created", Description = ""
                        });
                        partyType = partyTypeManager.Create(title, "Imported from partyTypes.xml", displayName, partyStatusTypes);
                        var customAttrs = new List <PartyCustomAttribute>();
                        foreach (XmlNode customAttrNode in partyTypeNode.ChildNodes)
                        {
                            var customAttrNodeAttributes = new List <XmlAttribute>();
                            foreach (XmlAttribute att in customAttrNode.Attributes)
                            {
                                customAttrNodeAttributes.Add(att);
                            }
                            PartyCustomAttribute partyCustomAttr = ParsePartyCustomAttribute(customAttrNodeAttributes);

                            customAttrs.Add(new PartyCustomAttribute()
                            {
                                DataType        = partyCustomAttr.DataType,
                                Description     = partyCustomAttr.Description,
                                IsMain          = partyCustomAttr.IsMain,
                                IsUnique        = partyCustomAttr.IsUnique,
                                IsValueOptional = partyCustomAttr.IsValueOptional,
                                Name            = partyCustomAttr.Name,
                                PartyType       = partyType,
                                ValidValues     = partyCustomAttr.ValidValues,
                                DisplayName     = partyCustomAttr.DisplayName,
                                Condition       = partyCustomAttr.Condition
                            });
                        }
                        if (!customAttrs.Any(c => c.IsMain))
                        {
                            customAttrs[0].IsMain = true;
                        }
                        foreach (var customAttr in customAttrs)
                        {
                            partyTypeManager.CreatePartyCustomAttribute(customAttr);
                        }
                    }
                    else
                    {
                        var customAttrs = new List <PartyCustomAttribute>();
                        foreach (XmlNode customAttrNode in partyTypeNode.ChildNodes)
                        {
                            var attributesList = new List <XmlAttribute>();
                            foreach (XmlAttribute att in customAttrNode.Attributes)
                            {
                                attributesList.Add(att);
                            }

                            var customAttrName = GetAttributeValue(attributesList, "Name", true);
                            //create new custom attribute if there is not such a name
                            if (!partyType.CustomAttributes.Any(item => item.Name == customAttrName))
                            {
                                var customAttrNodeAttributes = new List <XmlAttribute>();
                                foreach (XmlAttribute att in customAttrNode.Attributes)
                                {
                                    customAttrNodeAttributes.Add(att);
                                }

                                PartyCustomAttribute partyCustomAttr = ParsePartyCustomAttribute(customAttrNodeAttributes);
                                customAttrs.Add(new PartyCustomAttribute()
                                {
                                    DataType        = partyCustomAttr.DataType,
                                    Description     = partyCustomAttr.Description,
                                    IsMain          = partyCustomAttr.IsMain,
                                    IsUnique        = partyCustomAttr.IsUnique,
                                    IsValueOptional = partyCustomAttr.IsValueOptional,
                                    Name            = customAttrName,
                                    PartyType       = partyType,
                                    ValidValues     = partyCustomAttr.ValidValues,
                                    DisplayName     = partyCustomAttr.DisplayName,
                                    Condition       = partyCustomAttr.Condition
                                });
                            }
                        }
                        if (!customAttrs.Any(c => c.IsMain) && !partyType.CustomAttributes.Any(c => c.IsMain))
                        {
                            throw new Exception("There is no main field. Each party type needs at least one main field.");
                        }
                        foreach (var customAttr in customAttrs)
                        {
                            partyTypeManager.CreatePartyCustomAttribute(customAttr);
                        }
                    }
                }
            }
            var partyRelationshipTypesNodeList = xmlDoc.SelectNodes("//PartyRelationshipTypes");

            if (partyRelationshipTypesNodeList.Count > 0)
            {
                foreach (XmlNode partyRelationshipTypesNode in partyRelationshipTypesNodeList[0].ChildNodes)
                {
                    var customAttrNodeAttributes = new List <XmlAttribute>();
                    foreach (XmlAttribute att in partyRelationshipTypesNode.Attributes)
                    {
                        customAttrNodeAttributes.Add(att);
                    }
                    var partyRelationshipTypeManager = new PartyRelationshipTypeManager();
                    var title              = GetAttributeValue(customAttrNodeAttributes, "Name", true);
                    var displayName        = GetAttributeValue(customAttrNodeAttributes, "DisplayName", false);
                    var description        = GetAttributeValue(customAttrNodeAttributes, "Description", false);
                    var indicatesHierarchy = GetAttributeValue(customAttrNodeAttributes, "IndicatesHierarchy", true); // false;
                    var maxCardinality     = GetAttributeValue(customAttrNodeAttributes, "MaxCardinality", true);     // -1
                    var minCardinality     = GetAttributeValue(customAttrNodeAttributes, "MinCardinality", true);     // 0

                    //Import party type pairs
                    var partyTypePairs = new List <PartyTypePair>();
                    foreach (XmlNode partyTypesPairNode in partyRelationshipTypesNode.ChildNodes[0].ChildNodes)
                    {
                        var partyTypesPairNodeAttributes = new List <XmlAttribute>();
                        foreach (XmlAttribute att in partyTypesPairNode.Attributes)
                        {
                            partyTypesPairNodeAttributes.Add(att);
                        }
                        var allowedSourceTitle = GetAttributeValue(partyTypesPairNodeAttributes, "AllowedSource", true);
                        var allowedTargetTitle = GetAttributeValue(partyTypesPairNodeAttributes, "AllowedTarget", true);
                        var allowedSource      = partyTypeManager.PartyTypeRepository.Get(item => item.Title.ToLower() == allowedSourceTitle.ToLower()).FirstOrDefault();
                        if (allowedSource == null)
                        {
                            throw new Exception("Error in importing party relationship types ! \r\n " + allowedSourceTitle + " is not a party type!!");
                        }
                        var allowedTarget = partyTypeManager.PartyTypeRepository.Get(item => item.Title.ToLower() == allowedTargetTitle.ToLower()).FirstOrDefault();
                        if (allowedTarget == null)
                        {
                            throw new Exception("Error in importing party relationship types ! \r\n " + allowedTargetTitle + " is not a party type!!");
                        }

                        var typePairTitle       = GetAttributeValue(partyTypesPairNodeAttributes, "Title", true);
                        var typePairDescription = GetAttributeValue(partyTypesPairNodeAttributes, "Description", false);
                        var typePairDefault     = GetAttributeValue(partyTypesPairNodeAttributes, "Default", true);
                        var conditionSource     = GetAttributeValue(partyTypesPairNodeAttributes, "conditionSource", false);
                        var conditionTarget     = GetAttributeValue(partyTypesPairNodeAttributes, "conditionTarget", false);
                        partyTypePairs.Add(new PartyTypePair()
                        {
                            AllowedSource = allowedSource,
                            AllowedTarget = allowedTarget,
                            Description   = typePairDescription,
                            Title         = typePairTitle,
                            PartyRelationShipTypeDefault = typePairDefault == null ? true : Convert.ToBoolean(typePairDefault),
                            ConditionSource = conditionSource,
                            ConditionTarget = conditionTarget
                        });
                    }

                    var partyRelationshipType = partyRelationshipTypeManager.PartyRelationshipTypeRepository.Get(item => item.Title == title).FirstOrDefault();
                    //If there is not such a party relationship type
                    //It is mandatory to create at least one party type pair when we are creating a party type relation
                    //
                    if (partyRelationshipType == null)
                    {
                        partyRelationshipType = partyRelationshipTypeManager.Create(title, displayName, description, (indicatesHierarchy == null ? false : Convert.ToBoolean(indicatesHierarchy)), maxCardinality == null ? -1 : int.Parse(maxCardinality), minCardinality == null ? 0 : int.Parse(minCardinality), partyTypePairs.First().PartyRelationShipTypeDefault, partyTypePairs.First().AllowedSource, partyTypePairs.First().AllowedTarget,
                                                                                    partyTypePairs.First().Title, partyTypePairs.First().Description, partyTypePairs.First().ConditionSource, partyTypePairs.First().ConditionTarget);
                    }
                    else
                    {
                        partyRelationshipType = partyRelationshipTypeManager.Update(partyRelationshipType.Id, title, description, (indicatesHierarchy == null ? false : Convert.ToBoolean(indicatesHierarchy)), maxCardinality == null ? -1 : int.Parse(maxCardinality), minCardinality == null ? 0 : int.Parse(minCardinality));
                        UpdateOrCreatePartyTypePair(partyTypePairs.First(), partyRelationshipType, partyRelationshipTypeManager);
                    }
                    //If there are more than one partyTypepair exist
                    //if (partyTypePairs.Count() > 1)
                    foreach (var partyTypePair in partyTypePairs.Where(item => item != partyTypePairs.First()))
                    {
                        UpdateOrCreatePartyTypePair(partyTypePair, partyRelationshipType, partyRelationshipTypeManager);//
                    }
                }
            }
        }
        /// <summary>
        /// Update rules:
        /// Comparison for update is by the title of elements: title of elements are not editable
        /// if title of an element is changed because remove is forbiden here ,  it adds it as a new element and the old one will remain there
        ///
        /// </summary>
        private void ImportPartyTypes()
        {
            PartyTypeManager             partyTypeManager             = null;
            PartyManager                 partyManager                 = null;
            PartyRelationshipTypeManager partyRelationshipTypeManager = null;

            try
            {
                partyTypeManager             = new PartyTypeManager();
                partyManager                 = new PartyManager();
                partyRelationshipTypeManager = new PartyRelationshipTypeManager();
                var         filePath = Path.Combine(AppConfiguration.GetModuleWorkspacePath("BAM"), "partyTypes.xml");
                XDocument   xDoc     = XDocument.Load(filePath);
                XmlDocument xmlDoc   = new XmlDocument();
                xmlDoc.Load(xDoc.CreateReader());
                var partyTypesNodeList = xmlDoc.SelectNodes("//PartyTypes");

                var deleteAbleAttr       = new List <PartyCustomAttribute>();
                var deleteAbleAttrValues = new List <PartyCustomAttributeValue>();

                if (partyTypesNodeList.Count > 0)
                {
                    foreach (XmlNode partyTypeNode in partyTypesNodeList[0].ChildNodes)
                    {
                        if (!(partyTypeNode is XmlElement))
                        {
                            continue;
                        }

                        //Convert xmAttributeCollection to list to skipt the case sensitive and null problems
                        var attributes = new List <XmlAttribute>();
                        foreach (XmlAttribute att in partyTypeNode.Attributes)
                        {
                            attributes.Add(att);
                        }
                        var title       = GetAttributeValue(attributes, "Name", true);
                        var displayName = GetAttributeValue(attributes, "DisplayName", false);
                        var systemType  = GetAttributeValue(attributes, "SystemType", true);
                        var partyType   = partyTypeManager.PartyTypeRepository.Get(item => item.Title == title).FirstOrDefault();
                        //If there is not such a party type
                        if (partyType == null)
                        {
                            var partyStatusTypes = new List <PartyStatusType>();
                            partyStatusTypes.Add(new PartyStatusType()
                            {
                                Name = "Created", Description = ""
                            });
                            partyType = partyTypeManager.Create(title, "Imported from partyTypes.xml", displayName, partyStatusTypes, (systemType == null ? false : Convert.ToBoolean(systemType)));
                            var customAttrs = new List <PartyCustomAttribute>();
                            foreach (XmlNode customAttrNode in partyTypeNode.ChildNodes)
                            {
                                if (!(customAttrNode is XmlElement))
                                {
                                    continue;
                                }

                                var customAttrNodeAttributes = new List <XmlAttribute>();
                                foreach (XmlAttribute att in customAttrNode.Attributes)
                                {
                                    customAttrNodeAttributes.Add(att);
                                }
                                PartyCustomAttribute partyCustomAttr = ParsePartyCustomAttribute(customAttrNodeAttributes);

                                customAttrs.Add(new PartyCustomAttribute()
                                {
                                    DataType        = partyCustomAttr.DataType,
                                    Description     = partyCustomAttr.Description,
                                    IsMain          = partyCustomAttr.IsMain,
                                    IsUnique        = partyCustomAttr.IsUnique,
                                    IsValueOptional = partyCustomAttr.IsValueOptional,
                                    Name            = partyCustomAttr.Name,
                                    PartyType       = partyType,
                                    ValidValues     = partyCustomAttr.ValidValues,
                                    DisplayName     = partyCustomAttr.DisplayName,
                                    Condition       = partyCustomAttr.Condition
                                });
                            }

                            if (!customAttrs.Any(c => c.IsMain))
                            {
                                customAttrs[0].IsMain = true;
                            }

                            foreach (var customAttr in customAttrs)
                            {
                                partyTypeManager.CreatePartyCustomAttribute(customAttr);
                            }
                        }
                        else //partytype exist
                        {
                            var newCustomAttrs      = new List <PartyCustomAttribute>();
                            var existingCustomAttrs = new List <PartyCustomAttribute>();

                            foreach (XmlNode customAttrNode in partyTypeNode.ChildNodes)
                            {
                                if (!(customAttrNode is XmlElement))
                                {
                                    continue;
                                }

                                var attributesList = new List <XmlAttribute>();
                                foreach (XmlAttribute att in customAttrNode.Attributes)
                                {
                                    attributesList.Add(att);
                                }

                                var customAttrName = GetAttributeValue(attributesList, "Name", true);
                                //create new custom attribute if there is not such a name
                                if (!partyType.CustomAttributes.Any(item => item.Name == customAttrName))
                                {
                                    var customAttrNodeAttributes = new List <XmlAttribute>();
                                    foreach (XmlAttribute att in customAttrNode.Attributes)
                                    {
                                        customAttrNodeAttributes.Add(att);
                                    }

                                    PartyCustomAttribute partyCustomAttr = ParsePartyCustomAttribute(customAttrNodeAttributes);
                                    newCustomAttrs.Add(new PartyCustomAttribute()
                                    {
                                        DataType        = partyCustomAttr.DataType,
                                        Description     = partyCustomAttr.Description,
                                        IsMain          = partyCustomAttr.IsMain,
                                        IsUnique        = partyCustomAttr.IsUnique,
                                        IsValueOptional = partyCustomAttr.IsValueOptional,
                                        Name            = customAttrName,
                                        PartyType       = partyType,
                                        ValidValues     = partyCustomAttr.ValidValues,
                                        DisplayName     = partyCustomAttr.DisplayName,
                                        Condition       = partyCustomAttr.Condition
                                    });
                                }
                                else //update if exist
                                {
                                    //add to existingCustomAttr list
                                    var existingAttr = partyType.CustomAttributes.Where(item => item.Name == customAttrName).FirstOrDefault();
                                    if (existingAttr != null)
                                    {
                                        existingCustomAttrs.Add(existingAttr);
                                    }
                                }
                            }// end foreach customAttrNode

                            if (!newCustomAttrs.Any(c => c.IsMain) && !partyType.CustomAttributes.Any(c => c.IsMain))
                            {
                                throw new Exception("There is no main field. Each party type needs at least one main field.");
                            }

                            // create all custom Attr´s
                            foreach (var customAttr in newCustomAttrs)
                            {
                                partyTypeManager.CreatePartyCustomAttribute(customAttr);
                            }

                            // Delete all attrs that are no longer in the partytype.xml
                            newCustomAttrs.AddRange(existingCustomAttrs);
                            var currentListOfAttr = partyType.CustomAttributes;

                            foreach (var attr in currentListOfAttr)
                            {
                                if (!newCustomAttrs.Any(a => a.Id.Equals(attr.Id)))
                                {
                                    deleteAbleAttr.Add(attr);
                                    //select all value that are created based on the attr
                                    // the values need to delete befor the attr itself
                                    deleteAbleAttrValues.AddRange(
                                        partyManager.PartyCustomAttributeValueRepository.Query()
                                        .Where(v => v.CustomAttribute.Id.Equals(attr.Id)));
                                }
                            }
                        }
                    }
                }
                var partyRelationshipTypesNodeList = xmlDoc.SelectNodes("//PartyRelationshipTypes");
                if (partyRelationshipTypesNodeList.Count > 0)
                {
                    foreach (XmlNode partyRelationshipTypesNode in partyRelationshipTypesNodeList[0].ChildNodes)
                    {
                        if (!(partyRelationshipTypesNode is XmlElement))
                        {
                            continue;
                        }

                        var customAttrNodeAttributes = new List <XmlAttribute>();
                        foreach (XmlAttribute att in partyRelationshipTypesNode.Attributes)
                        {
                            customAttrNodeAttributes.Add(att);
                        }

                        var title              = GetAttributeValue(customAttrNodeAttributes, "Name", true);
                        var displayName        = GetAttributeValue(customAttrNodeAttributes, "DisplayName", false);
                        var description        = GetAttributeValue(customAttrNodeAttributes, "Description", false);
                        var indicatesHierarchy = GetAttributeValue(customAttrNodeAttributes, "IndicatesHierarchy", true); // false;
                        var maxCardinality     = GetAttributeValue(customAttrNodeAttributes, "MaxCardinality", true);     // -1
                        var minCardinality     = GetAttributeValue(customAttrNodeAttributes, "MinCardinality", true);     // 0

                        //Import party type pairs
                        var partyTypePairs = new List <PartyTypePair>();
                        foreach (XmlNode partyTypesPairNode in partyRelationshipTypesNode.ChildNodes[0].ChildNodes)
                        {
                            var partyTypesPairNodeAttributes = new List <XmlAttribute>();
                            foreach (XmlAttribute att in partyTypesPairNode.Attributes)
                            {
                                partyTypesPairNodeAttributes.Add(att);
                            }
                            var allowedSourceTitle = GetAttributeValue(partyTypesPairNodeAttributes, "SourceType", true);
                            var allowedTargetTitle = GetAttributeValue(partyTypesPairNodeAttributes, "TargetType", true);
                            var allowedSource      = partyTypeManager.PartyTypeRepository.Get(item => item.Title.ToLower() == allowedSourceTitle.ToLower()).FirstOrDefault();
                            if (allowedSource == null)
                            {
                                throw new Exception("Error in importing party relationship types ! \r\n " + allowedSourceTitle + " is not a party type!!");
                            }
                            var allowedTarget = partyTypeManager.PartyTypeRepository.Get(item => item.Title.ToLower() == allowedTargetTitle.ToLower()).FirstOrDefault();
                            if (allowedTarget == null)
                            {
                                throw new Exception("Error in importing party relationship types ! \r\n " + allowedTargetTitle + " is not a party type!!");
                            }

                            var typePairTitle       = GetAttributeValue(partyTypesPairNodeAttributes, "Title", true);
                            var typePairDescription = GetAttributeValue(partyTypesPairNodeAttributes, "Description", false);
                            var typePairDefault     = GetAttributeValue(partyTypesPairNodeAttributes, "Default", true);
                            var conditionSource     = GetAttributeValue(partyTypesPairNodeAttributes, "conditionSource", false);
                            var conditionTarget     = GetAttributeValue(partyTypesPairNodeAttributes, "conditionTarget", false);
                            var permissionsTemplate = GetAttributeValue(partyTypesPairNodeAttributes, "permissionsTemplate", false);
                            partyTypePairs.Add(new PartyTypePair()
                            {
                                SourcePartyType = allowedSource,
                                TargetPartyType = allowedTarget,
                                Description     = typePairDescription,
                                Title           = typePairTitle,
                                PartyRelationShipTypeDefault = typePairDefault == null ? true : Convert.ToBoolean(typePairDefault),
                                ConditionSource    = conditionSource,
                                ConditionTarget    = conditionTarget,
                                PermissionTemplate = Helper.GetPermissionValue(permissionsTemplate)
                            });
                        }

                        var partyRelationshipType = partyRelationshipTypeManager.PartyRelationshipTypeRepository.Get(item => item.Title == title).FirstOrDefault();
                        //If there is not such a party relationship type
                        //It is mandatory to create at least one party type pair when we are creating a party type relation
                        //
                        if (partyRelationshipType == null)
                        {
                            partyRelationshipType = partyRelationshipTypeManager.Create(title, displayName, description, (indicatesHierarchy == null ? false : Convert.ToBoolean(indicatesHierarchy)), maxCardinality == null ? -1 : int.Parse(maxCardinality), minCardinality == null ? 0 : int.Parse(minCardinality), partyTypePairs.First().PartyRelationShipTypeDefault, partyTypePairs.First().SourcePartyType, partyTypePairs.First().TargetPartyType,
                                                                                        partyTypePairs.First().Title, partyTypePairs.First().Description, partyTypePairs.First().ConditionSource, partyTypePairs.First().ConditionTarget, partyTypePairs.First().PermissionTemplate);
                        }
                        else
                        {
                            partyRelationshipType = partyRelationshipTypeManager.Update(partyRelationshipType.Id, title, "", description, (indicatesHierarchy == null ? false : Convert.ToBoolean(indicatesHierarchy)), maxCardinality == null ? -1 : int.Parse(maxCardinality), minCardinality == null ? 0 : int.Parse(minCardinality));
                            UpdateOrCreatePartyTypePair(partyTypePairs.First(), partyRelationshipType, partyRelationshipTypeManager);
                        }
                        //If there are more than one partyTypepair exist
                        //if (partyTypePairs.Count() > 1)
                        foreach (var partyTypePair in partyTypePairs.Where(item => item != partyTypePairs.First()))
                        {
                            UpdateOrCreatePartyTypePair(partyTypePair, partyRelationshipType, partyRelationshipTypeManager);//
                        }
                    }
                }
                //Add all the custom Attribute names ao custom grid column of default user
                foreach (var partyType in partyTypeManager.PartyTypeRepository.Get(cc => !cc.SystemType))
                {
                    foreach (var partyCustomAttr in partyType.CustomAttributes)
                    {
                        partyManager.UpdateOrAddPartyGridCustomColumn(partyType, partyCustomAttr, null);
                    }
                    var partyRelationshipTypePairs = partyRelationshipTypeManager.PartyTypePairRepository.Get(cc => cc.SourcePartyType.Id == partyType.Id && !cc.TargetPartyType.SystemType);
                    foreach (var partyTypePair in partyRelationshipTypePairs)
                    {
                        partyManager.UpdateOrAddPartyGridCustomColumn(partyType, null, partyTypePair);
                    }
                }

                if (deleteAbleAttr.Any())
                {
                    //delete all existing PartyCustomAttrValues
                    deleteAbleAttrValues.ForEach(a => partyManager.RemovePartyCustomAttributeValue(a));

                    // Delete all GridColumns of the CustomAttribute
                    var listOfIds         = deleteAbleAttr.Select(d => d.Id);
                    var gridColumns       = partyManager.PartyCustomGridColumnsRepository.Get();
                    var listOfGridColumns = gridColumns.Where(c => c.CustomAttribute != null && listOfIds.Contains(c.CustomAttribute.Id)).ToList();

                    listOfGridColumns.ForEach(c => partyManager.RemovePartyGridCustomColumn(c.Id));

                    // add CustomAttribute Grid Columns
                    deleteAbleAttr.ForEach(a => partyTypeManager.DeletePartyCustomAttribute(a));
                }
            }
            catch (Exception ex)
            {
                LoggerFactory.LogCustom("SeedData Failed: " + ex.Message);
                throw;
            }
            finally
            {
                partyManager?.Dispose();
                partyTypeManager?.Dispose();
                partyRelationshipTypeManager?.Dispose();
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="partyTypePair"></param>
        /// <param name="partyRelationshipType"></param>
        /// <param name="partyRelationshipTypeManager"></param>
        private static void UpdateOrCreatePartyTypePair(PartyTypePair partyTypePair, PartyRelationshipType partyRelationshipType, PartyRelationshipTypeManager partyRelationshipTypeManager)
        {
            var entity = partyRelationshipTypeManager.PartyTypePairRepository.Get(item => item.Title == partyTypePair.Title && item.PartyRelationshipType.Id == partyRelationshipType.Id).FirstOrDefault();

            if (entity != null)
            {
                partyRelationshipTypeManager.UpdatePartyTypePair(entity.Id, partyTypePair.Title, partyTypePair.SourcePartyType, partyTypePair.TargetPartyType, partyTypePair.Description, "", "", partyTypePair.PartyRelationShipTypeDefault, entity.PartyRelationshipType, entity.PermissionTemplate);
            }
            else
            {
                partyRelationshipTypeManager.AddPartyTypePair(partyTypePair.Title, partyTypePair.SourcePartyType, partyTypePair.TargetPartyType, partyTypePair.Description, partyTypePair.PartyRelationShipTypeDefault, partyRelationshipType, partyTypePair.ConditionSource, partyTypePair.ConditionTarget, partyTypePair.PermissionTemplate);
            }
        }
Exemple #16
0
        public ActionResult UserRegistration()
        {
            PartyManager                 partyManager                 = null;
            PartyTypeManager             partyTypeManager             = null;
            PartyRelationshipTypeManager partyRelationshipTypeManager = null;
            UserManager userManager = null;

            try
            {
                if (!HttpContext.User.Identity.IsAuthenticated)
                {
                    return(RedirectToAction("Index", "Home"));
                }
                //Defined AccountPartyTypes vallue in web config format is like PartyType1:PartyTypePairTitle1-PartyTypePairTitle2,PartyType2
                var accountPartyTypes     = new List <string>();
                var partyTypeAccountModel = new PartyTypeAccountModel();
                partyManager                 = new PartyManager();
                partyTypeManager             = new PartyTypeManager();
                partyRelationshipTypeManager = new PartyRelationshipTypeManager();
                userManager = new UserManager();
                var allowedAccountPartyTypes = GetPartyTypesForAccount();
                if (allowedAccountPartyTypes == null)
                {
                    throw new Exception("Allowed party types for registration in setting.xml are not exist!");
                }
                //Split them by "," and split each one by ":"
                foreach (var allowedAccountPartyType in allowedAccountPartyTypes)
                {
                    var partyType = partyTypeManager.PartyTypeRepository.Get(item => item.Title == allowedAccountPartyType.Key).FirstOrDefault();
                    if (partyType == null)
                    {
                        throw new Exception("AccountPartyType format in app setting is not correct or this 'partyType' doesn't exist.");
                    }
                    var allowedPartyTypePairs = new Dictionary <string, PartyTypePair>();
                    if (allowedAccountPartyType.Value != null)
                    {
                        var partyRelationshipsType = partyRelationshipTypeManager.PartyRelationshipTypeRepository.Get(item => allowedAccountPartyType.Value.Contains(item.Title));
                        foreach (var partyRelationshipType in partyRelationshipsType)
                        {
                            //filter AssociatedPairs to allowed pairs
                            partyRelationshipType.AssociatedPairs = partyRelationshipType.AssociatedPairs.Where(item => partyType.Id == item.SourcePartyType.Id && item.TargetPartyType.Parties.Any()).ToList();
                            //try to find first type pair which has PartyRelationShipTypeDefault otherwise the first one
                            var defaultPartyTypePair = partyRelationshipType.AssociatedPairs.FirstOrDefault(item => item.PartyRelationShipTypeDefault);

                            if (defaultPartyTypePair == null)
                            {
                                defaultPartyTypePair = partyRelationshipType.AssociatedPairs.FirstOrDefault();
                            }
                            if (defaultPartyTypePair != null)
                            {
                                if (defaultPartyTypePair.TargetPartyType.Parties != null)
                                {
                                    defaultPartyTypePair.TargetPartyType.Parties = defaultPartyTypePair.TargetPartyType.Parties.OrderBy(item => item.Name).ToList(); // order parties by name
                                }
                                allowedPartyTypePairs.Add(partyRelationshipType.DisplayName, defaultPartyTypePair);
                            }
                        }
                    }
                    partyTypeAccountModel.PartyRelationshipsTypes.Add(partyType, allowedPartyTypePairs);
                }
                //Bind party if there is already a user associated to this party
                var userTask = userManager.FindByNameAsync(HttpContext.User.Identity.Name);
                userTask.Wait();
                var user = userTask.Result;
                partyTypeAccountModel.Party = partyManager.GetPartyByUser(user.Id);
                //TODO: Discuss . Current soloution is to navigate the user to edit party
                if (partyTypeAccountModel.Party != null)
                {
                    return(RedirectToAction("Edit"));
                }
                return(View("_userRegisterationPartial", partyTypeAccountModel));
            }
            finally
            {
                partyManager?.Dispose();
                partyTypeManager?.Dispose();
                partyRelationshipTypeManager?.Dispose();
                userManager?.Dispose();
            }
        }
Exemple #17
0
        internal static DataTable getPartyDataTable(PartyType partyType, List <Party> parties)
        {
            using (PartyRelationshipTypeManager partyRelationshipTypeManager = new PartyRelationshipTypeManager())
                using (PartyManager partyManager = new PartyManager())
                {
                    DataTable table = new DataTable();
                    table.Columns.Add("PartyId");
                    table.Columns.Add("PartyName");
                    table.Columns.Add("PartyTypeTitle");
                    table.Columns.Add("StartDate");
                    table.Columns.Add("EndDate");
                    table.Columns.Add("IsTemp");
                    var partyCustomGridColumnsRepository = partyManager.GetPartyCustomGridColumns(partyType.Id);
                    foreach (var partyCustomGridColumn in partyCustomGridColumnsRepository)
                    {
                        if (partyCustomGridColumn.CustomAttribute != null)
                        {
                            table.Columns.Add(partyCustomGridColumn.CustomAttribute.DisplayName.Replace(" ", "_"));
                        }
                        else
                        {
                            table.Columns.Add(partyCustomGridColumn.TypePair.Title.Replace(" ", "_"));
                        }
                    }

                    for (int i = 0; i < parties.Count(); i++)
                    {
                        DataRow row   = table.NewRow();
                        var     party = parties[i];
                        //var pivotTable = forDataPivot.ToPivotTable(party.CustomAttributeValues,
                        //       item => item.CustomAttribute.Name,
                        //       item => item.Party.Id,
                        //       items => items.Any() ? items.Sum(x => x.VersionNo) : 0);

                        row["PartyId"]        = party.Id;
                        row["PartyName"]      = party.Name;
                        row["PartyTypeTitle"] = party.PartyType.DisplayName;
                        row["StartDate"]      = (party.StartDate != null && party.StartDate < new DateTime(1000, 1, 1) ? "" : party.StartDate.ToShortDateString());
                        row["EndDate"]        = (party.EndDate != null && party.EndDate > new DateTime(3000, 1, 1) ? "" : party.EndDate.ToShortDateString());
                        row["IsTemp"]         = party.IsTemp;
                        foreach (var customAttributeValue in party.CustomAttributeValues)
                        {
                            if (partyCustomGridColumnsRepository.Any(cc => cc.CustomAttribute != null && (cc.CustomAttribute.Id == (customAttributeValue.CustomAttribute.Id))))
                            {
                                row[customAttributeValue.CustomAttribute.DisplayName.Replace(" ", "_")] = customAttributeValue.Value;
                            }
                        }
                        var partyRelationships = partyManager.PartyRelationshipRepository.Get(cc => (cc.SourceParty.Id == party.Id && !cc.PartyTypePair.TargetPartyType.SystemType));
                        foreach (var partyRelationship in partyRelationships)
                        {
                            if (partyCustomGridColumnsRepository.Any(cc => cc.TypePair != null && cc.TypePair.Id == (partyRelationship.PartyTypePair.Id)))
                            {
                                row[partyRelationship.PartyTypePair.Title.Replace(" ", "_")] += "[" + partyRelationship.TargetParty.Name + "] ";
                            }
                        }

                        table.Rows.Add(row);
                    }
                    return(table);
                }
        }
        //toDo put this function to DIM
        public void SetRelationships(long datasetid, long metadataStructureId, XmlDocument metadata)
        {
            using (PartyManager partyManager = new PartyManager())
                using (PartyTypeManager partyTypeManager = new PartyTypeManager())
                    using (PartyRelationshipTypeManager partyRelationshipTypeManager = new PartyRelationshipTypeManager())
                    {
                        try
                        {
                            using (var uow = this.GetUnitOfWork())
                            {
                                //check if mappings exist between system/relationships and the metadatastructure/attr
                                // get all party mapped nodes
                                IEnumerable <XElement> complexElements = XmlUtility.GetXElementsByAttribute("partyid", XmlUtility.ToXDocument(metadata));



                                // get releaionship type id for owner
                                var releationships = uow.GetReadOnlyRepository <PartyRelationshipType>().Get().Where(
                                    p => p.AssociatedPairs.Any(
                                        ap => ap.SourcePartyType.Title.ToLower().Equals("dataset") || ap.TargetPartyType.Title.ToLower().Equals("dataset")
                                        ));

                                foreach (XElement item in complexElements)
                                {
                                    if (item.HasAttributes)
                                    {
                                        long   sourceId = Convert.ToInt64(item.Attribute("id").Value);
                                        string type     = item.Attribute("type").Value;
                                        long   partyid  = Convert.ToInt64(item.Attribute("partyid").Value);

                                        LinkElementType sourceType = LinkElementType.MetadataNestedAttributeUsage;
                                        if (type.Equals("MetadataPackageUsage"))
                                        {
                                            sourceType = LinkElementType.MetadataPackageUsage;
                                        }

                                        foreach (var releationship in releationships)
                                        {
                                            // when mapping in both directions are exist
                                            if (MappingUtils.ExistMappings(sourceId, sourceType, releationship.Id, LinkElementType.PartyRelationshipType) &&
                                                MappingUtils.ExistMappings(releationship.Id, LinkElementType.PartyRelationshipType, sourceId, sourceType))
                                            {
                                                // create releationship

                                                // create a Party for the dataset
                                                var customAttributes = new Dictionary <String, String>();
                                                customAttributes.Add("Name", datasetid.ToString());
                                                customAttributes.Add("Id", datasetid.ToString());

                                                var datasetParty = partyManager.Create(partyTypeManager.PartyTypeRepository.Get(cc => cc.Title == "Dataset").First(), "[description]", null, null, customAttributes);
                                                var person       = partyManager.GetParty(partyid);


                                                var partyTpePair = releationship.PartyRelationships.FirstOrDefault().PartyTypePair;

                                                if (partyTpePair != null && person != null && datasetParty != null)
                                                {
                                                    partyManager.AddPartyRelationship(
                                                        datasetParty.Id,
                                                        person.Id,
                                                        "Owner Releationship",
                                                        "",
                                                        partyTpePair.Id

                                                        );
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
        }
Exemple #19
0
        public static LinkElementRootModel LoadfromSystem(LinkElementPostion rootModelType, MappingManager mappingManager)
        {
            //get all parties - complex
            using (PartyTypeManager partyTypeManager = new PartyTypeManager())
                using (PartyRelationshipTypeManager partyRelationshipTypeManager = new PartyRelationshipTypeManager())
                    using (EntityManager entityManager = new EntityManager())
                    {
                        LinkElementRootModel model = new LinkElementRootModel(LinkElementType.System, 0, "System", rootModelType);

                        LinkElement SystemRoot = mappingManager.GetLinkElement(0, LinkElementType.System);

                        long id        = 0;
                        long elementId = 0;
                        if (SystemRoot != null)
                        {
                            id        = SystemRoot.Id;
                            elementId = SystemRoot.ElementId;
                        }

                        LinkElementModel LEParent = new LinkElementModel(
                            id,
                            elementId,
                            LinkElementType.System,
                            "System", "",
                            rootModelType,
                            LinkElementComplexity.Complex,
                            "");

                        #region get party types

                        IEnumerable <PartyType> partyTypes = partyTypeManager.PartyTypeRepository.Get();

                        foreach (var pt in partyTypes)
                        {
                            LinkElementModel ptModel = createLinkElementModelType(pt, model, LEParent, mappingManager);
                            model.LinkElements.Add(ptModel);
                            //get all partyCustomTypeAttr -> simple
                            model.LinkElements.AddRange(createLinkElementModelPartyCustomType(pt, model, ptModel, mappingManager));
                        }

                        #endregion get party types

                        #region keys

                        //get all keys -> simple
                        foreach (Key value in Key.GetValues(typeof(Key)))
                        {
                            long linkElementId = GetId((int)value, LinkElementType.Key, mappingManager);
                            //string mask = GetMask((int)value, LinkElementType.Key);

                            LinkElementModel LEModel = new LinkElementModel(
                                linkElementId,
                                (int)value,
                                LinkElementType.Key, value.ToString(), "", model.Position, LinkElementComplexity.Simple, "");

                            LEModel.Parent = LEParent;

                            model.LinkElements.Add(LEModel);
                        }

                        #endregion keys

                        #region get all relationships

                        IEnumerable <PartyRelationshipType> relationshipTypes = partyRelationshipTypeManager.PartyRelationshipTypeRepository.Get();

                        foreach (PartyRelationshipType partyRelationshipType in relationshipTypes)
                        {
                            long value         = partyRelationshipType.Id;
                            long linkElementId = GetId(partyRelationshipType.Id, LinkElementType.Key, mappingManager);

                            LinkElementModel LEModel = new LinkElementModel(
                                linkElementId,
                                partyRelationshipType.Id,
                                LinkElementType.PartyRelationshipType,
                                partyRelationshipType.DisplayName,
                                "",
                                model.Position,
                                LinkElementComplexity.Simple,
                                "");

                            LEModel.Parent = LEParent;

                            model.LinkElements.Add(LEModel);
                        }

                        #endregion get all relationships

                        #region entities

                        foreach (Entity entity in entityManager.Entities)
                        {
                            long value         = entity.Id;
                            long linkElementId = GetId(entity.Id, LinkElementType.Entity, mappingManager);

                            LinkElementModel LEModel = new LinkElementModel(
                                linkElementId,
                                entity.Id,
                                LinkElementType.Entity,
                                entity.Name,
                                "",
                                model.Position,
                                LinkElementComplexity.Simple,
                                "");

                            LEModel.Parent = LEParent;

                            model.LinkElements.Add(LEModel);
                        }

                        //test

                        #endregion entities

                        //create container
                        model = CreateLinkElementContainerModels(model);

                        return(model);
                    }
        }