public void RelateEntities() { for (int i = 0; i < this.dataObject.Count; i++) { string joinKeyEntity1 = JoinResolver.BuildExistingCheckKey(this.dataObject[i], this.Configuration.Entity1Mapping, this.dataObject.Metadata); string joinKeyEntity2 = JoinResolver.BuildExistingCheckKey(this.dataObject[i], this.Configuration.Entity2Mapping, this.dataObject.Metadata); if (!this.existingEntities1.ContainsKey(joinKeyEntity1) || !this.existingEntities2.ContainsKey(joinKeyEntity2)) { continue; // TODO Log, that one of the 2 entities could not be resolved! } if (this.Configuration.MultipleFoundMode == N2NMultipleFoundMode.None && (this.existingEntities1[joinKeyEntity1].Length > 1 || this.existingEntities2[joinKeyEntity2].Length > 1)) { continue; // TODO Log, that more than one entities were resolved by this key } Guid entity1id = this.existingEntities1[joinKeyEntity1][0]; Guid entity2id = this.existingEntities2[joinKeyEntity2][0]; Crm2013Wrapper.Crm2013Wrapper.AssociateEntities(service, relationshipMetadata.SchemaName, entity1Metadata.LogicalName, entity1id, entity2Metadata.LogicalName, entity2id); if (StatusHelper.MustShowProgress(i, dataObject.Count) == true) { reportProgress(new SimpleProgressReport("Processed " + (i + 1) + " of " + dataObject.Count + " many2many-records")); } } }
public void DoJoinMarketingLists() { EntityMapperLight entityMapper = new EntityMapperLight(this.listEntityMetaData, this.dataObject.Metadata, this.Configuration.ListMapping); for (int i = 0; i < this.dataObject.Count; i++) { string joinKey = JoinResolver.BuildExistingCheckKey(this.dataObject[i], this.Configuration.ListMapping, this.dataObject.Metadata); if (existingLists.ContainsKey(joinKey)) { if (existingLists[joinKey].Length > 1) { throw new Exception("Multiple lists with the joinvalues " + joinKey.Replace("#", " ").TrimEnd(' ') + " exists."); } } else { if (this.Configuration.IfJoinUnsuccessful == OnUnsuccessfulJoin.CreateNew) { Entity list = new Entity("list"); entityMapper.MapAttributes(list, this.dataObject[i]); Marketinglist marketingList = ListHelper.CreateMarketingList(service, list, this.Configuration.ListMemberType); existingLists.Add(joinKey, new Guid[] { marketingList.ListId }); } else { throw new Exception("List with joinvalues " + joinKey.Replace("#", " ").TrimEnd(' ') + " does not exist."); } } AddMemberToList(existingLists[joinKey][0], this.dataObject[i]); } }
public void WriteData(IConnection connection, IDatabaseInterface databaseInterface, IDatastore dataObject, ReportProgressMethod reportProgress) { reportProgress(new SimpleProgressReport("Building logging database")); this.logger = new Logger(databaseInterface); this.logger.InitializeDatabase(); reportProgress(new SimpleProgressReport("Connection to crm")); CrmConnection crmConnection = (CrmConnection)connection.GetConnection(); this.service = new OrganizationService(crmConnection); this.dataObject = dataObject; reportProgress(new SimpleProgressReport("Load " + this.Configuration.EntityName + " metadata")); this.entityMetaData = Crm2013Wrapper.Crm2013Wrapper.GetEntityMetadata(service, this.Configuration.EntityName); reportProgress(new SimpleProgressReport("Resolve existing entityrecords")); JoinResolver entityResolver = new JoinResolver(this.service, entityMetaData, this.Configuration.DeleteMapping); this.existingEntityRecords = entityResolver.BuildMassResolverIndex(); for (int i = 0; i < this.dataObject.Count; i++) { string joinKey = JoinResolver.BuildExistingCheckKey(this.dataObject[i], this.Configuration.DeleteMapping, this.dataObject.Metadata); if (existingEntityRecords.ContainsKey(joinKey)) { var existingRecordIds = existingEntityRecords[joinKey]; if (existingRecordIds.Length == 1 || this.Configuration.MultipleFoundMode == DeleteInCrmMultipleFoundMode.DeleteAll) { string entityIds = string.Empty; string deletionFaults = string.Empty; foreach (Guid entityId in existingRecordIds) { entityIds += entityId.ToString() + ","; try { Crm2013Wrapper.Crm2013Wrapper.DeleteRecordInCrm(this.service, this.Configuration.EntityName, entityId); } catch (FaultException <OrganizationServiceFault> ex) { deletionFaults += ex.Detail.Message + "\n"; } } entityIds = entityIds.TrimEnd(','); logger.AddRecord(i, joinKey, entityIds, deletionFaults); } else { // TODO Log that multiplerecords were found but none deleted } } else { // TODO Log that no record was found to delete } } }
public void AddMemberToList(Guid listId, object [] currentRecord) { string joinKey = JoinResolver.BuildExistingCheckKey(currentRecord, this.Configuration.ListMemberMapping, this.dataObject.Metadata); foreach (Guid memberId in this.existingMembers[joinKey]) { AddMemberListRequest addMemberListRequest = new AddMemberListRequest(); addMemberListRequest.ListId = listId; addMemberListRequest.EntityId = memberId; this.service.Execute(addMemberListRequest); } }
public void SetRelation(string relationMappingLogicalName, Entity[] sourceEntities, IDatastore dataObject, Dictionary <string, Guid[]> relatedEntities) { EntityMapper entityMapper = new EntityMapper(relatedEntityMetadata.GetAttributeMetadata(), dataObject.Metadata, relationMappings, null); for (int i = 0; i < sourceEntities.Length; i++) { Entity relatedEntity = new Entity(); entityMapper.MapAttributes(relatedEntity, dataObject[i]); string relatedEntityKey = JoinResolver.BuildExistingCheckKey(relatedEntity, attributeMetadataDictionary); if (relatedEntities.ContainsKey(relatedEntityKey)) { // TODO Check if attribute has already been mapped (cause multiple relations on one attribute) sourceEntities[i].Attributes.Add(relationMappingLogicalName, new EntityReference(this.relatedEntityMetadata.LogicalName, relatedEntities[relatedEntityKey][0])); } else { //throw new Exception("Could not resolve related entity with key " + relatedEntityKey); } } }