public void ValidateItemNames_Success(string value)
 {
     var validateItemNamesProcessor = new ValidateItemNames();
     var importItem = new ItemDto(value);
     validateItemNamesProcessor.ValidateName(importItem);
     Assert.Empty(validateItemNamesProcessor.Errors);
 }
 public override void Process(ImportItemsArgs args)
 {
     var rootItem = new ItemDto("<root>"); //ick
     foreach (var outputMap in args.Map.OutputMaps)
     {
         ImportMapItems(args, args.ImportData, outputMap, rootItem, true); //ick
     }
     args.ImportItems.AddRange(rootItem.Children); //ick
 }
 public void ValidateName(ItemDto item)
 {
     var suggestedName = Utils.GetValidItemName(item.Name);
     if (suggestedName != item.Name
         || suggestedName == Utils.UnNamedItem)
     {
         Errors.Add(string.Format("Invalid item name '{0}'.", item.Name));
     }
     if (item.Children != null)
     {
         foreach (var child in item.Children)
         {
             ValidateName(child);
         }
     }
 }
 private ItemDto CreateItem(DataRow dataRow, OutputMap outputMap)
 {
     var itemName = Convert.ToString(dataRow[outputMap.NameInputField]);
     var item = new ItemDto(itemName)
     {
         TemplateId = outputMap.TemplateId
     };
     for (int i = 0; i < outputMap.Fields.Count; i++)
     {
         var mapFieldName = outputMap.Fields[i].TargetFieldName;
         if (!string.IsNullOrEmpty(mapFieldName))
         {
             var fieldValue = dataRow[outputMap.Fields[i].SourceColumn].ToString();
             item.Fields.Add(mapFieldName, fieldValue);
         }
     }
     return item;
 }
 private void ImportMapItems(ImportItemsArgs args, DataTable dataTable, OutputMap outputMap, ItemDto parentItem,
     bool rootLevel)
 {
     var groupedTable = dataTable.GroupBy(outputMap.Fields.Select(f => f.SourceColumn).ToArray());
     for (int i = 0; i < groupedTable.Rows.Count; i++)
     {
         var row = groupedTable.Rows[i];
         if (rootLevel ||
             Convert.ToString(row[outputMap.ParentMap.NameInputField]) == parentItem.Name)
         {
             var createdItem = CreateItem(row, outputMap);
             createdItem.Parent = parentItem;
             parentItem.Children.Add(createdItem);
             if (outputMap.ChildMaps != null
                 && outputMap.ChildMaps.Any())
             {
                 foreach (var childMap in outputMap.ChildMaps)
                 {
                     ImportMapItems(args, dataTable, childMap, createdItem, false);
                 }
             }
         }
     }
 }
        private Item CreateItem(ImportItemsArgs args, ItemDto importItem, Item parentItem)
        {
            //CustomItemBase nItemTemplate = GetNewItemTemplate(dataRow);
            var templateItem = args.Database.GetTemplate(importItem.TemplateId);

            //get the parent in the specific language
            Item parent = args.Database.GetItem(parentItem.ID);

            Item item;
            //search for the child by name
            item = parent.GetChildren()[importItem.Name];
            if (item != null)
            {
                if (args.ImportOptions.ExistingItemHandling == ExistingItemHandling.AddVersion)
                {
                    args.Statistics.UpdatedItems++;
                    item = item.Versions.AddVersion();
                    Log.Info(string.Format("EzImporter:Creating new version of item {0}", item.Paths.ContentPath),
                        this);
                }
                else if (args.ImportOptions.ExistingItemHandling == ExistingItemHandling.Skip)
                {
                    Log.Info(string.Format("EzImporter:Skipping update of item {0}", item.Paths.ContentPath), this);
                    return item;
                }
                else if (args.ImportOptions.ExistingItemHandling == ExistingItemHandling.Update)
                {
                    //continue to update current item/version
                    args.Statistics.UpdatedItems++;
                }
            }
            else
            {
                //if not found then create one
                args.Statistics.CreatedItems++;
                item = parent.Add(importItem.Name, templateItem);
                Log.Info(string.Format("EzImporter:Creating item {0}", item.Paths.ContentPath), this);
            }

            if (item == null)
            {
                throw new NullReferenceException("the new item created was null");
            }

            using (new EditContext(item, true, false))
            {
                //add in the field mappings
                foreach (var key in importItem.Fields.Keys)
                {
                    var fieldValue = importItem.Fields[key];
                    var field = item.Fields[key];
                    if (field != null)
                    {
                        FieldUpdateManager.UpdateField(field, fieldValue, args.ImportOptions);
                        Log.Info(string.Format("'{0}' field set to '{1}'", key, fieldValue), this);
                    }
                    else
                    {
                        Log.Info(
                            string.Format(
                                "EzImporter:Field '{0}' not found on item, skipping update for this field",
                                key), this);
                    }
                }
                return item;
            }
        }
 private void ImportItems(ImportItemsArgs args, ItemDto importItem, Item parentItem,
     bool rootLevel)
 {
     if (rootLevel ||
         importItem.Parent.Name == parentItem.Name)
     {
         var createdItem = CreateItem(args, importItem, parentItem);
         if (createdItem != null
             && importItem.Children != null
             && importItem.Children.Any())
         {
             foreach (var childImportItem in importItem.Children)
             {
                 ImportItems(args, childImportItem, createdItem, false);
             }
         }
     }
 }