use of grails.core.GrailsDomainClass in project grails-core by grails.
the class DomainClassMarshaller method marshalObject.
@SuppressWarnings({ "unchecked", "rawtypes" })
public void marshalObject(Object value, XML xml) throws ConverterException {
Class clazz = value.getClass();
List<String> excludes = xml.getExcludes(clazz);
List<String> includes = xml.getIncludes(clazz);
IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
GrailsDomainClass domainClass = (GrailsDomainClass) application.getArtefact(DomainClassArtefactHandler.TYPE, ConverterUtil.trimProxySuffix(clazz.getName()));
BeanWrapper beanWrapper = new BeanWrapperImpl(value);
GrailsDomainClassProperty id = domainClass.getIdentifier();
if (shouldInclude(includeExcludeSupport, includes, excludes, value, id.getName())) {
Object idValue = beanWrapper.getPropertyValue(id.getName());
if (idValue != null)
xml.attribute("id", String.valueOf(idValue));
}
if (shouldInclude(includeExcludeSupport, includes, excludes, value, GrailsDomainClassProperty.VERSION) && includeVersion) {
Object versionValue = beanWrapper.getPropertyValue(domainClass.getVersion().getName());
xml.attribute("version", String.valueOf(versionValue));
}
GrailsDomainClassProperty[] properties = domainClass.getPersistentProperties();
for (GrailsDomainClassProperty property : properties) {
String propertyName = property.getName();
if (!shouldInclude(includeExcludeSupport, includes, excludes, value, property.getName()))
continue;
xml.startNode(propertyName);
if (!property.isAssociation()) {
// Write non-relation property
Object val = beanWrapper.getPropertyValue(propertyName);
xml.convertAnother(val);
} else {
if (isRenderDomainClassRelations()) {
Object referenceObject = beanWrapper.getPropertyValue(propertyName);
if (referenceObject != null && shouldInitializeProxy(referenceObject)) {
referenceObject = proxyHandler.unwrapIfProxy(referenceObject);
if (referenceObject instanceof SortedMap) {
referenceObject = new TreeMap((SortedMap) referenceObject);
} else if (referenceObject instanceof SortedSet) {
referenceObject = new TreeSet((SortedSet) referenceObject);
} else if (referenceObject instanceof Set) {
referenceObject = new LinkedHashSet((Set) referenceObject);
} else if (referenceObject instanceof Map) {
referenceObject = new LinkedHashMap((Map) referenceObject);
} else if (referenceObject instanceof Collection) {
referenceObject = new ArrayList((Collection) referenceObject);
}
xml.convertAnother(referenceObject);
}
} else {
Object referenceObject = beanWrapper.getPropertyValue(propertyName);
if (referenceObject != null) {
GrailsDomainClass referencedDomainClass = property.getReferencedDomainClass();
// Embedded are now always fully rendered
if (referencedDomainClass == null || property.isEmbedded() || property.getType().isEnum()) {
xml.convertAnother(referenceObject);
} else if (property.isOneToOne() || property.isManyToOne() || property.isEmbedded()) {
asShortObject(referenceObject, xml, referencedDomainClass.getIdentifier(), referencedDomainClass);
} else {
GrailsDomainClassProperty referencedIdProperty = referencedDomainClass.getIdentifier();
@SuppressWarnings("unused") String refPropertyName = referencedDomainClass.getPropertyName();
if (referenceObject instanceof Collection) {
Collection o = (Collection) referenceObject;
for (Object el : o) {
xml.startNode(xml.getElementName(el));
asShortObject(el, xml, referencedIdProperty, referencedDomainClass);
xml.end();
}
} 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();
xml.startNode("entry").attribute("key", key);
asShortObject(o, xml, referencedIdProperty, referencedDomainClass);
xml.end();
}
}
}
}
}
}
xml.end();
}
}
use of grails.core.GrailsDomainClass in project grails-core by grails.
the class HeirarchyDomainClassTests method testClassHeirarchy.
public void testClassHeirarchy() throws Exception {
GroovyClassLoader gcl = new GroovyClassLoader();
gcl.parseClass("@grails.persistence.Entity class Super { Long id;Long version;}\n" + "class Sub extends Super { }\n" + "class Sub2 extends Sub { }");
GrailsApplication ga = new DefaultGrailsApplication(gcl.getLoadedClasses(), gcl);
ga.initialise();
new MappingContextBuilder(ga).build(gcl.getLoadedClasses());
GrailsDomainClass gdc1 = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "Super");
assertNotNull(gdc1);
assertTrue(gdc1.isRoot());
assertEquals(2, gdc1.getSubClasses().size());
assertFalse(gdc1.getPropertyByName("id").isInherited());
GrailsDomainClass gdc2 = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "Sub");
assertFalse(gdc2.isRoot());
assertEquals(1, gdc2.getSubClasses().size());
assertTrue(gdc2.getPropertyByName("id").isInherited());
GrailsDomainClass gdc3 = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "Sub2");
assertFalse(gdc3.isRoot());
assertEquals(0, gdc3.getSubClasses().size());
assertTrue(gdc3.getPropertyByName("id").isInherited());
}
use of grails.core.GrailsDomainClass in project grails-core by grails.
the class DefaultGrailsDomainClass method getPersistentProperty.
public GrailsDomainClassProperty getPersistentProperty(String name) {
initializePersistentProperties();
if (propertyMap.containsKey(name)) {
return propertyMap.get(name);
}
int indexOfDot = name.indexOf('.');
if (indexOfDot > 0) {
String basePropertyName = name.substring(0, indexOfDot);
if (propertyMap.containsKey(basePropertyName)) {
verifyContextIsInitialized();
PersistentProperty prop = persistentEntity.getPropertyByName(basePropertyName);
PersistentEntity referencedEntity = prop.getOwner();
GrailsDomainClass referencedDomainClass = (GrailsDomainClass) grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, referencedEntity.getName());
if (referencedDomainClass != null) {
String restOfPropertyName = name.substring(indexOfDot + 1);
return referencedDomainClass.getPropertyByName(restOfPropertyName);
}
}
}
return null;
}
use of grails.core.GrailsDomainClass in project grails-core by grails.
the class ConstraintsEvaluatingPropertyTests method ensureConstraintsPresent.
@SuppressWarnings("rawtypes")
private void ensureConstraintsPresent(String[] classSource, int classIndexToTest, int constraintCount) throws Exception {
// We need to do a real test here to make sure
GroovyClassLoader gcl = new GroovyClassLoader();
Class[] classes = new Class[classSource.length];
for (int i = 0; i < classSource.length; i++) {
classes[i] = gcl.parseClass(classSource[i]);
}
GrailsApplication ga = new DefaultGrailsApplication(classes[classIndexToTest]);
ga.initialise();
new MappingContextBuilder(ga).build(classes[classIndexToTest]);
GrailsDomainClass domainClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, classes[classIndexToTest].getName());
Map constraints = domainClass.getConstrainedProperties();
grails.gorm.validation.ConstrainedProperty p = (grails.gorm.validation.ConstrainedProperty) constraints.get("name");
Collection cons = p.getAppliedConstraints();
assertEquals("Incorrect number of constraints extracted: " + constraints, constraintCount, cons.size());
}
use of grails.core.GrailsDomainClass in project grails-core by grails.
the class DomainClassMarshaller method marshalObject.
@SuppressWarnings({ "unchecked", "rawtypes" })
public void marshalObject(Object value, JSON json) throws ConverterException {
JSONWriter writer = json.getWriter();
value = proxyHandler.unwrapIfProxy(value);
Class<?> clazz = value.getClass();
List<String> excludes = json.getExcludes(clazz);
List<String> includes = json.getIncludes(clazz);
IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
GrailsDomainClass domainClass = (GrailsDomainClass) application.getArtefact(DomainClassArtefactHandler.TYPE, ConverterUtil.trimProxySuffix(clazz.getName()));
BeanWrapper beanWrapper = new BeanWrapperImpl(value);
writer.object();
if (includeClass && shouldInclude(includeExcludeSupport, includes, excludes, value, "class")) {
writer.key("class").value(domainClass.getClazz().getName());
}
GrailsDomainClassProperty id = domainClass.getIdentifier();
if (shouldInclude(includeExcludeSupport, includes, excludes, value, id.getName())) {
Object idValue = extractValue(value, id);
if (idValue != null) {
json.property(GrailsDomainClassProperty.IDENTITY, idValue);
}
}
if (shouldInclude(includeExcludeSupport, includes, excludes, value, GrailsDomainClassProperty.VERSION) && isIncludeVersion()) {
GrailsDomainClassProperty versionProperty = domainClass.getVersion();
Object version = extractValue(value, versionProperty);
if (version != null) {
json.property(GrailsDomainClassProperty.VERSION, version);
}
}
GrailsDomainClassProperty[] properties = domainClass.getPersistentProperties();
for (GrailsDomainClassProperty property : properties) {
if (!shouldInclude(includeExcludeSupport, includes, excludes, value, property.getName()))
continue;
writer.key(property.getName());
if (!property.isAssociation()) {
// Write non-relation property
Object val = beanWrapper.getPropertyValue(property.getName());
json.convertAnother(val);
} else {
Object referenceObject = beanWrapper.getPropertyValue(property.getName());
if (isRenderDomainClassRelations()) {
if (referenceObject == null) {
writer.valueNull();
} else {
referenceObject = proxyHandler.unwrapIfProxy(referenceObject);
if (referenceObject instanceof SortedMap) {
referenceObject = new TreeMap((SortedMap) referenceObject);
} else if (referenceObject instanceof SortedSet) {
referenceObject = new TreeSet((SortedSet) referenceObject);
} else if (referenceObject instanceof Set) {
referenceObject = new LinkedHashSet((Set) referenceObject);
} else if (referenceObject instanceof Map) {
referenceObject = new LinkedHashMap((Map) referenceObject);
} else if (referenceObject instanceof Collection) {
referenceObject = new ArrayList((Collection) referenceObject);
}
json.convertAnother(referenceObject);
}
} else {
if (referenceObject == null) {
json.value(null);
} else {
GrailsDomainClass referencedDomainClass = property.getReferencedDomainClass();
// Embedded are now always fully rendered
if (referencedDomainClass == null || property.isEmbedded() || property.getType().isEnum()) {
json.convertAnother(referenceObject);
} else if (property.isOneToOne() || property.isManyToOne() || property.isEmbedded()) {
asShortObject(referenceObject, json, referencedDomainClass.getIdentifier(), referencedDomainClass);
} else {
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();
}
Aggregations