Skip to content

Instantly share code, notes, and snippets.

@madsoulswe
Created April 28, 2016 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madsoulswe/efbb896fc6cebf5c5e5fdb8789f4ad01 to your computer and use it in GitHub Desktop.
Save madsoulswe/efbb896fc6cebf5c5e5fdb8789f4ad01 to your computer and use it in GitHub Desktop.
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