NHibernateUserData.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. public override void Initialise(string connect)
  52. {
  53. char[] split = {';'};
  54. string[] parts = connect.Split(split, 3);
  55. if (parts.Length != 3)
  56. {
  57. // TODO: make this a real exception type
  58. throw new Exception("Malformed Inventory connection string '" + connect + "'");
  59. }
  60. // This is stubbing for now, it will become dynamic later and support different db backends
  61. cfg = new Configuration();
  62. cfg.SetProperty(Environment.ConnectionProvider,
  63. "NHibernate.Connection.DriverConnectionProvider");
  64. cfg.SetProperty(Environment.Dialect,
  65. "NHibernate.Dialect." + parts[0]);
  66. cfg.SetProperty(Environment.ConnectionDriver,
  67. "NHibernate.Driver." + parts[1]);
  68. cfg.SetProperty(Environment.ConnectionString, parts[2]);
  69. cfg.AddAssembly("OpenSim.Data.NHibernate");
  70. HbmSerializer.Default.Validate = true;
  71. using (MemoryStream stream =
  72. HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly()))
  73. cfg.AddInputStream(stream);
  74. // new SchemaExport(cfg).Create(true, true);
  75. factory = cfg.BuildSessionFactory();
  76. InitDB();
  77. }
  78. private void InitDB()
  79. {
  80. string regex = @"no such table: UserProfiles";
  81. Regex RE = new Regex(regex, RegexOptions.Multiline);
  82. try
  83. {
  84. using (ISession session = factory.OpenSession())
  85. {
  86. session.Load(typeof(UserProfileData), LLUUID.Zero);
  87. }
  88. }
  89. catch (ObjectNotFoundException)
  90. {
  91. // yes, we know it's not there, but that's ok
  92. }
  93. catch (ADOException e)
  94. {
  95. Match m = RE.Match(e.ToString());
  96. if (m.Success)
  97. {
  98. // We don't have this table, so create it.
  99. new SchemaExport(cfg).Create(true, true);
  100. }
  101. }
  102. }
  103. private bool ExistsUser(LLUUID uuid)
  104. {
  105. UserProfileData user = null;
  106. try
  107. {
  108. using (ISession session = factory.OpenSession())
  109. {
  110. user = session.Load(typeof(UserProfileData), uuid) as UserProfileData;
  111. }
  112. // BUG: CATCHALL IS BAD.
  113. }
  114. catch (Exception) {}
  115. return (user != null);
  116. }
  117. override public UserProfileData GetUserByUUID(LLUUID uuid)
  118. {
  119. UserProfileData user;
  120. // TODO: I'm sure I'll have to do something silly here
  121. using (ISession session = factory.OpenSession())
  122. {
  123. user = session.Load(typeof(UserProfileData), uuid) as UserProfileData;
  124. user.CurrentAgent = GetAgentByUUID(uuid);
  125. }
  126. return user;
  127. }
  128. override public void AddNewUserProfile(UserProfileData profile)
  129. {
  130. if (!ExistsUser(profile.ID))
  131. {
  132. using (ISession session = factory.OpenSession())
  133. {
  134. using (ITransaction transaction = session.BeginTransaction())
  135. {
  136. session.Save(profile);
  137. SetAgentData(profile.ID, profile.CurrentAgent, session);
  138. // TODO: save agent
  139. transaction.Commit();
  140. }
  141. }
  142. }
  143. else
  144. {
  145. m_log.ErrorFormat("Attempted to add User {0} {1} that already exists, updating instead", profile.FirstName, profile.SurName);
  146. UpdateUserProfile(profile);
  147. }
  148. }
  149. private static void SetAgentData(LLUUID uuid, UserAgentData agent, ISession session)
  150. {
  151. if (agent == null)
  152. {
  153. // TODO: got to figure out how to do a delete right
  154. }
  155. else
  156. {
  157. UserAgentData old = session.Load(typeof(UserAgentData), uuid) as UserAgentData;
  158. if (old == null)
  159. {
  160. session.Save(agent);
  161. }
  162. else
  163. {
  164. session.Update(agent);
  165. }
  166. }
  167. }
  168. override public bool UpdateUserProfile(UserProfileData profile)
  169. {
  170. if (ExistsUser(profile.ID))
  171. {
  172. using (ISession session = factory.OpenSession())
  173. {
  174. using (ITransaction transaction = session.BeginTransaction())
  175. {
  176. session.Update(profile);
  177. SetAgentData(profile.ID, profile.CurrentAgent, session);
  178. transaction.Commit();
  179. return true;
  180. }
  181. }
  182. }
  183. else
  184. {
  185. m_log.ErrorFormat("Attempted to update User {0} {1} that doesn't exist, updating instead", profile.FirstName, profile.SurName);
  186. AddNewUserProfile(profile);
  187. return true;
  188. }
  189. }
  190. override public void AddNewUserAgent(UserAgentData agent)
  191. {
  192. using (ISession session = factory.OpenSession())
  193. {
  194. using (ITransaction transaction = session.BeginTransaction())
  195. {
  196. session.Save(agent);
  197. transaction.Commit();
  198. }
  199. }
  200. }
  201. public void UpdateUserAgent(UserAgentData agent)
  202. {
  203. using (ISession session = factory.OpenSession())
  204. {
  205. using (ITransaction transaction = session.BeginTransaction())
  206. {
  207. session.Update(agent);
  208. transaction.Commit();
  209. }
  210. }
  211. }
  212. override public UserAgentData GetAgentByUUID(LLUUID uuid)
  213. {
  214. try
  215. {
  216. using (ISession session = factory.OpenSession())
  217. {
  218. return session.Load(typeof(UserAgentData), uuid) as UserAgentData;
  219. }
  220. }
  221. catch
  222. {
  223. return null;
  224. }
  225. }
  226. override public UserProfileData GetUserByName(string fname, string lname)
  227. {
  228. using (ISession session = factory.OpenSession())
  229. {
  230. ICriteria criteria = session.CreateCriteria(typeof(UserProfileData));
  231. criteria.Add(Expression.Eq("FirstName", fname));
  232. criteria.Add(Expression.Eq("SurName", lname));
  233. foreach (UserProfileData profile in criteria.List())
  234. {
  235. profile.CurrentAgent = GetAgentByUUID(profile.ID);
  236. return profile;
  237. }
  238. return null;
  239. }
  240. }
  241. override public UserAgentData GetAgentByName(string fname, string lname)
  242. {
  243. return GetUserByName(fname, lname).CurrentAgent;
  244. }
  245. override public UserAgentData GetAgentByName(string name)
  246. {
  247. return GetAgentByName(name.Split(' ')[0], name.Split(' ')[1]);
  248. }
  249. override public List<AvatarPickerAvatar> GeneratePickerResults(LLUUID queryID, string query)
  250. {
  251. List<AvatarPickerAvatar> results = new List<AvatarPickerAvatar>();
  252. string[] querysplit;
  253. querysplit = query.Split(' ');
  254. if (querysplit.Length == 2)
  255. {
  256. using (ISession session = factory.OpenSession())
  257. {
  258. ICriteria criteria = session.CreateCriteria(typeof(UserProfileData));
  259. criteria.Add(Expression.Like("FirstName", querysplit[0]));
  260. criteria.Add(Expression.Like("SurName", querysplit[1]));
  261. foreach (UserProfileData profile in criteria.List())
  262. {
  263. AvatarPickerAvatar user = new AvatarPickerAvatar();
  264. user.AvatarID = profile.ID;
  265. user.firstName = profile.FirstName;
  266. user.lastName = profile.SurName;
  267. results.Add(user);
  268. }
  269. }
  270. }
  271. return results;
  272. }
  273. // TODO: actually implement these
  274. public override void UpdateUserCurrentRegion(LLUUID avatarid, LLUUID regionuuid) {return;}
  275. public override void StoreWebLoginKey(LLUUID agentID, LLUUID webLoginKey) {return;}
  276. public override void AddNewUserFriend(LLUUID friendlistowner, LLUUID friend, uint perms) {return;}
  277. public override void RemoveUserFriend(LLUUID friendlistowner, LLUUID friend) {return;}
  278. public override void UpdateUserFriendPerms(LLUUID friendlistowner, LLUUID friend, uint perms) {return;}
  279. public override List<FriendListItem> GetUserFriendList(LLUUID friendlistowner) {return new List<FriendListItem>();}
  280. public override bool MoneyTransferRequest(LLUUID from, LLUUID to, uint amount) {return true;}
  281. public override bool InventoryTransferRequest(LLUUID from, LLUUID to, LLUUID inventory) {return true;}
  282. /// Appearance
  283. /// TODO: stubs for now to get us to a compiling state gently
  284. public override AvatarAppearance GetUserAppearance(LLUUID user)
  285. {
  286. AvatarAppearance appearance;
  287. // TODO: I'm sure I'll have to do something silly here
  288. using (ISession session = factory.OpenSession())
  289. {
  290. appearance = session.Load(typeof(AvatarAppearance), user) as AvatarAppearance;
  291. }
  292. return appearance;
  293. }
  294. private bool ExistsAppearance(LLUUID uuid)
  295. {
  296. AvatarAppearance appearance;
  297. using (ISession session = factory.OpenSession())
  298. {
  299. appearance = session.Load(typeof(AvatarAppearance), uuid) as AvatarAppearance;
  300. }
  301. return (appearance == null) ? false : true;
  302. }
  303. public override void UpdateUserAppearance(LLUUID user, AvatarAppearance appearance)
  304. {
  305. bool exists = ExistsAppearance(user);
  306. using (ISession session = factory.OpenSession())
  307. {
  308. using (ITransaction transaction = session.BeginTransaction())
  309. {
  310. if (exists)
  311. {
  312. session.Update(appearance);
  313. }
  314. else
  315. {
  316. session.Save(appearance);
  317. }
  318. transaction.Commit();
  319. }
  320. }
  321. }
  322. override public void AddAttachment(LLUUID user, LLUUID item)
  323. {
  324. return;
  325. }
  326. override public void RemoveAttachment(LLUUID user, LLUUID item)
  327. {
  328. return;
  329. }
  330. override public List<LLUUID> GetAttachments(LLUUID user)
  331. {
  332. return new List<LLUUID>();
  333. }
  334. public override string Name {
  335. get { return "NHibernate"; }
  336. }
  337. public override string Version {
  338. get { return "0.1"; }
  339. }
  340. public void Dispose()
  341. {
  342. }
  343. }
  344. }