use of grails.core.GrailsDomainClassProperty in project grails-core by grails.
the class DefaultConstraintEvaluator method evaluate.
@Override
public Map<String, Constrained> evaluate(Class<?> cls, GrailsDomainClassProperty[] properties) {
Map<String, ConstrainedProperty> evaluated = resolveDelegate().evaluate(cls);
Map<String, Constrained> adapted = adaptConstraints(evaluated);
for (GrailsDomainClassProperty property : properties) {
String name = property.getName();
if (!adapted.containsKey(name)) {
adapted.remove(name);
}
}
return adapted;
}
use of grails.core.GrailsDomainClassProperty in project grails-core by grails.
the class DomainClassPropertyComparator method compare.
public int compare(Object o1, Object o2) {
if (o1.equals(domainClass.getIdentifier())) {
return -1;
}
if (o2.equals(domainClass.getIdentifier())) {
return 1;
}
GrailsDomainClassProperty prop1 = (GrailsDomainClassProperty) o1;
GrailsDomainClassProperty prop2 = (GrailsDomainClassProperty) o2;
Constrained cp1 = (Constrained) constrainedProperties.get(prop1.getName());
Constrained cp2 = (Constrained) constrainedProperties.get(prop2.getName());
if (cp1 == null & cp2 == null) {
return prop1.getName().compareTo(prop2.getName());
}
if (cp1 == null) {
return 1;
}
if (cp2 == null) {
return -1;
}
if (cp1.getOrder() > cp2.getOrder()) {
return 1;
}
if (cp1.getOrder() < cp2.getOrder()) {
return -1;
}
return 0;
}
use of grails.core.GrailsDomainClassProperty in project grails-core by grails.
the class GrailsDomainClassValidator method validate.
/**
* @see CascadingValidator#validate(Object, org.springframework.validation.Errors, boolean)
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void validate(Object obj, Errors errors, boolean cascade) {
if (obj == null) {
throw new IllegalArgumentException("Argument [" + obj + "] is not an instance of [" + domainClass.getClazz() + "] which this validator is configured for");
}
BeanWrapper bean = new BeanWrapperImpl(obj);
Map constrainedProperties = domainClass.getConstrainedProperties();
Set<String> constrainedPropertyNames = new HashSet(constrainedProperties.keySet());
for (Object key : constrainedProperties.keySet()) {
String propertyName = (String) key;
validatePropertyWithConstraint(propertyName, obj, errors, bean, constrainedProperties);
}
GrailsDomainClassProperty[] persistentProperties = domainClass.getPersistentProperties();
for (GrailsDomainClassProperty persistentProperty : persistentProperties) {
String propertyName = persistentProperty.getName();
if ((persistentProperty.isAssociation() || persistentProperty.isEmbedded()) && cascade) {
cascadeToAssociativeProperty(errors, bean, persistentProperty);
}
// Remove this property from the set of constrained property
// names because we have already processed it.
constrainedPropertyNames.remove(propertyName);
}
postValidate(obj, errors);
}
use of grails.core.GrailsDomainClassProperty in project grails-core by grails.
the class GrailsDomainClassValidator method cascadeValidationToOne.
/**
* Cascades validation to a one-to-one or many-to-one property.
*
* @param errors The Errors instance
* @param bean The original BeanWrapper
* @param associatedObject The associated object's current value
* @param persistentProperty The GrailsDomainClassProperty instance
* @param propertyName The name of the property
* @param indexOrKey
*/
@SuppressWarnings("rawtypes")
protected void cascadeValidationToOne(Errors errors, BeanWrapper bean, Object associatedObject, GrailsDomainClassProperty persistentProperty, String propertyName, Object indexOrKey) {
if (associatedObject == null) {
return;
}
GrailsDomainClass associatedDomainClass = getAssociatedDomainClass(associatedObject, persistentProperty);
if (associatedDomainClass == null || !isOwningInstance(bean, associatedDomainClass) && !persistentProperty.isExplicitSaveUpdateCascade()) {
return;
}
GrailsDomainClassProperty otherSide = null;
if (persistentProperty.isBidirectional()) {
otherSide = persistentProperty.getOtherSide();
}
Map associatedConstraintedProperties = associatedDomainClass.getConstrainedProperties();
GrailsDomainClassProperty[] associatedPersistentProperties = associatedDomainClass.getPersistentProperties();
String nestedPath = errors.getNestedPath();
try {
errors.setNestedPath(buildNestedPath(nestedPath, propertyName, indexOrKey));
for (GrailsDomainClassProperty associatedPersistentProperty : associatedPersistentProperties) {
if (persistentProperty.isEmbedded() && EMBEDDED_EXCLUDES.contains(associatedPersistentProperty.getName())) {
continue;
}
String associatedPropertyName = associatedPersistentProperty.getName();
if (associatedConstraintedProperties.containsKey(associatedPropertyName)) {
validatePropertyWithConstraint(errors.getNestedPath() + associatedPropertyName, associatedObject, errors, new BeanWrapperImpl(associatedObject), associatedConstraintedProperties);
}
if (associatedPersistentProperty.equals(otherSide))
continue;
if (associatedPersistentProperty.isAssociation()) {
cascadeToAssociativeProperty(errors, new BeanWrapperImpl(associatedObject), associatedPersistentProperty);
}
}
} finally {
errors.setNestedPath(nestedPath);
}
}
use of grails.core.GrailsDomainClassProperty in project grails-core by grails.
the class DefaultGrailsDomainClass method initializePersistentProperties.
private void initializePersistentProperties() {
if (!propertiesInitialized) {
verifyContextIsInitialized();
propertyMap = new LinkedHashMap<>();
PersistentProperty identity = persistentEntity.getIdentity();
if (identity != null) {
identifier = new DefaultGrailsDomainClassProperty(this, persistentEntity, identity);
}
// First go through the properties of the class and create domain properties
// populating into a map
populateDomainClassProperties();
// set properties from map values
persistentProperties = propertyMap.values().toArray(new GrailsDomainClassProperty[propertyMap.size()]);
// if no identifier property throw exception
if (identifier == null) {
throw new GrailsDomainException("Identity property not found, but required in domain class [" + getFullName() + "]");
} else {
propertyMap.put(identifier.getName(), identifier);
}
// if no version property throw exception
if (version != null) {
propertyMap.put(version.getName(), version);
}
List<MetaProperty> properties = getMetaClass().getProperties();
for (MetaProperty property : properties) {
String name = property.getName();
if (!propertyMap.containsKey(name) && !NameUtils.isConfigurational(name) && !Modifier.isStatic(property.getModifiers())) {
propertyMap.put(name, new MetaGrailsDomainClassProperty(this, property));
}
}
this.properties = propertyMap.values().toArray(new GrailsDomainClassProperty[propertyMap.size()]);
}
propertiesInitialized = true;
}
Aggregations