Skip to content

Instantly share code, notes, and snippets.

@sergiomichels
Created May 3, 2014 17:39
Show Gist options
  • Save sergiomichels/1c4a362960978f0ec392 to your computer and use it in GitHub Desktop.
Save sergiomichels/1c4a362960978f0ec392 to your computer and use it in GitHub Desktop.
Utility to register new constraints
class ConstraintRegistrar {
static void registerConstraints() {
List<GrailsDomainClass> domainClasses = Holders.getGrailsApplication().getArtefacts("Domain")
domainClasses.each ConstraintRegistrar.&registerConstraints
}
static void registerConstraints(Class clazz) {
GrailsDomainClass domainClass = Holders.getGrailsApplication().getDomainClass(clazz.name)
registerConstraints(domainClass)
}
static void registerConstraints( GrailsDomainClass domainClass ) {
registerConstraint(domainClass, SimNao.class, SimNaoConstraint.CONSTRAINT_NAME)
registerConstraint(domainClass, Cei.class, CeiConstraint.CEI_CONSTRAINT)
registerConstraint(domainClass, EmpresaIdentity.class, EmpresaIdentityConstraint.CONSTRAINT_NAME)
registerConstraint(domainClass, PessoaIdentity.class, PessoaIdentityConstraint.CONSTRAINT_NAME)
}
static void registerConstraint(GrailsDomainClass domainClass, Class type, String consName) {
List<DefaultGrailsDomainClassProperty> props = domainClass.properties.findAll { type.isAssignableFrom( it.type ) }
def constraints = domainClass.constraints
for(DefaultGrailsDomainClassProperty prop : props) {
String propName = prop.name
ConstrainedProperty constraint = (constraints."$propName")
if( !constraint.getAppliedConstraint(consName) ) {
constraint.applyConstraint(consName, true)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment