0

I have a grails-plugin called "listadmin" there is a domain model "Liste":

package listadmin
class Liste {

    String internal_name
    String public_name

    Boolean edtiable = true
    Boolean visible = true

    static hasMany = [eintrage : ListenEintrag]
    static constraints = {
        internal_name(unique : true , blank : false);
    }
    String toString() {
        "${public_name}"
    }

}

I have service called "SECO_ListenService" in the same module (grails-plugin):

package listadmin

class SECO_ListenService {

    def getEntriesOfList(String intnalListName) {
        def aList = Liste.findByInternal_name(intnalListName)
        return aList
    }
}

Now I try to call this service from an other module (grails-plugin) called "institutionadmin". The SECO_ListenService should return a list of strings for an select of a domain model in the inistitutionadmin:

package institutionadmin

import listadmin.SECO_ListenService

class Einrichtung {


    Long einrichtungs_type
    Long type_of_conzept
    int anzahl_gruppen
    int anzahl_kinder_pro_Gruppe
    String offnungszeiten
    static hasMany = [rooms : Raum]
    static constraints = {
        def aList  = []
        def sECO_ListenService = new SECO_ListenService()
        aList=sECO_ListenService.getEntriesOfList("einrichtung_type")
        einrichtungs_type(inList: aList)
    }
}

If I try to run this application with the both modules. I get the following error:

Caused by MissingMethodException: No signature of method: listadmin.Liste.methodMissing() is applicable for argument types: () values: []

It seemed to be that the service class don't know the "Liste"-domain-model. But I don't know where the error is. I also tried to call other standard methods like "findAll" but without any success.

Has anybody an idea where my mistake could be?

Taryn
  • 242,637
  • 56
  • 362
  • 405
Der_V
  • 177
  • 1
  • 16
  • Your service isn't declared as attribute in `Einrichtung`. –  May 13 '13 at 12:36
  • Yes i know. There where some errors if i try to define the attribute. That's why I call the service like normal class/object. But that shouldn't be the problem, or do you think so? Currently I try to solve this problem in other way. I tried to install the plugin: platform-core to call service of other modules with having a directly dependency... But there is another error :( http://stackoverflow.com/questions/16551275/grails-platform-core-plugin-no-signature-of-method-event-in-domain-model – Der_V May 14 '13 at 19:29
  • Now I see your dilemma. See my answer :-) –  May 15 '13 at 11:05

1 Answers1

0

To get a service in a static context you need to access the grailsApplication spring bean. This can be done thought Holders. Example:

class MyService {
  List<String> getAvailable() {
    return ['A','B','C']
  }
}

class MyDomainClass {

  String something

  static constraints = {
    something inList: getSomethingList()
  }

  static List<String> getSomethingList() {
    def myService = Holders.grailsApplication.mainContext.getBean('myService')
    return myService.getAvailable()
  }

}
  • Thank's for your help and interesting post. I don't realy understand why i have to use these holders, but i will google it and try it out ;) Could it be possible, that i get such a problem with methods like "event()", like the other problem/question i linked before? – Der_V May 15 '13 at 16:06
  • In a static context you don't have an instance of your domain class, so you need to get the service in another way. For the event, what happens if you use `myService.event()`? –  May 15 '13 at 16:09
  • I tried your solution above, but i get an error before. It seemed to be that the domain don't know the Holders-class: _Apparent variable 'Holders' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes: You attempted to reference a variable in the binding or an instance variable from a static context. You misspelled a classname or statically imported field. Please check the spelling. You attempted to use a method 'Holders' but left out brackets in a place not allowed by the grammar._ – Der_V May 15 '13 at 16:21
  • I don't beleave that _myService.event()_ will be possible. The method event comes from the plugin "_platform-core_" to call event from other grails-plugins without having a directly dependency. So normaly the method event is calling like: _event(for:"listadmin", topic:"getEntriesOfList", data:"einrichtung_type").waitFor()_ For more details see my other question: http://stackoverflow.com/questions/16551275/grails-2-2-2-platform-core-plugin-no-signature-of-method-event-in-domain-model – Der_V May 15 '13 at 16:23
  • Oh,I'm such an idiot ^^ Now i get an other error: ..bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Resource must not be null – Der_V May 15 '13 at 16:49