Operations related to creating new items and first checking to see if the item already exists
Inheritance: ICreateItem
Ejemplo n.º 1
0
        /// <summary>
        /// Checks to see if an item already exists under a parent. If it exists return the item,
        /// otherwise create and return the new item.
        /// </summary>
        /// <param name="parentItem">The parent item.</param>
        /// <param name="itemName">Name of the item.</param>
        /// <param name="template">The template.</param>
        /// <param name="db">The db to use.</param>
        /// <param name="itemCreated">if set to <c>true</c> [item created].</param>
        /// <returns>
        /// The found item if an item with the passed in name already existed under the parent,
        /// otherwise the newly created item.
        /// </returns>
        /// <remarks>
        /// This has been updated to call the testable method.  
        /// Remaining logic is the item name cleaning.
        /// </remarks>
        public static Item GetOrCreateItem(Item parentItem, string itemName, TemplateItem template, Database db, out bool itemCreated)
        {
            // Initialize itemCreated to be false.
            itemCreated = false;

            //If any of the passed in parameters are invalid, return null
            if (parentItem == null || string.IsNullOrEmpty(itemName) || template == null || db == null) return null;

            //Clean item name
            string cleanedItemName = ItemNameCleaner(itemName);

            // make a testable implementation of GetOrCreateItem with the interface
            ICreateItem iCreateItem;
            if (parentItem.Language != null)
            {
                iCreateItem = new CreateItem(parentItem.ID, cleanedItemName, template.ID, db.Name, parentItem.Language);
            }
            else
            {
                iCreateItem = new CreateItem(parentItem.ID, cleanedItemName, template.ID, db.Name);
            }
            ID itemId = GetOrCreateItem(iCreateItem, out itemCreated);
            return db.GetItem(itemId);
        }