NHibernateInventoryData.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 libsecondlife;
  33. using log4net;
  34. using NHibernate;
  35. using NHibernate.Cfg;
  36. using NHibernate.Expression;
  37. using NHibernate.Mapping.Attributes;
  38. using NHibernate.Tool.hbm2ddl;
  39. using OpenSim.Framework;
  40. using Environment=NHibernate.Cfg.Environment;
  41. namespace OpenSim.Data.NHibernate
  42. {
  43. public class NHibernateInventoryData: IInventoryData
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private Configuration cfg;
  47. private ISessionFactory factory;
  48. /// <summary>
  49. /// Initialises the interface
  50. /// </summary>
  51. public void Initialise(string connect)
  52. {
  53. // Split out the dialect, driver, and connect string
  54. char[] split = {';'};
  55. string[] parts = connect.Split(split, 3);
  56. if (parts.Length != 3)
  57. {
  58. // TODO: make this a real exception type
  59. throw new Exception("Malformed Inventory connection string '" + connect + "'");
  60. }
  61. // Establish NHibernate Connection
  62. cfg = new Configuration();
  63. cfg.SetProperty(Environment.ConnectionProvider,
  64. "NHibernate.Connection.DriverConnectionProvider");
  65. cfg.SetProperty(Environment.Dialect,
  66. "NHibernate.Dialect." + parts[0]);
  67. cfg.SetProperty(Environment.ConnectionDriver,
  68. "NHibernate.Driver." + parts[1]);
  69. cfg.SetProperty(Environment.ConnectionString, parts[2]);
  70. cfg.AddAssembly("OpenSim.Data.NHibernate");
  71. HbmSerializer.Default.Validate = true;
  72. using (MemoryStream stream =
  73. HbmSerializer.Default.Serialize(Assembly.GetExecutingAssembly()))
  74. cfg.AddInputStream(stream);
  75. // If uncommented this will auto create tables, but it
  76. // does drops of the old tables, so we need a smarter way
  77. // to acturally manage this.
  78. // new SchemaExport(cfg).Create(true, true);
  79. factory = cfg.BuildSessionFactory();
  80. InitDB();
  81. }
  82. private void InitDB()
  83. {
  84. string regex = @"no such table: Inventory";
  85. Regex RE = new Regex(regex, RegexOptions.Multiline);
  86. try
  87. {
  88. using (ISession session = factory.OpenSession())
  89. {
  90. session.Load(typeof(InventoryItemBase), LLUUID.Zero);
  91. }
  92. }
  93. catch (ObjectNotFoundException)
  94. {
  95. // yes, we know it's not there, but that's ok
  96. }
  97. catch (ADOException e)
  98. {
  99. Match m = RE.Match(e.ToString());
  100. if (m.Success)
  101. {
  102. // We don't have this table, so create it.
  103. new SchemaExport(cfg).Create(true, true);
  104. }
  105. }
  106. }
  107. /*****************************************************************
  108. *
  109. * Basic CRUD operations on Data
  110. *
  111. ****************************************************************/
  112. // READ
  113. /// <summary>
  114. /// Returns an inventory item by its UUID
  115. /// </summary>
  116. /// <param name="item">The UUID of the item to be returned</param>
  117. /// <returns>A class containing item information</returns>
  118. public InventoryItemBase getInventoryItem(LLUUID item)
  119. {
  120. using (ISession session = factory.OpenSession())
  121. {
  122. try
  123. {
  124. return session.Load(typeof(InventoryItemBase), item) as InventoryItemBase;
  125. }
  126. catch
  127. {
  128. m_log.ErrorFormat("Couldn't find inventory item: {0}", item);
  129. return null;
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// Creates a new inventory item based on item
  135. /// </summary>
  136. /// <param name="item">The item to be created</param>
  137. public void addInventoryItem(InventoryItemBase item)
  138. {
  139. if (!ExistsItem(item.ID))
  140. {
  141. using (ISession session = factory.OpenSession())
  142. {
  143. using (ITransaction transaction = session.BeginTransaction())
  144. {
  145. session.Save(item);
  146. transaction.Commit();
  147. }
  148. }
  149. }
  150. else
  151. {
  152. m_log.ErrorFormat("Attempted to add Inventory Item {0} that already exists, updating instead", item.ID);
  153. updateInventoryItem(item);
  154. }
  155. }
  156. /// <summary>
  157. /// Updates an inventory item with item (updates based on ID)
  158. /// </summary>
  159. /// <param name="item">The updated item</param>
  160. public void updateInventoryItem(InventoryItemBase item)
  161. {
  162. if (ExistsItem(item.ID))
  163. {
  164. using (ISession session = factory.OpenSession())
  165. {
  166. using (ITransaction transaction = session.BeginTransaction())
  167. {
  168. session.Update(item);
  169. transaction.Commit();
  170. }
  171. }
  172. }
  173. else
  174. {
  175. m_log.ErrorFormat("Attempted to add Inventory Item {0} that already exists", item.ID);
  176. }
  177. }
  178. /// <summary>
  179. ///
  180. /// </summary>
  181. /// <param name="item"></param>
  182. public void deleteInventoryItem(LLUUID itemID)
  183. {
  184. using (ISession session = factory.OpenSession())
  185. {
  186. using (ITransaction transaction = session.BeginTransaction())
  187. {
  188. session.Delete(itemID);
  189. transaction.Commit();
  190. }
  191. }
  192. }
  193. /// <summary>
  194. /// Returns an inventory folder by its UUID
  195. /// </summary>
  196. /// <param name="folder">The UUID of the folder to be returned</param>
  197. /// <returns>A class containing folder information</returns>
  198. public InventoryFolderBase getInventoryFolder(LLUUID folder)
  199. {
  200. using (ISession session = factory.OpenSession())
  201. {
  202. try
  203. {
  204. return session.Load(typeof(InventoryFolderBase), folder) as InventoryFolderBase;
  205. }
  206. catch
  207. {
  208. m_log.ErrorFormat("Couldn't find inventory item: {0}", folder);
  209. return null;
  210. }
  211. }
  212. }
  213. /// <summary>
  214. /// Creates a new inventory folder based on folder
  215. /// </summary>
  216. /// <param name="folder">The folder to be created</param>
  217. public void addInventoryFolder(InventoryFolderBase folder)
  218. {
  219. if (!ExistsFolder(folder.ID))
  220. {
  221. using (ISession session = factory.OpenSession())
  222. {
  223. using (ITransaction transaction = session.BeginTransaction())
  224. {
  225. session.Save(folder);
  226. transaction.Commit();
  227. }
  228. }
  229. }
  230. else
  231. {
  232. m_log.ErrorFormat("Attempted to add Inventory Folder {0} that already exists, updating instead", folder.ID);
  233. updateInventoryFolder(folder);
  234. }
  235. }
  236. /// <summary>
  237. /// Updates an inventory folder with folder (updates based on ID)
  238. /// </summary>
  239. /// <param name="folder">The updated folder</param>
  240. public void updateInventoryFolder(InventoryFolderBase folder)
  241. {
  242. if (ExistsFolder(folder.ID))
  243. {
  244. using (ISession session = factory.OpenSession())
  245. {
  246. using (ITransaction transaction = session.BeginTransaction())
  247. {
  248. session.Update(folder);
  249. transaction.Commit();
  250. }
  251. }
  252. }
  253. else
  254. {
  255. m_log.ErrorFormat("Attempted to add Inventory Folder {0} that already exists", folder.ID);
  256. }
  257. }
  258. /// <summary>
  259. ///
  260. /// </summary>
  261. /// <param name="folder"></param>
  262. public void deleteInventoryFolder(LLUUID folderID)
  263. {
  264. using (ISession session = factory.OpenSession())
  265. {
  266. using (ITransaction transaction = session.BeginTransaction())
  267. {
  268. session.Delete(folderID.ToString());
  269. transaction.Commit();
  270. }
  271. }
  272. }
  273. // useful private methods
  274. private bool ExistsItem(LLUUID uuid)
  275. {
  276. return (getInventoryItem(uuid) != null) ? true : false;
  277. }
  278. private bool ExistsFolder(LLUUID uuid)
  279. {
  280. return (getInventoryFolder(uuid) != null) ? true : false;
  281. }
  282. public void Shutdown()
  283. {
  284. // TODO: DataSet commit
  285. }
  286. /// <summary>
  287. /// Closes the interface
  288. /// </summary>
  289. public void Close()
  290. {
  291. }
  292. /// <summary>
  293. /// The plugin being loaded
  294. /// </summary>
  295. /// <returns>A string containing the plugin name</returns>
  296. public string getName()
  297. {
  298. return "NHibernate Inventory Data Interface";
  299. }
  300. /// <summary>
  301. /// The plugins version
  302. /// </summary>
  303. /// <returns>A string containing the plugin version</returns>
  304. public string getVersion()
  305. {
  306. Module module = GetType().Module;
  307. string dllName = module.Assembly.ManifestModule.Name;
  308. Version dllVersion = module.Assembly.GetName().Version;
  309. return
  310. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  311. dllVersion.Revision);
  312. }
  313. // Move seems to be just update
  314. public void moveInventoryFolder(InventoryFolderBase folder)
  315. {
  316. updateInventoryFolder(folder);
  317. }
  318. public void moveInventoryItem(InventoryItemBase item)
  319. {
  320. updateInventoryItem(item);
  321. }
  322. /// <summary>
  323. /// Returns a list of inventory items contained within the specified folder
  324. /// </summary>
  325. /// <param name="folderID">The UUID of the target folder</param>
  326. /// <returns>A List of InventoryItemBase items</returns>
  327. public List<InventoryItemBase> getInventoryInFolder(LLUUID folderID)
  328. {
  329. using (ISession session = factory.OpenSession())
  330. {
  331. // try {
  332. ICriteria criteria = session.CreateCriteria(typeof(InventoryItemBase));
  333. criteria.Add(Expression.Eq("Folder", folderID));
  334. List<InventoryItemBase> list = new List<InventoryItemBase>();
  335. foreach (InventoryItemBase item in criteria.List())
  336. {
  337. list.Add(item);
  338. }
  339. return list;
  340. // }
  341. // catch
  342. // {
  343. // return new List<InventoryItemBase>();
  344. // }
  345. }
  346. }
  347. public List<InventoryFolderBase> getUserRootFolders(LLUUID user)
  348. {
  349. return new List<InventoryFolderBase>();
  350. }
  351. // see InventoryItemBase.getUserRootFolder
  352. public InventoryFolderBase getUserRootFolder(LLUUID user)
  353. {
  354. using (ISession session = factory.OpenSession())
  355. {
  356. ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase));
  357. criteria.Add(Expression.Eq("ParentID", LLUUID.Zero));
  358. criteria.Add(Expression.Eq("Owner", user));
  359. foreach (InventoryFolderBase folder in criteria.List())
  360. {
  361. return folder;
  362. }
  363. m_log.ErrorFormat("No Inventory Root Folder Found for: {0}", user);
  364. return null;
  365. }
  366. }
  367. /// <summary>
  368. /// Append a list of all the child folders of a parent folder
  369. /// </summary>
  370. /// <param name="folders">list where folders will be appended</param>
  371. /// <param name="parentID">ID of parent</param>
  372. private void getInventoryFolders(ref List<InventoryFolderBase> folders, LLUUID parentID)
  373. {
  374. using (ISession session = factory.OpenSession())
  375. {
  376. ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase));
  377. criteria.Add(Expression.Eq("ParentID", parentID));
  378. foreach (InventoryFolderBase item in criteria.List())
  379. {
  380. folders.Add(item);
  381. }
  382. }
  383. }
  384. /// <summary>
  385. /// Returns a list of inventory folders contained in the folder 'parentID'
  386. /// </summary>
  387. /// <param name="parentID">The folder to get subfolders for</param>
  388. /// <returns>A list of inventory folders</returns>
  389. public List<InventoryFolderBase> getInventoryFolders(LLUUID parentID)
  390. {
  391. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  392. getInventoryFolders(ref folders, parentID);
  393. return folders;
  394. }
  395. // See IInventoryData
  396. public List<InventoryFolderBase> getFolderHierarchy(LLUUID parentID)
  397. {
  398. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  399. getInventoryFolders(ref folders, parentID);
  400. for (int i = 0; i < folders.Count; i++)
  401. getInventoryFolders(ref folders, folders[i].ID);
  402. return folders;
  403. }
  404. }
  405. }