NHibernateUserData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 OpenSim 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.Collections.Generic;
  28. using System.IO;
  29. using System.Reflection;
  30. using libsecondlife;
  31. using log4net;
  32. using NHibernate;
  33. using NHibernate.Cfg;
  34. using NHibernate.Expression;
  35. using NHibernate.Mapping.Attributes;
  36. using OpenSim.Framework;
  37. namespace OpenSim.Data.NHibernate
  38. {
  39. /// <summary>
  40. /// A User storage interface for the DB4o database system
  41. /// </summary>
  42. public class NHibernateUserData : UserDataBase
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private Configuration cfg;
  46. private ISessionFactory factory;
  47. public override void Initialise()
  48. {
  49. // TODO: hard coding for sqlite based stuff to begin with, just making it easier to test
  50. // This is stubbing for now, it will become dynamic later and support different db backends
  51. cfg = new Configuration();
  52. cfg.SetProperty(Environment.ConnectionProvider,
  53. "NHibernate.Connection.DriverConnectionProvider");
  54. cfg.SetProperty(Environment.Dialect,
  55. "NHibernate.Dialect.SQLiteDialect");
  56. cfg.SetProperty(Environment.ConnectionDriver,
  57. "NHibernate.Driver.SqliteClientDriver");
  58. cfg.SetProperty(Environment.ConnectionString,
  59. "URI=file:User.db,version=3");
  60. cfg.AddAssembly("OpenSim.Data.NHibernate");
  61. HbmSerializer.Default.Validate = true;
  62. using ( MemoryStream stream =
  63. HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly()))
  64. cfg.AddInputStream(stream);
  65. // new SchemaExport(cfg).Create(true, true);
  66. factory = cfg.BuildSessionFactory();
  67. }
  68. private bool ExistsUser(LLUUID uuid)
  69. {
  70. UserProfileData user;
  71. using(ISession session = factory.OpenSession()) {
  72. user = session.Load(typeof(UserProfileData), uuid) as UserProfileData;
  73. }
  74. return (user == null) ? false : true;
  75. }
  76. override public UserProfileData GetUserByUUID(LLUUID uuid)
  77. {
  78. UserProfileData user;
  79. // TODO: I'm sure I'll have to do something silly here
  80. using(ISession session = factory.OpenSession()) {
  81. user = session.Load(typeof(UserProfileData), uuid) as UserProfileData;
  82. user.CurrentAgent = GetAgentByUUID(uuid);
  83. }
  84. return user;
  85. }
  86. override public void AddNewUserProfile(UserProfileData profile)
  87. {
  88. if (!ExistsUser(profile.ID)) {
  89. using(ISession session = factory.OpenSession()) {
  90. using(ITransaction transaction = session.BeginTransaction()) {
  91. session.Save(profile);
  92. SetAgentData(profile.ID, profile.CurrentAgent, session);
  93. // TODO: save agent
  94. transaction.Commit();
  95. }
  96. }
  97. } else {
  98. m_log.ErrorFormat("Attempted to add User {0} {1} that already exists, updating instead", profile.FirstName, profile.SurName);
  99. UpdateUserProfile(profile);
  100. }
  101. }
  102. private static void SetAgentData(LLUUID uuid, UserAgentData agent, ISession session)
  103. {
  104. if (agent == null)
  105. {
  106. // TODO: got to figure out how to do a delete right
  107. }
  108. else
  109. {
  110. UserAgentData old = session.Load(typeof(UserAgentData), uuid) as UserAgentData;
  111. if (old == null)
  112. {
  113. session.Save(agent);
  114. }
  115. else
  116. {
  117. session.Update(agent);
  118. }
  119. }
  120. }
  121. override public bool UpdateUserProfile(UserProfileData profile)
  122. {
  123. if (ExistsUser(profile.ID)) {
  124. using(ISession session = factory.OpenSession()) {
  125. using(ITransaction transaction = session.BeginTransaction()) {
  126. session.Update(profile);
  127. SetAgentData(profile.ID, profile.CurrentAgent, session);
  128. transaction.Commit();
  129. return true;
  130. }
  131. }
  132. } else {
  133. m_log.ErrorFormat("Attempted to update User {0} {1} that doesn't exist, updating instead", profile.FirstName, profile.SurName);
  134. AddNewUserProfile(profile);
  135. return true;
  136. }
  137. }
  138. override public void AddNewUserAgent(UserAgentData agent)
  139. {
  140. using(ISession session = factory.OpenSession()) {
  141. using(ITransaction transaction = session.BeginTransaction()) {
  142. session.Save(agent);
  143. transaction.Commit();
  144. }
  145. }
  146. }
  147. public void UpdateUserAgent(UserAgentData agent)
  148. {
  149. using(ISession session = factory.OpenSession()) {
  150. using(ITransaction transaction = session.BeginTransaction()) {
  151. session.Update(agent);
  152. transaction.Commit();
  153. }
  154. }
  155. }
  156. override public UserAgentData GetAgentByUUID(LLUUID uuid)
  157. {
  158. using(ISession session = factory.OpenSession()) {
  159. return session.Load(typeof(UserAgentData), uuid) as UserAgentData;
  160. }
  161. }
  162. override public UserProfileData GetUserByName(string fname, string lname)
  163. {
  164. using(ISession session = factory.OpenSession()) {
  165. ICriteria criteria = session.CreateCriteria(typeof(UserProfileData));
  166. criteria.Add(Expression.Eq("FirstName", fname));
  167. criteria.Add(Expression.Eq("SurName", lname));
  168. foreach (UserProfileData profile in criteria.List())
  169. {
  170. profile.CurrentAgent = GetAgentByUUID(profile.ID);
  171. return profile;
  172. }
  173. return null;
  174. }
  175. }
  176. override public UserAgentData GetAgentByName(string fname, string lname)
  177. {
  178. return GetUserByName(fname, lname).CurrentAgent;
  179. }
  180. override public UserAgentData GetAgentByName(string name)
  181. {
  182. return GetAgentByName(name.Split(' ')[0], name.Split(' ')[1]);
  183. }
  184. override public List<AvatarPickerAvatar> GeneratePickerResults(LLUUID queryID, string query)
  185. {
  186. List<AvatarPickerAvatar> results = new List<AvatarPickerAvatar>();
  187. string[] querysplit;
  188. querysplit = query.Split(' ');
  189. if (querysplit.Length == 2)
  190. {
  191. using(ISession session = factory.OpenSession()) {
  192. ICriteria criteria = session.CreateCriteria(typeof(UserProfileData));
  193. criteria.Add(Expression.Like("FirstName", querysplit[0]));
  194. criteria.Add(Expression.Like("SurName", querysplit[1]));
  195. foreach(UserProfileData profile in criteria.List())
  196. {
  197. AvatarPickerAvatar user = new AvatarPickerAvatar();
  198. user.AvatarID = profile.ID;
  199. user.firstName = profile.FirstName;
  200. user.lastName = profile.SurName;
  201. results.Add(user);
  202. }
  203. }
  204. }
  205. return results;
  206. }
  207. // TODO: actually implement these
  208. public override void UpdateUserCurrentRegion(LLUUID avatarid, LLUUID regionuuid) {return;}
  209. public override void StoreWebLoginKey(LLUUID agentID, LLUUID webLoginKey) {return;}
  210. public override void AddNewUserFriend(LLUUID friendlistowner, LLUUID friend, uint perms) {return;}
  211. public override void RemoveUserFriend(LLUUID friendlistowner, LLUUID friend) {return;}
  212. public override void UpdateUserFriendPerms(LLUUID friendlistowner, LLUUID friend, uint perms) {return;}
  213. public override List<FriendListItem> GetUserFriendList(LLUUID friendlistowner) {return new List<FriendListItem>();}
  214. public override bool MoneyTransferRequest(LLUUID from, LLUUID to, uint amount) {return true;}
  215. public override bool InventoryTransferRequest(LLUUID from, LLUUID to, LLUUID inventory) {return true;}
  216. public override string getName()
  217. {
  218. return Name;
  219. }
  220. public string Name {
  221. get { return "NHibernate"; }
  222. }
  223. public override string GetVersion()
  224. {
  225. return Version;
  226. }
  227. public string Version {
  228. get { return "0.1"; }
  229. }
  230. public void Dispose()
  231. {
  232. }
  233. }
  234. }