Guest User

Untitled

a guest
Mar 20th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. namespace Common.CustomSitecore.DataExchange.PipelineSteps
  2. {
  3. public class ResolveSitecoreItemStepProcessor : Sitecore.DataExchange.Providers.Sc.Processors.PipelineSteps.ResolveSitecoreItemStepProcessor
  4. {
  5. public ResolveSitecoreItemStepProcessor()
  6. {
  7. }
  8.  
  9. //called in base CreateNewItem
  10. private string ConvertValueToValidItemName(object value, ILogger logger, PipelineContext pipelineContext)
  11. {
  12. if (value == null)
  13. {
  14. return null;
  15. }
  16. string str = value.ToString();
  17. SitecoreItemUtilities plugin = Context.GetPlugin<SitecoreItemUtilities>();
  18. if (plugin == null)
  19. {
  20. ILogger logger1 = logger;
  21. 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) });
  22. return str;
  23. }
  24. if (plugin.IsItemNameValid == null)
  25. {
  26. ILogger logger2 = logger;
  27. 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) });
  28. return str;
  29. }
  30. if (plugin.IsItemNameValid(str))
  31. {
  32. return str;
  33. }
  34. if (plugin.ProposeValidItemName != null)
  35. {
  36. return plugin.ProposeValidItemName(str);
  37. }
  38. 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 });
  39. return str;
  40. }
  41.  
  42. private ItemModel CreateNewItem(object identifierObject, IItemModelRepository repository, ResolveSitecoreItemSettings settings, ILogger logger, PipelineContext pipelineContext)
  43. {
  44. IValueReader valueReader = this.GetValueReader(settings.ItemNameValueAccessor);
  45. if (valueReader == null)
  46. {
  47. return null;
  48. }
  49. object obj = this.ReadValue(identifierObject, valueReader, new DataAccessContext());
  50. string validItemName = this.ConvertValueToValidItemName(obj, logger, pipelineContext);
  51. if (validItemName == null)
  52. {
  53. return null;
  54. }
  55.  
  56. #region Custom Code
  57. //add the first language supplied by the context of the pipeline if it exists
  58. var languageSetting = pipelineContext.GetPlugin<SelectedLanguagesSettings>();
  59. var selectedLanguage = languageSetting?.Languages.FirstOrDefault();
  60. #endregion
  61.  
  62. if (!settings.DoNotCreateItemIfDoesNotExist)
  63. {
  64. #region Custom Code
  65. //replaced null with selectedLanguage
  66. Guid guid = repository.Create(validItemName, settings.TemplateForNewItem, settings.ParentItemIdItem, selectedLanguage);
  67. #endregion
  68.  
  69. return repository.Get(guid, null, 0);
  70. }
  71.  
  72. ItemModel itemModel = new ItemModel();
  73. itemModel.Add(ItemModel.ItemName, validItemName);
  74. itemModel.Add(ItemModel.TemplateID, settings.TemplateForNewItem);
  75. itemModel.Add(ItemModel.ParentID, settings.ParentItemIdItem);
  76.  
  77. #region Custom Code
  78. if (selectedLanguage != null)
  79. {
  80. itemModel.Add(ItemModel.ItemLanguage, selectedLanguage);
  81. }
  82. #endregion
  83.  
  84. return itemModel;
  85. }
  86.  
  87. //called in base CreateNewItem
  88. private IValueReader GetValueReader(IValueAccessor config)
  89. {
  90. if (config == null)
  91. {
  92. return null;
  93. }
  94. return config.ValueReader;
  95. }
  96.  
  97. //called in base CreateNewItem
  98. private object ReadValue(object source, IValueReader reader, DataAccessContext context)
  99. {
  100. if (reader == null)
  101. {
  102. return null;
  103. }
  104. if (!reader.CanRead(source, context).CanReadValue)
  105. {
  106. return null;
  107. }
  108. ReadResult readResult = reader.Read(source, context);
  109. if (!readResult.WasValueRead)
  110. {
  111. return null;
  112. }
  113. return readResult.ReadValue;
  114. }
  115.  
  116. //completely pulled in from the base class in order to use the private CreateNewItem method
  117. protected override object ResolveObject(object identifierValue, Endpoint endpoint, PipelineStep pipelineStep, PipelineContext pipelineContext)
  118. {
  119. if (identifierValue == null)
  120. {
  121. throw new ArgumentException("The value cannot be null.", "identifierValue");
  122. }
  123. if (endpoint == null)
  124. {
  125. throw new ArgumentNullException("endpoint");
  126. }
  127. if (pipelineStep == null)
  128. {
  129. throw new ArgumentNullException("pipelineStep");
  130. }
  131. if (pipelineContext == null)
  132. {
  133. throw new ArgumentNullException("pipelineContext");
  134. }
  135. ItemModelRepositorySettings itemModelRepositorySettings = endpoint.GetItemModelRepositorySettings();
  136. if (itemModelRepositorySettings == null)
  137. {
  138. return null;
  139. }
  140. IItemModelRepository itemModelRepository = itemModelRepositorySettings.ItemModelRepository;
  141. if (itemModelRepository == null)
  142. {
  143. return null;
  144. }
  145. ResolveSitecoreItemSettings resolveSitecoreItemSettings = pipelineStep.GetResolveSitecoreItemSettings();
  146. if (resolveSitecoreItemSettings == null)
  147. {
  148. return null;
  149. }
  150. ResolveObjectSettings resolveObjectSettings = pipelineStep.GetResolveObjectSettings();
  151. if (resolveObjectSettings == null)
  152. {
  153. return null;
  154. }
  155. ILogger logger = pipelineContext.Logger;
  156. RepositoryObjectStatus repositoryObjectStatu = RepositoryObjectStatus.DoesNotExist;
  157. ItemModel itemModel = this.DoSearch(identifierValue, resolveSitecoreItemSettings, itemModelRepository, logger, pipelineContext);
  158. if (itemModel != null)
  159. {
  160. ILogger logger1 = pipelineContext.Logger;
  161. 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"]) });
  162. repositoryObjectStatu = RepositoryObjectStatus.Exists;
  163. }
  164. if (itemModel == null && !resolveObjectSettings.DoNotCreateIfObjectNotResolved)
  165. {
  166. ILogger logger2 = logger;
  167. this.Log(new Action<string>(logger2.Debug), pipelineContext, "Item was not resolved. Will create it.", new string[] { string.Format("identifier: {0}", identifierValue) });
  168. object identifierObject = base.GetIdentifierObject(pipelineStep, pipelineContext);
  169. itemModel = this.CreateNewItem(identifierObject, itemModelRepository, resolveSitecoreItemSettings, logger, pipelineContext);
  170. if (itemModel != null)
  171. {
  172. ILogger logger3 = logger;
  173. this.Log(new Action<string>(logger3.Debug), pipelineContext, "New item was created.", new string[] { string.Format("identifier: {0}", identifierValue) });
  174. }
  175. else
  176. {
  177. ILogger logger4 = logger;
  178. this.Log(new Action<string>(logger4.Error), pipelineContext, "Unable to create new item.", new string[] { string.Format("identifier: {0}", identifierValue) });
  179. }
  180. }
  181. this.SetRepositoryStatusSettings(repositoryObjectStatu, pipelineContext);
  182. return itemModel;
  183. }
  184. }
  185. }
Add Comment
Please, Sign In to add comment