Created
April 8, 2012 19:47
-
-
Save MichaelAstreiko/2339545 to your computer and use it in GitHub Desktop.
UserClassMarshaller
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 UserClassMarshaller extends SimpleDomainClassMarshaller { | |
private Map ALTERNATIVE_NAMES = ["username":"screenName"] | |
protected List getSkippedFields() { | |
return ["password", "email", "enabled", "accountExpired", "accountLocked", | |
"passwordExpired", "securityQuestion", "securityAnswer"] | |
} | |
protected String getAlternativeName(String originalName){ | |
return ALTERNATIVE_NAMES[originalName] | |
} | |
protected String getObjectIdentifier() { | |
return 'userId' | |
} | |
protected boolean processSpecificFields(BeanWrapper beanWrapper, | |
GrailsDomainClassProperty property, JSON json) { | |
Object referenceObject = beanWrapper.getPropertyValue(property.getName()) | |
//Calculate User rating | |
if ("reviews" == property.getName()) { | |
json.property("reviewsCount", referenceObject.size()); | |
if (referenceObject.size() > 1) { | |
json.property("rating", referenceObject.sum{it.rating} / referenceObject.size()); | |
} | |
} | |
return true | |
} | |
public boolean supports(Object object) { | |
return proxyHandler.unwrapIfProxy(object).getClass().isAssignableFrom(User.class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment