public Language GetImportItemLanguage(string fieldName)
        {
            Language l = LanguageManager.DefaultLanguage;

            //check the field
            string langID = ImportItem.GetItemField(fieldName, Logger);

            if (string.IsNullOrEmpty(langID))
            {
                Logger.Log(ImportItem.Paths.FullPath, "The 'Import Language' field is not set on the import item", ProcessStatus.ImportDefinitionError, fieldName);
                return(l);
            }

            //check item
            Item iLang = ToDB.GetItem(langID);

            if (iLang.IsNull())
            {
                Logger.Log(ImportItem.Paths.FullPath, "The 'Import Language' Item is null on the import item", ProcessStatus.ImportDefinitionError, fieldName);
                return(l);
            }

            //check language
            l = LanguageManager.GetLanguage(iLang.Name);
            if (l == null)
            {
                Logger.Log(ImportItem.Paths.FullPath, "The 'Import Language' name is not valid on the import item", ProcessStatus.ImportDefinitionError, fieldName);
            }

            return(l);
        }
        public override Item GetParentNode(object importRow, string newItemName)
        {
            Item    parentItem      = null;
            DataRow dataRow         = importRow as DataRow;
            string  parentPath      = dataRow[PathColumn].ToString();
            int     lastIndexOfPath = parentPath.LastIndexOf('/');

            try
            {
                parentPath = parentPath.Remove(lastIndexOfPath);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString(), parentPath, ProcessStatus.NewItemError, PathColumn, parentPath);
            }


            if (Config.MaintainHierarchy)
            {
                parentItem = ToDB.GetItem(parentPath);
            }

            if (parentItem == null)
            {
                parentItem = base.GetParentNode(importRow, newItemName);
            }

            return(parentItem);
        }
Exemple #3
0
        /// <summary>
        /// uses the sitecore database and xpath query to retrieve data
        /// </summary>
        /// <returns></returns>
        public override IEnumerable <object> GetImportData()
        {
            var items = new List <object>();

            foreach (var query in Query.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
            {
                IEnumerable <Item> queriedItems = FromDB.SelectItems(query);

                if (SkipIfExists)
                {
                    queriedItems = queriedItems.Where(i => ToDB.GetItem(i.ID) == null);
                }

                items.AddRange(queriedItems);

                foreach (var i in queriedItems)
                {
                    items.Add(i);
                    if (!RecursivelyFetchChildren)
                    {
                        continue;
                    }

                    items.AddRange(i.Axes.GetDescendants());
                }
            }

            return(items);
        }
        public virtual Item CreateNewItem(Item parent, object importRow, string newItemName)
        {
            CustomItemBase nItemTemplate = GetNewItemTemplate(importRow);

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

                Item newItem;
                //search for the child by name
                newItem = GetChild(parent, 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
                    List <IBaseField> fieldDefs = GetFieldDefinitionsByRow(importRow);
                    foreach (IBaseField d in fieldDefs)
                    {
                        string importValue = string.Empty;
                        try {
                            IEnumerable <string> values = GetFieldValues(d.GetExistingFieldNames(), importRow);
                            importValue = String.Join(d.GetFieldValueDelimiter(), values);
                            d.FillField(this, ref newItem, importValue);
                        } catch (Exception ex) {
                            Logger.Log(newItem.Paths.FullPath, "the FillField failed", ProcessStatus.FieldError, d.ItemName(), importValue);
                        }
                    }

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

                return(newItem);
            }
        }
Exemple #5
0
        protected virtual Item GetPathParentNode(string path)
        {
            var item = ToDB.GetItem(path);

            if (item == null)
            {
                var parentPath = path.Substring(0, path.LastIndexOf("/"));
                var itemName   = path.Substring(path.LastIndexOf("/") + 1);
                var parent     = GetPathParentNode(parentPath);
                item = parent.Add(itemName, new TemplateID(ImportToWhatTemplate.ID));
            }
            return(item);
        }
        public override CustomItemBase GetNewItemTemplate(object importRow)
        {
            DataRow dataRow    = importRow as DataRow;
            string  templateID = dataRow[ActionColumn].ToString();

            if (string.IsNullOrEmpty(templateID))
            {
                return(ImportToWhatTemplate);
            }

            BranchItem templateItem = ToDB.GetItem(templateID);

            return(templateItem);
        }
        protected string GetContentString(string contentPath)
        {
            var fileItem = (MediaItem)ToDB.GetItem(Query);

            if (fileItem == null)
            {
                return(string.Empty);
            }

            using (var reader = new StreamReader(MediaManager.GetMedia(fileItem).GetStream().Stream))
            {
                string text = reader.ReadToEnd();

                return(text);
            }
        }
Exemple #8
0
        protected Item GetPathParentNode(string path, Item sourceItem)
        {
            if (!path.Contains(ImportRoot.Paths.Path))
            {
                Logger.Log("SitecoreDataMap.GetPathParentNode", string.Format("Imported Item {0} is not under the Import Root, moving to top level", path));
                return(ImportToWhere);
            }

            var item = ToDB.GetItem(path);

            if (item == null)
            {
                var parentPath = path.Substring(0, path.LastIndexOf("/"));
                var itemName   = path.Substring(path.LastIndexOf("/") + 1);
                var parent     = GetPathParentNode(parentPath, sourceItem.Parent);

                // If the template mapping for this parent item is different, change the template
                item = parent.Add(itemName, new TemplateID(GetNewItemTemplate(sourceItem).ID));
            }
            return(item);
        }
        public virtual Item CreateNewItem(Item parent, object importRow, string newItemName)
        {
            CustomItemBase nItemTemplate = GetNewItemTemplate(importRow);
            Item           newItem;

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

                //search for the child by name
                newItem = GetChild(parent, newItemName);
                if (newItem?.Versions.Count == 0)                 //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");
                }
            }

            return(newItem);
        }
        private void RunProcessor(IBaseField fieldMap, Item newItem)
        {
            //Check for warnings
            MultilistField WarningTags = fieldMap.InnerItem.Fields[DataImporter.HtmlScraper.Constants.FieldNames.WarningTriggerTags];

            if (WarningTags.Count > 0)
            {
                WriteTagWarnings.Run(newItem, fieldMap.InnerItem);
            }

            //"To What Field"
            MultilistField processors = fieldMap.InnerItem.Fields[DataImporter.HtmlScraper.Constants.FieldNames.FieldPostProcessors];

            foreach (var targetId in processors.TargetIDs)
            {
                Item processor = ToDB.GetItem(targetId);
                if (processor == null)
                {
                    continue;
                }

                Processor.Execute(processor, newItem, fieldMap.InnerItem);
            }
        }
