NHibernateInventoryData.cs 13 KB

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