1
0

NHibernateInventoryData.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 OpenSimulator 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. public InventoryItemBase queryInventoryItem(UUID itemID)
  163. {
  164. return null;
  165. }
  166. public InventoryFolderBase queryInventoryFolder(UUID folderID)
  167. {
  168. return null;
  169. }
  170. /// <summary>
  171. /// Returns an inventory folder by its UUID
  172. /// </summary>
  173. /// <param name="folder">The UUID of the folder to be returned</param>
  174. /// <returns>A class containing folder information</returns>
  175. public InventoryFolderBase getInventoryFolder(UUID folder)
  176. {
  177. try
  178. {
  179. return (InventoryFolderBase)manager.Get(typeof(InventoryFolderBase), folder);
  180. }
  181. catch
  182. {
  183. m_log.ErrorFormat("[NHIBERNATE] Couldn't find inventory item: {0}", folder);
  184. return null;
  185. }
  186. }
  187. /// <summary>
  188. /// Creates a new inventory folder based on folder
  189. /// </summary>
  190. /// <param name="folder">The folder to be created</param>
  191. public void addInventoryFolder(InventoryFolderBase folder)
  192. {
  193. if (!ExistsFolder(folder.ID))
  194. {
  195. manager.Insert(folder);
  196. }
  197. else
  198. {
  199. m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Folder {0} that already exists, updating instead", folder.ID);
  200. updateInventoryFolder(folder);
  201. }
  202. }
  203. /// <summary>
  204. /// Updates an inventory folder with folder (updates based on ID)
  205. /// </summary>
  206. /// <param name="folder">The updated folder</param>
  207. public void updateInventoryFolder(InventoryFolderBase folder)
  208. {
  209. if (ExistsFolder(folder.ID))
  210. {
  211. manager.Update(folder);
  212. }
  213. else
  214. {
  215. m_log.ErrorFormat("[NHIBERNATE] Attempted to add Inventory Folder {0} that already exists", folder.ID);
  216. }
  217. }
  218. /// <summary>
  219. ///
  220. /// </summary>
  221. /// <param name="folder"></param>
  222. public void deleteInventoryFolder(UUID folderID)
  223. {
  224. InventoryFolderBase item = (InventoryFolderBase)manager.Get(typeof(InventoryFolderBase), folderID);
  225. if (item != null)
  226. {
  227. manager.Delete(item);
  228. }
  229. else
  230. {
  231. m_log.ErrorFormat("[NHIBERNATE] Error deleting InventoryFolderBase {0}", folderID);
  232. }
  233. manager.Delete(folderID);
  234. }
  235. // useful private methods
  236. private bool ExistsItem(UUID uuid)
  237. {
  238. return (getInventoryItem(uuid) != null) ? true : false;
  239. }
  240. private bool ExistsFolder(UUID uuid)
  241. {
  242. return (getInventoryFolder(uuid) != null) ? true : false;
  243. }
  244. public void Shutdown()
  245. {
  246. // TODO: DataSet commit
  247. }
  248. // Move seems to be just update
  249. public void moveInventoryFolder(InventoryFolderBase folder)
  250. {
  251. updateInventoryFolder(folder);
  252. }
  253. public void moveInventoryItem(InventoryItemBase item)
  254. {
  255. updateInventoryItem(item);
  256. }
  257. /// <summary>
  258. /// Returns a list of inventory items contained within the specified folder
  259. /// </summary>
  260. /// <param name="folderID">The UUID of the target folder</param>
  261. /// <returns>A List of InventoryItemBase items</returns>
  262. public List<InventoryItemBase> getInventoryInFolder(UUID folderID)
  263. {
  264. // try {
  265. ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryItemBase));
  266. criteria.Add(Expression.Eq("Folder", folderID));
  267. List<InventoryItemBase> list = new List<InventoryItemBase>();
  268. foreach (InventoryItemBase item in criteria.List())
  269. {
  270. list.Add(item);
  271. }
  272. return list;
  273. // }
  274. // catch
  275. // {
  276. // return new List<InventoryItemBase>();
  277. // }
  278. }
  279. public List<InventoryFolderBase> getUserRootFolders(UUID user)
  280. {
  281. return new List<InventoryFolderBase>();
  282. }
  283. // see InventoryItemBase.getUserRootFolder
  284. public InventoryFolderBase getUserRootFolder(UUID user)
  285. {
  286. ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryFolderBase));
  287. criteria.Add(Expression.Eq("ParentID", UUID.Zero));
  288. criteria.Add(Expression.Eq("Owner", user));
  289. foreach (InventoryFolderBase folder in criteria.List())
  290. {
  291. return folder;
  292. }
  293. m_log.ErrorFormat("No Inventory Root Folder Found for: {0}", user);
  294. return null;
  295. }
  296. /// <summary>
  297. /// Append a list of all the child folders of a parent folder
  298. /// </summary>
  299. /// <param name="folders">list where folders will be appended</param>
  300. /// <param name="parentID">ID of parent</param>
  301. private void getInventoryFolders(ref List<InventoryFolderBase> folders, UUID parentID)
  302. {
  303. ICriteria criteria = manager.GetSession().CreateCriteria(typeof(InventoryFolderBase));
  304. criteria.Add(Expression.Eq("ParentID", parentID));
  305. foreach (InventoryFolderBase item in criteria.List())
  306. {
  307. folders.Add(item);
  308. }
  309. }
  310. /// <summary>
  311. /// Returns a list of inventory folders contained in the folder 'parentID'
  312. /// </summary>
  313. /// <param name="parentID">The folder to get subfolders for</param>
  314. /// <returns>A list of inventory folders</returns>
  315. public List<InventoryFolderBase> getInventoryFolders(UUID parentID)
  316. {
  317. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  318. getInventoryFolders(ref folders, parentID);
  319. return folders;
  320. }
  321. // See IInventoryDataPlugin
  322. public List<InventoryFolderBase> getFolderHierarchy(UUID parentID)
  323. {
  324. if (parentID == UUID.Zero)
  325. {
  326. // Zero UUID is not a real parent folder.
  327. return new List<InventoryFolderBase>();
  328. }
  329. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  330. getInventoryFolders(ref folders, parentID);
  331. for (int i = 0; i < folders.Count; i++)
  332. getInventoryFolders(ref folders, folders[i].ID);
  333. return folders;
  334. }
  335. public List<InventoryItemBase> fetchActiveGestures (UUID avatarID)
  336. {
  337. return null;
  338. }
  339. }
  340. }