Exemple #11
0
        private void ProcessComponents(Item parent, object importRow, IEnumerable <ComponentMapping> componentMappings)
        {
            using (new LanguageSwitcher(ImportToLanguage))
            {
                //get the parent in the specific language
                foreach (var componentMapping in componentMappings ?? Enumerable.Empty <ComponentMapping>())
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(componentMapping.Rendering) && !string.IsNullOrEmpty(componentMapping.Placeholder))
                        {
                            CleanRenderings(parent, componentMapping.Rendering, componentMapping.Placeholder);
                        }
                        var items = componentMapping.GetImportItems((Item)importRow);
                        foreach (var item in items)
                        {
                            if ((componentMapping.RequiredFields.Any()))
                            {
                                var requiredValues = GetFieldValues(componentMapping.RequiredFields, item);
                                if (requiredValues.Any(x => string.IsNullOrEmpty(x)))
                                {
                                    Logger.Log("SitecoreDataMap.ProcessComponents", string.Format("Missing required field for component {0} on item {1}", componentMapping.ComponentName, ((Item)item).ID));
                                    continue;
                                }
                            }
                            Item folder = parent;
                            if (!string.IsNullOrEmpty(componentMapping.FolderName))
                            {
                                var folderPath = parent.Paths.FullPath + "/" + componentMapping.FolderName;
                                folder = ToDB.GetItem(folderPath);
                                if (folder == null)
                                {
                                    Logger.Log("SitecoreDataMap.ProcessComponents", string.Format("Could not find component Folder at {0}", folderPath));
                                    folder = parent;
                                }
                            }


                            var  nItemTemplate = GetComponentItemTemplate(item, componentMapping);
                            Item newItem;
                            //search for the child by name
                            var name = componentMapping.ComponentName;
                            switch (name)
                            {
                            case "$name":
                            case "@@name":
                                name = item.Name;
                                break;

                            case "$outcomePostQuestionName":
                                var questionName = item.Name.Replace('P', 'B');
                                name = GetChild(folder, questionName) != null ? questionName: item.Name;
                                break;
                            }
                            newItem = GetChild(folder, name);
                            if (newItem != null)                             //add version for lang
                            {
                                if (componentMapping.PreserveComponentId && newItem.ID != item.ID)
                                {
                                    UpdateReferences(newItem, item.ID);
                                    newItem.Delete();
                                    newItem = null;
                                }
                                else
                                {
                                    newItem = newItem.Versions.AddVersion();
                                }
                            }
                            //if not found then create one
                            if (newItem == null)
                            {
                                if (componentMapping.PreserveComponentId)
                                {
                                    newItem = ItemManager.AddFromTemplate(name, nItemTemplate.ID, folder, item.ID);
                                }
                                else
                                {
                                    newItem = ItemManager.AddFromTemplate(name, nItemTemplate.ID, folder);
                                }
                            }

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

                            using (new EditContext(newItem, true, false))
                            {
                                ProcessFields(item, newItem, componentMapping);
                            }

                            using (new EditContext(newItem, true, false))
                            {
                                ProcessReferenceFields(ref newItem, item, componentMapping.ReferenceFieldDefinitions);
                            }
                            using (new EditContext(newItem, true, false))
                            {
                                ProcessComponents(newItem, item, componentMapping);
                            }
                            using (new EditContext(newItem, true, false))
                            {
                                //calls the subclass method to handle custom fields and properties
                                ProcessCustomData(ref newItem, item, componentMapping);
                            }
                            if (!string.IsNullOrEmpty(componentMapping.Rendering) && !string.IsNullOrEmpty(componentMapping.Placeholder))
                            {
                                AddRendering(parent, componentMapping.Rendering, componentMapping.Placeholder, newItem);
                            }

                            Logger.Log("SitecoreDataMap.CreateNewItem", $"Import ID:{item.ID.Guid}, Import Path:{item.Paths.FullPath}, New ID:{newItem.ID.Guid}, New Path:{newItem.Paths.FullPath}");
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Log("SitecoreDataMap.ProcessComponents", string.Format("failed to import component {0} on item {1}", componentMapping.ComponentName, parent.Paths.FullPath));
                    }
                }
            }
        }
Exemple #12
0
        public override Item CreateNewItem(Item parent, object importRow, string newItemName)
        {
            CustomItemBase nItemTemplate = GetNewItemTemplate(importRow);

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

                Item newItem;
                //search for the child by name
                if (AllowItemNameMatch)
                {
                    newItem = GetChild(parent, newItemName);
                }
                else
                {
                    newItem = ToDB.GetItem(((Item)importRow).ID);
                }
                if (newItem != null)                 //add version for lang
                {
                    if (DeleteOnOverwrite)
                    {
                        newItem = HandleDelete(newItem, newItemName, nItemTemplate, parent, importRow);
                    }
                    else
                    {
                        if (newItem.ParentID != parent.ID)
                        {
                            newItem.MoveTo(parent);
                        }
                        newItem = newItem.Versions.AddVersion();
                    }
                }

                //if not found then create one
                if (newItem == null)
                {
                    newItem = ItemManager.AddFromTemplate(newItemName, nItemTemplate.ID, parent, GetItemID(((Item)importRow)));
                }

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

                //if found and is default template, change it to the correct one
                if (newItem.TemplateID == ImportToWhatTemplate.ID && nItemTemplate.ID != ImportToWhatTemplate.ID)
                {
                    using (new EditContext(newItem, true, false))
                    {
                        newItem.ChangeTemplate(new TemplateItem(nItemTemplate.InnerItem));
                    }
                }

                using (new EditContext(newItem, true, false))
                {
                    ProcessFields(importRow, newItem);
                }
                using (new EditContext(newItem, true, false))
                {
                    ProcessReferenceFields(ref newItem, importRow);
                }
                using (new EditContext(newItem, true, false))
                {
                    ProcessComponents(newItem, importRow);
                }
                using (new EditContext(newItem, true, false))
                {
                    //calls the subclass method to handle custom fields and properties
                    ProcessCustomData(ref newItem, importRow);
                }

                Logger.Log("SitecoreDataMap.CreateNewItem", $"Import ID:{((Item) importRow).ID.Guid}, Import Path:{((Item)importRow).Paths.FullPath}, New ID:{newItem.ID.Guid}, New Path:{newItem.Paths.FullPath}");

                return(newItem);
            }
        }
Exemple #13
0
        public override Item CreateNewItem(Item parent, object importRow, string newItemName)
        {
            var  oldItem = (Item)importRow;
            Item newItem = null;

            foreach (var language in oldItem.Languages)
            {
                ImportToLanguage   = AllLanguages.FirstOrDefault(l => l.Name.StartsWith(language.Name, StringComparison.InvariantCultureIgnoreCase));
                ImportFromLanguage = language;

                var oldLangItem = GetLastPublishableVersion(oldItem, ImportFromLanguage);
                if (oldLangItem.Versions.Count <= 0)
                {
                    continue;
                }

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

                    newItem = ToDB.GetItem(oldLangItem.ID);
                    if (newItem == null || SkipIfUpdatedAfter == DateTime.MinValue || newItem.Statistics.Updated < SkipIfUpdatedAfter)
                    {
                        if (newItem != null)                         //add version for lang
                        {
                            if (DeleteOnOverwrite || newItem.Version.Number > 2)
                            {
                                newItem.Versions.RemoveAll(false);
                            }
                            if (newItem.Versions.Count == 0)
                            {
                                newItem = newItem.Versions.AddVersion();
                            }
                            else if (!oldLangItem.Versions.IsLatestVersion())
                            {
                                newItem = newItem.Versions.GetVersions().FirstOrDefault();
                            }
                        }

                        //if not found then create one
                        if (newItem == null)
                        {
                            var nItemTemplate = GetNewItemTemplate(importRow);
                            newItem = ItemManager.AddFromTemplate(newItemName, nItemTemplate.ID, parent, oldItem.ID);
                            ProcessComponents(newItem, oldItem);
                        }

                        if (newItem == null)
                        {
                            throw new NullReferenceException("the new item created was null");
                        }
                    }
                    else if (SkipIfUpdatedAfter > DateTime.MinValue && newItem.Statistics.Updated > SkipIfUpdatedAfter)
                    {
                        Logger.Log("Item has been updated since last migration, skipping...", newItem.Paths.FullPath, LogType.Info, $"Language: {newItem.Language.Name}", $"Version: {newItem.Version.Number}");
                    }

                    // Now for draft versions
                    if (!oldLangItem.Versions.IsLatestVersion())
                    {
                        oldLangItem = oldLangItem.Versions.GetLatestVersion(oldLangItem.Language);

                        if (newItem.Versions.Count == 1)
                        {
                            newItem = newItem.Versions.AddVersion();
                        }
                        else
                        {
                            newItem = newItem.Versions.GetLatestVersion(newItem.Language);

                            if (SkipIfUpdatedAfter > DateTime.MinValue && newItem.Statistics.Updated > SkipIfUpdatedAfter)
                            {
                                Logger.Log("Item has been updated since last migration, skipping...", newItem.Paths.FullPath, LogType.Info, $"Language: {newItem.Language.Name}", $"Version: {newItem.Version.Number}");
                                continue;
                            }
                        }
                    }
                }
            }

            return(newItem);
        }
Exemple #14
0
        public Item CreateNewItem(Item parent, object importRow, string newItemName)
        {
            CustomItemBase nItemTemplate = GetNewItemTemplate(importRow);

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

                Item newItem;
                //search for the child by name
                newItem = GetChild(parent, newItemName);

                var dict = new Dictionary <string, bool>();
                // Check if item exists, flag article number field as not to update
                if (newItem != null)
                {
                    if (newItem.Fields["Legacy Sitecore ID"]?.Value == (importRow as Item)?.ID.ToString())
                    {
                        dict.Add("Article Number", false);
                    }
                }

                //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
                    List <IBaseField> fieldDefs = GetFieldDefinitionsByRow(importRow);
                    SetFieldUpdateFlags(fieldDefs, dict);
                    fieldDefs = fieldDefs.Where(i => i.DoUpdate).ToList();

                    foreach (IBaseField d in fieldDefs)
                    {
                        string importValue = string.Empty;
                        try
                        {
                            IEnumerable <string> values = GetFieldValues(d.GetExistingFieldNames(), importRow);
                            importValue = String.Join(d.GetFieldValueDelimiter(), values);
                            string id = string.Empty;
                            if (importRow is Item)
                            {
                                id = (importRow as Item).ID.ToString();
                            }
                            d.FillField(this, ref newItem, importValue, id);
                        }
                        catch (Exception ex)
                        {
                            Logger.Log(newItem.Paths.FullPath, "the FillField failed", ProcessStatus.FieldError, d.ItemName(), importValue);
                        }
                    }

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