Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. public static List<ConexioEntitySyncContext> FindMatches(IEnumerable<TC> conexioEntities,
  2. List<TE> externalEntities, SynchronizationUnitOfWork unitOfWork)
  3. {
  4. List<ConexioEntitySyncContext> result = new List<ConexioEntitySyncContext>();
  5. int maxScore;
  6. int usefullScore = typeof(T).GetCustomAttribute<ConexioUsefullScore>().MinScorePercentage;
  7. Dictionary<TC, int> scores;
  8. foreach (TE externalEntity in externalEntities)
  9. {
  10. maxScore = 0;
  11. scores = new Dictionary<TC, int>();
  12. foreach (TC conexioEntity in conexioEntities)
  13. {
  14. int matchingScore = GetMatchingScore(externalEntity as T, conexioEntity as T);
  15. scores.Add(conexioEntity,matchingScore);
  16. }
  17. maxScore = scores.Values.Max();
  18. ConexioEntitySyncContext matchingContext = new ConexioEntitySyncContext();
  19. if (maxScore > usefullScore)
  20. {
  21. TC matchingConexioEntity = scores.Where(s => s.Value == maxScore).FirstOrDefault().Key;
  22.  
  23. if (result.Where(c => c.ConexioEntity.Equals(matchingConexioEntity)).FirstOrDefault() == null)
  24. {
  25. matchingContext = new ConexioEntitySyncContext();
  26. matchingContext.ConexioEntity = matchingConexioEntity;
  27. }
  28. else
  29. {
  30. matchingContext = result.Where(c => c.ConexioEntity.Equals(matchingConexioEntity)).FirstOrDefault();
  31. }
  32. matchingContext.ExternalEntities.Add(externalEntity);
  33. //if (maxScore < usefullScore)
  34. //{
  35. // matchingContext.HandledType = HandledType.NotUsefull;
  36. //}
  37. externalEntity.ConexioEntity = matchingConexioEntity;
  38. externalEntity.ConexioID = matchingConexioEntity.ID;
  39. }
  40. else
  41. {
  42. var newConexioEntity = externalEntity as T;
  43. var insertConexioEntity = Mapper.Map<TC>(newConexioEntity);
  44. insertConexioEntity.IsNew = true;
  45. matchingContext.ConexioEntity = insertConexioEntity;
  46.  
  47. matchingContext.ExternalEntities = new List<TE>();
  48.  
  49. matchingContext.ExternalEntities.Add(externalEntity);
  50. externalEntity.ConexioEntity = insertConexioEntity;
  51. externalEntity.ConexioID = newConexioEntity.ID;
  52. unitOfWork.RepositoryAsync<TC>().Insert(insertConexioEntity);
  53. }
  54. result.Add(matchingContext);
  55. unitOfWork.RepositoryAsync<TE>().Update(externalEntity);
  56. }
  57. //TODO: Fix extra DB entries
  58. unitOfWork.SaveChanges();
  59. return result;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement