use of grails.core.GrailsDomainClass in project grails-core by grails.
the class GrailsDomainConfigurationUtil method configureDomainClassRelationships.
/**
* Configures the relationships between domain classes after they have been all loaded.
*
* @param domainClasses The domain classes to configure relationships for
* @param domainMap The domain class map
*/
public static void configureDomainClassRelationships(GrailsClass[] domainClasses, Map<?, ?> domainMap) {
// and configure how domain class properties reference each other
for (GrailsClass grailsClass : domainClasses) {
GrailsDomainClass domainClass = (GrailsDomainClass) grailsClass;
if (!domainClass.isRoot()) {
Class<?> superClass = grailsClass.getClazz().getSuperclass();
while (!superClass.equals(Object.class) && !superClass.equals(GroovyObject.class)) {
GrailsDomainClass gdc = (GrailsDomainClass) domainMap.get(superClass.getName());
if (gdc == null || gdc.getSubClasses() == null) {
break;
}
gdc.getSubClasses().add((GrailsDomainClass) grailsClass);
superClass = superClass.getSuperclass();
}
}
GrailsDomainClassProperty[] props = domainClass.getPersistentProperties();
for (GrailsDomainClassProperty prop : props) {
if (prop != null && prop.isAssociation()) {
GrailsDomainClass referencedGrailsDomainClass = (GrailsDomainClass) domainMap.get(prop.getReferencedPropertyType().getName());
prop.setReferencedDomainClass(referencedGrailsDomainClass);
}
}
}
// now configure so that the 'other side' of a property can be resolved by the property itself
for (GrailsClass domainClass1 : domainClasses) {
GrailsDomainClass domainClass = (GrailsDomainClass) domainClass1;
GrailsDomainClassProperty[] props = domainClass.getPersistentProperties();
for (GrailsDomainClassProperty prop : props) {
if (prop == null || !prop.isAssociation()) {
continue;
}
GrailsDomainClass referenced = prop.getReferencedDomainClass();
if (referenced == null) {
continue;
}
boolean isOwnedBy = referenced.isOwningClass(domainClass.getClazz());
prop.setOwningSide(isOwnedBy);
String refPropertyName = null;
try {
refPropertyName = prop.getReferencedPropertyName();
} catch (UnsupportedOperationException e) {
// ignore (to support Hibernate entities)
}
if (!StringUtils.hasText(refPropertyName)) {
GrailsDomainClassProperty[] referencedProperties = referenced.getPersistentProperties();
for (GrailsDomainClassProperty referencedProp : referencedProperties) {
// to be equal to self
if (prop.equals(referencedProp) && prop.isBidirectional()) {
continue;
}
if (isCandidateForOtherSide(domainClass, prop, referencedProp)) {
prop.setOtherSide(referencedProp);
break;
}
}
} else {
GrailsDomainClassProperty otherSide = referenced.getPropertyByName(refPropertyName);
prop.setOtherSide(otherSide);
otherSide.setOtherSide(prop);
}
}
}
}
use of grails.core.GrailsDomainClass in project grails-core by grails.
the class ConstrainedPropertyTests method testConstraintBuilder.
@SuppressWarnings("rawtypes")
public void testConstraintBuilder() throws Exception {
GroovyClassLoader gcl = new GroovyClassLoader();
Class groovyClass = gcl.parseClass("class TestClass {\n" + "Long id\n" + "Long version\n" + "String login\n" + "String other\n" + "String email\n" + "static constraints = {\n" + "login(size:5..15,nullable:false,blank:false)\n" + "other(blank:false,size:5..15,nullable:false)\n" + "email(email:true)\n" + "}\n" + "}");
GrailsApplication ga = new DefaultGrailsApplication(groovyClass);
ga.initialise();
new MappingContextBuilder(ga).build(groovyClass);
GrailsDomainClass domainClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "TestClass");
Map constrainedProperties = domainClass.getConstrainedProperties();
assert constrainedProperties.size() == 3;
grails.gorm.validation.ConstrainedProperty loginConstraint = (grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("login");
Collection appliedConstraints = loginConstraint.getAppliedConstraints();
assertTrue(appliedConstraints.size() == 3);
// Check the order of the constraints for the 'login' property...
int index = 0;
String[] constraintNames = new String[] { "size", "nullable", "blank" };
for (Iterator iter = appliedConstraints.iterator(); iter.hasNext(); ) {
Constraint c = (Constraint) iter.next();
assertEquals(constraintNames[index], c.getName());
index++;
}
// ...and for the 'other' property.
appliedConstraints = ((grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("other")).getAppliedConstraints();
index = 0;
constraintNames = new String[] { "blank", "size", "nullable" };
for (Iterator iter = appliedConstraints.iterator(); iter.hasNext(); ) {
Constraint c = (Constraint) iter.next();
assertEquals(constraintNames[index], c.getName());
index++;
}
grails.gorm.validation.ConstrainedProperty emailConstraint = (grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("email");
assertEquals(2, emailConstraint.getAppliedConstraints().size());
GroovyObject go = (GroovyObject) groovyClass.newInstance();
go.setProperty("email", "rubbish_email");
Errors errors = new BindException(go, "TestClass");
emailConstraint.validate(go, go.getProperty("email"), errors);
assertTrue(errors.hasErrors());
go.setProperty("email", "valid@email.com");
errors = new BindException(go, "TestClass");
emailConstraint.validate(go, go.getProperty("email"), errors);
assertFalse(errors.hasErrors());
}
use of grails.core.GrailsDomainClass in project grails-core by grails.
the class ConstraintsEvaluatingPropertyTests method testNullableConstraint.
/**
* Test that static constraints work
*/
@SuppressWarnings("rawtypes")
public void testNullableConstraint() throws Exception {
GroovyClassLoader gcl = new GroovyClassLoader();
gcl.parseClass("package org.grails.validation\n" + "@grails.persistence.Entity class Book {\n" + " Long id\n" + " Long version\n" + " String title\n" + " String description\n" + " Author author\n" + " Author assistent\n" + " Set chapters\n" + " Map remarks\n" + " static hasMany = [chapters:Chapter]\n" + " static constraints = {\n" + " description(nullable: true)\n" + " assistent(nullable: true)\n" + " }\n" + "}\n" + "@grails.persistence.Entity class Author {\n" + " Long id\n" + " Long version\n" + " String name\n" + "}\n" + "@grails.persistence.Entity class Chapter {\n" + " Long id\n" + " Long version\n" + " String text\n" + "}");
GrailsApplication ga = new DefaultGrailsApplication(gcl.getLoadedClasses());
ga.initialise();
new MappingContextBuilder(ga).build(gcl.getLoadedClasses());
GrailsDomainClass bookClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "org.grails.validation.Book");
Map constraints = bookClass.getConstrainedProperties();
Constrained p = (Constrained) constraints.get("title");
assertFalse("Title property should be required", p.isNullable());
p = (Constrained) constraints.get("description");
assertTrue("Description property should be optional", p.isNullable());
p = (Constrained) constraints.get("author");
assertFalse("Author property should be required", p.isNullable());
p = (Constrained) constraints.get("assistent");
assertTrue("Assistent property should be optional", p.isNullable());
// Test that Collections and Maps are nullable by default
p = (Constrained) constraints.get("chapters");
assertTrue("Chapters property should be optional", p.isNullable());
p = (Constrained) constraints.get("remarks");
assertTrue("Remarks property should be optional", p.isNullable());
}
use of grails.core.GrailsDomainClass in project grails-core by grails.
the class ConstraintsEvaluatingPropertyTests method testGet.
/*
* Test method for 'ConstraintsDynamicProperty.get(Object)'
*/
@SuppressWarnings("rawtypes")
public void testGet() throws Exception {
GroovyClassLoader gcl = new GroovyClassLoader();
Class<?> groovyClass = gcl.parseClass("package org.grails.validation\n" + "class Test {\n" + // WE NEED this even though GORM 2 doesn't, as we're not a "domain" class within grails-app
" Long id\n" + // WE NEED this even though GORM 2 doesn't, as we're not a "domain" class within grails-app
" Long version\n" + " String name\n" + "}");
GrailsApplication ga = new DefaultGrailsApplication(groovyClass);
ga.initialise();
new MappingContextBuilder(ga).build(groovyClass);
GrailsDomainClass domainClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, groovyClass.getName());
Map constraints = domainClass.getConstrainedProperties();
assertNotNull(constraints);
assertFalse(constraints.isEmpty());
}
use of grails.core.GrailsDomainClass in project grails-core by grails.
the class ControllersDomainBindingApi method initialize.
/**
* A map based constructor that binds the named arguments to the target instance
*
* @param instance The target instance
* @param namedArgs The named arguments
*/
public static void initialize(Object instance, Map namedArgs) {
GrailsDomainClass dc = getDomainClass(instance);
if (dc == null) {
DataBindingUtils.bindObjectToInstance(instance, namedArgs);
} else {
DataBindingUtils.bindObjectToDomainInstance(dc, instance, namedArgs);
DataBindingUtils.assignBidirectionalAssociations(instance, namedArgs, dc);
}
autowire(instance);
}
Aggregations