Esempio n. 1
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);
        }
Esempio n. 2
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);
        }