Modify Grail plugin Domain Class data-source in dependent application - grails

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.

Related

Add behavior (relationships) to grails plugin domain classes from main application?

I have a Grails plugin I've created which is intended to support a number of applications. This plugin has an Employee domain object. The problem is that, when used in the main application, domain objects from that application need to refer back to the Employee object. So, my main application might have an Address which belongsTo the Employee class from the plugin.
How can one handle this properly in Grails 2.5.0?
Thanks in advance.
It looks like that your main and plugin depend on each other. In this case you should add the plugin location of one to another:
grails.plugin.location.<PLUGIN-NAME> = "<PATH TO YOUR PLUGIN>" Ex: "../myPlugin" assuming it is located on the same folder structure as the plugin
and in your plugin
grails.plugin.location.<APP-NAME>= "<PATH TO YOUR APP>" EX: "../myApp"

Connect to Grails Datasource from Grails Plugin Command

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')

How to use Grails Domain objects in a Java project?

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

Grails Plugin Development - override domain class

Plugins in Grails are great method to modularise an application.The documentation suggest to override the artifacts from the plugin in the application, which uses this plugin.
Is it realy the best approach?
Let's describe it by example: There is a domain class "org.User" defined in the plugin. The application overrides this domain class. If I use "grails run-app" then there are no warnings and it works. But Eclipse (GGTS) complains about "Invalid duplicate class definition of class org.User". For some developers it wouldn't matter, but I like the IDE helping on coding by stuf like "autocomplete".
At the end both classes are compiled an put on the java class loader. The application version of the class is loaded before the version of the plugin. The class resolver finds it first and that's why it works. Please correct me if I'm wrong at this point. Is it realy a good idea to have two versions of a class in one class loader?
What are the alternatives?
You can do like Spring Security Core plugin does, provide the User class as a template, so your application that use this plugin can choose between creating his own class or installing your default User class.
The plugin user template is here, and the script responsible to create this in the application is here.
You will need also a config value to know the class to use, and use it dynamic.
P.S: there are good security plugins like Shiro and Spring Security, maybe it's easier to check them instead of create your own.

How to reuse Grails application's domain classes?

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.

Resources