Showing posts with label grailsApplication. Show all posts
Showing posts with label grailsApplication. Show all posts

Monday, June 30, 2014

Grails get bean/service from service or other class

import org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib
import grails.util.Holders
import org.codehaus.groovy.grails.commons.GrailsApplication

public static GrailsApplication grailsApplication = Holders.grailsApplication;
public static ServletContext servletContext = Holders.servletContext;
public static ApplicationTagLib g = Holders.applicationContext.getBean("org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib")

def bean = Holders.applicationContext.getBean(beanIdentifier);
// beanIdentifier = userService, groupService etc...

Monday, October 21, 2013

Registering new bean classes in grails application programmatically by registerBeanDefinition

As part of amazing goodies of grails is the ability to extend your applications - registering new classes problematically is a breeze


GenericApplicationContext context = new GenericApplicationContext();

context.setParent(applicationContext);
/* OR */
context.setParent(grailsApplication.mainContext);

// Create class from string
Class clazz = new GrailsAwareClassLoader().parseClass(classString);

// First create a bean definition
def myBeanDef = new GenericBeanDefinition()

// Set bean class
myBeanDef.setBeanClass(clazz)

// Set scope
myBeanDef.setScope(BeanDefinition.SCOPE_SINGLETON)

context.registerBeanDefinition("beandefName", myBeanDef);

Monday, September 30, 2013

Grails: how to get/set a meta-constraint on domain class property such as belongsTo or other variables

In the scaffolding templates there is a property domainClass from type org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass. These object has the property constrainedProperties. To have access to any properties such in this example: 'readonly' you have to do this:

Your domain class:


class Contact {
   static belongsTo = [subscription: Subscription]

   static constraints = {
       subscription(nullable: false, attributes: [readonly: true])  
   }

   String description
}


The DefaultGrailsDomainClass has a constructor with a attribute from type Class maybe you can do this:


def domainClass = new DefaultGrailsDomainClass(Contact.class)
def ro = domainClass.constrainedProperties.subscription.attributes.readonly



def domainClass = new DefaultGrailsDomainClass(it.metaClass.getTheClass())
def constrainedProperties = domainClass.constrainedProperties;
if(constrainedProperties.containsKey("subscription")) {
    ConstrainedProperty v = (ConstrainedProperty) constrainedProperties.get("subscription");
    /**
     * Now check
     */
    if(v.isNullable()) {
        println "Nullable";
    }
    if(v.isBlank()) {
        println "Blank";
    }
    /**
     * There are may other constraints
     * http://grails.org/doc/latest/api/org/codehaus/groovy/grails/validation/ConstrainedProperty.html 
     * Java/Grails Class ConstrainedProperty
     */ 
 }

Tuesday, September 17, 2013

Access Grails Application in BootStrap

Accessing the Grails application object in BootStrap.groovy is easy. 
We only have to define a variable named grailsApplication and Spring's 
name based injection takes care of everything.
 
class BootStrap {
    // Reference to Grails application.
    def grailsApplication

    def init = { servletContext ->
        // Access Grails application properties.
        grailsApplication.serviceClasses.each {
            println it 
        } 
    }

    def destroy = {}
}