internal static Dictionary <PartyCustomAttribute, string> toPartyCustomAttributeValues(Dictionary <string, string> partyCustomAttributeValuesDict, long partyTypeId = 0) { var partyCustomAttributeValues = new Dictionary <PartyCustomAttribute, string>(); using (PartyTypeManager partyTypeManager = new PartyTypeManager()) { foreach (var partyCustomAttributeValueDict in partyCustomAttributeValuesDict) { long id = 0; var partyCustomAttribute = new PartyCustomAttribute(); if (!long.TryParse(partyCustomAttributeValueDict.Key, out id)) { if (partyTypeId == 0) { throw new Exception("Party type id should not be zero when you use custom attribute name"); } partyCustomAttribute = partyTypeManager.PartyCustomAttributeRepository.Get(cc => cc.Name.ToLower() == partyCustomAttributeValueDict.Key.ToLower() && cc.PartyType.Id == partyTypeId).FirstOrDefault(); if (partyCustomAttribute == null) { throw new Exception(partyCustomAttributeValueDict.Key + " is not exist in custom attributes."); } } else { partyCustomAttribute = partyTypeManager.PartyCustomAttributeRepository.Get(id); } if (partyCustomAttribute == null) { throw new Exception("No custom attributes with this Id " + id); } partyCustomAttributeValues.Add(partyCustomAttribute, partyCustomAttributeValueDict.Value); } return(partyCustomAttributeValues); } }
public PartyCustomAttribute UpdatePartyCustomAttribute(PartyCustomAttribute partyCustomAttribute) { Contract.Requires(partyCustomAttribute != null, "Provided entities can not be null"); Contract.Requires(partyCustomAttribute.Id >= 0, "Provided entitities must have a permanent ID"); Contract.Ensures(Contract.Result <PartyCustomAttribute>() != null && Contract.Result <PartyCustomAttribute>().Id >= 0); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository <PartyCustomAttribute> repo = uow.GetRepository <PartyCustomAttribute>(); //Name is unique for PartyCustomAttribute with the same party type if (partyCustomAttribute.PartyType.CustomAttributes.Where(item => item.Name == partyCustomAttribute.Name && item.Id != partyCustomAttribute.Id).Count() > 0) { BexisException.Throw(partyCustomAttribute, "This name for this type of 'PartyCustomAttribute' is already exist.", BexisException.ExceptionType.Add); } //Calculate displayorder //if find the same displayOrder then it pushes the other items with the same displayOrder or greater than if (partyCustomAttribute.PartyType.CustomAttributes.FirstOrDefault(item => item.DisplayOrder == partyCustomAttribute.DisplayOrder && partyCustomAttribute.Id != item.Id) != null) { partyCustomAttribute.PartyType.CustomAttributes.Where(item => item.DisplayOrder >= partyCustomAttribute.DisplayOrder && partyCustomAttribute.Id != item.Id) .ToList().ForEach(item => item.DisplayOrder = item.DisplayOrder + 1); } repo.Merge(partyCustomAttribute); var merged = repo.Get(partyCustomAttribute.Id); repo.Put(merged); uow.Commit(); return(merged); } }
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); } }
[Test()] public void CreateCustomAttributeTest() { PartyTypeManager partyTypeManager = new PartyTypeManager(); try { var partyStatusTypes = new List <PartyStatusType>(); partyStatusTypes.Add(new PartyStatusType() { Name = "Created", Description = "" }); var partyType = partyTypeManager.Create("partyTypetest", "test", "party type test", partyStatusTypes); long partyTypeId = partyType.Id; partyType.Should().NotBeNull(); var partyCustomAttribute = partyTypeManager.CreatePartyCustomAttribute(partyType, "string", "Name", "nothing", "validVals", "noCondition", false, false, true); long partyCustomAttributeId = partyCustomAttribute.Id; partyCustomAttribute.Should().NotBeNull(); partyCustomAttribute.Id.Should().BeGreaterThan(0); var fetchedPartyCustomAttribute = partyTypeManager.PartyCustomAttributeRepository.Get(partyCustomAttribute.Id); partyCustomAttribute.Condition.Should().BeEquivalentTo(fetchedPartyCustomAttribute.Condition); partyCustomAttribute.DataType.Should().BeEquivalentTo(fetchedPartyCustomAttribute.DataType); partyCustomAttribute.DisplayName.Should().BeEquivalentTo(fetchedPartyCustomAttribute.DisplayName); partyCustomAttribute.Description.Should().BeEquivalentTo(fetchedPartyCustomAttribute.Description); partyCustomAttribute.DisplayOrder.Should().Equals(fetchedPartyCustomAttribute.DisplayOrder); partyCustomAttribute.IsMain.Should().Equals(fetchedPartyCustomAttribute.IsMain); partyCustomAttribute.IsUnique.Should().Equals(fetchedPartyCustomAttribute.IsUnique); partyCustomAttribute.DisplayOrder.Should().Equals(fetchedPartyCustomAttribute.DisplayOrder); partyTypeManager.DeletePartyCustomAttribute(partyCustomAttribute);// cleanup the DB var partyCustomAttributeAfterDelete = partyTypeManager.PartyCustomAttributeRepository.Get(cc => cc.Id == partyCustomAttributeId).FirstOrDefault(); partyCustomAttributeAfterDelete.Should().BeNull(); var objPartyCustomAttribute = new PartyCustomAttribute() { Condition = "condition", DataType = "datatype", Description = "description" , DisplayName = "displayname", DisplayOrder = 1, IsMain = false, IsUnique = false, IsValueOptional = false, Name = "name", PartyType = partyType, ValidValues = "someVals" }; var partyCustomAttributeFromObject = partyTypeManager.CreatePartyCustomAttribute(objPartyCustomAttribute); var fetchedPartyCustomAttributeFromObject = partyTypeManager.PartyCustomAttributeRepository.Get(partyCustomAttributeFromObject.Id); partyCustomAttributeFromObject.Condition.Should().BeEquivalentTo(fetchedPartyCustomAttributeFromObject.Condition); partyCustomAttributeFromObject.DataType.Should().BeEquivalentTo(fetchedPartyCustomAttributeFromObject.DataType); partyCustomAttributeFromObject.DisplayName.Should().BeEquivalentTo(fetchedPartyCustomAttributeFromObject.DisplayName); partyCustomAttributeFromObject.Description.Should().BeEquivalentTo(fetchedPartyCustomAttributeFromObject.Description); partyCustomAttributeFromObject.DisplayOrder.Should().Equals(fetchedPartyCustomAttributeFromObject.DisplayOrder); partyCustomAttributeFromObject.IsMain.Should().Equals(fetchedPartyCustomAttributeFromObject.IsMain); partyCustomAttributeFromObject.IsUnique.Should().Equals(fetchedPartyCustomAttributeFromObject.IsUnique); partyCustomAttributeFromObject.DisplayOrder.Should().Equals(fetchedPartyCustomAttributeFromObject.DisplayOrder); partyTypeManager.DeletePartyCustomAttribute(partyCustomAttributeFromObject);// cleanup the DB partyTypeManager.Delete(partyType);// var partyTypeAfterDelete = partyTypeManager.PartyTypeRepository.Get(cc => cc.Id == partyTypeId).FirstOrDefault(); partyTypeAfterDelete.Should().BeNull(); } finally { partyTypeManager.Dispose(); } }
public PartyCustomAttribute CreatePartyCustomAttribute(PartyCustomAttribute partyCustomeAttribute) { Contract.Requires(partyCustomeAttribute != null); Contract.Requires(partyCustomeAttribute.PartyType != null); Contract.Requires(!string.IsNullOrWhiteSpace(partyCustomeAttribute.Name)); Contract.Ensures(Contract.Result <PartyCustomAttribute>() != null && Contract.Result <PartyCustomAttribute>().Id >= 0); var entity = new PartyCustomAttribute() { DataType = partyCustomeAttribute.DataType.ToLower(), Description = partyCustomeAttribute.Description, PartyType = partyCustomeAttribute.PartyType, ValidValues = partyCustomeAttribute.ValidValues, IsValueOptional = partyCustomeAttribute.IsValueOptional, IsUnique = partyCustomeAttribute.IsUnique, IsMain = partyCustomeAttribute.IsMain, Name = partyCustomeAttribute.Name, Condition = partyCustomeAttribute.Condition }; using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository <PartyCustomAttribute> repo = uow.GetRepository <PartyCustomAttribute>(); //Name is unique for PartyCustomAttribute with the same party type if (repo.Get(item => item.Name == partyCustomeAttribute.Name && item.PartyType == partyCustomeAttribute.PartyType).Count > 0) { BexisException.Throw(entity, "This name for this type of 'PartyCustomAttribute' is already exist.", BexisException.ExceptionType.Add); } //Calculate displayorder var partyCustomAttrs = repo.Get(item => item.PartyType == partyCustomeAttribute.PartyType); if (partyCustomAttrs.Count() == 0) { entity.DisplayOrder = 0; } //if displayOrder is null then it goes to the last else if (partyCustomeAttribute.DisplayOrder == 0) { entity.DisplayOrder = partyCustomAttrs.Max(item => item.DisplayOrder) + 1; } //else it push the other items with the same displayOrder or greater than else { entity.DisplayOrder = partyCustomeAttribute.DisplayOrder; partyCustomAttrs.Where(item => item.DisplayOrder >= partyCustomeAttribute.DisplayOrder) .ToList().ForEach(item => item.DisplayOrder = item.DisplayOrder + 1); } repo.Put(entity); uow.Commit(); } return(entity); }
private void createFromPartyTypeMapping( string simpleNodeName, LinkElementType simpleType, string complexNodeName, LinkElementType complexType, PartyCustomAttribute partyCustomAttr, PartyType partyType, Mapping root, XDocument metadataRef, MappingManager mappingManager, TransformationRule transformationRule = null) { if (transformationRule == null) { transformationRule = new TransformationRule(); } LinkElement le = createLinkELementIfNotExist(mappingManager, Convert.ToInt64(partyType.Id), partyType.Title, LinkElementType.PartyType, LinkElementComplexity.Complex); XElement complex = getXElements(complexNodeName, metadataRef).FirstOrDefault(); string sIdComplex = complex.Attribute("id").Value; string nameComplex = complex.Attribute("name").Value; LinkElement tmpComplexElement = createLinkELementIfNotExist(mappingManager, Convert.ToInt64(sIdComplex), nameComplex, complexType, LinkElementComplexity.Complex); Mapping complexMapping = MappingHelper.CreateIfNotExistMapping(le, tmpComplexElement, 1, new TransformationRule(), root, mappingManager); IEnumerable <XElement> simpleElements = XmlUtility.GetAllChildren(complex).Where(s => s.Name.LocalName.Equals(simpleNodeName)); LinkElement simpleLe = createLinkELementIfNotExist(mappingManager, Convert.ToInt64(partyCustomAttr.Id), partyCustomAttr.Name, LinkElementType.PartyCustomType, LinkElementComplexity.Simple); foreach (XElement xElement in simpleElements) { string sId = xElement.Attribute("id").Value; string name = xElement.Attribute("name").Value; LinkElement tmp = createLinkELementIfNotExist(mappingManager, Convert.ToInt64(sId), name, simpleType, LinkElementComplexity.Simple); MappingHelper.CreateIfNotExistMapping(simpleLe, tmp, 2, transformationRule, complexMapping, mappingManager); } }
/// <summary> /// /// </summary> /// <param name="targetElementId"></param> /// <param name="targetElementType"></param> /// <param name="parentPartyId"></param> /// <returns></returns> public static bool PartyAttrIsMain(long targetElementId, LinkElementType targetElementType) { try { //get all mapppings where target is mapped // LinkElementType.PartyCustomType is set because of the function name // all mapped attributes are LinkElementType.PartyCustomType in this case using (IUnitOfWork uow = (new object()).GetUnitOfWork()) { List <MappingPartyResultElemenet> tmp = new List <MappingPartyResultElemenet>(); //Select all mappings where the target is mapped to a party custom attr with the party id IList <Entities.Mapping.Mapping> mapping = CachedMappings(); var mapping_result = mapping.Where(m => m.Target.ElementId.Equals(targetElementId) && m.Target.Type.Equals(targetElementType) && m.Source.Type.Equals(LinkElementType.PartyCustomType) && m.Parent != null ); foreach (var mapping_element in mapping_result) { if (mapping != null) { PartyCustomAttribute pca = uow.GetReadOnlyRepository <PartyCustomAttribute>().Get(mapping_element.Source.ElementId); if (pca != null && pca.IsMain == true) { return(true); } } } return(false); } } catch (Exception ex) { throw ex; } }
public bool DeletePartyCustomAttribute(PartyCustomAttribute entity) { Contract.Requires(entity != null); Contract.Requires(entity.Id >= 0); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository <PartyCustomAttribute> repo = uow.GetRepository <PartyCustomAttribute>(); IRepository <PartyCustomAttributeValue> repoCAV = uow.GetRepository <PartyCustomAttributeValue>(); entity = repo.Reload(entity); //Prevent of deleting if there is a 'customAttributeVaue' for this entity if (entity.CustomAttributeValues.Count() > 0) { BexisException.Throw(entity, "There is one or more 'customAttributeVaue' for this entity.", BexisException.ExceptionType.Delete); } //delete the entity repo.Delete(entity); // commit changes uow.Commit(); } // if any problem was detected during the commit, an exception will be thrown! return(true); }
/// <summary> /// add a single custom attribute value to a party object /// /// It's not checking uniqeness when it is not for a single custom attribute because it couldn't predict other values--> it should have all values to make a hash /// </summary> /// <param name="party"></param> /// <param name="partyCustomAttribute"></param> /// <param name="value"></param> /// <returns></returns> public PartyCustomAttributeValue AddPartyCustomAttributeValue(ref PartyX party, PartyCustomAttribute partyCustomAttribute, string value) { // create a dictionary to pass along var dic = new Dictionary <PartyCustomAttribute, string> { { partyCustomAttribute, value } }; // pass along var result = AddPartyCustomAttributeValues(ref party, dic); // find the corresponding attribute in the result return(result.Where((item) => (item.CustomAttribute == partyCustomAttribute) && (item.Value == value)).FirstOrDefault()); }
/// <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> /// 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);// } } } }
private void createPartyTypeMappings() { object tmp = ""; List <MetadataStructure> metadataStructures = tmp.GetUnitOfWork().GetReadOnlyRepository <MetadataStructure>().Get().ToList(); List <PartyType> partyTypes = tmp.GetUnitOfWork().GetReadOnlyRepository <PartyType>().Get().ToList(); List <PartyCustomAttribute> partyCustomAttrs = tmp.GetUnitOfWork().GetReadOnlyRepository <PartyCustomAttribute>().Get().ToList(); MappingManager mappingManager = new MappingManager(); XmlMetadataWriter xmlMetadataWriter = new XmlMetadataWriter(XmlNodeMode.xPath); try { #region ABCD BASIC if (metadataStructures.Any(m => m.Name.ToLower().Equals("basic abcd"))) { MetadataStructure metadataStructure = metadataStructures.FirstOrDefault(m => m.Name.ToLower().Equals("basic abcd")); XDocument metadataRef = xmlMetadataWriter.CreateMetadataXml(metadataStructure.Id); //create root mapping LinkElement abcdRoot = createLinkELementIfNotExist(mappingManager, metadataStructure.Id, metadataStructure.Name, LinkElementType.MetadataStructure, LinkElementComplexity.None); //create system mapping LinkElement system = createLinkELementIfNotExist(mappingManager, 0, "System", LinkElementType.System, LinkElementComplexity.None); #region mapping ABCD BASIC to System Keys Mapping rootTo = MappingHelper.CreateIfNotExistMapping(abcdRoot, system, 0, null, null, mappingManager); Mapping rootFrom = MappingHelper.CreateIfNotExistMapping(system, abcdRoot, 0, null, null, mappingManager); // create mapping for paryttypes #region person if (partyTypes.Any(p => p.Title.Equals("Person"))) { PartyType partyType = partyTypes.FirstOrDefault(p => p.Title.Equals("Person")); //FirstName string complexAttrName = "MicroAgentP"; if (partyCustomAttrs.Any( pAttr => pAttr.Name.Equals("FirstName") && pAttr.PartyType.Id.Equals(partyType.Id))) { PartyCustomAttribute partyCustomAttribute = partyCustomAttrs.FirstOrDefault( pAttr => pAttr.Name.Equals("FirstName") && pAttr.PartyType.Id.Equals(partyType.Id)); createToPartyTypeMapping( "Name", LinkElementType.MetadataNestedAttributeUsage, complexAttrName, LinkElementType.ComplexMetadataAttribute, partyCustomAttribute, partyType, rootTo, metadataRef, mappingManager, new TransformationRule(@"\w+", "Name[0]")); createFromPartyTypeMapping( "Name", LinkElementType.MetadataNestedAttributeUsage, complexAttrName, LinkElementType.ComplexMetadataAttribute, partyCustomAttribute, partyType, rootFrom, metadataRef, mappingManager, new TransformationRule(@"\w+", "FirstName[0] LastName[0]")); } if (partyCustomAttrs.Any( pAttr => pAttr.Name.Equals("LastName") && pAttr.PartyType.Id.Equals(partyType.Id))) { PartyCustomAttribute partyCustomAttribute = partyCustomAttrs.FirstOrDefault( pAttr => pAttr.Name.Equals("LastName") && pAttr.PartyType.Id.Equals(partyType.Id)); createToPartyTypeMapping( "Name", LinkElementType.MetadataNestedAttributeUsage, complexAttrName, LinkElementType.ComplexMetadataAttribute, partyCustomAttribute, partyType, rootTo, metadataRef, mappingManager, new TransformationRule(@"\w+", "Name[1]")); createFromPartyTypeMapping( "Name", LinkElementType.MetadataNestedAttributeUsage, complexAttrName, LinkElementType.ComplexMetadataAttribute, partyCustomAttribute, partyType, rootFrom, metadataRef, mappingManager, new TransformationRule(@"\w+", "FirstName[0] LastName[0]")); } //if (partyCustomAttrs.Any( // pAttr => pAttr.Name.Equals("Address") && pAttr.PartyType.Id.Equals(partyType.Id))) //{ // PartyCustomAttribute partyCustomAttribute = partyCustomAttrs.FirstOrDefault( // pAttr => pAttr.Name.Equals("Address") && pAttr.PartyType.Id.Equals(partyType.Id)); // createToPartyTypeMapping( // "Address", LinkElementType.MetadataNestedAttributeUsage, // complexAttrName, LinkElementType.ComplexMetadataAttribute, // partyCustomAttribute, partyType, rootTo, metadataRef, // mappingManager, // new TransformationRule()); // createFromPartyTypeMapping( // "Address", LinkElementType.MetadataNestedAttributeUsage, // complexAttrName, LinkElementType.ComplexMetadataAttribute, // partyCustomAttribute, partyType, rootFrom, metadataRef, // mappingManager, // new TransformationRule()); //} if (partyCustomAttrs.Any( pAttr => pAttr.Name.Equals("Phone") && pAttr.PartyType.Id.Equals(partyType.Id))) { PartyCustomAttribute partyCustomAttribute = partyCustomAttrs.FirstOrDefault( pAttr => pAttr.Name.Equals("Phone") && pAttr.PartyType.Id.Equals(partyType.Id)); createToPartyTypeMapping( "Phone", LinkElementType.MetadataNestedAttributeUsage, complexAttrName, LinkElementType.ComplexMetadataAttribute, partyCustomAttribute, partyType, rootTo, metadataRef, mappingManager, new TransformationRule()); createFromPartyTypeMapping( "Phone", LinkElementType.MetadataNestedAttributeUsage, complexAttrName, LinkElementType.ComplexMetadataAttribute, partyCustomAttribute, partyType, rootFrom, metadataRef, mappingManager, new TransformationRule()); } } #endregion #region Organisation if (partyTypes.Any(p => p.Title.Equals("Organization"))) { PartyType partyType = partyTypes.FirstOrDefault(p => p.Title.Equals("Organization")); //FirstName string complexAttrName = "Metadata/Metadata/MetadataType/Owners/OwnersType/Owner/Contact/Organisation/Organisation/Name/Label/Representation/RepresentationType"; if (partyCustomAttrs.Any( pAttr => pAttr.Name.Equals("Name") && pAttr.PartyType.Id.Equals(partyType.Id))) { PartyCustomAttribute partyCustomAttribute = partyCustomAttrs.FirstOrDefault( pAttr => pAttr.Name.Equals("Name") && pAttr.PartyType.Id.Equals(partyType.Id)); createToPartyTypeMapping( "Text", LinkElementType.MetadataNestedAttributeUsage, complexAttrName, LinkElementType.ComplexMetadataAttribute, partyCustomAttribute, partyType, rootTo, metadataRef, mappingManager, new TransformationRule()); createFromPartyTypeMapping( "Text", LinkElementType.MetadataNestedAttributeUsage, complexAttrName, LinkElementType.ComplexMetadataAttribute, partyCustomAttribute, partyType, rootFrom, metadataRef, mappingManager, new TransformationRule()); } } #endregion #region Project #endregion #region Insitute #endregion #region Project #endregion #endregion } #endregion #region GBIF if (metadataStructures.Any(m => m.Name.ToLower().Equals("gbif"))) { MetadataStructure metadataStructure = metadataStructures.FirstOrDefault(m => m.Name.ToLower().Equals("gbif")); XDocument metadataRef = xmlMetadataWriter.CreateMetadataXml(metadataStructure.Id); //create root mapping LinkElement abcdRoot = createLinkELementIfNotExist(mappingManager, metadataStructure.Id, metadataStructure.Name, LinkElementType.MetadataStructure, LinkElementComplexity.None); //create system mapping LinkElement system = createLinkELementIfNotExist(mappingManager, 0, "System", LinkElementType.System, LinkElementComplexity.None); #region mapping ABCD BASIC to System Keys Mapping rootTo = MappingHelper.CreateIfNotExistMapping(abcdRoot, system, 0, null, null, mappingManager); Mapping rootFrom = MappingHelper.CreateIfNotExistMapping(system, abcdRoot, 0, null, null, mappingManager); // create mapping for paryttypes #region person if (partyTypes.Any(p => p.Title.Equals("Person"))) { PartyType partyType = partyTypes.FirstOrDefault(p => p.Title.Equals("Person")); //FirstName string complexAttrName = "individualNameType"; if (partyCustomAttrs.Any( pAttr => pAttr.Name.Equals("FirstName") && pAttr.PartyType.Id.Equals(partyType.Id))) { PartyCustomAttribute partyCustomAttribute = partyCustomAttrs.FirstOrDefault( pAttr => pAttr.Name.Equals("FirstName") && pAttr.PartyType.Id.Equals(partyType.Id)); createToPartyTypeMapping( "givenName", LinkElementType.MetadataNestedAttributeUsage, complexAttrName, LinkElementType.ComplexMetadataAttribute, partyCustomAttribute, partyType, rootTo, metadataRef, mappingManager, new TransformationRule()); createFromPartyTypeMapping( "givenName", LinkElementType.MetadataNestedAttributeUsage, complexAttrName, LinkElementType.ComplexMetadataAttribute, partyCustomAttribute, partyType, rootFrom, metadataRef, mappingManager, new TransformationRule()); } if (partyCustomAttrs.Any( pAttr => pAttr.Name.Equals("LastName") && pAttr.PartyType.Id.Equals(partyType.Id))) { PartyCustomAttribute partyCustomAttribute = partyCustomAttrs.FirstOrDefault( pAttr => pAttr.Name.Equals("LastName") && pAttr.PartyType.Id.Equals(partyType.Id)); createToPartyTypeMapping( "surName", LinkElementType.MetadataNestedAttributeUsage, complexAttrName, LinkElementType.ComplexMetadataAttribute, partyCustomAttribute, partyType, rootTo, metadataRef, mappingManager, new TransformationRule()); createFromPartyTypeMapping( "surName", LinkElementType.MetadataNestedAttributeUsage, complexAttrName, LinkElementType.ComplexMetadataAttribute, partyCustomAttribute, partyType, rootFrom, metadataRef, mappingManager, new TransformationRule()); } } #endregion #region Project if (partyTypes.Any(p => p.Title.Equals("Project"))) { PartyType partyType = partyTypes.FirstOrDefault(p => p.Title.Equals("Project")); //FirstName string complexAttrName = "project"; if (partyCustomAttrs.Any( pAttr => pAttr.Name.Equals("Name") && pAttr.PartyType.Id.Equals(partyType.Id))) { PartyCustomAttribute partyCustomAttribute = partyCustomAttrs.FirstOrDefault( pAttr => pAttr.Name.Equals("Name") && pAttr.PartyType.Id.Equals(partyType.Id)); createToPartyTypeMapping( "title", LinkElementType.MetadataNestedAttributeUsage, complexAttrName, LinkElementType.MetadataPackageUsage, partyCustomAttribute, partyType, rootTo, metadataRef, mappingManager, new TransformationRule()); createFromPartyTypeMapping( "title", LinkElementType.MetadataNestedAttributeUsage, complexAttrName, LinkElementType.MetadataPackageUsage, partyCustomAttribute, partyType, rootFrom, metadataRef, mappingManager, new TransformationRule()); } } #endregion #region Insitute #endregion #region Project #endregion #endregion } #endregion } catch (Exception ex) { throw ex; } finally { mappingManager.Dispose(); } }