1
0

NHibernateUserData.cs 17 KB

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