NHibernateUserData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Text.RegularExpressions;
  32. using libsecondlife;
  33. using log4net;
  34. using NHibernate;
  35. using NHibernate.Cfg;
  36. using NHibernate.Expression;
  37. using NHibernate.Mapping.Attributes;
  38. using NHibernate.Tool.hbm2ddl;
  39. using OpenSim.Framework;
  40. using Environment=NHibernate.Cfg.Environment;
  41. namespace OpenSim.Data.NHibernate
  42. {
  43. /// <summary>
  44. /// A User storage interface for the DB4o database system
  45. /// </summary>
  46. public class NHibernateUserData : UserDataBase
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private Configuration cfg;
  50. private ISessionFactory factory;
  51. private ISession session;
  52. public override void Initialise(string connect)
  53. {
  54. char[] split = {';'};
  55. string[] parts = connect.Split(split, 3);
  56. if (parts.Length != 3)
  57. {
  58. // TODO: make this a real exception type
  59. throw new Exception("Malformed Inventory connection string '" + connect + "'");
  60. }
  61. string dialect = parts[0];
  62. // This is stubbing for now, it will become dynamic later and support different db backends
  63. cfg = new Configuration();
  64. cfg.SetProperty(Environment.ConnectionProvider,
  65. "NHibernate.Connection.DriverConnectionProvider");
  66. cfg.SetProperty(Environment.Dialect,
  67. "NHibernate.Dialect." + parts[0]);
  68. cfg.SetProperty(Environment.ConnectionDriver,
  69. "NHibernate.Driver." + parts[1]);
  70. cfg.SetProperty(Environment.ConnectionString, parts[2]);
  71. cfg.AddAssembly("OpenSim.Data.NHibernate");
  72. factory = cfg.BuildSessionFactory();
  73. session = factory.OpenSession();
  74. // This actually does the roll forward assembly stuff
  75. Assembly assem = GetType().Assembly;
  76. Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "UserStore");
  77. m.Update();
  78. }
  79. private bool ExistsUser(LLUUID uuid)
  80. {
  81. UserProfileData user = null;
  82. try
  83. {
  84. user = session.Load(typeof(UserProfileData), uuid) as UserProfileData;
  85. }
  86. catch (ObjectNotFoundException)
  87. {
  88. user = null;
  89. }
  90. return (user != null);
  91. }
  92. override public UserProfileData GetUserByUUID(LLUUID uuid)
  93. {
  94. UserProfileData user;
  95. // TODO: I'm sure I'll have to do something silly here
  96. try
  97. {
  98. user = session.Load(typeof(UserProfileData), uuid) as UserProfileData;
  99. user.CurrentAgent = GetAgentByUUID(uuid);
  100. }
  101. catch (ObjectNotFoundException)
  102. {
  103. user = null;
  104. }
  105. return user;
  106. }
  107. override public void AddNewUserProfile(UserProfileData profile)
  108. {
  109. if (!ExistsUser(profile.ID))
  110. {
  111. session.Save(profile);
  112. SetAgentData(profile.ID, profile.CurrentAgent);
  113. }
  114. else
  115. {
  116. m_log.ErrorFormat("Attempted to add User {0} {1} that already exists, updating instead", profile.FirstName, profile.SurName);
  117. UpdateUserProfile(profile);
  118. }
  119. }
  120. private void SetAgentData(LLUUID uuid, UserAgentData agent)
  121. {
  122. if (agent == null)
  123. {
  124. // TODO: got to figure out how to do a delete right
  125. }
  126. else
  127. {
  128. try
  129. {
  130. UserAgentData old = session.Load(typeof(UserAgentData), uuid) as UserAgentData;
  131. session.Delete(old);
  132. }
  133. catch (ObjectNotFoundException)
  134. {
  135. }
  136. session.Save(agent);
  137. }
  138. }
  139. override public bool UpdateUserProfile(UserProfileData profile)
  140. {
  141. if (ExistsUser(profile.ID))
  142. {
  143. session.Update(profile);
  144. SetAgentData(profile.ID, profile.CurrentAgent);
  145. return true;
  146. }
  147. else
  148. {
  149. m_log.ErrorFormat("Attempted to update User {0} {1} that doesn't exist, updating instead", profile.FirstName, profile.SurName);
  150. AddNewUserProfile(profile);
  151. return true;
  152. }
  153. }
  154. override public void AddNewUserAgent(UserAgentData agent)
  155. {
  156. try
  157. {
  158. UserAgentData old = session.Load(typeof(UserAgentData), agent.ProfileID) as UserAgentData;
  159. session.Delete(old);
  160. }
  161. catch (ObjectNotFoundException)
  162. {
  163. }
  164. session.Save(agent);
  165. }
  166. public void UpdateUserAgent(UserAgentData agent)
  167. {
  168. session.Update(agent);
  169. }
  170. override public UserAgentData GetAgentByUUID(LLUUID uuid)
  171. {
  172. try
  173. {
  174. return session.Load(typeof(UserAgentData), uuid) as UserAgentData;
  175. }
  176. catch
  177. {
  178. return null;
  179. }
  180. }
  181. override public UserProfileData GetUserByName(string fname, string lname)
  182. {
  183. ICriteria criteria = session.CreateCriteria(typeof(UserProfileData));
  184. criteria.Add(Expression.Eq("FirstName", fname));
  185. criteria.Add(Expression.Eq("SurName", lname));
  186. foreach (UserProfileData profile in criteria.List())
  187. {
  188. profile.CurrentAgent = GetAgentByUUID(profile.ID);
  189. return profile;
  190. }
  191. return null;
  192. }
  193. override public UserAgentData GetAgentByName(string fname, string lname)
  194. {
  195. return GetUserByName(fname, lname).CurrentAgent;
  196. }
  197. override public UserAgentData GetAgentByName(string name)
  198. {
  199. return GetAgentByName(name.Split(' ')[0], name.Split(' ')[1]);
  200. }
  201. override public List<AvatarPickerAvatar> GeneratePickerResults(LLUUID queryID, string query)
  202. {
  203. List<AvatarPickerAvatar> results = new List<AvatarPickerAvatar>();
  204. string[] querysplit;
  205. querysplit = query.Split(' ');
  206. if (querysplit.Length == 2)
  207. {
  208. ICriteria criteria = session.CreateCriteria(typeof(UserProfileData));
  209. criteria.Add(Expression.Like("FirstName", querysplit[0]));
  210. criteria.Add(Expression.Like("SurName", querysplit[1]));
  211. foreach (UserProfileData profile in criteria.List())
  212. {
  213. AvatarPickerAvatar user = new AvatarPickerAvatar();
  214. user.AvatarID = profile.ID;
  215. user.firstName = profile.FirstName;
  216. user.lastName = profile.SurName;
  217. results.Add(user);
  218. }
  219. }
  220. return results;
  221. }
  222. // TODO: actually implement these
  223. public override void UpdateUserCurrentRegion(LLUUID avatarid, LLUUID regionuuid, ulong regionhandle) { return; }
  224. public override void StoreWebLoginKey(LLUUID agentID, LLUUID webLoginKey) { return; }
  225. public override void AddNewUserFriend(LLUUID friendlistowner, LLUUID friend, uint perms) { return; }
  226. public override void RemoveUserFriend(LLUUID friendlistowner, LLUUID friend) { return; }
  227. public override void UpdateUserFriendPerms(LLUUID friendlistowner, LLUUID friend, uint perms) { return; }
  228. public override List<FriendListItem> GetUserFriendList(LLUUID friendlistowner) { return new List<FriendListItem>(); }
  229. public override bool MoneyTransferRequest(LLUUID from, LLUUID to, uint amount) { return true; }
  230. public override bool InventoryTransferRequest(LLUUID from, LLUUID to, LLUUID inventory) { return true; }
  231. /// Appearance
  232. /// TODO: stubs for now to get us to a compiling state gently
  233. public override AvatarAppearance GetUserAppearance(LLUUID user)
  234. {
  235. AvatarAppearance appearance;
  236. // TODO: I'm sure I'll have to do something silly here
  237. try {
  238. appearance = session.Load(typeof(AvatarAppearance), user) as AvatarAppearance;
  239. } catch (ObjectNotFoundException) {
  240. appearance = null;
  241. }
  242. return appearance;
  243. }
  244. private bool ExistsAppearance(LLUUID uuid)
  245. {
  246. AvatarAppearance appearance;
  247. try {
  248. appearance = session.Load(typeof(AvatarAppearance), uuid) as AvatarAppearance;
  249. } catch (ObjectNotFoundException) {
  250. appearance = null;
  251. }
  252. return (appearance == null) ? false : true;
  253. }
  254. public override void UpdateUserAppearance(LLUUID user, AvatarAppearance appearance)
  255. {
  256. if (appearance == null)
  257. return;
  258. appearance.Owner = user;
  259. bool exists = ExistsAppearance(user);
  260. if (exists)
  261. {
  262. session.Update(appearance);
  263. }
  264. else
  265. {
  266. session.Save(appearance);
  267. }
  268. }
  269. override public void AddAttachment(LLUUID user, LLUUID item)
  270. {
  271. return;
  272. }
  273. override public void RemoveAttachment(LLUUID user, LLUUID item)
  274. {
  275. return;
  276. }
  277. override public List<LLUUID> GetAttachments(LLUUID user)
  278. {
  279. return new List<LLUUID>();
  280. }
  281. public override string Name {
  282. get { return "NHibernate"; }
  283. }
  284. public override string Version {
  285. get { return "0.1"; }
  286. }
  287. public void Dispose()
  288. {
  289. }
  290. }
  291. }