NHibernateInventoryData.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. private ISession session;
  49. /// <summary>
  50. /// Initialises the interface
  51. /// </summary>
  52. public void Initialise(string connect)
  53. {
  54. // Split out the dialect, driver, and connect string
  55. char[] split = {';'};
  56. string[] parts = connect.Split(split, 3);
  57. if (parts.Length != 3)
  58. {
  59. // TODO: make this a real exception type
  60. throw new Exception("Malformed Inventory connection string '" + connect + "'");
  61. }
  62. string dialect = parts[0];
  63. // Establish NHibernate Connection
  64. cfg = new Configuration();
  65. cfg.SetProperty(Environment.ConnectionProvider,
  66. "NHibernate.Connection.DriverConnectionProvider");
  67. cfg.SetProperty(Environment.Dialect,
  68. "NHibernate.Dialect." + parts[0]);
  69. cfg.SetProperty(Environment.ConnectionDriver,
  70. "NHibernate.Driver." + parts[1]);
  71. cfg.SetProperty(Environment.ConnectionString, parts[2]);
  72. cfg.AddAssembly("OpenSim.Data.NHibernate");
  73. factory = cfg.BuildSessionFactory();
  74. session = factory.OpenSession();
  75. // This actually does the roll forward assembly stuff
  76. Assembly assem = GetType().Assembly;
  77. Migration m = new Migration((System.Data.Common.DbConnection)factory.ConnectionProvider.GetConnection(), assem, dialect, "InventoryStore");
  78. m.Update();
  79. }
  80. /*****************************************************************
  81. *
  82. * Basic CRUD operations on Data
  83. *
  84. ****************************************************************/
  85. // READ
  86. /// <summary>
  87. /// Returns an inventory item by its UUID
  88. /// </summary>
  89. /// <param name="item">The UUID of the item to be returned</param>
  90. /// <returns>A class containing item information</returns>
  91. public InventoryItemBase getInventoryItem(LLUUID item)
  92. {
  93. try
  94. {
  95. return session.Load(typeof(InventoryItemBase), item) as InventoryItemBase;
  96. }
  97. catch
  98. {
  99. m_log.ErrorFormat("Couldn't find inventory item: {0}", item);
  100. return null;
  101. }
  102. }
  103. /// <summary>
  104. /// Creates a new inventory item based on item
  105. /// </summary>
  106. /// <param name="item">The item to be created</param>
  107. public void addInventoryItem(InventoryItemBase item)
  108. {
  109. if (!ExistsItem(item.ID))
  110. {
  111. using (ITransaction transaction = session.BeginTransaction())
  112. {
  113. session.Save(item);
  114. transaction.Commit();
  115. }
  116. }
  117. else
  118. {
  119. m_log.ErrorFormat("Attempted to add Inventory Item {0} that already exists, updating instead", item.ID);
  120. updateInventoryItem(item);
  121. }
  122. }
  123. /// <summary>
  124. /// Updates an inventory item with item (updates based on ID)
  125. /// </summary>
  126. /// <param name="item">The updated item</param>
  127. public void updateInventoryItem(InventoryItemBase item)
  128. {
  129. if (ExistsItem(item.ID))
  130. {
  131. using (ITransaction transaction = session.BeginTransaction())
  132. {
  133. session.Update(item);
  134. transaction.Commit();
  135. }
  136. }
  137. else
  138. {
  139. m_log.ErrorFormat("Attempted to add Inventory Item {0} that already exists", item.ID);
  140. }
  141. }
  142. /// <summary>
  143. ///
  144. /// </summary>
  145. /// <param name="item"></param>
  146. public void deleteInventoryItem(LLUUID itemID)
  147. {
  148. using (ITransaction transaction = session.BeginTransaction())
  149. {
  150. session.Delete(itemID);
  151. transaction.Commit();
  152. }
  153. }
  154. /// <summary>
  155. /// Returns an inventory folder by its UUID
  156. /// </summary>
  157. /// <param name="folder">The UUID of the folder to be returned</param>
  158. /// <returns>A class containing folder information</returns>
  159. public InventoryFolderBase getInventoryFolder(LLUUID folder)
  160. {
  161. try
  162. {
  163. return session.Load(typeof(InventoryFolderBase), folder) as InventoryFolderBase;
  164. }
  165. catch
  166. {
  167. m_log.ErrorFormat("Couldn't find inventory item: {0}", folder);
  168. return null;
  169. }
  170. }
  171. /// <summary>
  172. /// Creates a new inventory folder based on folder
  173. /// </summary>
  174. /// <param name="folder">The folder to be created</param>
  175. public void addInventoryFolder(InventoryFolderBase folder)
  176. {
  177. if (!ExistsFolder(folder.ID))
  178. {
  179. using (ITransaction transaction = session.BeginTransaction())
  180. {
  181. session.Save(folder);
  182. transaction.Commit();
  183. }
  184. }
  185. else
  186. {
  187. m_log.ErrorFormat("Attempted to add Inventory Folder {0} that already exists, updating instead", folder.ID);
  188. updateInventoryFolder(folder);
  189. }
  190. }
  191. /// <summary>
  192. /// Updates an inventory folder with folder (updates based on ID)
  193. /// </summary>
  194. /// <param name="folder">The updated folder</param>
  195. public void updateInventoryFolder(InventoryFolderBase folder)
  196. {
  197. if (ExistsFolder(folder.ID))
  198. {
  199. using (ITransaction transaction = session.BeginTransaction())
  200. {
  201. session.Update(folder);
  202. transaction.Commit();
  203. }
  204. }
  205. else
  206. {
  207. m_log.ErrorFormat("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(LLUUID folderID)
  215. {
  216. using (ITransaction transaction = session.BeginTransaction())
  217. {
  218. session.Delete(folderID.ToString());
  219. transaction.Commit();
  220. }
  221. }
  222. // useful private methods
  223. private bool ExistsItem(LLUUID uuid)
  224. {
  225. return (getInventoryItem(uuid) != null) ? true : false;
  226. }
  227. private bool ExistsFolder(LLUUID uuid)
  228. {
  229. return (getInventoryFolder(uuid) != null) ? true : false;
  230. }
  231. public void Shutdown()
  232. {
  233. // TODO: DataSet commit
  234. }
  235. /// <summary>
  236. /// Closes the interface
  237. /// </summary>
  238. public void Close()
  239. {
  240. }
  241. /// <summary>
  242. /// The plugin being loaded
  243. /// </summary>
  244. /// <returns>A string containing the plugin name</returns>
  245. public string getName()
  246. {
  247. return "NHibernate Inventory Data Interface";
  248. }
  249. /// <summary>
  250. /// The plugins version
  251. /// </summary>
  252. /// <returns>A string containing the plugin version</returns>
  253. public string getVersion()
  254. {
  255. Module module = GetType().Module;
  256. // string dllName = module.Assembly.ManifestModule.Name;
  257. Version dllVersion = module.Assembly.GetName().Version;
  258. return
  259. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  260. dllVersion.Revision);
  261. }
  262. // Move seems to be just update
  263. public void moveInventoryFolder(InventoryFolderBase folder)
  264. {
  265. updateInventoryFolder(folder);
  266. }
  267. public void moveInventoryItem(InventoryItemBase item)
  268. {
  269. updateInventoryItem(item);
  270. }
  271. /// <summary>
  272. /// Returns a list of inventory items contained within the specified folder
  273. /// </summary>
  274. /// <param name="folderID">The UUID of the target folder</param>
  275. /// <returns>A List of InventoryItemBase items</returns>
  276. public List<InventoryItemBase> getInventoryInFolder(LLUUID folderID)
  277. {
  278. // try {
  279. ICriteria criteria = session.CreateCriteria(typeof(InventoryItemBase));
  280. criteria.Add(Expression.Eq("Folder", folderID));
  281. List<InventoryItemBase> list = new List<InventoryItemBase>();
  282. foreach (InventoryItemBase item in criteria.List())
  283. {
  284. list.Add(item);
  285. }
  286. return list;
  287. // }
  288. // catch
  289. // {
  290. // return new List<InventoryItemBase>();
  291. // }
  292. }
  293. public List<InventoryFolderBase> getUserRootFolders(LLUUID user)
  294. {
  295. return new List<InventoryFolderBase>();
  296. }
  297. // see InventoryItemBase.getUserRootFolder
  298. public InventoryFolderBase getUserRootFolder(LLUUID user)
  299. {
  300. ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase));
  301. criteria.Add(Expression.Eq("ParentID", LLUUID.Zero));
  302. criteria.Add(Expression.Eq("Owner", user));
  303. foreach (InventoryFolderBase folder in criteria.List())
  304. {
  305. return folder;
  306. }
  307. m_log.ErrorFormat("No Inventory Root Folder Found for: {0}", user);
  308. return null;
  309. }
  310. /// <summary>
  311. /// Append a list of all the child folders of a parent folder
  312. /// </summary>
  313. /// <param name="folders">list where folders will be appended</param>
  314. /// <param name="parentID">ID of parent</param>
  315. private void getInventoryFolders(ref List<InventoryFolderBase> folders, LLUUID parentID)
  316. {
  317. ICriteria criteria = session.CreateCriteria(typeof(InventoryFolderBase));
  318. criteria.Add(Expression.Eq("ParentID", parentID));
  319. foreach (InventoryFolderBase item in criteria.List())
  320. {
  321. folders.Add(item);
  322. }
  323. }
  324. /// <summary>
  325. /// Returns a list of inventory folders contained in the folder 'parentID'
  326. /// </summary>
  327. /// <param name="parentID">The folder to get subfolders for</param>
  328. /// <returns>A list of inventory folders</returns>
  329. public List<InventoryFolderBase> getInventoryFolders(LLUUID parentID)
  330. {
  331. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  332. getInventoryFolders(ref folders, parentID);
  333. return folders;
  334. }
  335. // See IInventoryData
  336. public List<InventoryFolderBase> getFolderHierarchy(LLUUID parentID)
  337. {
  338. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  339. getInventoryFolders(ref folders, parentID);
  340. for (int i = 0; i < folders.Count; i++)
  341. getInventoryFolders(ref folders, folders[i].ID);
  342. return folders;
  343. }
  344. }
  345. }