private void AddCreateSitecoreItemSettings(ItemModel source, PipelineStep step)
        {
            //The default
            ResolveSitecoreItemSettings sitecoreItemSettings = new ResolveSitecoreItemSettings()
            {
                ParentItemIdItem           = this.GetGuidValue(source, "ParentForItem"),
                MatchingFieldValueAccessor = this.ConvertReferenceToModel <IValueAccessor>(source, "MatchingFieldValueAccessor"),
                TemplateForNewItem         = this.GetGuidValue(source, "TemplateForNewItem"),
                ItemNameValueAccessor      = this.ConvertReferenceToModel <IValueAccessor>(source, "ItemNameValueAccessor")
            };

            step.Plugins.Add((IPlugin)sitecoreItemSettings);

            //My endpoint and Language
            var settings     = new ResolveSitecoreItemWithLanguageSettings();
            var endpointFrom = base.ConvertReferenceToModel <Endpoint>(source, ResolveSitecoreItemWithLanguageStepItemModel.EndpointFrom);

            if (endpointFrom != null)
            {
                settings.EndpointFrom = endpointFrom;
            }

            settings.LanguageField =
                base.GetStringValue(source, ResolveSitecoreItemWithLanguageStepItemModel.LanguageField);

            step.Plugins.Add(settings);
        }
        protected virtual ItemModel DoSearch(object value, ResolveSitecoreItemSettings resolveItemSettings, IItemModelRepository repository, ILogger logger, PipelineContext pipelineContext)
        {
            SitecoreItemFieldReader valueReader = this.GetValueReader(resolveItemSettings.MatchingFieldValueAccessor) as SitecoreItemFieldReader;

            if (valueReader == null)
            {
                this.Log(new Action <string>(logger.Error), pipelineContext, "The matching field value accessor is not a valid Sitecore item field reader.");
                Sitecore.Diagnostics.Log.Error($"The matching field value accessor is not a valid Sitecore item field reader:", this);
                return((ItemModel)null);
            }
            string str = this.ConvertValueForSearch(value);

            this.Log(new Action <string>(logger.Debug), pipelineContext, "Value converted for search.", string.Format("field: {0}", (object)valueReader.FieldName), string.Format("original value: {0}", value), string.Format("converted value: {0}", (object)str));
            this.Log(new Action <string>(logger.Debug), pipelineContext, "Starting search for item.", string.Format("field: {0}", (object)valueReader.FieldName), string.Format("value: {0}", (object)str));
            IEnumerable <ItemModel> source = null;

            try
            {
                source = repository.Search(new ItemSearchSettings()
                {
                    SearchFilters =
                    {
                        new SearchFilter()
                        {
                            FieldName = valueReader.FieldName,
                            Value     = str
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Sitecore.Diagnostics.Log.Error($"Error occurred while searching in repository FieldName: {valueReader.FieldName} and value: {str}", ex);
                return((ItemModel)null);
            }
            if (source == null)
            {
                return((ItemModel)null);
            }

            //Fixing a bug that Search would return any item with same name, regardless of where it is.
            foreach (var item in source)
            {
                //quick check of Templates
                if (item.GetTemplateId().Equals(resolveItemSettings.TemplateForNewItem))
                {
                    //Could consider also check path is under root folder.
                    var db         = Sitecore.Data.Database.GetDatabase("master");
                    var scItem     = db.GetItem(new Sitecore.Data.ID(item.GetItemId()));
                    var parentItem = db.GetItem(new Sitecore.Data.ID(resolveItemSettings.ParentItemIdItem));
                    if (scItem.Paths.IsDescendantOf(parentItem))
                    {
                        return(item);
                    }
                }
            }

            return((ItemModel)null);
        }
        protected override ItemModel CreateItemModel(object identifierObject, IItemModelRepository repository, ResolveSitecoreItemSettings settings, PipelineContext pipelineContext, ILogger logger, string language = null, int version = 0)
        {
            var parentItemId = this.GetParentItemIdForNewItem(repository, settings, pipelineContext, logger);
            var itemModel    = base.CreateItemModel(identifierObject, repository, settings, pipelineContext, logger, language, version);

            if (itemModel != null)
            {
                itemModel[ItemModel.ParentID] = parentItemId;
            }

            return(itemModel);
        }
        protected override object ResolveObject(object identifierValue, Endpoint endpoint, PipelineStep pipelineStep, PipelineContext pipelineContext)
        {
            if (identifierValue == null)
            {
                throw new ArgumentException("The value cannot be null.", "identifierValue");
            }
            if (endpoint == null)
            {
                throw new ArgumentNullException("endpoint");
            }
            if (pipelineStep == null)
            {
                throw new ArgumentNullException("pipelineStep");
            }
            if (pipelineContext == null)
            {
                throw new ArgumentNullException("pipelineContext");
            }
            ItemModelRepositorySettings repositorySettings = endpoint.GetItemModelRepositorySettings();

            if (repositorySettings == null)
            {
                return((object)null);
            }
            IItemModelRepository itemModelRepository = repositorySettings.ItemModelRepository;

            if (itemModelRepository == null)
            {
                return((object)null);
            }
            ResolveSitecoreItemSettings sitecoreItemSettings = pipelineStep.GetResolveSitecoreItemSettings();

            if (sitecoreItemSettings == null)
            {
                return((object)null);
            }
            ResolveObjectSettings resolveObjectSettings = pipelineStep.GetResolveObjectSettings();

            if (resolveObjectSettings == null)
            {
                return((object)null);
            }

            ResolveSitecoreItemWithLanguageSettings languageSettings = pipelineStep.GetPlugin <ResolveSitecoreItemWithLanguageSettings>();

            if (languageSettings == null)
            {
                return((ItemModel)null);
            }



            ItemModel sourceAsItemModel = this.GetSourceObjectAsItemModel(pipelineStep, pipelineContext);

            if (sourceAsItemModel == null)
            {
                return((ItemModel)null);
            }

            var language = sourceAsItemModel[languageSettings.LanguageField].ToString();

            ILogger logger = pipelineContext.Logger;
            RepositoryObjectStatus status = RepositoryObjectStatus.DoesNotExist;
            ItemModel itemModel           = this.DoSearch(identifierValue, sitecoreItemSettings, itemModelRepository, logger, pipelineContext);

            if (itemModel != null)
            {
                this.Log(new Action <string>(pipelineContext.Logger.Debug), pipelineContext, "Item was resolved.", string.Format("identifier: {0}", identifierValue), string.Format("item id: {0}", itemModel["ItemID"]));
                Sitecore.Diagnostics.Log.Error($"Item was resolved with identifierValue: {identifierValue} item id: {itemModel["ItemID"]}", this);
                status = RepositoryObjectStatus.Exists;
            }

            if (itemModel == null && !resolveObjectSettings.DoNotCreateIfObjectNotResolved)
            {
                this.Log(new Action <string>(logger.Debug), pipelineContext, "Item was not resolved. Will create it.", string.Format("identifier: {0}", identifierValue));
                Sitecore.Diagnostics.Log.Error($"Item was not resolved. Will create it: {identifierValue} ", this);

                itemModel = this.CreateNewItem(this.GetIdentifierObject(pipelineStep, pipelineContext), itemModelRepository, sitecoreItemSettings, logger, pipelineContext, language);
                if (itemModel == null)
                {
                    this.Log(new Action <string>(logger.Error), pipelineContext, "Unable to create new item.", string.Format("identifier: {0}", identifierValue));
                    Sitecore.Diagnostics.Log.Error($"Unable to create new item with identifierValue: {identifierValue} ", this);
                }
                else
                {
                    this.Log(new Action <string>(logger.Debug), pipelineContext, "New item was created.", string.Format("identifier: {0}", identifierValue));
                    Sitecore.Diagnostics.Log.Info($"Create new item with identifierValue: {identifierValue} ", this);
                }
            }
            this.SetRepositoryStatusSettings(status, pipelineContext);
            return((object)itemModel);
        }
        private ItemModel CreateNewItem(object identifierObject, IItemModelRepository repository, ResolveSitecoreItemSettings settings, ILogger logger, PipelineContext pipelineContext, string language)
        {
            IValueReader valueReader = this.GetValueReader(settings.ItemNameValueAccessor);

            if (valueReader == null)
            {
                return((ItemModel)null);
            }
            DataAccessContext context       = new DataAccessContext();
            string            validItemName = this.ConvertValueToValidItemName(this.ReadValue(identifierObject, valueReader, context), logger, pipelineContext);

            if (validItemName == null)
            {
                return((ItemModel)null);
            }


            Guid id = repository.Create(validItemName, settings.TemplateForNewItem, settings.ParentItemIdItem, language);

            return(repository.Get(id, (string)null, 0));
        }
コード例 #6
0
        public override void Process(PipelineStep pipelineStep, PipelineContext pipelineContext)
        {
            if (pipelineStep == null)
            {
                throw new ArgumentNullException(nameof(pipelineStep));
            }
            if (pipelineContext == null)
            {
                throw new ArgumentNullException(nameof(pipelineContext));
            }

            var xml = pipelineContext.GetPlugin <SynchronizationSettings>();

            XmlNode xmlData = (XmlNode)xml.Source;

            if (null == xmlData)
            {
                return;
            }
            var childnodeSettings = pipelineStep.GetPlugin <ProcessChildSettings>();

            if (null == childnodeSettings)
            {
                return;
            }

            var           xmlNodes = xmlData.SelectNodes(childnodeSettings.Xpath);
            string        itemName = xmlData.Attributes["itemName"].Value;
            List <string> lstLang  = null;

            string parentPath = string.Empty;

            foreach (var step in pipelineContext.CurrentPipeline.PipelineSteps)
            {
                if (step.PipelineStepProcessor.GetType().UnderlyingSystemType.FullName == "Sitecore.DataExchange.Providers.Sc.Processors.PipelineSteps.ResolveMultilanguageSitecoreItemDictionaryPipelineStep")
                {
                    foreach (var plugin in step.Plugins)
                    {
                        if (plugin.GetType().Name == "ResolveSitecoreItemSettings")
                        {
                            ResolveSitecoreItemSettings settings = (ResolveSitecoreItemSettings)plugin;

                            Item parentItem = dbContext.GetItem(new ID(settings.ParentItemIdItem.ToString()));
                            parentPath = parentItem.Paths.FullPath;
                        }
                    }
                }
                else if (step.PipelineStepProcessor.GetType().UnderlyingSystemType.FullName == "Sitecore.DataExchange.Providers.Sc.Processors.PipelineSteps.SelectLanguagesPipelineStep")
                {
                    foreach (var plugin in step.Plugins)
                    {
                        if (plugin.GetType().Name == "SelectedLanguagesSettings")
                        {
                            lstLang = ((SelectedLanguagesSettings)plugin).Languages.ToList();
                        }
                    }
                }
            }

            if ((!string.IsNullOrEmpty(parentPath)) && lstLang != null)
            {
                foreach (string lang in lstLang)
                {
                    Item item = dbContext.GetItem(string.Format("{0}/{1}", parentPath, itemName), Sitecore.Globalization.Language.Parse(lang));

                    if (item != null)
                    {
                        if (item.Name == item.Fields["Identifier"].Value.ToString())
                        {
                            ChildItemCreatorML obj = new ChildItemCreatorML(xmlData.OuterXml, item.ID.ToString(), lang);
                            obj.CreateChildItems();

                            ItemRendering.AddRenderings(item);
                        }

                        if (item.Name != item.Fields["Identifier"].Value.ToString())
                        {
                            item.Versions.RemoveVersion();
                        }
                    }
                }
            }
        }