Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class NestedContentMapper : IContentMapper
{
private IContentTypeService contentTypeService;
private IDataTypeService dataTypeService;
public NestedContentMapper()
{
contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
dataTypeService = ApplicationContext.Current.Services.DataTypeService;
}
public string GetExportValue(int dataTypeDefinitionId, string value)
{
var array = JsonConvert.DeserializeObject<JArray>(value);
foreach (var ncObj in array)
{
var doctype = contentTypeService.GetContentType(ncObj["ncContentTypeAlias"].ToString());
if (doctype == null)
continue;
foreach (var propertyType in doctype.PropertyTypes)
{
object o;
if ((o = ncObj[propertyType.Alias]) != null)
{
IDataTypeDefinition dataType = dataTypeService.GetDataTypeDefinitionById(propertyType.DataTypeDefinitionId);
uSyncContentMapping mapping = uSyncCoreContext.Instance.Configuration.Settings.ContentMappings.SingleOrDefault(x => x.EditorAlias == dataType.PropertyEditorAlias);
if (mapping != null)
{
IContentMapper mapper = ContentMapperFactory.GetMapper(mapping);
if (mapper != null)
{
ncObj[propertyType.Alias] = mapper.GetExportValue(dataType.Id, ncObj[propertyType.Alias].ToString());
}
}
}
}
}
return JsonConvert.SerializeObject(array);
}
public string GetImportValue(int dataTypeDefinitionId, string content)
{
var array = JsonConvert.DeserializeObject<JArray>(content);
foreach (var ncObj in array)
{
var doctype = contentTypeService.GetContentType(ncObj["ncContentTypeAlias"].ToString());
if (doctype == null)
continue;
foreach (var propertyType in doctype.PropertyTypes)
{
object o;
if ((o = ncObj[propertyType.Alias]) != null)
{
IDataTypeDefinition dataType = dataTypeService.GetDataTypeDefinitionById(propertyType.DataTypeDefinitionId);
uSyncContentMapping mapping = uSyncCoreContext.Instance.Configuration.Settings.ContentMappings.SingleOrDefault(x => x.EditorAlias == dataType.PropertyEditorAlias);
if (mapping != null)
{
IContentMapper mapper = ContentMapperFactory.GetMapper(mapping);
if (mapper != null)
{
ncObj[propertyType.Alias] = mapper.GetImportValue(dataType.Id, ncObj[propertyType.Alias].ToString());
}
}
}
}
}
return JsonConvert.SerializeObject(array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.