Search in sources :

Example 1 with CollectionDataBindingSource

use of grails.databinding.CollectionDataBindingSource 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();
    PersistentEntity entity = null;
    if (application != null) {
        try {
            entity = application.getMappingContext().getPersistentEntity(targetType.getClass().getName());
        } catch (GrailsConfigurationException e) {
        // no-op
        }
    }
    final List<DataBindingSource> dataBindingSources = collectionBindingSource.getDataBindingSources();
    for (final DataBindingSource dataBindingSource : dataBindingSources) {
        final T newObject = targetType.newInstance();
        bindObjectToDomainInstance(entity, newObject, dataBindingSource, getBindingIncludeList(newObject), Collections.emptyList(), null);
        collectionToPopulate.add(newObject);
    }
}
Also used : GrailsConfigurationException(org.grails.core.exceptions.GrailsConfigurationException) PersistentEntity(org.grails.datastore.mapping.model.PersistentEntity) GrailsApplication(grails.core.GrailsApplication) CollectionDataBindingSource(grails.databinding.CollectionDataBindingSource) DataBindingSource(grails.databinding.DataBindingSource)

Example 2 with CollectionDataBindingSource

use of grails.databinding.CollectionDataBindingSource in project grails-core by grails.

the class DataBindingUtils method bindToCollection.

public static <T> void bindToCollection(final Class<T> targetType, final Collection<T> collectionToPopulate, final ServletRequest request) throws InstantiationException, IllegalAccessException {
    final GrailsApplication grailsApplication = Holders.findApplication();
    final CollectionDataBindingSource collectionDataBindingSource = createCollectionDataBindingSource(grailsApplication, targetType, request);
    bindToCollection(targetType, collectionToPopulate, collectionDataBindingSource);
}
Also used : GrailsApplication(grails.core.GrailsApplication) CollectionDataBindingSource(grails.databinding.CollectionDataBindingSource)

Aggregations

GrailsApplication (grails.core.GrailsApplication)2 CollectionDataBindingSource (grails.databinding.CollectionDataBindingSource)2 DataBindingSource (grails.databinding.DataBindingSource)1 GrailsConfigurationException (org.grails.core.exceptions.GrailsConfigurationException)1 PersistentEntity (org.grails.datastore.mapping.model.PersistentEntity)1