private void DeleteRelationships(IEnumerable <CustomisationImportRequest> requests)
        {
            var response = new CustomisationImportResponse();

            var deleted = new List <string>();

            foreach (var request in requests)
            {
                foreach (
                    var metadata in
                    CustomisationImportService.ExtractRelationshipMetadataFromExcel(request.ExcelFile.FileName,
                                                                                    Controller, response).Values)
                {
                    if (XrmRecordService.RecordTypeExists(metadata.RecordType1))
                    {
                        if (!deleted.Contains(metadata.SchemaName) &&
                            XrmRecordService.GetManyToManyRelationships(metadata.RecordType1)
                            .Any(r => r.SchemaName == metadata.SchemaName))
                        {
                            XrmRecordService.DeleteRelationship(metadata.SchemaName);
                            deleted.Add(metadata.SchemaName);
                        }

                        //Assert.IsFalse(
                        //    XrmRecordService.GetManyToManyRelationships(metadata.RecordType1)
                        //        .Any(r => r.SchemaName == metadata.SchemaName));
                    }
                }
            }
        }
        private void VerifyRelationships(CustomisationImportRequest request)
        {
            var response = new CustomisationImportResponse();

            var relationShipmetadata =
                CustomisationImportService.ExtractRelationshipMetadataFromExcel(request.ExcelFile.FileName, Controller, response).Values;

            foreach (var metadata in relationShipmetadata)
            {
                Assert.IsTrue(XrmRecordService.GetManyToManyRelationships(metadata.RecordType1).Any(r => r.SchemaName == metadata.SchemaName));
                var relationshipMatch = XrmRecordService
                                        .GetManyToManyRelationships(metadata.RecordType1)
                                        .Where(r => r.SchemaName == metadata.SchemaName);
                Assert.IsTrue(relationshipMatch.Any());
                var relationship   = relationshipMatch.First();
                var loadedMetadata = XrmRecordService.GetManyRelationshipMetadata(relationship.SchemaName, relationship.RecordType1);
                Assert.AreEqual(loadedMetadata.RecordType1DisplayRelated, metadata.RecordType1DisplayRelated);
                Assert.AreEqual(loadedMetadata.RecordType2DisplayRelated, metadata.RecordType2DisplayRelated);
                Assert.AreEqual(loadedMetadata.RecordType1UseCustomLabel, metadata.RecordType1UseCustomLabel);
                Assert.AreEqual(loadedMetadata.RecordType2UseCustomLabel, metadata.RecordType2UseCustomLabel);
                if (metadata.RecordType1DisplayRelated)
                {
                    if (metadata.RecordType1UseCustomLabel)
                    {
                        Assert.AreEqual(loadedMetadata.RecordType1CustomLabel, metadata.RecordType1CustomLabel);
                    }
                    else
                    {
                        Assert.AreEqual(loadedMetadata.RecordType1CustomLabel, XrmRecordService.GetCollectionName(metadata.RecordType1));
                    }
                    Assert.AreEqual(loadedMetadata.RecordType1DisplayOrder, metadata.RecordType1DisplayOrder);
                }
                if (metadata.RecordType2DisplayRelated)
                {
                    if (metadata.RecordType2UseCustomLabel)
                    {
                        Assert.AreEqual(loadedMetadata.RecordType2CustomLabel, metadata.RecordType2CustomLabel);
                    }
                    else
                    {
                        Assert.AreEqual(loadedMetadata.RecordType2CustomLabel, XrmRecordService.GetCollectionName(metadata.RecordType2));
                    }
                    Assert.AreEqual(loadedMetadata.RecordType2DisplayOrder, metadata.RecordType2DisplayOrder);
                }
            }
        }
        private void VerifyRelationships(CustomisationImportRequest request)
        {
            var response = new CustomisationImportResponse();

            var relationShipmetadata =
                CustomisationImportService.ExtractRelationshipMetadataFromExcel(request.ExcelFile.FileName, Controller, response).Values;

            foreach (var metadata in relationShipmetadata)
            {
                Assert.IsTrue(XrmRecordService.GetManyToManyRelationships(metadata.RecordType1).Any(r => r.SchemaName == metadata.SchemaName));
                var relationshipMatch = XrmRecordService
                                        .GetManyToManyRelationships(metadata.RecordType1)
                                        .Where(r => r.SchemaName == metadata.SchemaName);
                Assert.IsTrue(relationshipMatch.Any());
                var relationship   = relationshipMatch.First();
                var loadedMetadata = XrmRecordService.GetManyRelationshipMetadata(relationship.SchemaName, relationship.RecordType1);
                Assert.AreEqual(loadedMetadata.RecordType1DisplayRelated, metadata.RecordType1DisplayRelated);
                Assert.AreEqual(loadedMetadata.RecordType2DisplayRelated, metadata.RecordType2DisplayRelated);
                Assert.AreEqual(loadedMetadata.RecordType1UseCustomLabel, metadata.RecordType1UseCustomLabel);
                Assert.AreEqual(loadedMetadata.RecordType2UseCustomLabel, metadata.RecordType2UseCustomLabel);

                //commented out as wasnt populating the custom labels in latest online for unknown reason
                //if (metadata.RecordType1DisplayRelated)
                //{
                //    if (metadata.RecordType1UseCustomLabel)
                //        Assert.AreEqual(loadedMetadata.RecordType1CustomLabel, metadata.RecordType1CustomLabel);
                //    else
                //        Assert.AreEqual(loadedMetadata.RecordType1CustomLabel, XrmRecordService.GetCollectionName(metadata.RecordType1));
                //    Assert.AreEqual(loadedMetadata.RecordType1DisplayOrder, metadata.RecordType1DisplayOrder);
                //}
                //if (metadata.RecordType2DisplayRelated)
                //{
                //    if (metadata.RecordType2UseCustomLabel)
                //        Assert.AreEqual(loadedMetadata.RecordType2CustomLabel, metadata.RecordType2CustomLabel);
                //    else
                //        Assert.AreEqual(loadedMetadata.RecordType2CustomLabel, XrmRecordService.GetCollectionName(metadata.RecordType2));
                //    Assert.AreEqual(loadedMetadata.RecordType2DisplayOrder, metadata.RecordType2DisplayOrder);
                //}
            }
        }
