I'm developing an app using Grails and there are some app-wide configuration settings I'd like to store somewhere. The only way I've thought of is to create a domain class that stores the configuration values, and to use a service that queries that domain class. The problem I see is that there should be just one instance of that domain class, but I haven't found anything to enforce that restriction.
There may be other best practices to store app's own configuration that I may not be aware of, all suggestions are welcome.
Edit: the settings are supposed to be configurable from within the app.
There is special place: /grails-app/conf/Config.groovy. You can add values there like:
my.own.x=1
and read values by:
def x = grailsApplication.config.my.own.x
See docs for more details: http://grails.org/doc/latest/guide/conf.html#config
There is a plugin for that: Settings. It allows you to create named setting like my.own.x of different types (String, date, BigDecimal and integer), and provides you with the basic CRUD pages to manage them.
You can access the settings from either gsp:
<g:setting valueFor="my.own.x" default="50" encodeAs="HTML"/>
or controllers/services/domains
Setting.valueFor("my.own.x", 50)
I use it in several projects and think it works great.
You can enforce your single domain class db entry via custom validator:
// No more than one entry in DB
class MasterAccount {
boolean singleEntry = true
static constraints = {
singleEntry nullable: false, validator: { val, obj ->
if(val && obj.id != getMasterAccount()?.id && MasterAccount.count > 0){
return "Master account already exists in database"
}
}
}
MasterAccount static getMasterAccount(){
MasterAccount.list()?.first()
}
}
You can defer its configuration and persistence to Bootstrap.groovy, which would achieve the same effect as Config.groovy
If you're using 1.3.* you can try grails dynamic-config plugin (http://www.grails.org/plugin/dynamic-config). "This plugin gives your application the ability to change the config properties without restarting the application. The values in Config.groovy are persisted in database when application is run-for the first time after installing the plugin. "
I've never used it on a grails 2.0.* project.
Related
since Typo3 v10 you have to use Classes.php file in Configuration/extbase/Persistence Folder for configuration of persistence table mapping.
Does anyone know how to implement
config.tx_extbase.persistence.classes {
Domain\DomainUsergroupMailer\Domain\Model\FrontendUserGroups {
mapping {
tableName = fe_groups
columns {
subgroup.foreignClass = TYPO3\CMS\Extbase\Domain\Model\FrontendUserGroup
}
}
}
I can't find documentation concerning the foreignClass Parameter.
I found parameter subclasses in https://docs.typo3.org/m/typo3/book-extbasefluid/10.4/en-us/6-Persistence/5-modeling-the-class-hierarchy.html
Does anyone know if this is the right way parameter and how to use it?
Thank you
There never was such a feature in TYPO3 as confirmed by searching the TYPO3v9 source code for foreignClass. So this must be provided by a 3rd party extension.
However, from the name it sounds like you only need to use an appropriate element type for your collection relation:
/**
* #var ObjectStorage<FrontendUserGroup>
*/
private ObjectStorage $subgroup;
See Implementing the domain model for details.
A pattern we use here for large dynamic forms is to allow the client to edit pretty much the entire graph including children and removal and addition of children is handled in javascript. Failing use case is when user deletes a property of one of the children and for this example we'll say the email address of the second child is removed and the parm is sent empty.
Example Class Structure
Domain X
Long id
String name
SortedSet<Child> children
Domain Child
Long id
Integer position // used to sort
String name (required)
String email (required)
Parms are named in this manner:
x.id = 1
x.name = 'blah'
x.children[0].id = 1
x.children[0].position = 0
x.children[0].name = 'childa0'
x.children[0].email = 'child0#any.com'
x.children[1].id = 2
x.children[1].position = 1
x.children[1].name = 'child1'
x.children[1].email = ''
Current stored values:
x.id = 1
x.name = 'blah'
x.children[0].id = 1
x.children[0].position = 0
x.children[0].name = 'childa0'
x.children[0].email = 'child0#any.com'
x.children[1].id = 2
x.children[1].position = 1
x.children[1].name = 'child1'
x.children[1].email = 'child1#any.com'
The pattern to update has been:
Browser
user edits all and submits changes
parms are sent as shown above and in this use case user accidentally deleted email address that is required (yes submit could be prevented in this simple case but there are more extensive validation rules and this is just a simple example to illustrate issue)
Server
retrieve current parent domain class using id parm
check current stored version against edited version and ensure it has not changed
apply all parms to current domain object in this manner: x.properties = parms.x
This is where it breaks down. With Grails 2.2.2 the email property of child[1] is updated to an empty string and running validation catches the error. Grails 2.4.4 does NOT update the child value. I know the data binding changed and perhaps there are valid reasons for this change but I can't fathom it. This has worked from 1.3.6 to 2.2.2.
One thing to note is that if I do not retrieve the domain class and instead create a new instance with the parms passed in all of the child parms are set correctly.
X x = new X(params.x)
This seems to indicate some Grails issue as the user guide talks about both methods as equal - one to use for new instances and the other for updates but the examples clearly demonstrate that both methods should handle indexed child properties.
Any help or thoughts are appreciated!
I ran into this problem my self upgrading an application from 2.2.4 to 2.4.4.
Setting grails.databinding.useSpringBinder = true for config.groovy
and adding grails-web-databinding-spring to the pom worked flawlessy for me.
Obviously doing this was a lot less hassle than rewriting all the binding code :-)
Looking into this further Grails 2.4.4 documentation states that it will bind sets positionally if they are indexed (ie. [0]).
http://grails.org/doc/latest/guide/single.html#dataBinding
The section titled "Data Binding and Many-ended Associations" states that these parms will be bound and that new instances of the set will be created if their index is greater than the current set. This was true for me in Grails 2.2.2 but NOT in 2.4.4.
HOWEVER, earlier in the documentation in the section "Binding To Collections And Maps" it states clearly: "When updating existing elements in a Set the Map being assigned to the Set must have id elements in it which represent the element in the Set".
If you are upgrading to Grails 2.4.4 and have any complex form data binding I suggest you look there first to decide whether to upgrade. Due to these changes in data binding this has become the most complex upgrade we've encountered yet with Grails.
The other disappointment with 2.4.4 is that it states you can override the new binding and fall back to the old spring binding but you cannot past 2.4 - at least from what I can gather from others' posts and my attempts. Setting the config property as documented and adding the dependency required resulted in startup runtime errors.
Apparently older versions of grails prior to 2.4.4 would match the child instances to params positionally, i.e. using the indexes ([0],[1], etc.) to match the children not caring if the IDs of the instances matched. The new binder does want the IDs of the instances to match before binding. I had never realised I was not placing the ID in a hidden field as it always worked perfectly.
I had a similar problem after migrating to Grails 2.4.4 (from 2.2.2). After a lot of reading and seaching (you must to definitly read http://docs.grails.org/2.4.4/guide/single.html#dataBinding mencioned in user2863513 post) I've resolved it in a very simple way
In my application I had nested domains relationships. Example:
class A {
String name
static hasMany = [b:B]
}
class B {
String name
static hasMany = [c:C]
}
class C {
String name
}
Due to the form complexity, class C object were not binding correcly. I've resolved this defining the C atribute on class B as a List (remember, has many relationships are implemented as Sets by default). So, my class B changed to:
class B {
String name
static hasMany = [c:C]
List c
}
And voila! Everything works perfectly again.
I'd like to store api key in my grails application. Api key should be global and can be changed every several minutes or hours.
Where is the best place to store this key globally?
I don't want to store it in database, guess that config is the best place.
But I suppose that config is a good place to store constants, not changeable data.
So, where to store api key globally?
Update: maybe class with static fields like String apiKey will be enough?
Api key should be global and can be changed every several minutes or hours.
Why would an API key change so frequently?
guess that config is the best place. But I suppose that config is a good place to store constants, not changeable data.
Normally properties in Config.groovy are read-only, but there's nothing to prevent you modifying them, e.g.
class MyController {
def grailsApplication
def updateApiKey(String newKey) {
grailsApplication.config.apiKey = newKey
}
}
The mail plugin is documented to require its settings in Config.groovy. I want to have it stored in the database so it can be changed at runtime. And this is just one example.
I've read Does externalizing sensitive config variables into a .properties outside of Config.groovy provide a security advantage in Grails? but this is about using an external property file, I need to store the settings in the database (the app has an admin interface, with auditing and so on).
Thank you.
In Bootstrap.groovy you can read the property values from the database and replace those read from the config files by updating grailsApplication.config. For example, assume you want to replace the value of a config property named foo.bar
class BootStrap {
GrailsApplication grailsApplication
def init = { servletContext ->
def fooBarDB = // read the value of foo.bar from the database
grailsApplication.config.foo.bar = fooBarDB
}
}
Rather than writing this code yourself, you could instead use the Dynamic Config Plugin to achieve the same outcome.
Yes, you can do this. During the grails bootstrap process using Bootstrap.groovy, retrieve the properties from the database and override them. It's similar to a properties file override, you're just storing them in a database.
Also see Properties in the database
First way to change value dynamic:
grailsApplication.config.grails.mail.put("port",your database fetch value of port)
grailsApplication.config.grails.mail.put("username","your database fetch value of username")
grailsApplication.config.grails.mail.put("password","your database fetch value of username")
grailsApplication.config.grails.mail.put("props","your database fetch value of username")
second way to change value dynamic:
org.codehaus.groovy.grails.commons.DefaultGrailsApplication
DefaultGrailsApplication class provide the all the information about config. you can get email information using following code and change it dynamic.
def defaultGrailsApplication= new DefaultGrailsApplication()
// to get mails information from Config.groovy
defaultGrailsApplication?.config?.grails.mail?.get("host")
defaultGrailsApplication?.config?.grails.mail?.get("port")
defaultGrailsApplication?.config?.grails.mail?.get("username")
defaultGrailsApplication?.config?.grails.mail?.get("password")
defaultGrailsApplication?.config?.grails.mail?.get("props")
// to change mails information of Config.groovy
defaultGrailsApplication?.config?.grails.mail?.put("host","your database fetch value of host")
defaultGrailsApplication?.config?.grails.mail?.put("port",your database fetch value of port)
defaultGrailsApplication?.config?.grails.mail?.put("username","your database fetch value of username")
defaultGrailsApplication?.config?.grails.mail?.put("password","your database fetch value of username")
defaultGrailsApplication?.config?.grails.mail?.put("props","your database fetch value of username")
Now you need to assign this "defaultGrailsApplication" instance to inject value of application. just assume that we have one class service Temp.service having grailsApplication injected like:
Temp.service
class Temp{
def grailsApplication
def your method(){
please add above code for set value
grailsApplication = defaultGrailsApplication
}
}
What I know: mongod is running, the database exists, the collection exists, I can do inserts using the mongodb console, and a connection to the mongod instance is made by Grails (i.e. I can query and so on).
Other details: Grails 2.01, mongodb plugin 1.0.0 RC4, hibernate plugin 2.01, mongodb 2.0.3
class Node {
static mapWith = "mongo"
static constraints = {
}
ObjectId id
Integer someId //someId can be null or just non-existent
String name
}
What doesn't work: I can't insert any data through GORM, even when I explicitly set flush to true.
def n = com.company.project.Node(name: "test")
n.save(flush:true)
I've also tried this out in the Grails console, but there's no error message. Is there anything I've done wrong?
I'm new to Grails so someone else may or may not correct me. Anyways the issue is that the Mongodb GORM plugin by default doesn't allow null values of any fields that you specify in Mongodb domain classes.
As of now I'm not sure how to override this behavior, but I will update this if I ever figure it out.
Have you tried to make your properties nullable? Something like this:
static constraints =
{
someId nullable: true
}
Execute validate() method before save and take a look on the result, most probably there is some constraint error of which you may not know.