-
-
Save madsoulswe/efbb896fc6cebf5c5e5fdb8789f4ad01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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