UserInventoryHelpers.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 OpenMetaverse;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.CoreModules.Avatar.Inventory.Archiver;
  32. using OpenSim.Region.Framework.Scenes;
  33. using OpenSim.Services.Interfaces;
  34. namespace OpenSim.Tests.Common
  35. {
  36. /// <summary>
  37. /// Utility functions for carrying out user inventory tests.
  38. /// </summary>
  39. public static class UserInventoryHelpers
  40. {
  41. public static readonly string PATH_DELIMITER = "/";
  42. /// <summary>
  43. /// Creates a notecard in the objects folder and specify an item id.
  44. /// </summary>
  45. /// <param name="scene"></param>
  46. /// <param name="itemName"></param>
  47. /// <param name="itemId"></param>
  48. /// <param name="userId"></param>
  49. /// <returns></returns>
  50. public static InventoryItemBase CreateInventoryItem(Scene scene, string itemName, UUID userId)
  51. {
  52. return CreateInventoryItem(scene, itemName, UUID.Random(), UUID.Random(), userId, InventoryType.Notecard);
  53. }
  54. /// <summary>
  55. /// Creates an item of the given type with an accompanying asset.
  56. /// </summary>
  57. /// <param name="scene"></param>
  58. /// <param name="itemName"></param>
  59. /// <param name="itemId"></param>
  60. /// <param name="userId"></param>
  61. /// <param name="type">Type of item to create</param>
  62. /// <returns></returns>
  63. public static InventoryItemBase CreateInventoryItem(
  64. Scene scene, string itemName, UUID userId, InventoryType type)
  65. {
  66. return CreateInventoryItem(scene, itemName, UUID.Random(), UUID.Random(), userId, type);
  67. }
  68. /// <summary>
  69. /// Creates a notecard in the objects folder and specify an item id.
  70. /// </summary>
  71. /// <param name="scene"></param>
  72. /// <param name="itemName"></param>
  73. /// <param name="itemId"></param>
  74. /// <param name="assetId"></param>
  75. /// <param name="userId"></param>
  76. /// <param name="type">Type of item to create</param>
  77. /// <returns></returns>
  78. public static InventoryItemBase CreateInventoryItem(
  79. Scene scene, string itemName, UUID itemId, UUID assetId, UUID userId, InventoryType type)
  80. {
  81. AssetBase asset = null;
  82. if (type == InventoryType.Notecard)
  83. asset = AssetHelpers.CreateAsset(scene, userId);
  84. else if (type == InventoryType.Object)
  85. asset
  86. = AssetHelpers.CreateAsset(assetId, SceneHelpers.CreateSceneObject(1, userId));
  87. else
  88. throw new Exception(string.Format("Inventory type {0} not supported", type));
  89. scene.AssetService.Store(asset);
  90. InventoryItemBase item = new InventoryItemBase();
  91. item.Name = itemName;
  92. item.AssetID = asset.FullID;
  93. item.ID = itemId;
  94. item.Owner = userId;
  95. item.AssetType = asset.Type;
  96. item.InvType = (int)type;
  97. InventoryFolderBase folder = scene.InventoryService.GetFolderForType(userId, AssetType.Notecard);
  98. item.Folder = folder.ID;
  99. scene.AddInventoryItem(item);
  100. return item;
  101. }
  102. /// <summary>
  103. /// Create inventory folders starting from the user's root folder.
  104. /// </summary>
  105. ///
  106. /// Ignores any existing folders with the same name
  107. ///
  108. /// <param name="inventoryService"></param>
  109. /// <param name="userId"></param>
  110. /// <param name="path">
  111. /// The folders to create. Multiple folders can be specified on a path delimited by the PATH_DELIMITER
  112. /// </param>
  113. /// <returns>
  114. /// The folder created. If the path contains multiple folders then the last one created is returned.
  115. /// Will return null if the root folder could not be found.
  116. /// </returns>
  117. public static InventoryFolderBase CreateInventoryFolder(
  118. IInventoryService inventoryService, UUID userId, string path)
  119. {
  120. InventoryFolderBase rootFolder = inventoryService.GetRootFolder(userId);
  121. if (null == rootFolder)
  122. return null;
  123. return CreateInventoryFolder(inventoryService, rootFolder, path);
  124. }
  125. /// <summary>
  126. /// Create inventory folders starting from a given parent folder
  127. /// </summary>
  128. ///
  129. /// Ignores any existing folders with the same name
  130. ///
  131. /// <param name="inventoryService"></param>
  132. /// <param name="parentFolder"></param>
  133. /// <param name="path">
  134. /// The folders to create. Multiple folders can be specified on a path delimited by the PATH_DELIMITER
  135. /// </param>
  136. /// <returns>
  137. /// The folder created. If the path contains multiple folders then the last one created is returned.
  138. /// </returns>
  139. public static InventoryFolderBase CreateInventoryFolder(
  140. IInventoryService inventoryService, InventoryFolderBase parentFolder, string path)
  141. {
  142. string[] components = path.Split(new string[] { PATH_DELIMITER }, 2, StringSplitOptions.None);
  143. InventoryFolderBase newFolder
  144. = new InventoryFolderBase(UUID.Random(), components[0], parentFolder.Owner, parentFolder.ID);
  145. inventoryService.AddFolder(newFolder);
  146. if (components.Length > 1)
  147. return CreateInventoryFolder(inventoryService, newFolder, components[1]);
  148. else
  149. return newFolder;
  150. }
  151. /// <summary>
  152. /// Get the inventory folder that matches the path name. If there are multiple folders then only the first
  153. /// is returned.
  154. /// </summary>
  155. /// <param name="inventoryService"></param>
  156. /// <param name="userId"></param>
  157. /// <param name="path"></param>
  158. /// <returns>null if no folder matching the path was found</returns>
  159. public static InventoryFolderBase GetInventoryFolder(IInventoryService inventoryService, UUID userId, string path)
  160. {
  161. List<InventoryFolderBase> folders = GetInventoryFolders(inventoryService, userId, path);
  162. if (folders.Count != 0)
  163. return folders[0];
  164. else
  165. return null;
  166. }
  167. /// <summary>
  168. /// Get the inventory folders that match the path name.
  169. /// </summary>
  170. /// <param name="inventoryService"></param>
  171. /// <param name="userId"></param>
  172. /// <param name="path"></param>
  173. /// <returns>An empty list if no matching folders were found</returns>
  174. public static List<InventoryFolderBase> GetInventoryFolders(IInventoryService inventoryService, UUID userId, string path)
  175. {
  176. return InventoryArchiveUtils.FindFolderByPath(inventoryService, userId, path);
  177. }
  178. /// <summary>
  179. /// Get the inventory item that matches the path name. If there are multiple items then only the first
  180. /// is returned.
  181. /// </summary>
  182. /// <param name="inventoryService"></param>
  183. /// <param name="userId"></param>
  184. /// <param name="path"></param>
  185. /// <returns>null if no item matching the path was found</returns>
  186. public static InventoryItemBase GetInventoryItem(IInventoryService inventoryService, UUID userId, string path)
  187. {
  188. return InventoryArchiveUtils.FindItemByPath(inventoryService, userId, path);
  189. }
  190. /// <summary>
  191. /// Get the inventory items that match the path name.
  192. /// </summary>
  193. /// <param name="inventoryService"></param>
  194. /// <param name="userId"></param>
  195. /// <param name="path"></param>
  196. /// <returns>An empty list if no matching items were found.</returns>
  197. public static List<InventoryItemBase> GetInventoryItems(IInventoryService inventoryService, UUID userId, string path)
  198. {
  199. return InventoryArchiveUtils.FindItemsByPath(inventoryService, userId, path);
  200. }
  201. }
  202. }