Created
April 8, 2012 19:35
-
-
Save MichaelAstreiko/2339498 to your computer and use it in GitHub Desktop.
New DomainClassMarshaller
This file contains hidden or 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
class SimpleDomainClassMarshaller extends DomainClassMarshaller { | |
protected static final Log log = LogFactory.getLog(BaseDomainClassMarshaller.class) | |
protected ProxyHandler proxyHandler | |
private static List SKIPPED_FIELDS = Arrays.asList("lastUpdated", "dateCreated", "createdBy", "class", "id"); | |
public SimpleDomainClassMarshaller() { | |
this(false, new HibernateProxyHandler()); | |
} | |
public SimpleDomainClassMarshaller(boolean includeVersion, ProxyHandler proxyHandler) { | |
super(includeVersion, proxyHandler); | |
this.proxyHandler = proxyHandler; | |
} | |
@Override | |
public void marshalObject(Object value, JSON json) throws ConverterException { | |
JSONWriter writer = json.getWriter(); | |
value = proxyHandler.unwrapIfProxy(value); | |
Class<?> clazz = value.getClass(); | |
GrailsDomainClass domainClass = ConverterUtil.getDomainClass(clazz.getName()); | |
BeanWrapper beanWrapper = new BeanWrapperImpl(value); | |
writer.object(); | |
GrailsDomainClassProperty[] properties = domainClass.getPersistentProperties(); | |
if (needToDefineId()) { | |
def id | |
if (domainClass.hasProperty(getObjectIdentifier())) { | |
id = extractValue(value, domainClass.getPropertyByName(getObjectIdentifier())) | |
} else { | |
id = extractValue(value, domainClass.getPropertyByName("id")) | |
} | |
json.property("id", id) | |
} | |
processAdditionalFields(beanWrapper, json) | |
List excludedFields = SKIPPED_FIELDS + getSkippedFields() | |
for (GrailsDomainClassProperty property: properties) { | |
if (!excludedFields.contains(property.getName())) { | |
if (!property.isAssociation() && !processSimpleField(beanWrapper, property, json)) { | |
writeFieldName(writer, property); | |
// Write non-relation property | |
Object val = beanWrapper.getPropertyValue(property.getName()); | |
if (val instanceof String && !val) { | |
val = null | |
} else if (property.getType() == Boolean && val == null) { | |
val = false | |
} | |
json.convertAnother(val); | |
} else { | |
Object referenceObject = beanWrapper.getPropertyValue(property.getName()); | |
GrailsDomainClass referencedDomainClass = property.getReferencedDomainClass(); | |
if (processSpecificFields(beanWrapper, property, json)) { | |
//do nothing | |
} else if (referencedDomainClass == null || property.isEmbedded() || GrailsClassUtils.isJdk5Enum(property.getType()) || | |
property.isOneToOne() || property.isManyToOne() || property.isEmbedded()) { | |
writeFieldName(writer, property); | |
json.convertAnother(proxyHandler.unwrapIfProxy(referenceObject)) | |
} else { | |
writeFieldName(writer, property); | |
GrailsDomainClassProperty referencedIdProperty = referencedDomainClass.getIdentifier(); | |
@SuppressWarnings("unused") | |
String refPropertyName = referencedDomainClass.getPropertyName(); | |
if (referenceObject instanceof Collection) { | |
Collection o = (Collection) referenceObject; | |
writer.array(); | |
for (Object el: o) { | |
asShortObject(el, json, referencedIdProperty, referencedDomainClass); | |
} | |
writer.endArray(); | |
} else if (referenceObject instanceof Map) { | |
Map<Object, Object> map = (Map<Object, Object>) referenceObject; | |
for (Map.Entry<Object, Object> entry: map.entrySet()) { | |
String key = String.valueOf(entry.getKey()); | |
Object o = entry.getValue(); | |
writer.object(); | |
writer.key(key); | |
asShortObject(o, json, referencedIdProperty, referencedDomainClass); | |
writer.endObject(); | |
} | |
} | |
} | |
} | |
} | |
} | |
writer.endObject(); | |
} | |
protected String getDataUrl(String val) { | |
if (val) { | |
if (val.startsWith("http://") || val.startsWith("https://") | |
|| val.startsWith("ftp://") || val.startsWith("file://")) { | |
return val | |
} else { | |
return ConfigurationHolder.config.grails.contentURL + "/" + val | |
} | |
} | |
return null | |
} | |
protected def writeFieldName(JSONWriter writer, GrailsDomainClassProperty property) { | |
def propertyName = property.getName() | |
if (getAlternativeName(propertyName)) { | |
propertyName = getAlternativeName(propertyName) | |
} | |
writer.key(propertyName) | |
} | |
protected String getAlternativeName(String originalName) { | |
return null | |
} | |
protected List getSkippedFields() { | |
return [] | |
} | |
protected List getCommonSkippedFields() { | |
return SKIPPED_FIELDS | |
} | |
protected boolean processSpecificFields(BeanWrapper beanWrapper, | |
GrailsDomainClassProperty property, JSON json) { | |
//do not do anything with all references | |
return true | |
} | |
protected boolean processSimpleField(BeanWrapper beanWrapper, | |
GrailsDomainClassProperty property, JSON json) { | |
return false | |
} | |
protected void processAdditionalFields(BeanWrapper beanWrapper, JSON json) { | |
} | |
protected String getObjectIdentifier() { | |
return 'publicId' | |
} | |
protected boolean needToDefineId() { | |
return true | |
} | |
public boolean supports(Object object) { | |
return ConverterUtil.isDomainClass(object.getClass()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment