PageRenderTime 79ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/sandbox/simondud/Envers.NET.Spring/Envers.NET.SLN/Envers.NET/Envers/Entities/Mapper/Relation/ToOneIdMapper.cs

https://bitbucket.org/dabide/nhcontrib
C# | 96 lines | 76 code | 11 blank | 9 comment | 5 complexity | 9f80752d7d8d7b0419ab28acc94def20 MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Apache-2.0, LGPL-3.0, LGPL-2.1
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using NHibernate.Envers.Entities.Mapper.Id;
  7. using NHibernate.Engine;
  8. using Iesi.Collections;
  9. using NHibernate.Util;
  10. using NHibernate.Envers.Tools;
  11. using NHibernate.Envers.Configuration;
  12. using NHibernate.Envers.Reader;
  13. using NHibernate.Properties;
  14. using System.Runtime.Serialization;
  15. using NHibernate.Collection;
  16. using System.Reflection;
  17. using System.Runtime.Remoting;
  18. namespace NHibernate.Envers.Entities.Mapper.Relation
  19. {
  20. /**
  21. * @author Catalina Panait, port of Envers omonyme class by Adam Warski (adam at warski dot org)
  22. */
  23. public class ToOneIdMapper : IPropertyMapper
  24. {
  25. private IIdMapper delegat;
  26. private PropertyData propertyData;
  27. private String referencedEntityName;
  28. private bool nonInsertableFake;
  29. public ToOneIdMapper(IIdMapper delegat, PropertyData propertyData, String referencedEntityName, bool nonInsertableFake)
  30. {
  31. this.delegat = delegat;
  32. this.propertyData = propertyData;
  33. this.referencedEntityName = referencedEntityName;
  34. this.nonInsertableFake = nonInsertableFake;
  35. }
  36. public bool MapToMapFromEntity(ISessionImplementor session, IDictionary<String, Object> data, Object newObj, Object oldObj)
  37. {
  38. //Simon 27/06/2010 - era new LinkedHashMap
  39. IDictionary<String, Object> newData = new Dictionary<String, Object>();
  40. data.Add(propertyData.Name, newData);
  41. // If this property is originally non-insertable, but made insertable because it is in a many-to-one "fake"
  42. // bi-directional relation, we always store the "old", unchaged data, to prevent storing changes made
  43. // to this field. It is the responsibility of the collection to properly update it if it really changed.
  44. delegat.MapToMapFromEntity(newData, nonInsertableFake ? oldObj : newObj);
  45. //noinspection SimplifiableConditionalExpression
  46. return nonInsertableFake ? false : !Toolz.EntitiesEqual(session, newObj, oldObj);
  47. }
  48. public void MapToEntityFromMap(AuditConfiguration verCfg, Object obj, IDictionary<String, Object> data, Object primaryKey,
  49. IAuditReaderImplementor versionsReader, long revision)
  50. {
  51. if (obj == null)
  52. {
  53. return;
  54. }
  55. Object entityId = delegat.MapToIdFromMap(DictionaryWrapper<String, Object>.Wrap((IDictionary)data[propertyData.Name]));
  56. Object value;
  57. if (entityId == null)
  58. {
  59. value = null;
  60. }
  61. else
  62. {
  63. if (versionsReader.FirstLevelCache.Contains(referencedEntityName, revision, entityId))
  64. {
  65. value = versionsReader.FirstLevelCache[referencedEntityName, revision, entityId];
  66. }
  67. else
  68. {
  69. //java: Class<?> entityClass = ReflectionTools.loadClass(referencedEntityName);
  70. value = versionsReader.SessionImplementor.Factory.GetEntityPersister(referencedEntityName).CreateProxy(
  71. entityId, new ToOneDelegateSessionImplementor(versionsReader, Toolz.ResolveDotnetType(referencedEntityName), entityId, revision, verCfg));
  72. }
  73. }
  74. PropertyInfo propInfo = obj.GetType().GetProperty(propertyData.Name);
  75. propInfo.SetValue(obj, value, null);
  76. }
  77. public IList<PersistentCollectionChangeData> MapCollectionChanges(String referencingPropertyName,
  78. IPersistentCollection newColl,
  79. Object oldColl,
  80. Object id)
  81. {
  82. return null;
  83. }
  84. }
  85. }