예제 #1
0
        public void RemoveItem(Item parent, object importRow, string itemName)
        {
            using (new LanguageSwitcher(ImportToLanguage))
            {
                //get the parent in the specific language
                parent = SitecoreDB.GetItem(parent.ID);

                Item item = null;
                //search for the child by name
                if (string.IsNullOrEmpty(SearchIndex))
                {
                    item = parent.Axes.GetDescendants().FirstOrDefault(x => x.Name == itemName);
                }
                else
                {
                    var index = ContentSearchManager.GetIndex(SearchIndex);
                    using (var context = index.CreateSearchContext())
                    {
                        var query =
                            context.GetQueryable <SearchResultItem>().Where(m => m.Name == itemName);
                        if (query.Any())
                        {
                            item = SitecoreDB.GetItem(query.First().ItemId);
                        }
                    }
                }
                if (item != null)
                {
                    item.Recycle();
                }
            }
        }
        public void CreateNewItem(Item parent, object importRow, string newItemName)
        {
            CustomItemBase nItemTemplate = GetNewItemTemplate(importRow);

            using (new LanguageSwitcher(ImportToLanguage)) {
                //get the parent in the specific language
                parent = SitecoreDB.GetItem(parent.ID);

                Item newItem;
                //search for the child by name
                newItem = parent.GetChildren()[newItemName];
                if (newItem != null)                 //add version for lang
                {
                    newItem = newItem.Versions.AddVersion();
                }

                //if not found then create one
                if (newItem == null)
                {
                    if (nItemTemplate is BranchItem)
                    {
                        newItem = parent.Add(newItemName, (BranchItem)nItemTemplate);
                    }
                    else
                    {
                        newItem = parent.Add(newItemName, (TemplateItem)nItemTemplate);
                    }
                }

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

                using (new EditContext(newItem, true, false)) {
                    //add in the field mappings
                    foreach (IBaseField d in this.FieldDefinitions)
                    {
                        try
                        {
                            IEnumerable <string> values = GetFieldValues(d.GetExistingFieldNames(), importRow);

                            string importValue = String.Join(d.GetFieldValueDelimiter(), values);
                            if (!string.IsNullOrEmpty(importValue))
                            {
                                d.FillField(this, ref newItem, importValue);
                            }
                        }
                        catch (Exception ex)
                        {
                            Log("Field Level Error", $"item '{newItem.DisplayName}', field '{d.ItemName()}'");
                        }
                    }

                    //calls the subclass method to handle custom fields and properties
                    ProcessCustomData(ref newItem, importRow);
                }
            }
        }
예제 #3
0
        public void CreateNewItem(Item parent, object importRow, string newItemName)
        {
            var nItemTemplate = GetNewItemTemplate(importRow);
            var updating      = false;

            using (new LanguageSwitcher(ImportToLanguage))
            {
                //get the parent in the specific language
                parent = SitecoreDB.GetItem(parent.ID);

                Item newItem = null;
                //search for the child by name
                if (string.IsNullOrEmpty(SearchIndex))
                {
                    var existingItems = parent.Axes.GetDescendants().Where(x => x.Name == newItemName);
                    var firstItem     = true;
                    foreach (var existingItem in existingItems)
                    {
                        if (firstItem)
                        {
                            newItem = existingItem;
                        }
                        else
                        {
                            existingItem.Delete();
                            Log("Duplicate Item Found", "Deleting Duplicate Item (matNum: " + existingItem.Name + ")");
                        }

                        firstItem = false;
                    }
                }
                else
                {
                    var index = ContentSearchManager.GetIndex(SearchIndex);
                    using (var context = index.CreateSearchContext())
                    {
                        var query =
                            context.GetQueryable <SearchResultItem>().Where(m => m.Name == newItemName);
                        if (query.Any())
                        {
                            newItem = SitecoreDB.GetItem(query.First().ItemId);
                        }
                        var firstItem = true;
                        foreach (var existingItem in query)
                        {
                            if (firstItem)
                            {
                                newItem = SitecoreDB.GetItem(existingItem.ItemId);
                            }
                            else
                            {
                                var duplicateItem = SitecoreDB.GetItem(existingItem.ItemId);
                                if (duplicateItem != null)
                                {
                                    duplicateItem.Delete();
                                    Log("Duplicate Item Found", "Deleting Duplicate Item (matNum: " + existingItem.Name + ")");
                                }
                            }
                            firstItem = false;
                        }
                    }
                }
                if (newItem != null)
                {
                    updating = true;
                    if (SkipExistingItems)
                    {
                        return;
                    }
                    if (!Overwrite)
                    {
                        newItem = newItem.Versions.AddVersion();
                    }
                }

                //if not found then create one
                if (newItem == null)
                {
                    if (nItemTemplate is BranchItem)
                    {
                        newItem = parent.Add(newItemName, (BranchItem)nItemTemplate);
                    }
                    else
                    {
                        newItem = parent.Add(newItemName, (TemplateItem)nItemTemplate);
                    }
                }

                if (newItem == null)
                {
                    Log("Null Item", "the new item created was null)");
                    throw new NullReferenceException("the new item created was null");
                }
                using (new EditContext(newItem, true, false))
                {
                    //add in the field mappings
                    foreach (var d in FieldDefinitions)
                    {
                        var values = GetFieldValues(d.GetExistingFieldNames(), importRow);

                        d.FillField(this, ref newItem, String.Join(d.GetFieldValueDelimiter(), values));
                    }

                    //calls the subclass method to handle custom fields and properties
                    ProcessCustomData(ref newItem, importRow);

                    Log("INFO", string.Format("{0} Material {1}", newItem.DisplayName, updating ? "Updated" : "Added"));
                }
            }
        }
