config.groovy meare with .property file in grails - grails

In my config.groovy i have set ..
emailTo="admin#xzyz.com"
emailForm="notification#oabc.com"
but i want some flexibility in this. Can I change this while my project is running form property file ? Please help

Yes, you can use .property file as well, by default Grails looks for properties at:
in calss path ${appName}-config.properties
and global config ~/.grails/${appName}-config.properties
path passed as a system vaiable named ${appName}.config.location
Just take a look at first few lines of your Config.groovy, you can put there any other path to your own .properties
See also http://grails.org/doc/2.1.0/guide/conf.html#configExternalized

I believe there is a plugin to support dynamic reloading of external config files whenever they change, but conceptually if this is data that is supposed to be modifiable at runtime then maybe it would be better to represent it as a domain object and store it in the database.
I use this method on a number of projects, defining a domain class
class AppConfiguration {
String adminEmail
String appTitle
// etc
}
and ensuring there is always exactly one instance in the database, creating an initial instance in BootStrap if it is not already present. Now anywhere in the app that requires this config data I just do AppConfiguration.list()[0]
I just use dynamic scaffolding for the edit pages, which are restricted to be accessible only to admin users through Spring Security.

Related

keep variable throughout the life of the application in mvc

I need to keep variable throughout the life of the application. It shouldn't be disposed NEVER.
I was thinking of two methods:
Keep it the file
Store in database in separate table
But in the second case, create a separate table for one row isn't very clever.
So how to achieve that?
Depending on where the property is initialised:
It could be stored within the Application Scope -
There are different scopes with ASP.NET and other scopes are Session and Request
ASP.NET Application State Overview
ASP.NET Session State Overview
Alternatively set as a static property within the Global.asax
Set as a app setting in Web.Config if its immutable
<appSettings>
<add name="fixedProperty" value="fixedValue" />
</appSettings>
and reference anywhere within the application using
ConfigurationManager.AppSettings["fixedProperty"]
(although this call to configuration to should moved to a single static Settings file to make call to property reusable with one accessor to the web.config - type can also be converted from string to whatever you may require too)
As you already mentioned, persist to file or database - there is nothing wrong with persisting the single record to a database. That's what the data store is for.
You can make that variable as a static variable in a class like StaticData.java and use it with the class name. If you want to access the variable only at client side you can use localstorage.set().

Auto generated values in Grails domain class

Is there a way to add my own auto generated field to domain like id and version , If yes the please guide me . provide me URL form where i can read and under stand the core concept of Grails and Domain specific language .
use install-template in the app to get all default templates:
grails install-template
after which you would be able to see /src/templates (newly created)
Modify DomainClass.groovy under /src/templates/artifacts as below:
#artifact.package#class #artifact.name# {
//according to your need
Long myId
Integer myVersion
static constraints = {
}
}
Done!!!!
Henceforth, when create-domain-class command is used to create a domain class, those fields will be auto populated.
I am not sure I am understanding your question correctly but here is the link to the web features of Grails documentation. The "Link Generation API" may be something you are asking after.
If you would like to manage ID and version than using Spring Security (plugin or full docs) or SQL features may be the direction you want to read more about.
EDIT: Try this Stackoverflow question and answer on using inheritance. Seems to be very similar to what you are asking.
You would need to write an AST transform to inject the fields you want to add automatically. The one that injects ‘id’ and ‘version’ can be found here as an example:
https://github.com/grails/grails-core/blob/master/grails-core/src/main/groovy/org/codehaus/groovy/grails/compiler/injection/DefaultGrailsDomainClassInjector.java
You would then need to write a GORM event listener to automatically update the values of these properties. See
https://github.com/grails/grails-data-mapping/blob/master/grails-datastore-gorm/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java
For an example of the one that updates the dateCreated/lastUpdated properties.
These can both be written in a separate Gradle/Maven project which you then reference in the dependencies of your BuildConfig.groovy file.

