UserInventoryHelpers.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 = AssetHelpers.CreateAsset(assetId, SceneHelpers.CreateSceneObject(1, userId));
  86. else
  87. throw new Exception(string.Format("Inventory type {0} not supported", type));
  88. scene.AssetService.Store(asset);
  89. InventoryItemBase item = new InventoryItemBase();
  90. item.Name = itemName;
  91. item.AssetID = asset.FullID;
  92. item.ID = itemId;
  93. item.Owner = userId;
  94. item.AssetType = asset.Type;
  95. item.InvType = (int)type;
  96. InventoryFolderBase folder = scene.InventoryService.GetFolderForType(userId, AssetType.Notecard);
  97. item.Folder = folder.ID;
  98. scene.AddInventoryItem(item);
  99. return item;
  100. }
  101. /// <summary>
  102. /// Create inventory folders starting from the user's root folder.
  103. /// </summary>
  104. ///
  105. /// Ignores any existing folders with the same name
  106. ///
  107. /// <param name="inventoryService"></param>
  108. /// <param name="userId"></param>
  109. /// <param name="path">
  110. /// The folders to create. Multiple folders can be specified on a path delimited by the PATH_DELIMITER
  111. /// </param>
  112. /// <returns>
  113. /// The folder created. If the path contains multiple folders then the last one created is returned.
  114. /// Will return null if the root folder could not be found.
  115. /// </returns>
  116. public static InventoryFolderBase CreateInventoryFolder(
  117. IInventoryService inventoryService, UUID userId, string path)
  118. {
  119. InventoryFolderBase rootFolder = inventoryService.GetRootFolder(userId);
  120. if (null == rootFolder)
  121. return null;
  122. return CreateInventoryFolder(inventoryService, rootFolder, path);
  123. }
  124. /// <summary>
  125. /// Create inventory folders starting from a given parent folder
  126. /// </summary>
  127. ///
  128. /// Ignores any existing folders with the same name
  129. ///
  130. /// <param name="inventoryService"></param>
  131. /// <param name="parentFolder"></param>
  132. /// <param name="path">
  133. /// The folders to create. Multiple folders can be specified on a path delimited by the PATH_DELIMITER
  134. /// </param>
  135. /// <returns>
  136. /// The folder created. If the path contains multiple folders then the last one created is returned.
  137. /// </returns>
  138. public static InventoryFolderBase CreateInventoryFolder(
  139. IInventoryService inventoryService, InventoryFolderBase parentFolder, string path)
  140. {
  141. string[] components = path.Split(new string[] { PATH_DELIMITER }, 2, StringSplitOptions.None);
  142. InventoryFolderBase newFolder
  143. = new InventoryFolderBase(UUID.Random(), components[0], parentFolder.Owner, parentFolder.ID);
  144. inventoryService.AddFolder(newFolder);
  145. if (components.Length > 1)
  146. return CreateInventoryFolder(inventoryService, newFolder, components[1]);
  147. else
  148. return newFolder;
  149. }
  150. /// <summary>
  151. /// Get the inventory folder that matches the path name. If there are multiple folders then only the first
  152. /// is returned.
  153. /// </summary>
  154. /// <param name="inventoryService"></param>
  155. /// <param name="userId"></param>
  156. /// <param name="path"></param>
  157. /// <returns>null if no folder matching the path was found</returns>
  158. public static InventoryFolderBase GetInventoryFolder(IInventoryService inventoryService, UUID userId, string path)
  159. {
  160. List<InventoryFolderBase> folders = GetInventoryFolders(inventoryService, userId, path);
  161. if (folders.Count != 0)
  162. return folders[0];
  163. else
  164. return null;
  165. }
  166. /// <summary>
  167. /// Get the inventory folders that match the path name.
  168. /// </summary>
  169. /// <param name="inventoryService"></param>
  170. /// <param name="userId"></param>
  171. /// <param name="path"></param>
  172. /// <returns>An empty list if no matching folders were found</returns>
  173. public static List<InventoryFolderBase> GetInventoryFolders(IInventoryService inventoryService, UUID userId, string path)
  174. {
  175. return InventoryArchiveUtils.FindFolderByPath(inventoryService, userId, path);
  176. }
  177. /// <summary>
  178. /// Get the inventory item that matches the path name. If there are multiple items then only the first
  179. /// is returned.
  180. /// </summary>
  181. /// <param name="inventoryService"></param>
  182. /// <param name="userId"></param>
  183. /// <param name="path"></param>
  184. /// <returns>null if no item matching the path was found</returns>
  185. public static InventoryItemBase GetInventoryItem(IInventoryService inventoryService, UUID userId, string path)
  186. {
  187. return InventoryArchiveUtils.FindItemByPath(inventoryService, userId, path);
  188. }
  189. /// <summary>
  190. /// Get the inventory items that match the path name.
  191. /// </summary>
  192. /// <param name="inventoryService"></param>
  193. /// <param name="userId"></param>
  194. /// <param name="path"></param>
  195. /// <returns>An empty list if no matching items were found.</returns>
  196. public static List<InventoryItemBase> GetInventoryItems(IInventoryService inventoryService, UUID userId, string path)
  197. {
  198. return InventoryArchiveUtils.FindItemsByPath(inventoryService, userId, path);
  199. }
  200. }
  201. }