use of grails.core.GrailsDomainClass 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.GrailsDomainClass in project grails-core by grails.
the class DataBindingUtils method bindToCollection.
/**
* For each DataBindingSource provided by collectionBindingSource a new instance of targetType is created,
* data binding is imposed on that instance with the DataBindingSource and the instance is added to the end of
* collectionToPopulate
*
* @param targetType The type of objects to create, must be a concrete class
* @param collectionToPopulate A collection to populate with new instances of targetType
* @param collectionBindingSource A CollectionDataBindingSource
* @since 2.3
*/
public static <T> void bindToCollection(final Class<T> targetType, final Collection<T> collectionToPopulate, final CollectionDataBindingSource collectionBindingSource) throws InstantiationException, IllegalAccessException {
final GrailsApplication application = Holders.findApplication();
GrailsDomainClass domain = null;
if (application != null) {
domain = (GrailsDomainClass) application.getArtefact(DomainClassArtefactHandler.TYPE, targetType.getName());
}
final List<DataBindingSource> dataBindingSources = collectionBindingSource.getDataBindingSources();
for (final DataBindingSource dataBindingSource : dataBindingSources) {
final T newObject = targetType.newInstance();
bindObjectToDomainInstance(domain, newObject, dataBindingSource, getBindingIncludeList(newObject), Collections.emptyList(), null);
collectionToPopulate.add(newObject);
}
}
use of grails.core.GrailsDomainClass in project grails-core by grails.
the class DataBindingUtils method bindObjectToInstance.
/**
* Binds the given source object to the given target object performing type conversion if necessary
*
* @param object The object to bind to
* @param source The source object
* @param include The list of properties to include
* @param exclude The list of properties to exclude
* @param filter The prefix to filter by
*
* @return A BindingResult or null if it wasn't successful
*/
public static BindingResult bindObjectToInstance(Object object, Object source, List include, List exclude, String filter) {
if (include == null && exclude == null) {
include = getBindingIncludeList(object);
}
GrailsApplication application = Holders.findApplication();
GrailsDomainClass domain = null;
if (application != null) {
domain = (GrailsDomainClass) application.getArtefact(DomainClassArtefactHandler.TYPE, object.getClass().getName());
}
return bindObjectToDomainInstance(domain, object, source, include, exclude, filter);
}
use of grails.core.GrailsDomainClass in project grails-core by grails.
the class DefaultGrailsDomainClass method getComponents.
/**
* Retrieves a list of embedded components
*
* @deprecated Use {@link org.grails.datastore.mapping.model.MappingContext} API instead
*/
@Deprecated
public List<GrailsDomainClass> getComponents() {
verifyContextIsInitialized();
List<GrailsDomainClass> components = new ArrayList<>();
for (Association a : persistentEntity.getAssociations()) {
if (a.isEmbedded()) {
components.add((GrailsDomainClass) grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, a.getAssociatedEntity().getName()));
}
}
return components;
}
use of grails.core.GrailsDomainClass in project grails-core by grails.
the class DefaultGrailsDomainClass method getSubClasses.
/**
* Obtains a Set of subclasses
*
* @deprecated Use {@link org.grails.datastore.mapping.model.MappingContext} API instead
*/
@Deprecated
@Override
@SuppressWarnings("unchecked")
public Set getSubClasses() {
verifyContextIsInitialized();
Set<GrailsDomainClass> subClasses = new LinkedHashSet<>();
for (PersistentEntity e : mappingContext.getChildEntities(persistentEntity)) {
subClasses.add((GrailsDomainClass) grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, e.getName()));
}
return subClasses;
}
Aggregations