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 WriteData(IConnection connection, IDatabaseInterface databaseInterface, IDatastore dataObject, ReportProgressMethod reportProgress) { this.reportProgress = reportProgress; reportProgress(new SimpleProgressReport("Connection to crm")); this.service = connection.GetConnection() as IOrganizationService; this.dataObject = dataObject; reportProgress(new SimpleProgressReport("Load marketinglist metadata")); this.listEntityMetaData = Crm2013Wrapper.Crm2013Wrapper.GetEntityMetadata(service, "list"); reportProgress(new SimpleProgressReport("Resolve existing marketinglists")); JoinResolver listResolver = new JoinResolver(this.service, listEntityMetaData, this.Configuration.ListMapping); this.existingLists = listResolver.BuildMassResolverIndex(); reportProgress(new SimpleProgressReport("Load members metadata")); EntityMetadata memberEntityMetaData = Crm2013Wrapper.Crm2013Wrapper.GetEntityMetadata(service, this.Configuration.ListMemberType.ToString().ToLower()); reportProgress(new SimpleProgressReport("Resolve listmembers")); JoinResolver memberResolver = new JoinResolver(this.service, memberEntityMetaData, this.Configuration.ListMemberMapping); this.existingMembers = memberResolver.BuildMassResolverIndex(); switch (this.Configuration.JoinList) { case MarketinglistJoinType.Manual: DoManualMarketingList(); break; case MarketinglistJoinType.Join: DoJoinMarketingLists(); break; } }
public void WriteData(IConnection connection, IDatabaseInterface databaseInterface, IDatastore dataObject, ReportProgressMethod reportProgress) { this.reportProgress = reportProgress; reportProgress(new SimpleProgressReport("Connection to crm")); CrmConnection crmConnection = (CrmConnection)connection.GetConnection(); this.service = new OrganizationService(crmConnection); this.dataObject = dataObject; reportProgress(new SimpleProgressReport("Load required metadata from crm")); entity1Metadata = Crm2013Wrapper.Crm2013Wrapper.GetEntityMetadata(this.service, this.Configuration.Entity1Name); entity2Metadata = Crm2013Wrapper.Crm2013Wrapper.GetEntityMetadata(this.service, this.Configuration.Entity2Name.Split(';')[0]); relationshipMetadata = Crm2013Wrapper.Crm2013Wrapper.GetRelationshipMetadata(this.service, this.Configuration.Entity2Name.Split(';')[1]) as ManyToManyRelationshipMetadata; reportProgress(new SimpleProgressReport("Cache keys of existing " + entity1Metadata.LogicalName + "-records")); var entity1Resolver = new JoinResolver(this.service, entity1Metadata, this.Configuration.Entity1Mapping); this.existingEntities1 = entity1Resolver.BuildMassResolverIndex(); reportProgress(new SimpleProgressReport("Cache keys of existing " + entity2Metadata.LogicalName + "-records")); var entity2Resolver = new JoinResolver(this.service, entity2Metadata, this.Configuration.Entity2Mapping); this.existingEntities2 = entity2Resolver.BuildMassResolverIndex(); RelateEntities(); }
private static void BuildJoinList(StringBuilder builder, List <DbAttribute> attributes) { var tables = attributes .Select(a => a.TableName) .Distinct() .ToList(); var resolver = new JoinResolver(); var joins = resolver.Resolve(tables); foreach (var @join in joins) { builder.Append("\njoin "); builder.Append("\""); builder.Append(join.ForeignTable); builder.Append("\" on \""); builder.Append(join.ForeignTable); builder.Append("\".\""); builder.Append(join.ForeignColumn); builder.Append("\" = \""); builder.Append(join.ThisTable); builder.Append("\".\""); builder.Append(join.ThisColumn); builder.Append("\""); } }
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 ThreeTables() { var resolver = new JoinResolver(); var result = resolver.Resolve( new List <string> { "empires", "government_types", "planets" }) .ToList(); Assert.Equal(2, result.Count); var first = new JoinInfo { ThisTable = "empires", ThisColumn = "government_type_id", ForeignTable = "government_types", ForeignColumn = "id" }; var second = new JoinInfo { ThisTable = "empires", ThisColumn = "id", ForeignTable = "planets", ForeignColumn = "empire_id" }; Assert.Equal(first, result[0]); Assert.Equal(second, result[1]); }
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 OneTable() { var resolver = new JoinResolver(); var result = resolver.Resolve(new List <string> { "empires" }); Assert.Empty(result); }
private async Task RefreshAsync() { using (var cn = _getConnection.Invoke()) { _objects = await Analyzers[_providerType].GetDbObjectsAsync(cn); _joinResolver = new JoinResolver(_objects, _aliasManager); } LoadObjects(); }
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 IndirectJoins() { var resolver = new JoinResolver(); var joins = resolver.Resolve(new List <string> { "empires", "alliances" }).ToList(); Assert.Equal(2, joins.Count); Assert.Equal("empires", joins[0].ThisTable); Assert.Equal("id", joins[0].ThisColumn); Assert.Equal("alliances_entries", joins[0].ForeignTable); Assert.Equal("empire_id", joins[0].ForeignColumn); Assert.Equal("alliances_entries", joins[1].ThisTable); Assert.Equal("alliance_id", joins[1].ThisColumn); Assert.Equal("alliances", joins[1].ForeignTable); Assert.Equal("id", joins[1].ForeignColumn); }
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); } } }
public void TwoTables() { var resolver = new JoinResolver(); var result = resolver.Resolve( new List <string> { "empires", "government_types" }) .ToList(); Assert.Single(result); var expected = new JoinInfo { ThisTable = "empires", ThisColumn = "government_type_id", ForeignTable = "government_types", ForeignColumn = "id" }; Assert.Equal(expected, result[0]); }
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(); service = new OrganizationService(crmConnection); reportProgress(new SimpleProgressReport("Loading Entitymetadata")); EntityMetadata entityMetaData = Crm2013Wrapper.Crm2013Wrapper.GetEntityMetadata(service, this.Configuration.EntityName); Dictionary <string, AttributeMetadata> primaryKeyAttributeMetadataDictionary = new Dictionary <string, AttributeMetadata>(); foreach (string primaryKey in this.Configuration.PrimaryKeyAttributes) { AttributeMetadata attributeMetadata = entityMetaData.Attributes.Where(t => t.LogicalName == primaryKey).FirstOrDefault(); primaryKeyAttributeMetadataDictionary.Add(primaryKey, attributeMetadata); } reportProgress(new SimpleProgressReport("Mapping attributes of records")); EntityMapper entityMapper = new EntityMapper(entityMetaData, dataObject.Metadata, this.Configuration.Mapping, this.Configuration.PicklistMapping); Entity[] entities = new Entity[dataObject.Count]; for (int i = 0; i < dataObject.Count; i++) { object[] data = dataObject[i]; Entity entity = new Entity(this.Configuration.EntityName); entityMapper.MapAttributes(entity, data); entities[i] = entity; logger.AddRecord(i); if (StatusHelper.MustShowProgress(i, dataObject.Count) == true) { reportProgress(new SimpleProgressReport("Mapped " + (i + 1) + " of " + dataObject.Count + " records")); } } reportProgress(new SimpleProgressReport("Resolving relationship entities")); foreach (var relationMapping in Configuration.RelationMapping) { reportProgress(new SimpleProgressReport("Resolving relationship - load metadata for entity " + relationMapping.EntityName)); EntityMetadata relationEntityMetadata = Crm2013Wrapper.Crm2013Wrapper.GetEntityMetadata(service, relationMapping.EntityName); reportProgress(new SimpleProgressReport("Resolving relationship - load related records")); JoinResolver relationResolver = new JoinResolver(service, relationEntityMetadata, relationMapping.Mapping); Dictionary <string, Guid[]> relatedEntities = relationResolver.BuildMassResolverIndex(); reportProgress(new SimpleProgressReport("Resolving relationship - set relations")); RelationSetter relationSetter = new RelationSetter(relationEntityMetadata, relationMapping.Mapping); relationSetter.SetRelation(relationMapping.LogicalName, entities, dataObject, relatedEntities); } reportProgress(new SimpleProgressReport("Resolving primarykeys of records")); PrimaryKeyResolver primaryKeyResolver = new PrimaryKeyResolver(service, entityMetaData, primaryKeyAttributeMetadataDictionary); Dictionary <string, Guid[]> resolvedEntities = primaryKeyResolver.OneByOneResolver(entities); reportProgress(new SimpleProgressReport("Writing records to crm")); WriteEntity(entities, resolvedEntities, primaryKeyAttributeMetadataDictionary, reportProgress); }