private static void SetTextMetaAndMergeMedia(FdoCache cache, Interlineartext interlinText, ILgWritingSystemFactory wsFactory,
FDO.IText newText, bool merging)
{
if (interlinText.Items != null) // apparently it is null if there are no items.
{
foreach (var item in interlinText.Items)
{
switch (item.type)
{
case "title":
newText.Name.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
break;
case "title-abbreviation":
newText.Abbreviation.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
break;
case "source":
newText.Source.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
break;
case "comment":
newText.Description.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
break;
}
}
}
if (interlinText.mediafiles != null)
{
if (newText.MediaFilesOA == null)
newText.MediaFilesOA = cache.ServiceLocator.GetInstance<ICmMediaContainerFactory>().Create();
newText.MediaFilesOA.OffsetType = interlinText.mediafiles.offsetType;
foreach (var mediaFile in interlinText.mediafiles.media)
{
ICmObject extantObject;
cache.ServiceLocator.ObjectRepository.TryGetObject(new Guid(mediaFile.guid), out extantObject);
var media = extantObject as ICmMediaURI;
if (media == null)
{
media = cache.ServiceLocator.GetInstance<ICmMediaURIFactory>().Create(cache, new Guid(mediaFile.guid));
newText.MediaFilesOA.MediaURIsOC.Add(media);
}
else if (!merging)
{
// If a media URI with the same GUID exists, and we are not merging, create a new media URI with a new GUID
media = cache.ServiceLocator.GetInstance<ICmMediaURIFactory>().Create();
newText.MediaFilesOA.MediaURIsOC.Add(media);
// Update references to this Media URI
foreach (var phrase in interlinText.paragraphs.SelectMany(para => para.phrases))
{
if (mediaFile.guid.Equals(phrase.mediaFile))
phrase.mediaFile = media.Guid.ToString();
}
}
// else, the media URI already exists and we are merging; simply update the location
media.MediaURI = mediaFile.location;
}
}
}
}