UserInventoryHelpers.cs 9.7 KB

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