Example #4
0
        private IEnumerable <Entity> MapToEntities(IEnumerable <IRecord> queryRows, IMapSpreadsheetImport mapping, ParseIntoEntitiesResponse response, bool useAmericanDates)
        {
            var result = new List <Entity>();

            var nNRelationshipEntityNames = XrmRecordService
                                            .GetManyToManyRelationships()
                                            .Select(m => m.IntersectEntityName)
                                            .ToArray();

            var duplicateLogged = false;

            var rowNumber = 0;

            foreach (var row in queryRows)
            {
                rowNumber++;
                var targetType = mapping.TargetType;
                try
                {
                    var isNnRelation = nNRelationshipEntityNames.Contains(targetType);
                    var entity       = new Entity(targetType);
                    //this is used in the import to output the rownumber
                    //if the import throws an error
                    foreach (var fieldMapping in mapping.FieldMappings)
                    {
                        var targetField = fieldMapping.TargetField;
                        if (fieldMapping.TargetField != null)
                        {
                            var stringValue = row.GetStringField(fieldMapping.SourceField);
                            if (stringValue != null)
                            {
                                stringValue = stringValue.Trim();
                            }
                            if (isNnRelation)
                            {
                                //bit of hack
                                //for csv relationships just set to a string and map it later
                                //as the referenced record may not be created yet
                                entity.SetField(targetField, stringValue);
                            }
                            else if (XrmRecordService.XrmService.IsLookup(targetField, targetType))
                            {
                                //for lookups am going to set to a empty guid and allow the import part to replace with a correct guid
                                if (!stringValue.IsNullOrWhiteSpace())
                                {
                                    entity.SetField(targetField,
                                                    new EntityReference(XrmRecordService.XrmService.GetLookupTargetEntity(targetField, targetType),
                                                                        Guid.Empty)
                                    {
                                        Name = stringValue
                                    });
                                }
                            }
                            else
                            {
                                try
                                {
                                    entity.SetField(targetField, XrmRecordService.XrmService.ParseField(targetField, targetType, stringValue, useAmericanDates));
                                }
                                catch (Exception ex)
                                {
                                    response.AddResponseItem(new ParseIntoEntitiesResponse.ParseIntoEntitiesError(rowNumber, targetType, targetField, null, stringValue, "Error Parsing Field - " + ex.Message, ex));
                                }
                            }
                        }
                    }
                    if (entity.GetFieldsInEntity().All(f => XrmEntity.FieldsEqual(null, entity.GetField(f))))
                    {
                        //ignore any where all fields emopty
                        continue;
                    }
                    //okay any which are exact duplicates to previous ones lets ignore
                    if (result.Any(r => r.GetFieldsInEntity().Except(new[] { "Sheet.RowNumber" }).All(f =>
                    {
                        //since for entity references we just load the name with empty guids
                        //we check the dipslay name for them
                        var fieldValue1 = r.GetField(f);
                        var fieldValue2 = entity.GetField(f);
                        if (fieldValue1 is EntityReference && fieldValue2 is EntityReference)
                        {
                            return(((EntityReference)fieldValue1).Name == ((EntityReference)fieldValue2).Name);
                        }
                        else
                        {
                            return(XrmRecordService.FieldsEqual(fieldValue1, fieldValue2));
                        }
                    })))
                    {
                        if (!duplicateLogged)
                        {
                            response.AddResponseItem(new ParseIntoEntitiesResponse.ParseIntoEntitiesError(rowNumber, targetType, null, null, null, "At Least One Duplicate Removed", null));
                            duplicateLogged = true;
                        }
                        continue;
                    }
                    result.Add(entity);
                }
                catch (Exception ex)
                {
                    response.AddResponseItem(new ParseIntoEntitiesResponse.ParseIntoEntitiesError("Mapping Error", ex));
                }
            }
            return(result);
        }
