2

Currently converting DEF from 1.4 to 2.0.1 and also from Sitecore 8.2 to Sitecore 9. I imported new code and templates from old site. I noticed some minor code changes between the different versions of DEF. I made those changes. No validation errors and no log errors. However when I run the pipeline batch process the code gets as far as the ValueAccessorConverter. It does not eventually continue on to the ReadResult part. I noticed a lot of template changes in Sitecore for the newest DEF have changed. Is there something I need to do differently in order for ReadResult to execute?

 public class RedditFeedValueReader: IValueReader
{
    public readonly string FieldName;

    public bool UseValueProperty { get; set; }

    public RedditFeedValueReader(string fieldName)
    {
        this.FieldName = fieldName;

    }

    public ReadResult CanRead(object source, DataAccessContext context)
    {
        bool flag = source != null && source is RedditSharp.Things.Post;
        return new ReadResult(DateTime.Now)
        {
            ReadValue = source,
            WasValueRead = flag,

        };
    }

    public ReadResult Read(object source, DataAccessContext context)
    {
        var flag = false;
        object readValue = (object) null;
        var feeditem = source as RedditSharp.Things.Post;
        if (feeditem != null)
        {
            if (FieldName == "Title" && !string.IsNullOrEmpty(feeditem.Title))
            {
                readValue = feeditem.Title;
                flag = true;
            }
            else if (FieldName == "AuthorName" && !string.IsNullOrEmpty(feeditem.AuthorName))
            {
                readValue = feeditem.AuthorName;
                flag = true;
            }
            else if (FieldName == "SelfText" && !string.IsNullOrEmpty(feeditem.SelfText))
            {
                readValue = feeditem.SelfText;
                flag = true;
            }
            else if (FieldName == "Url" && !string.IsNullOrEmpty(feeditem.Url.ToString()))
            {
                readValue = feeditem.Url;
                flag = true;
            }
        }
        return new ReadResult(DateTime.Now)
        {
            WasValueRead = flag,
            ReadValue = readValue
        };
    }
}

  private static readonly Guid TemplateId = Guid.Parse("{68BD9AAD-635F-40F3-9ACD-711662C59EEC}");
    private IValueReader ValueReader { get; set; }
    private IValueWriter ValueWriter { get; set; }

    public RedditFeedFieldValueAccessorConverter(IItemModelRepository repository) : base(repository)
    {
        this.SupportedTemplateIds.Add(TemplateId);
    }
    protected override ConvertResult<IValueAccessor> ConvertSupportedItem(ItemModel source)
    {
        var accessor = base.Convert(source);
        if (accessor == null)
        {
            return null;
        }

        var fieldName = base.GetStringValue(source, RedditFeedFieldValueValueAccessorItemModel.RedditFeedFieldName);
        if (string.IsNullOrEmpty(fieldName))
        {
            return null;
        }

        ValueWriter = this.GetValueWriter(source);
       // ValueReader= this.GetValueReader(source) ?? new RedditFeedValueReader(fieldName);
        if (accessor.ValueReader == null)
        {
            accessor.ValueReader = new RedditFeedValueReader(fieldName);
        }

        if (ValueWriter == null)
        {
            ValueWriter = new PropertyValueWriter(fieldName);
        }
        return this.PositiveResult((IValueAccessor) new ValueAccessor());
    }
 public class RedditFeedFieldValueAccessorConverter:ValueAccessorConverter
{
    private static readonly Guid TemplateId = Guid.Parse("{68BD9AAD-635F-40F3-9ACD-711662C59EEC}");
    private IValueReader ValueReader { get; set; }
    private IValueWriter ValueWriter { get; set; }

    public RedditFeedFieldValueAccessorConverter(IItemModelRepository repository) : base(repository)
    {
        this.SupportedTemplateIds.Add(TemplateId);
    }
    protected override ConvertResult<IValueAccessor> ConvertSupportedItem(ItemModel source)
    {
        var accessor = base.Convert(source);
        if (accessor == null)
        {
            return null;
        }

        var fieldName = base.GetStringValue(source, RedditFeedFieldValueValueAccessorItemModel.RedditFeedFieldName);
        if (string.IsNullOrEmpty(fieldName))
        {
            return null;
        }

        ValueWriter = this.GetValueWriter(source);
       // ValueReader= this.GetValueReader(source) ?? new RedditFeedValueReader(fieldName);
        if (accessor.ValueReader == null)
        {
            accessor.ValueReader = new RedditFeedValueReader(fieldName);
        }

        if (ValueWriter == null)
        {
            ValueWriter = new PropertyValueWriter(fieldName);
        }
        return this.PositiveResult((IValueAccessor) new ValueAccessor());
    }
  • You want to say that value accessor converter assigns this reader to a value accessor but this read method is not being hit when you run pipeline batch? – Vlad Shpak Apr 18 '18 at 8:58
  • Yes that is correct. – RReilley Apr 18 '18 at 9:34
  • Means that value accessor is not being run even when it converts. Looks like it fails elsewhere before execute reader. Can you share screenshot of vale accessor and converter class that is used? – Vlad Shpak Apr 18 '18 at 13:16
  • I updated the above code. I did have to change this from the original code since the code had changed from 1.4. – RReilley Apr 18 '18 at 13:47
  • From what I can tell it might be failing on this line. var accessor = base.Convert(source); This worked in the previous version. Source does have a value. Eventually it is null though and it seems the Convert function does not work the same as previous versions. – RReilley Apr 18 '18 at 14:04
0

Try to override GetValueWriter instead of ConvertResult.

[SupportedIds("{68BD9AAD-635F-40F3-9ACD-711662C59EEC}")]
public class RedditFeedFieldValueAccessorConverter : ValueAccessorConverter
{
    public RedditFeedFieldValueAccessorConverter(IItemModelRepository repository) : base(repository)
    {
    }

    protected override IValueReader GetValueReader(ItemModel source)
    {
        IValueReader valueReader = base.GetValueReader(source);
        if (valueReader != null)
        {
            return valueReader;
        }

        var fieldName = base.GetStringValue(source, RedditFeedFieldValueValueAccessorItemModel.RedditFeedFieldName);
        if (string.IsNullOrEmpty(fieldName))
        {
            return null;
        }

        return new RedditFeedValueReader(fieldName);
    }

    protected override IValueWriter GetValueWriter(ItemModel source)
    {
        IValueWriter valueWriter = base.GetValueWriter(source);
        if (valueWriter != null)
        {
            return valueWriter;
        }

        var fieldName = base.GetStringValue(source, RedditFeedFieldValueValueAccessorItemModel.RedditFeedFieldName);
        if (string.IsNullOrEmpty(fieldName))
        {
            return null;
        }

        return new PropertyValueWriter(fieldName);
    }
}
| improve this answer | |
  • I get the following error. Should RedditFeedValueReader inherit from ValueWriter instead? Not sure if it will return what is expected. Cannot implicitly convert type 'Sitecore.DEF.RedditImport.Converters.DataAccess.Reader.RedditFeedValueReader' to 'Sitecore.DataExchange.DataAccess.IValueWriter'. An explicit conversion exists (are you missing a cast?) – RReilley Apr 18 '18 at 18:59
  • I will update. I hurried up and did mistake. Sorry – Vlad Shpak Apr 18 '18 at 19:17
  • I am trying to see if I override reader method that will work. – RReilley Apr 18 '18 at 19:19
  • Updated my answer – Vlad Shpak Apr 18 '18 at 19:25
  • Thanks. Should it inherit JTokenValueAccessorConverter now? It is not currently hitting my breakpoint. – RReilley Apr 18 '18 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.