Search in sources :

Example 1 with GrailsApplication

use of grails.core.GrailsApplication 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);
    }
}
Also used : GrailsApplication(grails.core.GrailsApplication) GrailsDomainClass(grails.core.GrailsDomainClass) CollectionDataBindingSource(grails.databinding.CollectionDataBindingSource) DataBindingSource(grails.databinding.DataBindingSource)

Example 2 with GrailsApplication

use of grails.core.GrailsApplication 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);
}
Also used : GrailsApplication(grails.core.GrailsApplication) GrailsDomainClass(grails.core.GrailsDomainClass)

Example 3 with GrailsApplication

use of grails.core.GrailsApplication in project grails-core by grails.

the class ConstrainedPropertyTests method testConstraintBuilder.

@SuppressWarnings("rawtypes")
public void testConstraintBuilder() throws Exception {
    GroovyClassLoader gcl = new GroovyClassLoader();
    Class groovyClass = gcl.parseClass("class TestClass {\n" + "Long id\n" + "Long version\n" + "String login\n" + "String other\n" + "String email\n" + "static constraints = {\n" + "login(size:5..15,nullable:false,blank:false)\n" + "other(blank:false,size:5..15,nullable:false)\n" + "email(email:true)\n" + "}\n" + "}");
    GrailsApplication ga = new DefaultGrailsApplication(groovyClass);
    ga.initialise();
    new MappingContextBuilder(ga).build(groovyClass);
    GrailsDomainClass domainClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "TestClass");
    Map constrainedProperties = domainClass.getConstrainedProperties();
    assert constrainedProperties.size() == 3;
    grails.gorm.validation.ConstrainedProperty loginConstraint = (grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("login");
    Collection appliedConstraints = loginConstraint.getAppliedConstraints();
    assertTrue(appliedConstraints.size() == 3);
    // Check the order of the constraints for the 'login' property...
    int index = 0;
    String[] constraintNames = new String[] { "size", "nullable", "blank" };
    for (Iterator iter = appliedConstraints.iterator(); iter.hasNext(); ) {
        Constraint c = (Constraint) iter.next();
        assertEquals(constraintNames[index], c.getName());
        index++;
    }
    // ...and for the 'other' property.
    appliedConstraints = ((grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("other")).getAppliedConstraints();
    index = 0;
    constraintNames = new String[] { "blank", "size", "nullable" };
    for (Iterator iter = appliedConstraints.iterator(); iter.hasNext(); ) {
        Constraint c = (Constraint) iter.next();
        assertEquals(constraintNames[index], c.getName());
        index++;
    }
    grails.gorm.validation.ConstrainedProperty emailConstraint = (grails.gorm.validation.ConstrainedProperty) constrainedProperties.get("email");
    assertEquals(2, emailConstraint.getAppliedConstraints().size());
    GroovyObject go = (GroovyObject) groovyClass.newInstance();
    go.setProperty("email", "rubbish_email");
    Errors errors = new BindException(go, "TestClass");
    emailConstraint.validate(go, go.getProperty("email"), errors);
    assertTrue(errors.hasErrors());
    go.setProperty("email", "valid@email.com");
    errors = new BindException(go, "TestClass");
    emailConstraint.validate(go, go.getProperty("email"), errors);
    assertFalse(errors.hasErrors());
}
Also used : GrailsDomainClass(grails.core.GrailsDomainClass) BindException(org.springframework.validation.BindException) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GroovyObject(groovy.lang.GroovyObject) GroovyClassLoader(groovy.lang.GroovyClassLoader) Errors(org.springframework.validation.Errors) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GrailsApplication(grails.core.GrailsApplication) Iterator(java.util.Iterator) Collection(java.util.Collection) GrailsDomainClass(grails.core.GrailsDomainClass) Map(java.util.Map)

Example 4 with GrailsApplication

use of grails.core.GrailsApplication in project grails-core by grails.

the class DefaultGrailsPluginManagerTests method testDependenciesWithDelayedLoadingWithVersionRangeStrings.

/**
     * Test the known 1.0.2 failure where:
     *
     * mail 0.3 = has no deps
     * quartz 0.3-SNAPSHOT: loadAfter = ['core', 'hibernate']
     * emailconfirmation 0.4: dependsOn = [quartz:'0.3 > *', mail: '0.2 > *']
     *
     * ...and emailconfirmation is NOT loaded first.
     */
@SuppressWarnings("rawtypes")
public void testDependenciesWithDelayedLoadingWithVersionRangeStrings() {
    GroovyClassLoader gcl = new GroovyClassLoader();
    // These are defined in a specific order so that the one with the range dependencies
    // is the first in the list, and its dependencies load after
    first = gcl.parseClass("class FirstGrailsPlugin {\n" + "def version = \"0.4\"\n" + "def dependsOn = [second:'0.3 > *', third:'0.2 > *']\n" + "}");
    second = gcl.parseClass("class SecondGrailsPlugin {\n" + "def version = \"0.3\"\n" + "def dependsOn = [:]\n" + "}");
    third = gcl.parseClass("class ThirdGrailsPlugin {\n" + "def version = \"0.3-SNAPSHOT\"\n" + "def loadAfter = ['core', 'hibernate']\n" + "}");
    GrailsApplication app = new DefaultGrailsApplication(new Class[] {}, gcl);
    GenericApplicationContext parent = new GenericApplicationContext();
    parent.getDefaultListableBeanFactory().registerSingleton(GrailsApplication.APPLICATION_ID, app);
    DefaultGrailsPluginManager manager = new DefaultGrailsPluginManager(new Class[] { first, second, third }, app);
    manager.setParentApplicationContext(parent);
    manager.setPluginFilter(new IncludingPluginFilter(new String[] { "dataSource", "first", "second", "third" }));
    manager.loadPlugins();
    List pluginList = manager.getPluginList();
    assertNotNull(manager.getGrailsPlugin("first"));
    assertNotNull(manager.getGrailsPlugin("second"));
    //dataSource depends on core
    assertNotNull(manager.getGrailsPlugin("core"));
    //third depends on i18n
    assertNotNull(manager.getGrailsPlugin("third"));
    assertEquals("Expected plugins not loaded. Expected " + 5 + " but got " + pluginList, 5, pluginList.size());
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) IncludingPluginFilter(org.grails.plugins.IncludingPluginFilter) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) List(java.util.List) ArrayList(java.util.ArrayList) DefaultGrailsApplication(grails.core.DefaultGrailsApplication)

