NHibernateEstateData.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System.Reflection;
  28. using log4net;
  29. using OpenMetaverse;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Framework.Interfaces;
  32. using NHibernate;
  33. using NHibernate.Criterion;
  34. using System.Collections;
  35. using System;
  36. namespace OpenSim.Data.NHibernate
  37. {
  38. /// <summary>
  39. /// A User storage interface for the DB4o database system
  40. /// </summary>
  41. public class NHibernateEstateData : IEstateDataStore
  42. {
  43. #region Fields
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private NHibernateManager manager;
  46. public NHibernateManager Manager
  47. {
  48. get
  49. {
  50. return manager;
  51. }
  52. }
  53. public string Name
  54. {
  55. get { return "NHibernateEstateData"; }
  56. }
  57. public string Version
  58. {
  59. get { return "0.1"; }
  60. }
  61. #endregion
  62. #region Startup and shutdown.
  63. public void Initialise()
  64. {
  65. m_log.Info("[NHIBERNATE]: " + Name + " cannot be default-initialized!");
  66. throw new PluginNotInitialisedException(Name);
  67. }
  68. public void Initialise(string connect)
  69. {
  70. m_log.InfoFormat("[NHIBERNATE] Initializing " + Name + ".");
  71. manager = new NHibernateManager(connect, "EstateStore");
  72. }
  73. public void Dispose() { }
  74. #endregion
  75. #region IEstateDataStore Members
  76. public EstateSettings LoadEstateSettings(UUID regionID)
  77. {
  78. EstateRegionLink link = LoadEstateRegionLink(regionID);
  79. // Ensure that estate settings exist for the link
  80. if (link != null)
  81. {
  82. if (manager.GetWithStatefullSession(typeof(EstateSettings), link.EstateID) == null)
  83. {
  84. // Delete broken link
  85. manager.Delete(link);
  86. link = null;
  87. }
  88. }
  89. // If estate link does not exist create estate settings and link it to region.
  90. if (link == null)
  91. {
  92. EstateSettings estateSettings = new EstateSettings();
  93. //estateSettings.EstateOwner = UUID.Random();
  94. //estateSettings.BlockDwell = false;
  95. object identifier = manager.Insert(estateSettings);
  96. if (identifier == null)
  97. {
  98. // Saving failed. Error is logged in the manager.
  99. return null;
  100. }
  101. uint estateID = (uint)identifier;
  102. link = new EstateRegionLink();
  103. link.EstateRegionLinkID = UUID.Random();
  104. link.RegionID = regionID;
  105. link.EstateID = estateID;
  106. manager.InsertWithStatefullSession(link);
  107. }
  108. // Load estate settings according to the existing or created link.
  109. return (EstateSettings)manager.GetWithStatefullSession(typeof(EstateSettings), link.EstateID);
  110. }
  111. public void StoreEstateSettings(EstateSettings estateSettings)
  112. {
  113. // Estates are always updated when stored.
  114. // Insert is always done via. load method as with the current API
  115. // this is explicitly the only way to create region link.
  116. manager.UpdateWithStatefullSession(estateSettings);
  117. }
  118. #endregion
  119. #region Private Utility Methods
  120. private EstateRegionLink LoadEstateRegionLink(UUID regionID)
  121. {
  122. ICriteria criteria = manager.GetSession().CreateCriteria(typeof(EstateRegionLink));
  123. criteria.Add(Expression.Eq("RegionID", regionID));
  124. IList links = criteria.List();
  125. // Fail fast if more than one estate links exist
  126. if (links.Count > 1)
  127. {
  128. m_log.Error("[NHIBERNATE]: Region had more than one estate linked: " + regionID);
  129. throw new Exception("[NHIBERNATE]: Region had more than one estate linked: " + regionID);
  130. }
  131. if (links.Count == 1)
  132. {
  133. return (EstateRegionLink)links[0];
  134. }
  135. else
  136. {
  137. return null;
  138. }
  139. }
  140. #endregion
  141. }
  142. }