NHibernateUserData.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 OpenMetaverse;
  33. using log4net;
  34. using NHibernate;
  35. using NHibernate.Criterion;
  36. using OpenSim.Framework;
  37. using Environment=NHibernate.Cfg.Environment;
  38. namespace OpenSim.Data.NHibernate
  39. {
  40. /// <summary>
  41. /// A User storage interface for the DB4o database system
  42. /// </summary>
  43. public class NHibernateUserData : UserDataBase
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. public NHibernateManager manager;
  47. public override void Initialise()
  48. {
  49. m_log.Info("[NHibernateUserData]: " + Name + " cannot be default-initialized!");
  50. throw new PluginNotInitialisedException (Name);
  51. }
  52. public override void Initialise(string connect)
  53. {
  54. m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateUserData");
  55. manager = new NHibernateManager(connect, "UserStore");
  56. }
  57. private bool ExistsUser(UUID uuid)
  58. {
  59. UserProfileData user = null;
  60. m_log.InfoFormat("[NHIBERNATE] ExistsUser; {0}", uuid);
  61. user = (UserProfileData)manager.Load(typeof(UserProfileData), uuid);
  62. if (user == null)
  63. {
  64. m_log.InfoFormat("[NHIBERNATE] User with given UUID does not exist {0} ", uuid);
  65. return false;
  66. }
  67. return true;
  68. }
  69. override public UserProfileData GetUserByUUID(UUID uuid)
  70. {
  71. UserProfileData user;
  72. m_log.InfoFormat("[NHIBERNATE] GetUserByUUID: {0} ", uuid);
  73. user = (UserProfileData)manager.Load(typeof(UserProfileData), uuid);
  74. if (user != null)
  75. {
  76. UserAgentData agent = GetAgentByUUID(uuid);
  77. if (agent != null)
  78. {
  79. user.CurrentAgent = agent;
  80. }
  81. }
  82. return user;
  83. }
  84. override public void AddNewUserProfile(UserProfileData profile)
  85. {
  86. if (profile.ID == UUID.Zero)
  87. {
  88. m_log.ErrorFormat("[NHIBERNATE] Attempted to add User {0} {1} with zero UUID, throwintg exception as this is programming error ", profile.FirstName, profile.SurName);
  89. return;
  90. }
  91. if (!ExistsUser(profile.ID))
  92. {
  93. m_log.InfoFormat("[NHIBERNATE] AddNewUserProfile {0}", profile.ID);
  94. manager.Save(profile);
  95. // Agent should not be saved according to BasicUserTest.T015_UserPersistency()
  96. // SetAgentData(profile.ID, profile.CurrentAgent);
  97. }
  98. else
  99. {
  100. m_log.ErrorFormat("[NHIBERNATE] Attempted to add User {0} {1} that already exists, updating instead", profile.FirstName, profile.SurName);
  101. UpdateUserProfile(profile);
  102. }
  103. }
  104. /*
  105. private void SetAgentData(UUID uuid, UserAgentData agent)
  106. {
  107. UserAgentData old = (UserAgentData)manager.Load(typeof(UserAgentData), uuid);
  108. if (old != null)
  109. {
  110. m_log.InfoFormat("[NHIBERNATE] SetAgentData deleting old: {0} ",uuid);
  111. manager.Delete(old);
  112. }
  113. if (agent != null)
  114. {
  115. m_log.InfoFormat("[NHIBERNATE] SetAgentData: {0} ", agent.ProfileID);
  116. manager.Save(agent);
  117. }
  118. }
  119. */
  120. override public bool UpdateUserProfile(UserProfileData profile)
  121. {
  122. if (ExistsUser(profile.ID))
  123. {
  124. manager.Update(profile);
  125. // Agent should not be saved according to BasicUserTest.T015_UserPersistency()
  126. // SetAgentData(profile.ID, profile.CurrentAgent);
  127. return true;
  128. }
  129. else
  130. {
  131. m_log.ErrorFormat("[NHIBERNATE] Attempted to update User {0} {1} that doesn't exist, updating instead", profile.FirstName, profile.SurName);
  132. AddNewUserProfile(profile);
  133. return true;
  134. }
  135. }
  136. override public void AddNewUserAgent(UserAgentData agent)
  137. {
  138. if (agent.ProfileID == UUID.Zero)
  139. {
  140. m_log.ErrorFormat("[NHIBERNATE] Attempted to add new user agent with zero user id. Agent session id: {0}", agent.SessionID);
  141. return;
  142. }
  143. if (agent.SessionID == UUID.Zero)
  144. {
  145. m_log.ErrorFormat("[NHIBERNATE] Attempted to add new user agent with zero session id. User profile id: {0}", agent.SessionID);
  146. return;
  147. }
  148. UserAgentData old = (UserAgentData)manager.Load(typeof(UserAgentData), agent.ProfileID);
  149. if (old != null)
  150. {
  151. manager.Delete(old);
  152. }
  153. manager.Save(agent);
  154. }
  155. public void UpdateUserAgent(UserAgentData agent)
  156. {
  157. m_log.InfoFormat("[NHIBERNATE] UpdateUserAgent: {0} ", agent.ProfileID);
  158. manager.Update(agent);
  159. }
  160. override public UserAgentData GetAgentByUUID(UUID uuid)
  161. {
  162. m_log.InfoFormat("[NHIBERNATE] GetAgentByUUID: {0} ", uuid);
  163. return (UserAgentData)manager.Load(typeof(UserAgentData), uuid);
  164. }
  165. override public UserProfileData GetUserByName(string fname, string lname)
  166. {
  167. m_log.InfoFormat("[NHIBERNATE] GetUserByName: {0} {1} ", fname, lname);
  168. ICriteria criteria = manager.GetSession().CreateCriteria(typeof(UserProfileData));
  169. criteria.Add(Expression.Eq("FirstName", fname));
  170. criteria.Add(Expression.Eq("SurName", lname));
  171. foreach (UserProfileData profile in criteria.List())
  172. {
  173. profile.CurrentAgent = GetAgentByUUID(profile.ID);
  174. return profile;
  175. }
  176. return null;
  177. }
  178. override public UserAgentData GetAgentByName(string fname, string lname)
  179. {
  180. return GetUserByName(fname, lname).CurrentAgent;
  181. }
  182. override public UserAgentData GetAgentByName(string name)
  183. {
  184. return GetAgentByName(name.Split(' ')[0], name.Split(' ')[1]);
  185. }
  186. override public List<AvatarPickerAvatar> GeneratePickerResults(UUID queryID, string query)
  187. {
  188. List<AvatarPickerAvatar> results = new List<AvatarPickerAvatar>();
  189. string[] querysplit;
  190. querysplit = query.Split(' ');
  191. if (querysplit.Length == 2)
  192. {
  193. ICriteria criteria = manager.GetSession().CreateCriteria(typeof(UserProfileData));
  194. criteria.Add(Expression.Like("FirstName", querysplit[0]));
  195. criteria.Add(Expression.Like("SurName", querysplit[1]));
  196. foreach (UserProfileData profile in criteria.List())
  197. {
  198. AvatarPickerAvatar user = new AvatarPickerAvatar();
  199. user.AvatarID = profile.ID;
  200. user.firstName = profile.FirstName;
  201. user.lastName = profile.SurName;
  202. results.Add(user);
  203. }
  204. }
  205. return results;
  206. }
  207. // TODO: actually implement these
  208. public override void StoreWebLoginKey(UUID agentID, UUID webLoginKey)
  209. {
  210. UserProfileData user=GetUserByUUID(agentID);
  211. user.WebLoginKey = webLoginKey;
  212. UpdateUserProfile(user);
  213. return;
  214. }
  215. public override void AddNewUserFriend(UUID ownerId, UUID friendId, uint perms)
  216. {
  217. if (!FriendRelationExists(ownerId,friendId))
  218. {
  219. manager.Save(new UserFriend(UUID.Random(), ownerId, friendId, perms));
  220. }
  221. if (!FriendRelationExists(friendId, ownerId))
  222. {
  223. manager.Save(new UserFriend(UUID.Random(), friendId, ownerId, perms));
  224. }
  225. return;
  226. }
  227. private bool FriendRelationExists(UUID ownerId, UUID friendId)
  228. {
  229. using (ISession session = manager.GetSession())
  230. {
  231. ICriteria criteria = session.CreateCriteria(typeof(UserFriend));
  232. criteria.Add(Expression.Eq("OwnerID", ownerId));
  233. criteria.Add(Expression.Eq("FriendID", friendId));
  234. return criteria.List().Count > 0;
  235. }
  236. }
  237. public override void RemoveUserFriend(UUID ownerId, UUID friendId)
  238. {
  239. using (ISession session = manager.GetSession())
  240. {
  241. using (ITransaction transaction = session.BeginTransaction())
  242. {
  243. {
  244. ICriteria criteria = session.CreateCriteria(typeof(UserFriend));
  245. criteria.Add(Expression.Eq("OwnerID", ownerId));
  246. criteria.Add(Expression.Eq("FriendID", friendId));
  247. foreach (UserFriend userFriend in criteria.List())
  248. {
  249. session.Delete(userFriend);
  250. }
  251. }
  252. {
  253. ICriteria criteria = session.CreateCriteria(typeof(UserFriend));
  254. criteria.Add(Expression.Eq("OwnerID", friendId));
  255. criteria.Add(Expression.Eq("FriendID", ownerId));
  256. foreach (UserFriend userFriend in criteria.List())
  257. {
  258. session.Delete(userFriend);
  259. }
  260. }
  261. transaction.Commit();
  262. }
  263. }
  264. return;
  265. }
  266. public override void UpdateUserFriendPerms(UUID ownerId, UUID friendId, uint perms)
  267. {
  268. using (ISession session = manager.GetSession())
  269. {
  270. using (ITransaction transaction = session.BeginTransaction())
  271. {
  272. {
  273. ICriteria criteria = session.CreateCriteria(typeof(UserFriend));
  274. criteria.Add(Expression.Eq("OwnerID", ownerId));
  275. criteria.Add(Expression.Eq("FriendID", friendId));
  276. foreach (UserFriend userFriend in criteria.List())
  277. {
  278. userFriend.FriendPermissions = perms;
  279. session.Update(userFriend);
  280. }
  281. }
  282. transaction.Commit();
  283. }
  284. }
  285. return;
  286. }
  287. public override List<FriendListItem> GetUserFriendList(UUID ownerId)
  288. {
  289. List<FriendListItem> friendList=new List<FriendListItem>();
  290. Dictionary<UUID, FriendListItem> friendListItemDictionary = new Dictionary<UUID, FriendListItem>();
  291. using (ISession session = manager.GetSession())
  292. {
  293. ICriteria criteria = session.CreateCriteria(typeof(UserFriend));
  294. criteria.Add(Expression.Or(
  295. Expression.Eq("OwnerID", ownerId),
  296. Expression.Eq("FriendID", ownerId)
  297. ));
  298. foreach (UserFriend userFriend in criteria.List())
  299. {
  300. if (userFriend.OwnerID == ownerId)
  301. {
  302. FriendListItem friendListItem = new FriendListItem();
  303. friendListItem.FriendListOwner = userFriend.OwnerID;
  304. friendListItem.Friend = userFriend.FriendID;
  305. friendListItem.FriendPerms = userFriend.FriendPermissions;
  306. friendListItemDictionary.Add(userFriend.FriendID, friendListItem);
  307. friendList.Add(friendListItem);
  308. }
  309. }
  310. // Reading permissions to other direction
  311. foreach (UserFriend userFriend in criteria.List())
  312. {
  313. if (userFriend.FriendID == ownerId)
  314. {
  315. //Ignore if there is no reverse relation existing.
  316. //if (friendListItemDictionary.ContainsKey(userFriend.OwnerID))
  317. {
  318. FriendListItem friendListItem = friendListItemDictionary[userFriend.OwnerID];
  319. friendListItem.FriendListOwnerPerms = userFriend.FriendPermissions;
  320. }
  321. }
  322. }
  323. }
  324. return friendList;
  325. }
  326. public override Dictionary<UUID, FriendRegionInfo> GetFriendRegionInfos (List<UUID> friendsIds)
  327. {
  328. Dictionary<UUID, FriendRegionInfo> friendRegionInfos=new Dictionary<UUID, FriendRegionInfo>();
  329. foreach (UUID friendId in friendsIds)
  330. {
  331. UserAgentData agent=GetAgentByUUID(friendId);
  332. if (agent != null)
  333. {
  334. FriendRegionInfo fri = new FriendRegionInfo();
  335. fri.isOnline = agent.AgentOnline;
  336. fri.regionHandle = agent.Handle;
  337. friendRegionInfos[friendId] = fri;
  338. }
  339. }
  340. return friendRegionInfos;
  341. }
  342. public override bool MoneyTransferRequest(UUID from, UUID to, uint amount) { return true; }
  343. public override bool InventoryTransferRequest(UUID from, UUID to, UUID inventory) { return true; }
  344. /// Appearance
  345. /// TODO: stubs for now to get us to a compiling state gently
  346. public override AvatarAppearance GetUserAppearance(UUID user)
  347. {
  348. return (AvatarAppearance)manager.Load(typeof(AvatarAppearance), user);
  349. }
  350. private bool ExistsAppearance(UUID uuid)
  351. {
  352. AvatarAppearance appearance = (AvatarAppearance)manager.Load(typeof(AvatarAppearance), uuid);
  353. if (appearance == null)
  354. {
  355. return false;
  356. }
  357. return true;
  358. }
  359. public override void UpdateUserAppearance(UUID user, AvatarAppearance appearance)
  360. {
  361. if (appearance == null)
  362. return;
  363. appearance.Owner = user;
  364. bool exists = ExistsAppearance(user);
  365. if (exists)
  366. {
  367. manager.Update(appearance);
  368. }
  369. else
  370. {
  371. manager.Save(appearance);
  372. }
  373. }
  374. public override void ResetAttachments(UUID userID)
  375. {
  376. }
  377. public override void LogoutUsers(UUID regionID)
  378. {
  379. }
  380. public override string Name {
  381. get { return "NHibernate"; }
  382. }
  383. public override string Version {
  384. get { return "0.1"; }
  385. }
  386. public override void Dispose()
  387. {
  388. }
  389. }
  390. }