Example #5
0
        private IEnumerable <Entity> MapToEntities(IEnumerable <IRecord> queryRows, IMapSpreadsheetImport mapping, ParseIntoEntitiesResponse response, LogController logController, bool useAmericanDates)
        {
            var result = new List <Entity>();

            var nNRelationshipEntityNames = XrmRecordService
                                            .GetManyToManyRelationships()
                                            .Select(m => m.IntersectEntityName)
                                            .ToArray();

            var rowNumber = 0;
            var rowCount  = queryRows.Count();

            foreach (var row in queryRows)
            {
                rowNumber++;
                var targetType = mapping.TargetType;
                logController.LogLiteral($"Mapping {targetType} Data Into Records {rowNumber}/{rowCount}");
                try
                {
                    var isNnRelation = nNRelationshipEntityNames.Contains(targetType);

                    var hasFieldValue = false;
                    var fieldValues   = new ConcurrentDictionary <string, object>();
                    //this is used in the import to output the row number
                    //if the import throws an error
                    Parallel.ForEach(mapping.FieldMappings, (fieldMapping) =>
                                     //foreach (var fieldMapping in mapping.FieldMappings)
                    {
                        var targetField = fieldMapping.TargetField;
                        if (fieldMapping.TargetField != null)
                        {
                            var stringValue = row.GetStringField(fieldMapping.SourceField);
                            if (stringValue != null)
                            {
                                stringValue = stringValue.Trim();
                            }

                            if (!stringValue.IsNullOrWhiteSpace())
                            {
                                hasFieldValue = true;
                            }
                            if (isNnRelation)
                            {
                                //bit of hack
                                //for csv relationships just set to a string and map it later
                                //as the referenced record may not be created yet
                                fieldValues[targetField] = stringValue;
                            }
                            else if (XrmRecordService.XrmService.IsLookup(targetField, targetType))
                            {
                                //for lookups am going to set to a empty guid and allow the import part to replace with a correct guid
                                if (!stringValue.IsNullOrWhiteSpace())
                                {
                                    var isGuid = Guid.Empty;
                                    if (Guid.TryParse(stringValue, out isGuid))
                                    {
                                        fieldValues[targetField] =
                                            new EntityReference(XrmRecordService.XrmService.GetLookupTargetEntity(targetField, targetType),
                                                                isGuid)
                                        {
                                            Name = stringValue
                                        };
                                    }
                                    else
                                    {
                                        fieldValues[targetField] =
                                            new EntityReference(XrmRecordService.XrmService.GetLookupTargetEntity(targetField, targetType),
                                                                Guid.Empty)
                                        {
                                            Name = stringValue
                                        };
                                    }
                                }
                            }
                            else
                            {
                                try
                                {
                                    fieldValues[targetField] = XrmRecordService.XrmService.ParseField(targetField, targetType, stringValue, useAmericanDates);
                                }
                                catch (Exception ex)
                                {
                                    response.AddResponseItem(new ParseIntoEntitiesResponse.ParseIntoEntitiesError(rowNumber, targetType, targetField, null, stringValue, "Error Parsing Field - " + ex.Message, ex));
                                }
                            }
                        }
                    });
                    var entity = new Entity(targetType);
                    foreach (var fieldValue in fieldValues)
                    {
                        entity[fieldValue.Key] = fieldValues[fieldValue.Key];
                    }
                    if (!hasFieldValue)
                    {
                        //ignore any where all fields emopty
                        continue;
                    }
                    //okay if remove duplicates
                    //any which are exact duplicates to previous ones lets ignore
                    if (mapping.IgnoreDuplicates)
                    {
                        if (result.Any(r => r.GetFieldsInEntity().Except(new[] { "Sheet.RowNumber" }).All(f =>
                        {
                            //since for entity references we may load the name with empty guid
                            //check the display name for them
                            var fieldValue1 = r.GetField(f);
                            var fieldValue2 = entity.GetField(f);
                            if (fieldValue1 is EntityReference && fieldValue2 is EntityReference)
                            {
                                return(((EntityReference)fieldValue1).Name == ((EntityReference)fieldValue2).Name);
                            }
                            else
                            {
                                return(XrmRecordService.FieldsEqual(fieldValue1, fieldValue2));
                            }
                        })))
                        {
                            continue;
                        }
                    }
                    result.Add(entity);
                }
                catch (Exception ex)
                {
                    response.AddResponseItem(new ParseIntoEntitiesResponse.ParseIntoEntitiesError("Mapping Error", ex));
                }
            }
            return(result);
        }
