NHibernateInventoryData.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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.Reflection;
  30. using log4net;
  31. using NHibernate;
  32. using NHibernate.Criterion;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. namespace OpenSim.Data.NHibernate
  36. {
  37. public class NHibernateInventoryData: IInventoryDataPlugin
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. private NHibernateManager manager;
  41. public NHibernateManager Manager
  42. {
  43. get
  44. {
  45. return manager;
  46. }
  47. }
  48. /// <summary>
  49. /// The plugin being loaded
  50. /// </summary>
  51. /// <returns>A string containing the plugin name</returns>
  52. public string Name
  53. {
  54. get { return "NHibernate Inventory Data Interface"; }
  55. }
  56. /// <summary>
  57. /// The plugins version
  58. /// </summary>
  59. /// <returns>A string containing the plugin version</returns>
  60. public string Version
  61. {
  62. get
  63. {
  64. Module module = GetType().Module;
  65. // string dllName = module.Assembly.ManifestModule.Name;
  66. Version dllVersion = module.Assembly.GetName().Version;
  67. return
  68. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  69. dllVersion.Revision);
  70. }
  71. }
  72. public void Initialise()
  73. {
  74. m_log.Info("[NHibernateInventoryData]: " + Name + " cannot be default-initialized!");
  75. throw new PluginNotInitialisedException (Name);
  76. }
  77. /// <summary>
  78. /// Initialises the interface
  79. /// </summary>
  80. public void Initialise(string connect)
  81. {
  82. m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateInventoryData");
  83. manager = new NHibernateManager(connect, "InventoryStore");
  84. }
  85. /// <summary>
  86. /// Closes the interface
  87. /// </summary>
  88. public void Dispose()
  89. {
  90. }
  91. /*****************************************************************
  92. *
  93. * Basic CRUD operations on Data
  94. *
  95. ****************************************************************/
  96. // READ
  97. /// <summary>
  98. /// Returns an inventory item by its UUID
  99. /// </summary>
  100. /// <param name="item">The UUID of the item to be returned</param>
  101. /// <returns>A class containing item information</returns>
  102. public InventoryItemBase getInventoryItem(UUID item)
  103. {
  104. try
  105. {
  106. m_log.InfoFormat("[NHIBERNATE] getInventoryItem {0}", item);
  107. return (InventoryItemBase)manager.Get(typeof(InventoryItemBase), item);
  108. }
  109. catch
  110. {
  111. m_log.ErrorFormat("Couldn't find inventory item: {0}", item);
  112. return null;
  113. }
  114. }
  115. /// <summary>
  116. /// Creates a new inventory item based on item
  117. /// </summary>
  118. /// <param name="item">The item to be created</param>
  119. public void addInventoryItem(InventoryItemBase item)
  120. {
  121. if (!ExistsItem(item.ID))
  122. {
  123. manager.Insert(item);
  124. }
  125. else
  126. {
  127. m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Item {0} that already exists, updating instead", item.ID);
  128. updateInventoryItem(item);
  129. }
  130. }
  131. /// <summary>
  132. /// Updates an inventory item with item (updates based on ID)
  133. /// </summary>
  134. /// <param name="item">The updated item</param>
  135. public void updateInventoryItem(InventoryItemBase item)
  136. {
  137. if (ExistsItem(item.ID))
  138. {
  139. manager.Update(item);
  140. }
  141. else
  142. {
  143. m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Item {0} that already exists", item.ID);
  144. }
  145. }
  146. /// <summary>
  147. ///
  148. /// </summary>
  149. /// <param name="item"></param>
  150. public void deleteInventoryItem(UUID itemID)
  151. {
  152. InventoryItemBase item = (InventoryItemBase)manager.Get(typeof(InventoryItemBase), itemID);
  153. if (item != null)
  154. {
  155. manager.Delete(item);
  156. }
  157. else
  158. {
  159. m_log.ErrorFormat("[NHIBERNATE] Error deleting InventoryItemBase {0}", itemID);
  160. }
  161. }
  162. /// <summary>
  163. /// Returns an inventory folder by its UUID
  164. /// </summary>
  165. /// <param name="folder">The UUID of the folder to be returned</param>
  166. /// <returns>A class containing folder information</returns>
  167. public InventoryFolderBase getInventoryFolder(UUID folder)
  168. {
  169. try
  170. {
  171. return (InventoryFolderBase)manager.Get(typeof(InventoryFolderBase), folder);
  172. }
  173. catch
  174. {
  175. m_log.ErrorFormat("[NHIBERNATE] Couldn't find inventory item: {0}", folder);
  176. return null;
  177. }
  178. }
  179. /// <summary>
  180. /// Creates a new inventory folder based on folder
  181. /// </summary>
  182. /// <param name="folder">The folder to be created</param>
  183. public void addInventoryFolder(InventoryFolderBase folder)
  184. {
  185. if (!ExistsFolder(folder.ID))
  186. {
  187. manager.Insert(folder);
  188. }
  189. else
  190. {
  191. m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Folder {0} that already exists, updating instead", folder.ID);
  192. updateInventoryFolder(folder);
  193. }
  194. }
  195. /// <summary>
  196. /// Updates an inventory folder with folder (updates based on ID)
  197. /// </summary>
  198. /// <param name="folder">The updated folder</param>
  199. public void updateInventoryFolder(InventoryFolderBase folder)
  200. {
  201. if (ExistsFolder(folder.ID))
  202. {
  203. manager.Update(folder);
  204. }
  205. else
  206. {
  207. m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Folder {0} that already exists", folder.ID);
  208. }
  209. }
  210. /// <summary>
  211. ///
  212. /// </summary>
  213. /// <param name="folder"></param>
  214. public void deleteInventoryFolder(UUID folderID)
  215. {
  216. InventoryFolderBase item = (InventoryFolderBase)manager.Get(typeof(InventoryFolderBase), folderID);
  217. if (item != null)
  218. {
  219. manager.Delete(item);
  220. }
  221. else
  222. {
  223. m_log.ErrorFormat("[NHIBERNATE] Error deleting InventoryFolderBase {0}", folderID);
  224. }
  225. manager.Delete(folderID);
  226. }
  227. // useful private methods
  228. private bool ExistsItem(UUID uuid)
  229. {
  230. return (getInventoryItem(uuid) != null) ? true : false;
  231. }
  232. private bool ExistsFolder(UUID uuid)
  233. {
  234. return (getInventoryFolder(uuid) != null) ? true : false;
  235. }
  236. public void Shutdown()
  237. {
  238. // TODO: DataSet commit
  239. }
  240. // Move seems to be just update
  241. public void moveInventoryFolder(InventoryFolderBase folder)
  242. {
  243. updateInventoryFolder(folder);
  244. }
  245. public void moveInventoryItem(InventoryItemBase item)
  246. {
  247. updateInventoryItem(item);
  248. }
  249. /// <summary>
  250. /// Returns a list of inventory items contained within the specified folder
  251. /// </summary>
  252. /// <param name="folderID">The UUID of the target folder</param>
  253. /// <returns>A List of InventoryItemBase items</returns>
  254. public List<InventoryItemBase> getInventoryInFolder(UUID folderID)
  255. {
  256. // try {
  257. ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryItemBase));
  258. criteria.Add(Expression.Eq("Folder", folderID));
  259. List<InventoryItemBase> list = new List<InventoryItemBase>();
  260. foreach (InventoryItemBase item in criteria.List())
  261. {
  262. list.Add(item);
  263. }
  264. return list;
  265. // }
  266. // catch
  267. // {
  268. // return new List<InventoryItemBase>();
  269. // }
  270. }
  271. public List<InventoryFolderBase> getUserRootFolders(UUID user)
  272. {
  273. return new List<InventoryFolderBase>();
  274. }
  275. // see InventoryItemBase.getUserRootFolder
  276. public InventoryFolderBase getUserRootFolder(UUID user)
  277. {
  278. ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryFolderBase));
  279. criteria.Add(Expression.Eq("ParentID", UUID.Zero));
  280. criteria.Add(Expression.Eq("Owner", user));
  281. foreach (InventoryFolderBase folder in criteria.List())
  282. {
  283. return folder;
  284. }
  285. m_log.ErrorFormat("No Inventory Root Folder Found for: {0}", user);
  286. return null;
  287. }
  288. /// <summary>
  289. /// Append a list of all the child folders of a parent folder
  290. /// </summary>
  291. /// <param name="folders">list where folders will be appended</param>
  292. /// <param name="parentID">ID of parent</param>
  293. private void getInventoryFolders(ref List<InventoryFolderBase> folders, UUID parentID)
  294. {
  295. ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryFolderBase));
  296. criteria.Add(Expression.Eq("ParentID", parentID));
  297. foreach (InventoryFolderBase item in criteria.List())
  298. {
  299. folders.Add(item);
  300. }
  301. }
  302. /// <summary>
  303. /// Returns a list of inventory folders contained in the folder 'parentID'
  304. /// </summary>
  305. /// <param name="parentID">The folder to get subfolders for</param>
  306. /// <returns>A list of inventory folders</returns>
  307. public List<InventoryFolderBase> getInventoryFolders(UUID parentID)
  308. {
  309. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  310. getInventoryFolders(ref folders, parentID);
  311. return folders;
  312. }
  313. // See IInventoryDataPlugin
  314. public List<InventoryFolderBase> getFolderHierarchy(UUID parentID)
  315. {
  316. if (parentID == UUID.Zero)
  317. {
  318. // Zero UUID is not a real parent folder.
  319. return new List<InventoryFolderBase>();
  320. }
  321. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  322. getInventoryFolders(ref folders, parentID);
  323. for (int i = 0; i < folders.Count; i++)
  324. getInventoryFolders(ref folders, folders[i].ID);
  325. return folders;
  326. }
  327. public List<InventoryItemBase> fetchActiveGestures (UUID avatarID)
  328. {
  329. return null;
  330. }
  331. }
  332. }