예제 #4
0
        public SitecoreDataMap(Database db, string connectionString, Item importItem) : base(db, connectionString, importItem)
        {
            Item fLang = SitecoreDB.GetItem(importItem.Fields["Import From Language"].Value);

            ImportFromLanguage = LanguageManager.GetLanguage(fLang.Name);

            CheckboxField cf = importItem.Fields["Recursively Fetch Children"];

            RecursivelyFetchChildren = cf.Checked;

            //deal with sitecore properties if any
            Item Props = GetItemByTemplate(importItem, PropertiesFolderID);

            if (Props.IsNotNull())
            {
                ChildList c = Props.GetChildren();
                if (c.Any())
                {
                    foreach (Item child in c)
                    {
                        //create an item to get the class / assembly name from
                        BaseMapping bm = new BaseMapping(child);
                        if (!string.IsNullOrEmpty(bm.HandlerAssembly))
                        {
                            if (!string.IsNullOrEmpty(bm.HandlerClass))
                            {
                                //create the object from the class and cast as base field to add it to field definitions
                                IBaseProperty bp = null;
                                try {
                                    bp = (IBaseProperty)Sitecore.Reflection.ReflectionUtil.CreateObject(bm.HandlerAssembly, bm.HandlerClass, new object[] { child });
                                } catch (FileNotFoundException fnfe) {
                                    Log("Error", string.Format("the property:{0} binary {1} specified could not be found", child.Name, bm.HandlerAssembly));
                                }
                                if (bp != null)
                                {
                                    PropertyDefinitions.Add(bp);
                                }
                                else
                                {
                                    Log("Error", string.Format("the property: '{0}' class type {1} could not be instantiated", child.Name, bm.HandlerClass));
                                }
                            }
                            else
                            {
                                Log("Error", string.Format("the property: '{0}' Handler Class {1} is not defined", child.Name, bm.HandlerClass));
                            }
                        }
                        else
                        {
                            Log("Error", string.Format("the property: '{0}' Handler Assembly {1} is not defined", child.Name, bm.HandlerAssembly));
                        }
                    }
                }
                else
                {
                    Log("Warn", "there are no properties to import");
                }
            }

            Item Temps = GetItemByTemplate(importItem, TemplatesFolderID);

            if (Temps.IsNotNull())
            {
                ChildList c = Temps.GetChildren();
                if (c.Any())
                {
                    foreach (Item child in c)
                    {
                        //create an item to get the class / assembly name from
                        TemplateMapping tm = new TemplateMapping(child);
                        if (string.IsNullOrEmpty(tm.FromWhatTemplate))
                        {
                            Log("Error", string.Format("the template mapping field 'FromWhatTemplate' on '{0}' is not defined", child.Name));
                            break;
                        }
                        if (string.IsNullOrEmpty(tm.ToWhatTemplate))
                        {
                            Log("Error", string.Format("the template mapping field 'ToWhatTemplate' on '{0}' is not defined", child.Name));
                            break;
                        }
                        TemplateMappingDefinitions.Add(tm.FromWhatTemplate, tm);
                    }
                }
            }
        }