Created
May 3, 2014 17:39
-
-
Save sergiomichels/1c4a362960978f0ec392 to your computer and use it in GitHub Desktop.
Utility to register new constraints
This file contains 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 ConstraintRegistrar { | |
static void registerConstraints() { | |
List<GrailsDomainClass> domainClasses = Holders.getGrailsApplication().getArtefacts("Domain") | |
domainClasses.each ConstraintRegistrar.®isterConstraints | |
} | |
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