On June 21, 2016 at 12:02:58 PM, Saleh Mamdouh (
salehmam...@gmail.com) wrote:
> Hi,
>
> Will also post sub-question on the Slack channel.
> Actually, generating the code I need does not seem to be that difficult.
> However, from within the custom script, I do not have access to the
> grailsApplicaiton implicit object nor can I access the domain classes.
> Although I import domain classes, I get a runtime error when running the
> script saying that class cannot be resolved.
>
> Below code works well from a controller method :
>
> def gdClass = grailsApplication.getDomainClass(Student.name)
> def props = gdClass.persistentProperties as List
>
> for (property in props) {
> println "+++++++++++++++++++++++++++"
> println
property.name
> }
>
> THe code breaks if I put it in a custom script. I am not sure if that
> what @Jeff you mean that I do not
>
> have access to the class ?
>
>
Your options really depend on what you need to access and what you want to do with whatever it is you need access to. One option you have is if you write a custom ApplicationCommand, you can have the GrailsApplication instance auto wired right into the command and then navigate from there. Commands take longer to initialize, but that is one option.
import grails.core.GrailsApplication
import grails.dev.commands.ApplicationCommand
import grails.dev.commands.ExecutionContext
import org.springframework.beans.factory.annotation.Autowired
class SomeCommand implements ApplicationCommand {
@Autowired
GrailsApplication grailsApplication
boolean handle(ExecutionContext ctx) {
// go to town...
return true