Example #6
0
 private void ProcessForRelationships(CustomisationExporterRequest request,
                                      CustomisationExporterResponse response,
                                      LogController controller)
 {
     if (request.Relationships)
     {
         var allRelationship = new List <RelationshipExport>();
         var types           = GetRecordTypesToExport(request).OrderBy(t => Service.GetDisplayName(t)).ToArray();
         var count           = types.Count();
         var manyToManyDone  = new List <string>();
         for (var i = 0; i < count; i++)
         {
             var thisType      = types.ElementAt(i);
             var thisTypeLabel = Service.GetDisplayName(thisType);
             controller.UpdateProgress(i, count, "Exporting Relationships For " + thisTypeLabel);
             try
             {
                 var relationships = Service.GetManyToManyRelationships(thisType);
                 for (var j = 0; j < relationships.Count(); j++)
                 {
                     var relationship = relationships.ElementAt(j);
                     try
                     {
                         if (relationship.RecordType1 == thisType
                             ||
                             (!request.DuplicateManyToManyRelationshipSides &&
                              !manyToManyDone.Contains(relationship.SchemaName))
                             )
                         {
                             allRelationship.Add(new RelationshipExport(relationship.SchemaName,
                                                                        relationship.RecordType1, relationship.RecordType2,
                                                                        relationship.IsCustomRelationship, relationship.RecordType1DisplayRelated,
                                                                        relationship.RecordType2DisplayRelated
                                                                        , relationship.Entity1IntersectAttribute, relationship.Entity2IntersectAttribute,
                                                                        RelationshipExport.RelationshipType.ManyToMany,
                                                                        relationship.RecordType1UseCustomLabel, relationship.RecordType2UseCustomLabel,
                                                                        relationship.RecordType1CustomLabel, relationship.RecordType2CustomLabel
                                                                        , relationship.RecordType1DisplayOrder, relationship.RecordType2DisplayOrder, relationship.MetadataId
                                                                        , null));
                             manyToManyDone.Add(relationship.SchemaName);
                         }
                         if (relationship.RecordType2 == thisType &&
                             (request.DuplicateManyToManyRelationshipSides ||
                              (!manyToManyDone.Contains(relationship.SchemaName))))
                         {
                             allRelationship.Add(new RelationshipExport(relationship.SchemaName,
                                                                        relationship.RecordType2, relationship.RecordType1,
                                                                        relationship.IsCustomRelationship, relationship.RecordType2DisplayRelated,
                                                                        relationship.RecordType1DisplayRelated
                                                                        , relationship.Entity2IntersectAttribute, relationship.Entity1IntersectAttribute,
                                                                        RelationshipExport.RelationshipType.ManyToMany,
                                                                        relationship.RecordType2UseCustomLabel, relationship.RecordType1UseCustomLabel,
                                                                        relationship.RecordType2CustomLabel, relationship.RecordType1CustomLabel
                                                                        , relationship.RecordType2DisplayOrder, relationship.RecordType1DisplayOrder
                                                                        , relationship.MetadataId, null));
                             manyToManyDone.Add(relationship.SchemaName);
                         }
                     }
                     catch (Exception ex)
                     {
                         response.AddResponseItem(
                             new CustomisationExporterResponseItem("Error Exporting Relationship",
                                                                   relationship.SchemaName, ex));
                     }
                 }
                 if (request.IncludeOneToManyRelationships)
                 {
                     var oneTorelationships = Service.GetOneToManyRelationships(thisType);
                     for (var j = 0; j < oneTorelationships.Count(); j++)
                     {
                         var relationship = oneTorelationships.ElementAt(j);
                         try
                         {
                             var isCustomRelationship = Service.FieldExists(relationship.ReferencingAttribute, relationship.ReferencingEntity) &&
                                                        Service.GetFieldMetadata(relationship.ReferencingAttribute, relationship.ReferencingEntity).IsCustomField;
                             allRelationship.Add(new RelationshipExport(relationship.SchemaName,
                                                                        relationship.ReferencedEntity, relationship.ReferencingEntity,
                                                                        isCustomRelationship, false,
                                                                        relationship.DisplayRelated
                                                                        , null, relationship.ReferencingAttribute,
                                                                        RelationshipExport.RelationshipType.OneToMany, false, relationship.IsCustomLabel,
                                                                        null, relationship.GetRelationshipLabel
                                                                        , 0, relationship.DisplayOrder, relationship.MetadataId, relationship.DeleteCascadeConfiguration));
                         }
                         catch (Exception ex)
                         {
                             response.AddResponseItem(
                                 new CustomisationExporterResponseItem("Error Exporting Relationship",
                                                                       relationship.SchemaName, ex));
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 response.AddResponseItem(new CustomisationExporterResponseItem("Error Exporting Relationships",
                                                                                thisType, ex));
             }
         }
         response.AddListToOutput("Relationships", allRelationship);
     }
 }