Creating a 'configuration settings' object, persisted to the db, that you can create properties for

Since Rails is using Ruby (dynamic language), would it be possible to create a very flexible
'configuration' class that has properties that you use throughout the website, AND have the ability to add new class properties (in the db for web modification) and then just use it in the code.
Each property would be of a specific type like a string, integer, bool etc.
You can't do this in a strongly typed language, but it must be possible with Ruby!
So say my class is like:
globalConfig.is_active
globalConfig.admin_email
I guess to make this work, I would loop through all the properties in the db, create properties in the class and assign the value right?
I actually have a settings plugin on GitHub you can use:
http://github.com/bellmyer/settings
It makes this easier for you. Right now it's not rails3-ready, so let me know if you need that. I also need to put in the time to roll it into a gem, instead of a plugin.
If you end up using it, let me know and I'll get it up to date. Otherwise, you can look at the code to see how I did things, and use that to help build your own custom solution.

How to reference a grails GSP model variable indirectly e.g. via .get(...)

I'm using a GSP for sending out emails based on the MailService plug-in. The sendMail closure passes (amongst others) body(view:..., model:myModel)
I know that I can access every item of the myModel Map just using ${itemName} in the GSP. However as I sometimes want to build the item name dynamically like 'item'+i, I need to have some surrounding method to access the variable.
I already tried ${model.get('item'+i), and ${params.get('item'+i), but model is null and params is an empty Map. I also tried pageScope, but though I can access an item via ${pageScope.itemName, I can not use ${pageScope.get('item'+i)} because pageScope is not a Map.
Probably there are multiple solutions to solve this; I'd be glad about an easy one ;-). One solution I see is to pass myModel as the only parameter and then always use myModel.get(...), however this would mean that I had to change all my existing GSPs to always refer to myModel instead of accessing items (with fixed names) directly; so if there's a way where I don't have to change the model passed to the GSP, this would be my favorite.
If someone could also say a few words about the difference of model and params in this context, this would be additionally helpful!
I've managed it now using ${pageScope.getProperty(...)}.
There's no 'model' scope or variable. Instead objects in the model map are set as Request attributes to make them available to the GSP. This is a Spring feature which makes it easy to access variables in JSPs using JSTL and since the GSP syntax is very similar to JSTL it works the same way in Grails.
So you can use this:
${request.getAttribute('item'+i)}
to access model variables using dynamic names.
You can use ${fieldValue(bean: book, field: 'title')}
See: http://grails.github.io/grails-doc/latest/ref/Tags/fieldValue.html

Is it possible to use a JNDI dataSource read only in Grails?

I need to add a JNDI datasource from a legacy database to my Grails (1.2.2) application.
So far, the resource is added to my Tomcat (5.5) and DataSource.groovy contains:
development {
dataSource {
jndiName = "jdbc/lrc_legacy_db"
}
}
I also created some domain objects mapping the different tables to comfortably load and handle data from the DB with GORM. But I now want to assure, that every connection to this DB is really read-only. My biggest concern here is the dbCreate- property and the automatic database manipulation through GORM and the GORM classes.
Is it enough to just skip dbCreate?
How do I assure that the database will only be read and never ever manipulated in any way?
You should use the validate option for dbCreate.
EDIT: The documentation is quite a bit different than when I first posted this answer so the link doesn't quite get you to where the validate option is explained. A quick find will get you to the right spot.
According to the Grails documentation:
If your application needs to read but never modify instances of a persistent class, a read-only cache may be used
A read-only cache for a domain class can be configured by
1. Enable Caching
Add something like the following to DataSource.groovy
hibernate {
cache.use_second_level_cache=true
cache.use_query_cache=true
cache.provider_class='org.hibernate.cache.EhCacheProvider'
}
2. Make Cache Read-Only
For each domain class, you will need to add the following to the mapping closure:
static mapping = {
cache usage:'read-only', include:'non-lazy'
}

Resources