Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Common.CustomSitecore.DataExchange.PipelineSteps
- {
- public class ResolveSitecoreItemStepProcessor : Sitecore.DataExchange.Providers.Sc.Processors.PipelineSteps.ResolveSitecoreItemStepProcessor
- {
- public ResolveSitecoreItemStepProcessor()
- {
- }
- //called in base CreateNewItem
- private string ConvertValueToValidItemName(object value, ILogger logger, PipelineContext pipelineContext)
- {
- if (value == null)
- {
- return null;
- }
- string str = value.ToString();
- SitecoreItemUtilities plugin = Context.GetPlugin<SitecoreItemUtilities>();
- if (plugin == null)
- {
- ILogger logger1 = logger;
- this.Log(new Action<string>(logger1.Error), pipelineContext, "No plugin is specified on the context to determine whether or not the specified value is a valid item name. The original value will be used.", new string[] { string.Format("missing plugin: {0}", typeof(SitecoreItemUtilities).FullName) });
- return str;
- }
- if (plugin.IsItemNameValid == null)
- {
- ILogger logger2 = logger;
- this.Log(new Action<string>(logger2.Error), pipelineContext, "No delegate is specified on the plugin that can determine whether or not the specified value is a valid item name. The original value will be used.", new string[] { string.Format("plugin: {0}", typeof(SitecoreItemUtilities).FullName), string.Format("delegate: {0}", "IsItemNameValid"), string.Format("original value: {0}", str) });
- return str;
- }
- if (plugin.IsItemNameValid(str))
- {
- return str;
- }
- if (plugin.ProposeValidItemName != null)
- {
- return plugin.ProposeValidItemName(str);
- }
- logger.Error("No delegate is specified on the plugin that can propose a valid item name. The original value will be used. (plugin: {0}, delegate: {1}, original value: {2})", new object[] { typeof(SitecoreItemUtilities).FullName, "ProposeValidItemName", str });
- return str;
- }
- private ItemModel CreateNewItem(object identifierObject, IItemModelRepository repository, ResolveSitecoreItemSettings settings, ILogger logger, PipelineContext pipelineContext)
- {
- IValueReader valueReader = this.GetValueReader(settings.ItemNameValueAccessor);
- if (valueReader == null)
- {
- return null;
- }
- object obj = this.ReadValue(identifierObject, valueReader, new DataAccessContext());
- string validItemName = this.ConvertValueToValidItemName(obj, logger, pipelineContext);
- if (validItemName == null)
- {
- return null;
- }
- #region Custom Code
- //add the first language supplied by the context of the pipeline if it exists
- var languageSetting = pipelineContext.GetPlugin<SelectedLanguagesSettings>();
- var selectedLanguage = languageSetting?.Languages.FirstOrDefault();
- #endregion
- if (!settings.DoNotCreateItemIfDoesNotExist)
- {
- #region Custom Code
- //replaced null with selectedLanguage
- Guid guid = repository.Create(validItemName, settings.TemplateForNewItem, settings.ParentItemIdItem, selectedLanguage);
- #endregion
- return repository.Get(guid, null, 0);
- }
- ItemModel itemModel = new ItemModel();
- itemModel.Add(ItemModel.ItemName, validItemName);
- itemModel.Add(ItemModel.TemplateID, settings.TemplateForNewItem);
- itemModel.Add(ItemModel.ParentID, settings.ParentItemIdItem);
- #region Custom Code
- if (selectedLanguage != null)
- {
- itemModel.Add(ItemModel.ItemLanguage, selectedLanguage);
- }
- #endregion
- return itemModel;
- }
- //called in base CreateNewItem
- private IValueReader GetValueReader(IValueAccessor config)
- {
- if (config == null)
- {
- return null;
- }
- return config.ValueReader;
- }
- //called in base CreateNewItem
- private object ReadValue(object source, IValueReader reader, DataAccessContext context)
- {
- if (reader == null)
- {
- return null;
- }
- if (!reader.CanRead(source, context).CanReadValue)
- {
- return null;
- }
- ReadResult readResult = reader.Read(source, context);
- if (!readResult.WasValueRead)
- {
- return null;
- }
- return readResult.ReadValue;
- }
- //completely pulled in from the base class in order to use the private CreateNewItem method
- 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 itemModelRepositorySettings = endpoint.GetItemModelRepositorySettings();
- if (itemModelRepositorySettings == null)
- {
- return null;
- }
- IItemModelRepository itemModelRepository = itemModelRepositorySettings.ItemModelRepository;
- if (itemModelRepository == null)
- {
- return null;
- }
- ResolveSitecoreItemSettings resolveSitecoreItemSettings = pipelineStep.GetResolveSitecoreItemSettings();
- if (resolveSitecoreItemSettings == null)
- {
- return null;
- }
- ResolveObjectSettings resolveObjectSettings = pipelineStep.GetResolveObjectSettings();
- if (resolveObjectSettings == null)
- {
- return null;
- }
- ILogger logger = pipelineContext.Logger;
- RepositoryObjectStatus repositoryObjectStatu = RepositoryObjectStatus.DoesNotExist;
- ItemModel itemModel = this.DoSearch(identifierValue, resolveSitecoreItemSettings, itemModelRepository, logger, pipelineContext);
- if (itemModel != null)
- {
- ILogger logger1 = pipelineContext.Logger;
- this.Log(new Action<string>(logger1.Debug), pipelineContext, "Item was resolved.", new string[] { string.Format("identifier: {0}", identifierValue), string.Format("item id: {0}", itemModel["ItemID"]) });
- repositoryObjectStatu = RepositoryObjectStatus.Exists;
- }
- if (itemModel == null && !resolveObjectSettings.DoNotCreateIfObjectNotResolved)
- {
- ILogger logger2 = logger;
- this.Log(new Action<string>(logger2.Debug), pipelineContext, "Item was not resolved. Will create it.", new string[] { string.Format("identifier: {0}", identifierValue) });
- object identifierObject = base.GetIdentifierObject(pipelineStep, pipelineContext);
- itemModel = this.CreateNewItem(identifierObject, itemModelRepository, resolveSitecoreItemSettings, logger, pipelineContext);
- if (itemModel != null)
- {
- ILogger logger3 = logger;
- this.Log(new Action<string>(logger3.Debug), pipelineContext, "New item was created.", new string[] { string.Format("identifier: {0}", identifierValue) });
- }
- else
- {
- ILogger logger4 = logger;
- this.Log(new Action<string>(logger4.Error), pipelineContext, "Unable to create new item.", new string[] { string.Format("identifier: {0}", identifierValue) });
- }
- }
- this.SetRepositoryStatusSettings(repositoryObjectStatu, pipelineContext);
- return itemModel;
- }
- }
- }
Add Comment
Please, Sign In to add comment