Example 5 with GrailsApplication

use of grails.core.GrailsApplication in project grails-core by grails.

the class ConstraintsEvaluatingPropertyTests method testNullableConstraint.

/**
     * Test that static constraints work
     */
@SuppressWarnings("rawtypes")
public void testNullableConstraint() throws Exception {
    GroovyClassLoader gcl = new GroovyClassLoader();
    gcl.parseClass("package org.grails.validation\n" + "@grails.persistence.Entity class Book {\n" + "   Long id\n" + "   Long version\n" + "   String title\n" + "   String description\n" + "   Author author\n" + "   Author assistent\n" + "   Set chapters\n" + "   Map remarks\n" + "   static hasMany = [chapters:Chapter]\n" + "   static constraints = {\n" + "      description(nullable: true)\n" + "      assistent(nullable: true)\n" + "   }\n" + "}\n" + "@grails.persistence.Entity class Author {\n" + "   Long id\n" + "   Long version\n" + "   String name\n" + "}\n" + "@grails.persistence.Entity class Chapter {\n" + "   Long id\n" + "   Long version\n" + "   String text\n" + "}");
    GrailsApplication ga = new DefaultGrailsApplication(gcl.getLoadedClasses());
    ga.initialise();
    new MappingContextBuilder(ga).build(gcl.getLoadedClasses());
    GrailsDomainClass bookClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "org.grails.validation.Book");
    Map constraints = bookClass.getConstrainedProperties();
    Constrained p = (Constrained) constraints.get("title");
    assertFalse("Title property should be required", p.isNullable());
    p = (Constrained) constraints.get("description");
    assertTrue("Description property should be optional", p.isNullable());
    p = (Constrained) constraints.get("author");
    assertFalse("Author property should be required", p.isNullable());
    p = (Constrained) constraints.get("assistent");
    assertTrue("Assistent property should be optional", p.isNullable());
    // Test that Collections and Maps are nullable by default
    p = (Constrained) constraints.get("chapters");
    assertTrue("Chapters property should be optional", p.isNullable());
    p = (Constrained) constraints.get("remarks");
    assertTrue("Remarks property should be optional", p.isNullable());
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) Constrained(grails.gorm.validation.Constrained) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GrailsDomainClass(grails.core.GrailsDomainClass) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) Map(java.util.Map)

Aggregations

GrailsApplication (grails.core.GrailsApplication)18 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)9 GrailsDomainClass (grails.core.GrailsDomainClass)8 GroovyClassLoader (groovy.lang.GroovyClassLoader)8 Map (java.util.Map)4 CollectionDataBindingSource (grails.databinding.CollectionDataBindingSource)3 IncludingPluginFilter (org.grails.plugins.IncludingPluginFilter)3 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)3 DataBindingSource (grails.databinding.DataBindingSource)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 WebApplicationContext (org.springframework.web.context.WebApplicationContext)2 GrailsClass (grails.core.GrailsClass)1 GrailsControllerClass (grails.core.GrailsControllerClass)1 DataBinder (grails.databinding.DataBinder)1 grails.gorm.validation (grails.gorm.validation)1 Constrained (grails.gorm.validation.Constrained)1 ValidationErrors (grails.validation.ValidationErrors)1 GrailsWebDataBinder (grails.web.databinding.GrailsWebDataBinder)1