I'm attempting to write a plugin for Grails that will automatically generate my domain classes based on special views that we're designing in our legacy database. I basically just want to save myself some time manually writing all the mapping stuff required to make the domain classes work.
Do I have access to the dataSource defined in the application.yml of the project from a custom ApplicationCommand implementation? If so, how do I pull it so I can open my connection to the database?
I found the answer here. The dataSource bean that is injected into regular artefacts can be accessed through the ApplicationContext like this:
def dataSource = applicationContext.getBean('dataSource')
Related
In my custom Grails plugin, how can I set up a JSON view for a non-domain class and get client apps to use it by default?
I have a view file in the plugin:
/views/com/mycompany/myplugin/myclass/_myClass.gson
When I do grails install, I can see that this .gson file is in the generated JAR. However, the client app is not using it.
What can I do to make it work?
Are there any settings or steps that can make troubleshooting easier?
I am using Grails 3.2.4.
Update:
When I copy the view into a client app, using the exact same path, the view is getting invoked. It's only when the view is defined in the plugin that the view cannot be found.
The framework seems to be trying to look up the plugin as a class from the classloader:
myclientproject_com_mycompany_myplugin_myclass__myClass_gson
How do I get my plugin to add this class to the classpath?
For my use case, what I actually needed was a custom converter.
See:
In JSON views, how do I flatten an object out as a single string?
This obviated the need for my plugin to publish a view.
I have multiple plugins with Grails domain classes that are stored in separate databases. I want to be able to configure within the dependent project what sources each class comes from. It seems like a similar question is here:
Grails changing datasource at runtime
Is it still not possible to add additionaly sources to a class at runtime? And, I don't necessarily need to do it at runtime either. Just configure the class in the dependent Grails application. The method for setting this up (http://www.grails.org/doc/2.2.1/guide/conf.html#multipleDatasources) requires direct access to the class definitions, and I'd rather avoid having to do that.
I have a Grails application with outsourced (in a separate Grails Plugin) domain objects and would like to use the domain objects in a Java application.
If this is possible, how to do that?
Thanks
In order to use them as domain classes, you need GORM-plugin to be installed somehow in your java code. Without the plugin the domain classes lose their "active record" aspect and can be used only as POJOs.
I can barely imagine how to do that w/o lot of pain
You need to define your custom hbm.xml hibernate mapping files. See hibernate doc
I cite from mybatis plugin documentation:
"When working with MyBatis plugin your "Domain" classes should be
located in src/groovy and not in grails-app/domain. This is necessary
to avoid conflict with GROM since MyBatis plugin can coexist with
existing GORM Domain classes."
So where should the validation and constraints be located, when I want to use grails with MyBatis plugin ?
You can add a #Validateable annotation to any Groovy class in Grails, and you will be able to validate it... The Plugin currently doesn't check for any validation errors so you will have to implement that code yourself.
From official Grails documentation:
Classes which define the static constraints property and are annotated
with #Validateable can be made validateable by the framework
http://grails.org/doc/2.1.0/guide/validation.html#validationNonDomainAndCommandObjectClasses
You could even write a custom MyBatis Interceptor (see https://github.com/fzilic/Grails-MyBatis/blob/master/src/groovy/org/grails/plugins/mybatis/locking/OptimisticLockingInterceptor.groovy) and register it after the SqlSession is created...
Currently the MyBatis plugin doesn't support registering custom Interceptors in it's configuration, but they could be added to the interceptor chain
def factory = GrailsApplication.mainContext.getBean("sqlSessionFactoryBean_dataSource")
factory.configuration.interceptorChain.addInterceptor(Interceptor)
Support for this might be added in future versions.
I have already create a Grails application. For some reasons I need to create another console application for someone to modify database data.
Is it possible to package Grails application as a JAR library, so that the console application can reuse those domain classes?
Or, I add/create some classes in Grails application and package it as a JAR and run as console application?
If no better answer, probably I will use the batch-launcher plugin to do that.
You can put the domain classes in a JAR and tell Grails that these are your domain classes by adding a Hibernate XML file (in grails-app/conf/hibernate) that refers to the classes in this JAR. You can use this JAR in any other Java/Groovy application, but obviously they'll only have the persistence methods (dynamic finders, save(), etc.) when used in a Grails app.